From f51b8ce1d48da35b369c2604a68def4376840839 Mon Sep 17 00:00:00 2001 From: takahashim Date: Thu, 24 Nov 2016 02:09:50 +0900 Subject: [PATCH 1/9] support Rouge --- lib/review/htmlutils.rb | 39 ++++++++++++++++++++++++++++++++++-- review.gemspec | 1 + test/test_htmlbuilder.rb | 43 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 2 deletions(-) diff --git a/lib/review/htmlutils.rb b/lib/review/htmlutils.rb index 8aef9edf2..d9c4b0c58 100644 --- a/lib/review/htmlutils.rb +++ b/lib/review/htmlutils.rb @@ -9,6 +9,7 @@ # require 'cgi/util' +require 'rouge' module ReVIEW module HTMLUtils @@ -44,14 +45,25 @@ def escape_comment(str) def highlight? @book.config["highlight"] && - @book.config["highlight"]["html"] == "pygments" + @book.config["highlight"]["html"] end def highlight(ops) if @book.config["pygments"].present? raise ReVIEW::ConfigError, "'pygments:' in config.yml is obsoleted." end + return ops[:body].to_s if !highlight? + if @book.config["highlight"]["html"] == "pygments" + highlight_pygments(ops) + elsif @book.config["highlight"]["html"] == "rouge" + highlight_rouge(ops) + else + raise ReVIEW::ConfigError, "unknown highlight method #{@book.config["highlight"]["html"]} in config.yml." + end + end + + def highlight_pygments(ops) body = ops[:body] || '' if @book.config["highlight"] && @book.config["highlight"]["lang"] lexer = @book.config["highlight"]["lang"] # default setting @@ -64,7 +76,6 @@ def highlight(ops) if ops[:options] && ops[:options].kind_of?(Hash) options.merge!(ops[:options]) end - return body if !highlight? begin require 'pygments' @@ -81,6 +92,30 @@ def highlight(ops) end end + def highlight_rouge(ops) + body = ops[:body] || '' + if ops[:lexer].present? + lexer = ops[:lexer] + elsif @book.config["highlight"] && @book.config["highlight"]["lang"] + lexer = @book.config["highlight"]["lang"] # default setting + else + lexer = 'text' + end + format = ops[:format] || '' + + lexer = Rouge::Lexer.find(lexer) + raise "unknown lexer #{lexer}" unless lexer + + #formatter = Rouge::Formatters::HTML.new() + #formatter = Rouge::Formatters::HTMLLegacy.new(:css_class => "highlight #{lexer.tag}") + #formatter = Rouge::Formatters::HTMLPygments.new(Rouge::Formatters::HTML.new, "highlight #{lexer.tag}") + formatter = Rouge::Formatters::HTMLInline.new(Rouge::Themes::Colorful.new) + raise "unknown formatter #{formatter}" unless formatter + + text = unescape_html(body) + formatter.format(lexer.lex(text)) + end + def normalize_id(id) if id =~ /\A[a-z][a-z0-9_.-]*\Z/i return id diff --git a/review.gemspec b/review.gemspec index 5b03e182c..c11e64c06 100644 --- a/review.gemspec +++ b/review.gemspec @@ -24,6 +24,7 @@ Gem::Specification.new do |gem| gem.require_paths = ["lib"] gem.add_dependency("rubyzip") + gem.add_dependency("rouge") gem.add_development_dependency("rake") gem.add_development_dependency("test-unit") gem.add_development_dependency("pygments.rb") diff --git a/test/test_htmlbuilder.rb b/test/test_htmlbuilder.rb index adc52b656..8720aa9b0 100644 --- a/test/test_htmlbuilder.rb +++ b/test/test_htmlbuilder.rb @@ -618,6 +618,49 @@ def @chapter.list(id) assert_equal expected, actual end + def test_list_rouge + def @chapter.list(id) + Book::ListIndex::Item.new("samplelist",1) + end + @book.config["highlight"] = {} + @book.config["highlight"]["html"] = "rouge" + actual = compile_block("//list[samplelist][this is @{test}<&>_]{\ntest1\ntest1.5\n\ntest@{2}\n//}\n") + + assert_equal %Q|
\n

リスト1.1: this is test<&>_

