3

以下のようなHTMLがたくさんあるとしましょう:

bla bla bla long paragraph here
<br/>
<br/>
bla bla bla more paragraph text
<br/>
<br/>

<p>Javascript を使用して適切なセマンティックタグに変換する簡単な方法はありますか? 例えば:

<p>
  bla bla bla long paragraph here
</p>
<p>
  bla bla bla more paragraph text
</p>

出力間隔は重要ではありません。理想的には、任意の入力間隔で機能します。

正規表現を作成しようと考えていますが、その前に、a) 傷ついた世界を避けていることと、b) 他に何かがないことを確認したかったのです-私はやろうとしましたグーグル検索ですが、まだ何も出ていません。

アドバイスをありがとう!

4

4 に答える 4

7

I got bored. I'm sure there are optimizations / tweaks needed. Uses a little bit of jQuery to do its magic. Worked in FF3. And the answer to your question is that there isnt a very "simple" way :)

$(function() {
  $.fn.pmaker = function() {
    var brs = 0;
    var nodes = [];

    function makeP()
    {
      // only bother doing this if we have nodes to stick into a P
      if (nodes.length) {
        var p = $("<p/>");
        p.insertBefore(nodes[0]);  // insert a new P before the content
        p.append(nodes); // add the children        
        nodes = [];
      }
      brs=0;
    }

    this.contents().each(function() {    
      if (this.nodeType == 3) // text node 
      {
        // if the text has non whitespace - reset the BR counter
        if (/\S+/.test(this.data)) {
          nodes.push(this);
          brs = 0;
        }
      } else if (this.nodeType == 1) {
        if (/br/i.test(this.tagName)) {
          if (++brs == 2) {
            $(this).remove(); // remove this BR from the dom
            $(nodes.pop()).remove(); // delete the previous BR from the array and the DOM
            makeP();
          } else {
            nodes.push(this);
          }
        } else if (/^(?:p)$/i.test(this.tagName)) {
          // these tags for the P break but dont scan within
          makeP();
        } else if (/^(?:div)$/i.test(this.tagName)) {
          // force a P break and scan within
          makeP();
          $(this).pmaker();
        } else {
          brs = 0; // some other tag - reset brs.
          nodes.push(this); // add the node 
          // specific nodes to not peek inside of - inline tags
          if (!(/^(?:b|i|strong|em|span|u)$/i.test(this.tagName))) {
            $(this).pmaker(); // peek inside for P needs            
          }
        } 
      } 
    });
    while ((brs--)>0) { // remove any extra BR's at the end
      $(nodes.pop()).remove();
    }
    makeP();
    return this;
  };

  // run it against something:
  $(function(){ 
    $("#worker").pmaker();
  });

And this was the html portion I tested against:

<div id="worker">
bla bla bla long <b>paragraph</b> here
<br/>
<br/>
bla bla bla more paragraph text
<br/>
<br/>
this text should end up in a P
<div class='test'>
  and so should this
  <br/>
  <br/>
  and this<br/>without breaking at the single BR
</div>
and then we have the a "buggy" clause
<p>
  fear the real P!
</p>
and a trailing br<br/>
</div>

And the result:

<div id="worker"><p>
bla bla bla long <b>paragraph</b> here
</p>
<p>
bla bla bla more paragraph text
</p>
<p>
this text should end up in a P
</p><div class="test"><p>
  and so should this
  </p>
  <p>
  and this<br/>without breaking at the single BR
</p></div><p>
and then we have the a "buggy" clause
</p><p>
  fear the real P!
</p><p>
and a trailing br</p>
</div>
于 2009-08-14T01:05:47.887 に答える
5

各子要素 + 囲んでいる要素のテキストをスキャンします。「br」要素に遭遇するたびに、「p」要素を作成し、保留中のものをすべて追加します。泡立てて、すすぎ、繰り返します。

新しい「p」要素に再配置するものを削除することを忘れないでください。

このライブラリ (prototype.js)は、この種の作業に役立つことがわかりました。

于 2009-08-13T23:44:57.143 に答える
4

場合によっては、単一の改行を保持する必要があり (すべての要素が悪いわけではありません)、 の二重インスタンスのみを段落区切りに変換し<br />たい場合があります。<br />

そうすることで、私は:

  1. すべての改行を削除
  2. 全体を段落にまとめる
  3. <br /><br />と置き換えます</p>\n<p>
  4. <p></p>最後に、生成された可能性のある空の要素をすべて削除します

したがって、コードは次のようになります。

var ConvertToParagraphs = function(text) {
    var lineBreaksRemoved = text.replace(/\n/g, "");
    var wrappedInParagraphs = "<p>" + lineBreaksRemoved + "</p>";
    var brsRemoved = wrappedInParagraphs.replace(/<br[^>]*>[\s]*<br[^>]*>/gi, "</p>\n<p>");
    var emptyParagraphsRemoved = brsRemoved.replace(/<p><\/p>/g, "");
    return emptyParagraphsRemoved;
}

注:プロセスを示すために非常に冗長でした。もちろん、単純化してください。

これにより、サンプルが次のようになります。

bla bla bla long paragraph here
<br/>
<br/>
bla bla bla more paragraph text
<br/>
<br/>

の中へ:

<p>bla bla bla long paragraph here</p>
<p>bla bla bla more paragraph text</p>

<br />ただし、実際に必要な要素を削除することなくそうします。

于 2009-08-14T01:47:30.060 に答える
0

私はいくつかの段階でそれを行います:

  1. RegExp: すべての br タグを改行に変換します。
  2. RegExp: すべての空白を取り除きます。
  3. RegExp: 複数の改行を単一の改行に変換します。
  4. 結果に Array.split('\n') を使用します。

これにより、すべての「実際の」段落を含む配列が得られるはずです(理論上)。その後、それを反復処理して、各行を p-tag でラップできます。

于 2009-08-13T23:43:29.583 に答える