如何使用Javascript或Ruby智能地为大块文本添加换行符

时间:2016-01-06 22:49:57

标签: javascript ruby

我正在寻找一个库或算法,可以使用我的长文本块没有换行符,并将其拆分成对人类可读性最有意义的段落。

实施例

I am looking for a library or algorithm that can take my long block of text that has no line breaks and split it into paragraphs that mostly make sense to human readability. On a different note, there is something else.

对此:

I am looking for a library or algorithm that can take my long block of text that has no line breaks and split it into paragraphs that mostly make sense to human readability.

On a different note, there is something else.

1 个答案:

答案 0 :(得分:0)

如果你只想要点的换行符,你可以使用(对于js):

mybigline.split(". ").join("\n\n");

如果您还想将每一行格式化为maxlen图表,打破单词,您还可以在单​​词边界处打破每个段落,例如:

var maxlen = 20;
var line = "vlaze lkdf lskdjf sldfsldfk sldkjf sldsd qsdkj qlskdj qlsdkj qlsdkj qlsdkj qlsdkj sldkfjsldfj fkj sldkfj s. qsldkjqsdlj. skdjhqksdjhqskjdq sd.";

// split into lines at dots
var par = line.split(". ");
var out = []
for (i in par) {
  // each line into words
  var pline = par[i].split(" ");
  var curline = "";
  for (j in pline) {
    // add words to output up to maxlength
    var curch = pline[j];
    if(curline.length + curch.length < maclent) {
      curline += curch + (pline.length==1?"":" ");
    } else {
      out.push(curline);
      curline = curch + (pline.length==1?"":" ");    
    }
  }
  out.push(curline);
  out.push("\n");
}
res = out.join("\n");
alert(res);

https://jsfiddle.net/37Lbj701/