\n
test1\ntest1.5\n\ntest<i>2</i>\n
\n
\n|, actual + end + + def test_list_rouge_lang + def @chapter.list(id) + Book::ListIndex::Item.new("samplelist",1) + end + @book.config["highlight"] = {} + @book.config["highlight"]["html"] = "rouge" + actual = compile_block("//list[samplelist][this is @{test}<&>_][ruby]{\ndef foo(a1, a2=:test)\n (1..3).times{|i| a.include?(:foo)}\n return true\nend\n\n//}\n") + + expected = "
\n" + + "

リスト1.1: this is test<&>_

\n" + + "
def foo(a1, a2=:test)\n" +
+               "  (1..3).times{|i| a.include?(:foo)}\n" +
+               "  return true\n" +
+               "end\n" +
+               "\n" +
+               "
\n" + + "
\n" + + assert_equal expected, actual + end + + def test_list_rouge_nulllang + def @chapter.list(id) + Book::ListIndex::Item.new("samplelist",1) + end + @book.config["highlight"] = {} + @book.config["highlight"]["html"] = "rouge" + actual = compile_block("//list[samplelist][this is @{test}<&>_][]{\ndef foo(a1, a2=:test)\n (1..3).times{|i| a.include?(:foo)}\n return true\nend\n\n//}\n") + + assert_equal "
\n

リスト1.1: this is test<&>_

\n
def foo(a1, a2=:test)\n  (1..3).times{|i| a.include?(:foo)}\n  return true\nend\n\n
\n
\n", actual + end + def test_list_ext def @chapter.list(id) Book::ListIndex::Item.new("samplelist.rb",1) From f63c2e98ccd25bf9b87be10333ff053a787cfefd Mon Sep 17 00:00:00 2001 From: takahashim Date: Sun, 11 Dec 2016 02:14:36 +0900 Subject: [PATCH 2/9] fix css use classes instead of inline style attributes --- lib/review/htmlutils.rb | 3 ++- test/test_htmlbuilder.rb | 16 ++++++++-------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/lib/review/htmlutils.rb b/lib/review/htmlutils.rb index d9c4b0c58..cb9273065 100644 --- a/lib/review/htmlutils.rb +++ b/lib/review/htmlutils.rb @@ -107,9 +107,10 @@ def highlight_rouge(ops) raise "unknown lexer #{lexer}" unless lexer #formatter = Rouge::Formatters::HTML.new() + formatter = Rouge::Formatters::HTML.new(:css_class => 'highlight') #formatter = Rouge::Formatters::HTMLLegacy.new(:css_class => "highlight #{lexer.tag}") #formatter = Rouge::Formatters::HTMLPygments.new(Rouge::Formatters::HTML.new, "highlight #{lexer.tag}") - formatter = Rouge::Formatters::HTMLInline.new(Rouge::Themes::Colorful.new) + #formatter = Rouge::Formatters::HTMLInline.new(Rouge::Themes::Colorful.new) raise "unknown formatter #{formatter}" unless formatter text = unescape_html(body) diff --git a/test/test_htmlbuilder.rb b/test/test_htmlbuilder.rb index 8720aa9b0..dff9c21c6 100644 --- a/test/test_htmlbuilder.rb +++ b/test/test_htmlbuilder.rb @@ -638,14 +638,14 @@ def @chapter.list(id) actual = compile_block("//list[samplelist][this is @{test}<&>_][ruby]{\ndef foo(a1, a2=:test)\n (1..3).times{|i| a.include?(:foo)}\n return true\nend\n\n//}\n") expected = "
\n" + - "

リスト1.1: this is test<&>_

\n" + - "
def foo(a1, a2=:test)\n" +
-               "  (1..3).times{|i| a.include?(:foo)}\n" +
-               "  return true\n" +
-               "end\n" +
-               "\n" +
-               "
\n" + - "
\n" + "

リスト1.1: this is test<&>_

