ジェイドテンプレートエンジン(nower node.js):パイプシンボルのないマルチラインブロック

StackOverflow https://stackoverflow.com/questions/4717703

  •  12-10-2019
  •  | 
  •  

質問

現在、新しいプロジェクトでJadeを使用しています。 WebAppレイアウトを作成するのに適しているように思えますが、の静的コンテンツを作成するのには適していません。

テキストを含む要素。

たとえば、そのような段落を作成するには、これを行う必要があると思います。

p
  | This is my long,
  | multi-line
  | paragraph.

テキストの実際の段落でいっぱいの静的なWebページの場合、Jadeを使用することは、各行の先頭にそのパイプシンボルのために負担になります。

パイプシンボルが行ごとに行うように、ブロック全体をテキストノードとしてマークするためのある種の構文砂糖はありますか?または私が気づいていない既存のフィルター?

私が探求している解決策の1つは、A:ブロックフィルターまたは何かの作成です。そして、それをジェイドに渡しますが、フィルターの作成に関するジェイドのドキュメントは控えめに言ってもまばらであるため、理解するのに時間がかかるかもしれません。誰かがそのような解決策に関するガイダンスを提供できるなら、私はそれを感謝します。

役に立ちましたか?

解決

から Jade Githubページ:

p.
foo asdf
asdf
 asdfasdfaf
 asdf
asd.

出力を生成します:

<p>foo asdf
asdf
  asdfasdfaf
  asdf
asd
.
</p>

後の後期 p あなたが探しているものです。

他のヒント

いじくり回した後、私はこれを達成するフィルターの詳細を解決しました。これはジェイドを使用して他の人に役立つと思うので、ここに答えを投稿してください。

フィルターを作成するコードは非常に単純であることが判明しました:

var jade = require ("jade");

jade.filters.text = function(block, compiler){
    return new TextBlockFilter(block).compile();
};

function TextBlockFilter(node) {
    this.node = node;
}

TextBlockFilter.prototype.__proto__ = jade.Compiler.prototype;

TextBlockFilter.prototype.visit = function(node){

    // first this is called with a node containing all the block's lines
    // as sub-nodes, with their first word interpreted as the node's name
    //
    // so here, collect all the nodes' text (including its name)
    // into a single Text node, and then visit that instead.
    // the child nodes won't be visited - we're cutting them out of the
    // parse tree

    var text = new jade.nodes.Text();
    for (var i=0; i < node.length; i++) {
        text.push (node[i].name + (node[i].text ? node[i].text[0] : ""));
    }
    this.visitNode (text);
};

そして、マークアップは次のようになります。その間に他のジェイドのものを含めることができることに注意してください:テキストブロック:

p
  :text
    This is my first line of text,
    followed by another
    and another.  Now let's include a jade link tag:
  a(href="http://blahblah.com")
  :text
    and follow it with even more text 
    and more,
    etc
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top