From b7ef0ad33c81110b0fd6dd02637314fa8dbfa851 Mon Sep 17 00:00:00 2001 From: vvakame Date: Sat, 10 Sep 2016 13:57:16 +0900 Subject: [PATCH 1/3] implement MD2INAOBuilder --- lib/review/md2inaobuilder.rb | 57 ++++++++++++++++++++++++++++++++ test/test_md2inaobuilder.rb | 64 ++++++++++++++++++++++++++++++++++++ 2 files changed, 121 insertions(+) create mode 100644 lib/review/md2inaobuilder.rb create mode 100644 test/test_md2inaobuilder.rb diff --git a/lib/review/md2inaobuilder.rb b/lib/review/md2inaobuilder.rb new file mode 100644 index 000000000..1cabee8b8 --- /dev/null +++ b/lib/review/md2inaobuilder.rb @@ -0,0 +1,57 @@ +# -*- coding: utf-8 -*- +# This program is free software. +# You can distribute or modify this program under the terms of +# the GNU LGPL, Lesser General Public License version 2.1. + +require 'review/builder' + +module ReVIEW + + class MD2INAOBuilder < MARKDOWNBuilder + def paragraph(lines) + puts " " + lines.join + puts "\n" + end + + def list_header(id, caption, lang) + lang ||= "" + puts "```#{lang}" + print %Q[●リスト#{@chapter.list(id).number}::#{compile_inline(caption)}\n\n] + end + + def cmd(lines) + # WEB+DB では使っていないらしいけど + puts "!!! cmd" + lines.each do |line| + puts detab(line) + end + puts "" + end + + def dl_begin + puts '
' + end + + def dt(line) + puts "
#{line}
" + end + + def dd(lines) + puts "
#{lines.join}
" + end + + def dl_end + puts '
' + end + + def comment(lines, comment = nil) + lines ||= [] + lines.unshift comment unless comment.blank? + str = lines.join("\n") + puts '' + puts str + puts '' + end + end + +end # module ReVIEW diff --git a/test/test_md2inaobuilder.rb b/test/test_md2inaobuilder.rb new file mode 100644 index 000000000..8e32e0a4f --- /dev/null +++ b/test/test_md2inaobuilder.rb @@ -0,0 +1,64 @@ +# encoding: utf-8 + +require 'test_helper' +require 'review/compiler' +require 'review/book' +require 'review/md2inaobuilder' +require 'review/i18n' + +class MD2INAOBuilderTest < Test::Unit::TestCase + include ReVIEW + + def setup + @builder = MD2INAOBuilder.new() + @config = { + "secnolevel" => 2, # for IDGXMLBuilder, HTMLBuilder + "stylesheet" => nil, # for HTMLBuilder + } + @book = Book::Base.new(".") + @book.config = @config + @compiler = ReVIEW::Compiler.new(@builder) + @chapter = Book::Chapter.new(@book, 1, '-', nil, StringIO.new) + location = Location.new(nil, nil) + @builder.bind(@compiler, @chapter, location) + I18n.setup("ja") + end + + def test_paragraph + actual = compile_block("Hello, world!") + assert_equal " Hello, world!\n\n", actual + end + + def test_cmd + actual = compile_block("//cmd{\nlineA\nlineB\n//}\n") + assert_equal "!!! cmd\nlineA\nlineB\n\n", actual + end + + def test_dlist + actual = compile_block(": foo\n foo.\n bar.\n") + assert_equal "
\n
foo
\n
foo.bar.
\n
\n", actual + end + + def test_list + actual = compile_block(<<-EOS) +//list[name][caption]{ +AAA +BBB +//} + EOS + + assert_equal <<-EOS, actual +``` +●リスト1::caption + +AAA +BBB +``` + EOS + end + + def test_comment + actual = compile_block("//comment{\nHello, world!\n//}\n") + assert_equal "\nHello, world!\n\n", actual + end +end From 82a3391a7a4b8c591a00bd87cdc7293add0d79b1 Mon Sep 17 00:00:00 2001 From: vvakame Date: Sat, 10 Sep 2016 14:15:13 +0900 Subject: [PATCH 2/3] add ruby support to MARKDOWNBuilder & MD2INAOBuilder --- lib/review/markdownbuilder.rb | 11 +++++++++++ lib/review/md2inaobuilder.rb | 9 +++++++++ test/test_markdownbuilder.rb | 6 ++++++ test/test_md2inaobuilder.rb | 11 +++++++++++ 4 files changed, 37 insertions(+) diff --git a/lib/review/markdownbuilder.rb b/lib/review/markdownbuilder.rb index d32b4a769..0be8399b7 100644 --- a/lib/review/markdownbuilder.rb +++ b/lib/review/markdownbuilder.rb @@ -5,11 +5,13 @@ require 'review/builder' require 'review/textutils' +require 'review/htmlutils' module ReVIEW class MARKDOWNBuilder < Builder include TextUtils + include HTMLUtils def extname '.md' @@ -290,6 +292,15 @@ def inline_br(str) def nofunc_text(str) str end + + def compile_ruby(base, ruby) + if @book.htmlversion == 5 + %Q[#{escape_html(base)}#{I18n.t("ruby_prefix")}#{escape_html(ruby)}#{I18n.t("ruby_postfix")}] + else + %Q[#{escape_html(base)}#{I18n.t("ruby_prefix")}#{ruby}#{I18n.t("ruby_postfix")}] + end + end + end end # module ReVIEW diff --git a/lib/review/md2inaobuilder.rb b/lib/review/md2inaobuilder.rb index 1cabee8b8..b7d4987da 100644 --- a/lib/review/md2inaobuilder.rb +++ b/lib/review/md2inaobuilder.rb @@ -52,6 +52,15 @@ def comment(lines, comment = nil) puts str puts '' end + + def compile_ruby(base, ruby) + if base.length == 1 + %Q[#{escape_html(base)}(#{escape_html(ruby)})] + else + %Q[#{escape_html(base)}(#{escape_html(ruby)})] + end + end + end end # module ReVIEW diff --git a/test/test_markdownbuilder.rb b/test/test_markdownbuilder.rb index 3b91edf65..ba06a2ebc 100644 --- a/test/test_markdownbuilder.rb +++ b/test/test_markdownbuilder.rb @@ -139,4 +139,10 @@ def test_table actual = compile_block("//table{\ntestA\ttestB\n------------\ncontentA\tcontentB\n//}\n") assert_equal "|testA|testB|\n|:--|:--|\n|contentA|contentB|\n\n", actual end + + def test_ruby + actual = compile_block("@{謳,うた}い文句") + assert_equal "うたい文句\n\n", actual + end + end diff --git a/test/test_md2inaobuilder.rb b/test/test_md2inaobuilder.rb index 8e32e0a4f..1ee2b3cf5 100644 --- a/test/test_md2inaobuilder.rb +++ b/test/test_md2inaobuilder.rb @@ -61,4 +61,15 @@ def test_comment actual = compile_block("//comment{\nHello, world!\n//}\n") assert_equal "\nHello, world!\n\n", actual end + + def test_ruby_mono + actual = compile_block("@{謳,うた}い文句") + assert_equal " 謳(うた)い文句\n\n", actual + end + + def test_ruby_group + actual = compile_block("@{欠伸,あくび}が出る") + assert_equal " 欠伸(あくび)が出る\n\n", actual + end + end From 05079cdd0e2c18216312fec679bfbc5c32c1d6ad Mon Sep 17 00:00:00 2001 From: vvakame Date: Sat, 10 Sep 2016 14:33:50 +0900 Subject: [PATCH 3/3] refactor --- lib/review/md2inaobuilder.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/review/md2inaobuilder.rb b/lib/review/md2inaobuilder.rb index b7d4987da..4dc290abf 100644 --- a/lib/review/md2inaobuilder.rb +++ b/lib/review/md2inaobuilder.rb @@ -3,7 +3,7 @@ # You can distribute or modify this program under the terms of # the GNU LGPL, Lesser General Public License version 2.1. -require 'review/builder' +require 'review/markdownbuilder' module ReVIEW