\n" + + "
def foo(a1, a2=:test)\n" +
+	           "  (1..3).times{|i| a.include?(:foo)}\n" +
+	           "  return true\n" +
+	           "end\n" +
+	           "\n" +
+	           "
\n" + + "\n" assert_equal expected, actual end From c91529fc5ff3ca7a4ab6d25fd1f40b2623fd84c2 Mon Sep 17 00:00:00 2001 From: takahashim Date: Fri, 23 Dec 2016 14:18:16 +0900 Subject: [PATCH 3/9] fix sample config.yml --- doc/config.yml.sample | 4 ++-- doc/config.yml.sample-simple | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/doc/config.yml.sample b/doc/config.yml.sample index 5550ca1af..a9c10fe56 100644 --- a/doc/config.yml.sample +++ b/doc/config.yml.sample @@ -172,9 +172,9 @@ secnolevel: 2 # fontdir内から取り込まれる対象となるファイル拡張子。省略した場合は以下 # font_ext: ["ttf", "woff", "otf"] -# ソースコードハイライトを利用する (pygmentsには外部gemが必要) +# ソースコードハイライトを利用する (rouge,pygmentsには外部gemが必要) # highlight: -# html: "pygments" +# html: "rouge" # latex: "listings" # カタログファイル名を指定する diff --git a/doc/config.yml.sample-simple b/doc/config.yml.sample-simple index 8e40dcd8e..4f2b98e0c 100644 --- a/doc/config.yml.sample-simple +++ b/doc/config.yml.sample-simple @@ -51,7 +51,7 @@ colophon: true ## Syntax Highlighting highlight: - html: "pygments" + html: "rouge" latex: "listings" ## for HTML From 1edd3baa0c6c4aec33bbdc794936b9a5272694a3 Mon Sep 17 00:00:00 2001 From: takahashim Date: Fri, 23 Dec 2016 14:18:51 +0900 Subject: [PATCH 4/9] support rouge in CSS --- doc/sample.css | 212 ++++++++++++++++++++++++++++++++ test/sample-book/src/style.css | 213 +++++++++++++++++++++++++++++++++ 2 files changed, 425 insertions(+) diff --git a/doc/sample.css b/doc/sample.css index 909554813..665cc3fee 100644 --- a/doc/sample.css +++ b/doc/sample.css @@ -107,6 +107,218 @@ p.flushright { text-align: right; } +/** + * from Rouge + */ +.highlight table td { padding: 5px; } +.highlight table pre { margin: 0; } +.highlight .cm { + color: #999988; + font-style: italic; +} +.highlight .cp { + color: #999999; + font-weight: bold; +} +.highlight .c1 { + color: #999988; + font-style: italic; +} +.highlight .cs { + color: #999999; + font-weight: bold; + font-style: italic; +} +.highlight .c, .highlight .cd { + color: #999988; + font-style: italic; +} +.highlight .err { + color: #a61717; + background-color: #e3d2d2; +} +.highlight .gd { + color: #000000; + background-color: #ffdddd; +} +.highlight .ge { + color: #000000; + font-style: italic; +} +.highlight .gr { + color: #aa0000; +} +.highlight .gh { + color: #999999; +} +.highlight .gi { + color: #000000; + background-color: #ddffdd; +} +.highlight .go { + color: #888888; +} +.highlight .gp { + color: #555555; +} +.highlight .gs { + font-weight: bold; +} +.highlight .gu { + color: #aaaaaa; +} +.highlight .gt { + color: #aa0000; +} +.highlight .kc { + color: #000000; + font-weight: bold; +} +.highlight .kd { + color: #000000; + font-weight: bold; +} +.highlight .kn { + color: #000000; + font-weight: bold; +} +.highlight .kp { + color: #000000; + font-weight: bold; +} +.highlight .kr { + color: #000000; + font-weight: bold; +} +.highlight .kt { + color: #445588; + font-weight: bold; +} +.highlight .k, .highlight .kv { + color: #000000; + font-weight: bold; +} +.highlight .mf { + color: #009999; +} +.highlight .mh { + color: #009999; +} +.highlight .il { + color: #009999; +} +.highlight .mi { + color: #009999; +} +.highlight .mo { + color: #009999; +} +.highlight .m, .highlight .mb, .highlight .mx { + color: #009999; +} +.highlight .sb { + color: #d14; +} +.highlight .sc { + color: #d14; +} +.highlight .sd { + color: #d14; +} +.highlight .s2 { + color: #d14; +} +.highlight .se { + color: #d14; +} +.highlight .sh { + color: #d14; +} +.highlight .si { + color: #d14; +} +.highlight .sx { + color: #d14; +} +.highlight .sr { + color: #009926; +} +.highlight .s1 { + color: #d14; +} +.highlight .ss { + color: #990073; +} +.highlight .s { + color: #d14; +} +.highlight .na { + color: #008080; +} +.highlight .bp { + color: #999999; +} +.highlight .nb { + color: #0086B3; +} +.highlight .nc { + color: #445588; + font-weight: bold; +} +.highlight .no { + color: #008080; +} +.highlight .nd { + color: #3c5d5d; + font-weight: bold; +} +.highlight .ni { + color: #800080; +} +.highlight .ne { + color: #990000; + font-weight: bold; +} +.highlight .nf { + color: #990000; + font-weight: bold; +} +.highlight .nl { + color: #990000; + font-weight: bold; +} +.highlight .nn { + color: #555555; +} +.highlight .nt { + color: #000080; +} +.highlight .vc { + color: #008080; +} +.highlight .vg { + color: #008080; +} +.highlight .vi { + color: #008080; +} +.highlight .nv { + color: #008080; +} +.highlight .ow { + color: #000000; + font-weight: bold; +} +.highlight .o { + color: #000000; + font-weight: bold; +} +.highlight .w { + color: #bbbbbb; +} +.highlight { + background-color: #f8f8f8; +} /** * from EBPAJ EPUB 3 File Creation Guide sample style diff --git a/test/sample-book/src/style.css b/test/sample-book/src/style.css index 75c67c1bd..cd8adabf3 100644 --- a/test/sample-book/src/style.css +++ b/test/sample-book/src/style.css @@ -250,6 +250,219 @@ em { font-style: italic; } +/** + * from Rouge + */ +.highlight table td { padding: 5px; } +.highlight table pre { margin: 0; } +.highlight .cm { + color: #999988; + font-style: italic; +} +.highlight .cp { + color: #999999; + font-weight: bold; +} +.highlight .c1 { + color: #999988; + font-style: italic; +} +.highlight .cs { + color: #999999; + font-weight: bold; + font-style: italic; +} +.highlight .c, .highlight .cd { + color: #999988; + font-style: italic; +} +.highlight .err { + color: #a61717; + background-color: #e3d2d2; +} +.highlight .gd { + color: #000000; + background-color: #ffdddd; +} +.highlight .ge { + color: #000000; + font-style: italic; +} +.highlight .gr { + color: #aa0000; +} +.highlight .gh { + color: #999999; +} +.highlight .gi { + color: #000000; + background-color: #ddffdd; +} +.highlight .go { + color: #888888; +} +.highlight .gp { + color: #555555; +} +.highlight .gs { + font-weight: bold; +} +.highlight .gu { + color: #aaaaaa; +} +.highlight .gt { + color: #aa0000; +} +.highlight .kc { + color: #000000; + font-weight: bold; +} +.highlight .kd { + color: #000000; + font-weight: bold; +} +.highlight .kn { + color: #000000; + font-weight: bold; +} +.highlight .kp { + color: #000000; + font-weight: bold; +} +.highlight .kr { + color: #000000; + font-weight: bold; +} +.highlight .kt { + color: #445588; + font-weight: bold; +} +.highlight .k, .highlight .kv { + color: #000000; + font-weight: bold; +} +.highlight .mf { + color: #009999; +} +.highlight .mh { + color: #009999; +} +.highlight .il { + color: #009999; +} +.highlight .mi { + color: #009999; +} +.highlight .mo { + color: #009999; +} +.highlight .m, .highlight .mb, .highlight .mx { + color: #009999; +} +.highlight .sb { + color: #d14; +} +.highlight .sc { + color: #d14; +} +.highlight .sd { + color: #d14; +} +.highlight .s2 { + color: #d14; +} +.highlight .se { + color: #d14; +} +.highlight .sh { + color: #d14; +} +.highlight .si { + color: #d14; +} +.highlight .sx { + color: #d14; +} +.highlight .sr { + color: #009926; +} +.highlight .s1 { + color: #d14; +} +.highlight .ss { + color: #990073; +} +.highlight .s { + color: #d14; +} +.highlight .na { + color: #008080; +} +.highlight .bp { + color: #999999; +} +.highlight .nb { + color: #0086B3; +} +.highlight .nc { + color: #445588; + font-weight: bold; +} +.highlight .no { + color: #008080; +} +.highlight .nd { + color: #3c5d5d; + font-weight: bold; +} +.highlight .ni { + color: #800080; +} +.highlight .ne { + color: #990000; + font-weight: bold; +} +.highlight .nf { + color: #990000; + font-weight: bold; +} +.highlight .nl { + color: #990000; + font-weight: bold; +} +.highlight .nn { + color: #555555; +} +.highlight .nt { + color: #000080; +} +.highlight .vc { + color: #008080; +} +.highlight .vg { + color: #008080; +} +.highlight .vi { + color: #008080; +} +.highlight .nv { + color: #008080; +} +.highlight .ow { + color: #000000; + font-weight: bold; +} +.highlight .o { + color: #000000; + font-weight: bold; +} +.highlight .w { + color: #bbbbbb; +} +.highlight { + background-color: #f8f8f8; +} + /** * from EBPAJ EPUB 3 File Creation Guide sample style * From 1848c66ff0afc113e369d38ec1489bf4f17b5345 Mon Sep 17 00:00:00 2001 From: takahashim Date: Fri, 23 Dec 2016 15:14:05 +0900 Subject: [PATCH 5/9] fix rouge --- lib/review/htmlbuilder.rb | 10 ++++++---- lib/review/htmlutils.rb | 7 +++++++ test/test_htmlbuilder.rb | 25 ++++++++++++++++++------- 3 files changed, 31 insertions(+), 11 deletions(-) diff --git a/lib/review/htmlbuilder.rb b/lib/review/htmlbuilder.rb index 5e3ad6ba1..eb1a4fed2 100644 --- a/lib/review/htmlbuilder.rb +++ b/lib/review/htmlbuilder.rb @@ -449,6 +449,7 @@ def list_body(id, lines, lang) class_names = ["list"] lexer = lang || File.extname(id).gsub(/\./, '') class_names.push("language-#{lexer}") unless lexer.blank? + class_names.push("highlight") if highlight? print %Q[
]
       body = lines.inject(''){|i, j| i + detab(j) + "\n"}
       puts highlight(:body => body, :lexer => lexer, :format => 'html')
