Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Set default highlight lang
  • Loading branch information
kdmsnr committed Mar 4, 2015
1 parent ba64f73 commit 22b330a
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
7 changes: 6 additions & 1 deletion lib/review/htmlutils.rb
Expand Up @@ -47,7 +47,12 @@ def highlight_pygments?

def highlight(ops)
body = ops[:body] || ''
lexer = ops[:lexer].blank? ? 'text' : ops[:lexer]
if @book.config["highlight"] && @book.config["highlight"]["lang"]
lexer = @book.config["highlight"]["lang"] # default setting
else
lexer = 'text'
end
lexer = ops[:lexer] if ops[:lexer].present?
format = ops[:format] || ''
options = {:nowrap => true, :noclasses => true}
if ops[:options] && ops[:options].kind_of?(Hash)
Expand Down
7 changes: 6 additions & 1 deletion lib/review/latexbuilder.rb
Expand Up @@ -320,7 +320,12 @@ def common_code_block_lst(lines, command, title, caption, lang)
caption_str = "\\relax" ## dummy charactor to remove lstname
print "\\vspace{-1.5em}"
end
lexer = lang || ""
if @book.config["highlight"] && @book.config["highlight"]["lang"]
lexer = @book.config["highlight"]["lang"] # default setting
else
lexer = ""
end
lexer = lang if lang.present?
body = lines.inject(''){|i, j| i + detab(unescape_latex(j)) + "\n"}
puts "\\begin{"+command+"}["+title+"={"+caption_str+"},language={"+ lexer+"}]"
print body
Expand Down
19 changes: 19 additions & 0 deletions test/test_htmlbuilder.rb
Expand Up @@ -531,6 +531,25 @@ def @chapter.list(id)
assert_equal "<div class=\"code\">\n<p class=\"caption\">リスト1.1: this is <b>test</b>&lt;&amp;&gt;_</p>\n<div class=\"highlight\" style=\"background: #f8f8f8\"><pre style=\"line-height: 125%\"><span style=\"background-color: #f0f0f0; padding: 0 5px 0 5px\">1</span> <span style=\"color: #008000; font-weight: bold\">def</span> <span style=\"color: #0000FF\">foo</span>(a1, a2<span style=\"color: #666666\">=</span><span style=\"color: #19177C\">:test</span>)\n<span style=\"background-color: #f0f0f0; padding: 0 5px 0 5px\">2</span> (<span style=\"color: #666666\">1.</span>.<span style=\"color: #666666\">3</span>)<span style=\"color: #666666\">.</span>times{<span style=\"color: #666666\">|</span>i<span style=\"color: #666666\">|</span> a<span style=\"color: #666666\">.</span>include?(<span style=\"color: #19177C\">:foo</span>)}\n<span style=\"background-color: #f0f0f0; padding: 0 5px 0 5px\">3</span> <span style=\"color: #008000; font-weight: bold\">return</span> <span style=\"color: #008000\">true</span>\n<span style=\"background-color: #f0f0f0; padding: 0 5px 0 5px\">4</span> <span style=\"color: #008000; font-weight: bold\">end</span>\n</pre></div>\n</div>\n", actual
end

def test_listnum_pygments_lang_without_lang
def @chapter.list(id)
Book::ListIndex::Item.new("samplelist",1)
end
begin
require 'pygments'
rescue LoadError
$stderr.puts "skip test_listnum_pygments_lang (cannot find pygments.rb)"
return true
end
@book.config["highlight"] = {}
@book.config["highlight"]["html"] = "pygments"
@book.config["highlight"]["lang"] = "ruby"
actual = compile_block("//listnum[samplelist][this is @<b>{test}<&>_]{\ndef foo(a1, a2=:test)\n (1..3).times{|i| a.include?(:foo)}\n return true\nend\n\n//}\n")

assert_equal "<div class=\"code\">\n<p class=\"caption\">リスト1.1: this is <b>test</b>&lt;&amp;&gt;_</p>\n<div class=\"highlight\" style=\"background: #f8f8f8\"><pre style=\"line-height: 125%\"><span style=\"background-color: #f0f0f0; padding: 0 5px 0 5px\">1</span> <span style=\"color: #008000; font-weight: bold\">def</span> <span style=\"color: #0000FF\">foo</span>(a1, a2<span style=\"color: #666666\">=</span><span style=\"color: #19177C\">:test</span>)\n<span style=\"background-color: #f0f0f0; padding: 0 5px 0 5px\">2</span> (<span style=\"color: #666666\">1.</span>.<span style=\"color: #666666\">3</span>)<span style=\"color: #666666\">.</span>times{<span style=\"color: #666666\">|</span>i<span style=\"color: #666666\">|</span> a<span style=\"color: #666666\">.</span>include?(<span style=\"color: #19177C\">:foo</span>)}\n<span style=\"background-color: #f0f0f0; padding: 0 5px 0 5px\">3</span> <span style=\"color: #008000; font-weight: bold\">return</span> <span style=\"color: #008000\">true</span>\n<span style=\"background-color: #f0f0f0; padding: 0 5px 0 5px\">4</span> <span style=\"color: #008000; font-weight: bold\">end</span>\n</pre></div>\n</div>\n", actual
end


def test_emlist
actual = compile_block("//emlist{\nlineA\nlineB\n//}\n")
assert_equal %Q|<div class="emlist-code">\n<pre class="emlist">lineA\nlineB\n</pre>\n</div>\n|, actual
Expand Down
9 changes: 9 additions & 0 deletions test/test_latexbuilder.rb
Expand Up @@ -254,6 +254,15 @@ def test_emlist_lst
assert_equal %Q|\n\\vspace{-1.5em}\\begin{reviewemlistlst}[title={\\relax},language={sql}]\nSELECT COUNT(*) FROM tests WHERE tests.no > 10 AND test.name LIKE 'ABC%'\n\\end{reviewemlistlst}\n|, actual
end

def test_emlist_lst_without_lang
@book.config["highlight"] = {}
@book.config["highlight"]["latex"] = "listings"
@book.config["highlight"]["lang"] = "sql"
actual = compile_block("//emlist[]{\nSELECT COUNT(*) FROM tests WHERE tests.no > 10 AND test.name LIKE 'ABC%'\n//}\n")
assert_equal %Q|\n\\vspace{-1.5em}\\begin{reviewemlistlst}[title={\\relax},language={sql}]\nSELECT COUNT(*) FROM tests WHERE tests.no > 10 AND test.name LIKE 'ABC%'\n\\end{reviewemlistlst}\n|, actual
end


def test_emlist_caption
actual = compile_block("//emlist[cap1]{\nfoo\nbar\n\nbuz\n//}\n")
assert_equal %Q|\n\\reviewemlistcaption{cap1}\n\\begin{reviewemlist}\nfoo\nbar\n\nbuz\n\\end{reviewemlist}\n|, actual
Expand Down

0 comments on commit 22b330a

Please sign in to comment.