将html减少到n个字符,同时保持格式


11

几乎每种语言都有一个内置函数,可以在给定位置拆分字符串。但是,一旦字符串中包含html标记,内置函数将无法正常工作。

您的任务是编写一个程序或函数,该程序或函数在第n个字符处分割字符串,但不计算html标签的字符,并将输出有效的html。该程序必须保留格式。html标记之外的空格可以根据您的意愿进行计数或不计数,但必须保留。但是,您可以将多个连续的空间交换为一个空间。

输入:

  1. 字符串
  2. 分割位置(从0开始)

这些可以作为程序或函数参数,也可以从标准输入中读取。

输出:可以返回或写入标准输出的分割字符串。

输入将是有效的html,它将不包含任何实体(例如 )。在字符数限制之后打开的标签应从输出中省略(请参阅最后一个示例)。

例:

输入:<i>test</i>,3
输出:<i>tes</i>

输入:<strong><i>more</i> <span style="color: red">complicated</span></strong>,7
输出:<strong><i>more</i> <span style="color: red">co</span></strong>

输入:no html,2
输出:no

输入:<b>no</b> <i>html root</i>,5
输出:<b>no</b> <i>ht</i>

输入:<b>no img</b><img src="test.png" />more text,6
输出:<b>no img</b>

您可以使用任何语言以及给定语言的标准库。这是代码高尔夫,最短的程序获胜。玩得开心!


1
输入中可以包含不属于HTML标记的“ <”和“>”吗?
xem 2014年

一个人应该使用&lt;&gt;而不是<>,所以不存在(&lt;或者&gt;也不存在)。
David Frank

您能否提供一个示例,其中发生拆分的文本节点后面有标记?喜欢<i>ab</i><b>cd</b> 1吗?
Martin Ender 2014年

除了以外还有其他选择<i>a</i>吗?
David Frank

@DavidFrank <i>a</i><b></b>(如果您认为这b也可能是div或,这很有道理img
Martin Ender 2014年

Answers:


2

该答案对最新规则不再有效。

Javascript(ES694 91

f=(s,l)=>s.split(/(<[^>]+>)/).map(x=>x[0]=='<'?x:[l-->0?y:''for(y of x)].join('')).join('')
f('<strong><i>more</i> <span style="color: red">complicated</span></strong>', 7);
// '<strong><i>more</i> <span style="color: red">co</span></strong>'

取消高尔夫:

f=(s,l)=>
    s.split(/(<[^>]+>)/). // split string s by <*>, capture group is spliced into the array 
    map(x=> // map function to every item in the array
        x[0]=='<'? // if first character is a <
            x // don't modify the string
        : // else
            [ // array comprehension
                for(y of x) // for every character y in x
                    l-->0? // if l > 0 (and decrement l)
                        y // character y
                    : // else
                        '' // empty string 
            ].join('') // join characters in array
        ).
    join('') // join all strings in array

您能否提供未使用的代码,或者仅说明代码的内容和原因?它目前有点难以掌握。谢谢!
Gaurang Tandon 2014年

@GaurangTandon添加了未注释的代码以及注释
nderscore 2014年

2

Rebol-252个字符

c: complement charset"<>"f: func[s n][t: e: 0 to-string collect[parse s[any[(m: 0)copy w[["</"some c">"](-- t)|["<"some c"/>"]|["<"some c">"](++ t)| any c(m: 1)](if e = 0[if m = 1[w: copy/part w n n: n - length? w]keep w]if all[n <= 0 t = 0][e: 1])]]]]

取消评论:

c: complement charset "<>"

f: func [s n] [
    t: e: 0             ;; tag level (nesting) & end output flag
    to-string collect [
        parse s [
            any [
                (m: 0)                            ;; tag mode
                copy w [
                      ["</" some c ">" ] (-- t)   ;; close tag
                    | ["<"  some c "/>"]          ;; self-closing / void elements
                    | ["<"  some c ">" ] (++ t)   ;; open tag
                    | any c (m: 1)                ;; text mode
                ] (
                    ;; flag not set so can still output
                    if e = 0 [
                        ;; in text mode - so trim text
                        if m = 1 [
                            w: copy/part w n
                            n: n - length? w
                        ]
                        keep w
                    ]

                    ; if all trimmed and returned to flat tag level then end future output
                    if all [n <= 0  t = 0] [e: 1]
                )
            ]
        ]
    ]
]

Rebol控制台中的示例:

>> f "<i>test</i>" 3
== "<i>tes</i>"

>> f {<strong><i>more</i> <span style="color: red">complicated</span></strong>} 7
== {<strong><i>more</i> <span style="color: red">co</span></strong>}

>> f {no html} 2
== "no"

>> f {<b>no</b> <i>html root</i>} 5
== "<b>no</b> <i>ht</i>"

>> f {<b>no img</b><img src="test.png" />more text} 6
== "<b>no img</b>"

>> f {<i>a</i><b></b>} 1
== "<i>a</i>"

>> f {<strong><i>even</i> <span style="color: red">more <b>difficult</b></span></strong>} 14
== {<strong><i>even</i> <span style="color: red">more <b>diff</b></span></strong>}

>> f {<strong><i>even</i> <span style="color: red">more <b>difficult</b></span></strong>} 3 
== {<strong><i>eve</i><span style="color: red"><b></b></span></strong>}

同样,这违反了最后一条规则:在字符数限制之后打开的标签应从输出中省略(请参见最后一个示例)。在最后一个示例中,应该省略span和b标签。这个规则使挑战几乎是不可能的。
edc65

@ edc65-不幸的是(@David Frank)尚未评论或更新他的示例,因此不清楚他是否想要这种行为?我希望我的最后一个例子能引起轰动!照原样离开,直到我们得到澄清。无论如何,只需花费额外的17个字符就可以按照您建议的方式工作。我不太喜欢这种骇客,所以在这里改写了它(ungolfed)-gist.github.com/draegtun/93682f5a07c40bd86e31
draegtun 2014年

0

Ruby ...非常不适合循环播放

def split(str,n)

  i = current = 0 
  return_str = ""

  while i < n
    if str[current] == "<"
      while str[current] != ">"
        return_str.concat str[current]
        current += 1
      end
      return_str.concat str[current]
      current += 1
    else
      return_str.concat str[current]
      i += 1
      current += 1
    end
  end

  while current < str.length
    if str[current] == "<"
      while str[current] != ">"
        return_str.concat str[current]
        current += 1
      end
      return_str.concat str[current]
      current += 1
    end
    current += 1
  end


  return_str + str[current..-1]
end

这个问题被标记为codegolf,您应该回答一下。您可以通过将变量名称替换为一个字母名称,使用较短的函数名称并尽可能删除空格来开始
sagiksp

0

(IE)JS-135

function f(t,n){b=document.body;b.innerHTML=t;r=b.createTextRange();r.moveStart("character",n);r.select();r.execCommand('cut');return b.innerHTML}

现在我感到肮脏。但是需要开始删除所有这些字符...

function f(t,n)
{b=document.body;b.innerHTML=t;r=b.createTextRange();r.collapse();r.moveEnd("character",n);
r.select();return r.htmlText}

免责声明:

  • 在IE控制台中运行

1
这打破了最后一个(疯狂的)规则:在字符限制之后打开的标签应从输出中省略(尝试上面注释中的示例)。
edc65

@ edc65希望,所有的规则更新版本检查
eithed
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.