@@ -492,11 +493,11 @@ def listnum_body(lines, lang)
       if highlight?
         body = lines.inject(''){|i, j| i + detab(j) + "\n"}
         lexer = lang
-        puts highlight(:body => body, :lexer => lexer, :format => 'html',
-                       :options => {:linenos => 'inline', :nowrap => false})
+        puts highlight(:body => body, :lexer => lexer, :format => 'html', :linenum => true)
       else
         class_names = ["list"]
         class_names.push("language-#{lang}") unless lang.blank?
+        class_names.push("highlight") if highlight?
         print %Q[
]
         lines.each_with_index do |line, i|
           puts detab((i+1).to_s.rjust(2) + ": " + line)
@@ -512,6 +513,7 @@ def emlist(lines, caption = nil, lang = nil)
       end
       class_names = ["emlist"]
       class_names.push("language-#{lang}") unless lang.blank?
+      class_names.push("highlight") if highlight?
       print %Q[
]
       body = lines.inject(''){|i, j| i + detab(j) + "\n"}
       lexer = lang
@@ -529,11 +531,11 @@ def emlistnum(lines, caption = nil, lang = nil)
       if highlight?
         body = lines.inject(''){|i, j| i + detab(j) + "\n"}
         lexer = lang
-        puts highlight(:body => body, :lexer => lexer, :format => 'html',
-                       :options => {:linenos => 'inline', :nowrap => false})
+        puts highlight(:body => body, :lexer => lexer, :format => 'html', :linenum => true)
       else
         class_names = ["emlist"]
         class_names.push("language-#{lang}") unless lang.blank?
