Skip to content

Instantly share code, notes, and snippets.

@skoji
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skoji/9476487 to your computer and use it in GitHub Desktop.
Save skoji/9476487 to your computer and use it in GitHub Desktop.
NoraMark (http://github.com/skoji/noramark) customization sample
text =<<EOF
speak(Alice): Alice is speaking.
speak(Bob): and this is Bob.
EOF
document = NoraMark::Document.parse(text)
document.add_transformer(generator: :html) do
modify "speak" do
@node.name = 'p'
@node.prepend_child inline('span', @node.parameters[0], classes: ['speaker'])
end
end
puts document.html[0]
<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>NoraMark generated document</title>
</head>
<body>
<p><span class='speaker'>Alice</span>Alice is speaking.</p>
<p><span class='speaker'>Bob</span>and this is Bob.</p>
</body>
</html>
text = <<EOF
---
lang: ja
---
=: 見出し
パラグラフ。
パラグラフ。
EOF
document = NoraMark::Document.parse(text)
document.add_transformer(generator: :html) do
replace({:type => :HeadedSection}) do # 引数はNodeセレクタ
# この中では@nodeで処理対象のNodeにアクセスできる
# block() はBlock Nodeを生成するメソッド。inline()もある。他のNodeも直接Newすることは可能だが通常は不要
header = block('header',
block('div',
block("h#{@node.level}", @node.heading),
classes: ['hgroup']))
body = block('div', @node.children, classes:['section-body'])
block('section', [ header, body ], template: @node) # 操作が:replaceの場合は、このブロックが返すNodeで元のNodeが置き換えられる
end
end
document.html[0] # output follows
<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml" lang="ja" xml:lang="ja">
<head>
<title>NoraMark generated document</title>
</head>
<body>
<section><header><div class='hgroup'><h1 id='heading_index_1'>見出し</h1>
</div>
</header>
<div class='section-body'><div class='pgroup'><p>パラグラフ。</p>
<p>パラグラフ。</p>
</div>
</div>
</section>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment