Skip to content

Commit

Permalink
Merge pull request #881 from kmuto/markdown-add-markup
Browse files Browse the repository at this point in the history
MARKDOWNBuilder: support some commands
  • Loading branch information
takahashim authored Jun 21, 2018
2 parents 2deb034 + c883043 commit 9c30ede
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 2 deletions.
64 changes: 62 additions & 2 deletions lib/review/markdownbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def extname
end

def builder_init_file
@noindent = nil
@blank_seen = nil
@ul_indent = 0
@chapter.book.image_types = %w[.png .jpg .jpeg .gif .svg]
Expand Down Expand Up @@ -48,8 +49,18 @@ def quote(lines)
end

def paragraph(lines)
puts lines.join
puts "\n"
if @noindent
puts %Q(<p class="noindent">#{lines.join}</p>)
puts "\n"
@noindent = nil
else
puts lines.join
puts "\n"
end
end

def noindent
@noindent = true
end

def list_header(id, caption, lang)
Expand Down Expand Up @@ -129,6 +140,14 @@ def emlist(lines, caption = nil, lang = nil)
blank
end

def captionblock(type, lines, caption, _specialstyle = nil)
puts %Q(<div class="#{type}">)
puts %Q(<p class="caption">#{compile_inline(caption)}</p>) if caption.present?
blocked_lines = split_paragraph(lines)
puts blocked_lines.join("\n")
puts '</div>'
end

def hr
puts '----'
end
Expand Down Expand Up @@ -160,10 +179,22 @@ def inline_code(str)
"`#{str}`"
end

def inline_sub(str)
"<sub>#{str}</sub>"
end

def inline_sup(str)
"<sup>#{str}</sup>"
end

def inline_tt(str)
"`#{str}`"
end

def inline_u(str)
"<u>#{str}</u>"
end

def image_image(id, caption, _metric)
blank
puts "![#{compile_inline(caption)}](#{@chapter.image(id).path.sub(%r{\A\./}, '')})"
Expand All @@ -180,6 +211,10 @@ def inline_img(id)
error "unknown image: #{id}"
end

def inline_dtp(str)
"<!-- DTP:#{str} -->"
end

def indepimage(_lines, id, caption = '', _metric = nil)
blank
puts "![#{compile_inline(caption)}](#{@chapter.image(id).path.sub(%r{\A\./}, '')})"
Expand Down Expand Up @@ -299,6 +334,16 @@ def compile_ruby(base, ruby)
end
end

def compile_kw(word, alt)
%Q(<b class="kw">) +
if alt
escape_html(word + " (#{alt.strip})")
else
escape_html(word)
end +
"</b><!-- IDX:#{escape_comment(escape_html(word))} -->"
end

def comment(lines, comment = nil)
return unless @book.config['draft']
lines ||= []
Expand All @@ -309,12 +354,27 @@ def comment(lines, comment = nil)
puts %Q(<div class="red">#{escape(str)}</div>)
end

def inline_icon(id)
begin
"![](#{@chapter.image(id).path.sub(%r{\A\./}, '')})"
rescue
warn "image not bound: #{id}"
%Q(<pre>missing image: #{id}</pre>)
end
end

def inline_comment(str)
if @book.config['draft']
%Q(<span class="red">#{escape(str)}</span>)
else
''
end
end

def flushright(lines)
puts %Q(<div class="flushright">)
puts lines.join
puts %Q(</div>)
end
end
end # module ReVIEW
10 changes: 10 additions & 0 deletions test/test_markdownbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ def test_quote
assert_equal %Q(\n> foobar\n> \n> buz\n\n), actual
end

def test_memo
actual = compile_block("//memo[this is @<b>{test}<&>_]{\ntest1\n\ntest@<i>{2}\n//}\n")
assert_equal %Q(<div class="memo">\n<p class="caption">this is **test**<&>_</p>\ntest1\ntest*2*\n</div>\n), actual
end

def test_noindent
actual = compile_block("//noindent\nfoo\nbar\n\nfoo2\nbar2\n")
assert_equal %Q(<p class="noindent">foobar</p>\n\nfoo2bar2\n\n), actual
end

def test_inline_em
assert_equal 'test*foo*abc', compile_inline('test@<em>{foo}abc')
end
Expand Down

0 comments on commit 9c30ede

Please sign in to comment.