+        class_names.push("highlight") if highlight?
         print %Q[
]
         lines.each_with_index do |line, i|
           puts detab((i+1).to_s.rjust(2) + ": " + line)
diff --git a/lib/review/htmlutils.rb b/lib/review/htmlutils.rb
index cb9273065..53694a7eb 100644
--- a/lib/review/htmlutils.rb
+++ b/lib/review/htmlutils.rb
@@ -73,6 +73,10 @@ def highlight_pygments(ops)
       lexer = ops[:lexer] if ops[:lexer].present?
       format = ops[:format] || ''
       options = {:nowrap => true, :noclasses => true}
+      if ops[:linenum]
+        options[:nowrap] = false
+        options[:linenos] = 'inline'
+      end
       if ops[:options] && ops[:options].kind_of?(Hash)
         options.merge!(ops[:options])
       end
@@ -108,6 +112,9 @@ def highlight_rouge(ops)
 
       #formatter = Rouge::Formatters::HTML.new()
       formatter = Rouge::Formatters::HTML.new(:css_class => 'highlight')
+      if ops[:linenum]
+        formatter = Rouge::Formatters::HTMLTable.new(formatter, :code_class => 'highlight rouge-code')
+      end
       #formatter = Rouge::Formatters::HTMLLegacy.new(:css_class => "highlight #{lexer.tag}")
       #formatter = Rouge::Formatters::HTMLPygments.new(Rouge::Formatters::HTML.new, "highlight #{lexer.tag}")
       #formatter = Rouge::Formatters::HTMLInline.new(Rouge::Themes::Colorful.new)
diff --git a/test/test_htmlbuilder.rb b/test/test_htmlbuilder.rb
index dff9c21c6..a41138df4 100644
--- a/test/test_htmlbuilder.rb
+++ b/test/test_htmlbuilder.rb
@@ -557,7 +557,7 @@ def @chapter.list(id)
     expected = <<-EOS
 

リスト1.1: this is test<&>_

-
test1
+
test1
 test1.5
 
 test<i>2</i>
@@ -583,7 +583,7 @@ def @chapter.list(id)
 
     assert_equal %Q|
\n| + %Q|

リスト1.1: this is test<&>_

\n| + - %Q|
def foo(a1, a2=:test)\n| +
+                 %Q|
def foo(a1, a2=:test)\n| +
                  %Q|  (1..3).times{\|i\| a.include?(:foo)}\n| +
                  %Q|  return true\n| +
                  %Q|end\n| +
@@ -608,7 +608,7 @@ def @chapter.list(id)
     expected = <<-EOS
 

リスト1.1: this is test<&>_

-
def foo(a1, a2=:test)
+
def foo(a1, a2=:test)
   (1..3).times{|i| a.include?(:foo)}
   return true
 end
@@ -626,7 +626,7 @@ def @chapter.list(id)
     @book.config["highlight"]["html"] = "rouge"
     actual = compile_block("//list[samplelist][this is @{test}<&>_]{\ntest1\ntest1.5\n\ntest@{2}\n//}\n")
 
-    assert_equal %Q|
\n

リスト1.1: this is test<&>_

\n
test1\ntest1.5\n\ntest<i>2</i>\n
\n
\n|, actual + assert_equal %Q|
\n

リスト1.1: this is test<&>_

\n
test1\ntest1.5\n\ntest<i>2</i>\n
\n
\n|, actual end def test_list_rouge_lang @@ -639,7 +639,7 @@ def @chapter.list(id) expected = "
\n" + "

リスト1.1: this is test<&>_

\n" + - "
def foo(a1, a2=:test)\n" +
+	           "
def foo(a1, a2=:test)\n" +
 	           "  (1..3).times{|i| a.include?(:foo)}\n" +
 	           "  return true\n" +
 	           "end\n" +
@@ -658,7 +658,7 @@ def @chapter.list(id)
     @book.config["highlight"]["html"] = "rouge"
     actual = compile_block("//list[samplelist][this is @{test}<&>_][]{\ndef foo(a1, a2=:test)\n  (1..3).times{|i| a.include?(:foo)}\n  return true\nend\n\n//}\n")
 
-    assert_equal "
\n

リスト1.1: this is test<&>_

\n
def foo(a1, a2=:test)\n  (1..3).times{|i| a.include?(:foo)}\n  return true\nend\n\n
\n
\n", actual + assert_equal "
\n

リスト1.1: this is test<&>_

\n
def foo(a1, a2=:test)\n  (1..3).times{|i| a.include?(:foo)}\n  return true\nend\n\n
\n
\n", actual end def test_list_ext @@ -753,6 +753,17 @@ def @chapter.list(id) assert_equal expected, actual end + def test_listnum_rouge_lang + def @chapter.list(id) + Book::ListIndex::Item.new("samplelist",1) + end + @book.config["highlight"] = {} + @book.config["highlight"]["html"] = "rouge" + actual = compile_block("//listnum[samplelist][this is @{test}<&>_][ruby]{\ndef foo(a1, a2=:test)\n (1..3).times{|i| a.include?(:foo)}\n return true\nend\n\n//}\n") + + assert_equal "
\n

リスト1.1: this is test<&>_

\n
1\n2\n3\n4\n5\n
def foo(a1, a2=:test)\n  (1..3).times{|i| a.include?(:foo)}\n  return true\nend\n\n
\n
\n", actual + end + def test_emlist actual = compile_block("//emlist{\nlineA\nlineB\n//}\n") @@ -771,7 +782,7 @@ def test_emlist_pygments_lang actual = compile_block("//emlist[][sql]{\nSELECT COUNT(*) FROM tests WHERE tests.no > 10 AND test.name LIKE 'ABC%'\n//}\n") expected = <<-EOS
-
SELECT COUNT(*) FROM tests WHERE tests.no > 10 AND test.name LIKE 'ABC%'
+
SELECT COUNT(*) FROM tests WHERE tests.no > 10 AND test.name LIKE 'ABC%'
 
EOS From 743ccd23ae82937623ed7e02ab87bdc595f2a76c Mon Sep 17 00:00:00 2001 From: takahashim Date: Fri, 30 Dec 2016 11:28:22 +0900 Subject: [PATCH 6/9] fix styles --- doc/sample.css | 2 ++ lib/review/htmlutils.rb | 2 +- test/test_htmlbuilder.rb | 2 +- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/doc/sample.css b/doc/sample.css index 665cc3fee..1e4d2d966 100644 --- a/doc/sample.css +++ b/doc/sample.css @@ -319,6 +319,8 @@ p.flushright { .highlight { background-color: #f8f8f8; } +.rouge-table { border-spacing: 0 } +.rouge-gutter { text-align: right } /** * from EBPAJ EPUB 3 File Creation Guide sample style diff --git a/lib/review/htmlutils.rb b/lib/review/htmlutils.rb index 53694a7eb..256cf80e4 100644 --- a/lib/review/htmlutils.rb +++ b/lib/review/htmlutils.rb @@ -113,7 +113,7 @@ def highlight_rouge(ops) #formatter = Rouge::Formatters::HTML.new() formatter = Rouge::Formatters::HTML.new(:css_class => 'highlight') if ops[:linenum] - formatter = Rouge::Formatters::HTMLTable.new(formatter, :code_class => 'highlight rouge-code') + formatter = Rouge::Formatters::HTMLTable.new(formatter, :table_class => 'highlight rouge-table') end #formatter = Rouge::Formatters::HTMLLegacy.new(:css_class => "highlight #{lexer.tag}") #formatter = Rouge::Formatters::HTMLPygments.new(Rouge::Formatters::HTML.new, "highlight #{lexer.tag}") diff --git a/test/test_htmlbuilder.rb b/test/test_htmlbuilder.rb index 5e3d15b85..960aab100 100644 --- a/test/test_htmlbuilder.rb +++ b/test/test_htmlbuilder.rb @@ -819,7 +819,7 @@ def @chapter.list(id) @book.config["highlight"]["html"] = "rouge" actual = compile_block("//listnum[samplelist][this is @{test}<&>_][ruby]{\ndef foo(a1, a2=:test)\n (1..3).times{|i| a.include?(:foo)}\n return true\nend\n\n//}\n") - assert_equal "
\n

リスト1.1: this is test<&>_

\n
1\n2\n3\n4\n5\n
def foo(a1, a2=:test)\n  (1..3).times{|i| a.include?(:foo)}\n  return true\nend\n\n
\n
\n", actual + assert_equal "
\n

リスト1.1: this is test<&>_

\n
1\n2\n3\n4\n5\n
def foo(a1, a2=:test)\n  (1..3).times{|i| a.include?(:foo)}\n  return true\nend\n\n
\n
\n", actual end From a028bb921dfa137396f01859edad1c733f4fae96 Mon Sep 17 00:00:00 2001 From: takahashim Date: Fri, 30 Dec 2016 15:12:49 +0900 Subject: [PATCH 7/9] remove comments and useless spaces --- lib/review/htmlbuilder.rb | 4 ++-- lib/review/htmlutils.rb | 4 ---- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/lib/review/htmlbuilder.rb b/lib/review/htmlbuilder.rb index 68d1f2302..3b713645a 100644 --- a/lib/review/htmlbuilder.rb +++ b/lib/review/htmlbuilder.rb @@ -496,7 +496,7 @@ def listnum_body(lines, lang) lexer = lang first_line_number = get_line_num puts highlight(:body => body, :lexer => lexer, :format => 'html', :linenum => true, - :options => {:linenostart => first_line_number}) + :options => {:linenostart => first_line_number}) else class_names = ["list"] class_names.push("language-#{lang}") unless lang.blank? @@ -537,7 +537,7 @@ def emlistnum(lines, caption = nil, lang = nil) lexer = lang first_line_number = get_line_num puts highlight(:body => body, :lexer => lexer, :format => 'html', :linenum => true, - :options => {:linenostart => first_line_number}) + :options => {:linenostart => first_line_number}) else class_names = ["emlist"] class_names.push("language-#{lang}") unless lang.blank? diff --git a/lib/review/htmlutils.rb b/lib/review/htmlutils.rb index 256cf80e4..b2ad64961 100644 --- a/lib/review/htmlutils.rb +++ b/lib/review/htmlutils.rb @@ -110,14 +110,10 @@ def highlight_rouge(ops) lexer = Rouge::Lexer.find(lexer) raise "unknown lexer #{lexer}" unless lexer - #formatter = Rouge::Formatters::HTML.new() formatter = Rouge::Formatters::HTML.new(:css_class => 'highlight') if ops[:linenum] formatter = Rouge::Formatters::HTMLTable.new(formatter, :table_class => 'highlight rouge-table') end - #formatter = Rouge::Formatters::HTMLLegacy.new(:css_class => "highlight #{lexer.tag}") - #formatter = Rouge::Formatters::HTMLPygments.new(Rouge::Formatters::HTML.new, "highlight #{lexer.tag}") - #formatter = Rouge::Formatters::HTMLInline.new(Rouge::Themes::Colorful.new) raise "unknown formatter #{formatter}" unless formatter text = unescape_html(body) From e74eadbe2fa85ee29163711af0d75be73baecadd Mon Sep 17 00:00:00 2001 From: takahashim Date: Fri, 30 Dec 2016 15:14:05 +0900 Subject: [PATCH 8/9] fix css in sample-book --- test/sample-book/src/style.css | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/sample-book/src/style.css b/test/sample-book/src/style.css index cd8adabf3..a30603cdb 100644 --- a/test/sample-book/src/style.css +++ b/test/sample-book/src/style.css @@ -462,6 +462,8 @@ em { .highlight { background-color: #f8f8f8; } +.rouge-table { border-spacing: 0 } +.rouge-gutter { text-align: right } /** * from EBPAJ EPUB 3 File Creation Guide sample style From cf3ad103e387ba814b03e29ad8f96fe054b77a35 Mon Sep 17 00:00:00 2001 From: takahashim Date: Fri, 30 Dec 2016 15:22:11 +0900 Subject: [PATCH 9/9] support //firstlinenum --- lib/review/htmlutils.rb | 9 ++++++++- test/test_htmlbuilder.rb | 27 +++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/lib/review/htmlutils.rb b/lib/review/htmlutils.rb index b2ad64961..9442de40c 100644 --- a/lib/review/htmlutils.rb +++ b/lib/review/htmlutils.rb @@ -107,12 +107,19 @@ def highlight_rouge(ops) end format = ops[:format] || '' + first_line_num = 1 ## default + if ops[:options] && ops[:options][:linenostart] + first_line_num = ops[:options][:linenostart] + end + lexer = Rouge::Lexer.find(lexer) raise "unknown lexer #{lexer}" unless lexer formatter = Rouge::Formatters::HTML.new(:css_class => 'highlight') if ops[:linenum] - formatter = Rouge::Formatters::HTMLTable.new(formatter, :table_class => 'highlight rouge-table') + formatter = Rouge::Formatters::HTMLTable.new(formatter, + :table_class => 'highlight rouge-table', + :start_line => first_line_num) end raise "unknown formatter #{formatter}" unless formatter diff --git a/test/test_htmlbuilder.rb b/test/test_htmlbuilder.rb index 960aab100..a5627096f 100644 --- a/test/test_htmlbuilder.rb +++ b/test/test_htmlbuilder.rb @@ -822,6 +822,33 @@ def @chapter.list(id) assert_equal "
\n

リスト1.1: this is test<&>_

\n
1\n2\n3\n4\n5\n
def foo(a1, a2=:test)\n  (1..3).times{|i| a.include?(:foo)}\n  return true\nend\n\n
\n
\n", actual end + def test_listnum_rouge_lang_linenum + def @chapter.list(id) + Book::ListIndex::Item.new("samplelist",1) + end + @book.config["highlight"] = {} + @book.config["highlight"]["html"] = "rouge" + actual = compile_block("//firstlinenum[100]\n//listnum[samplelist][this is @{test}<&>_][ruby]{\ndef foo(a1, a2=:test)\n (1..3).times{|i| a.include?(:foo)}\n return true\nend\n\n//}\n") + + expected = <<-EOB +
+

リスト1.1: this is test<&>_

+
100
+101
+102
+103
+104
+
def foo(a1, a2=:test)
+  (1..3).times{|i| a.include?(:foo)}
+  return true
+end
+
+
+
+EOB + + assert_equal expected, actual + end def test_emlist actual = compile_block("//emlist{\nlineA\nlineB\n//}\n")