Skip to content

Commit

Permalink
Merge pull request #688 from kmuto/firstlinenum
Browse files Browse the repository at this point in the history
suppot //firstlinenum
  • Loading branch information
takahashim authored Dec 29, 2016
2 parents 6c2a583 + 1b13010 commit a30dd66
Show file tree
Hide file tree
Showing 9 changed files with 158 additions and 17 deletions.
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ Style/SafeNavigation:
Style/EmptyMethod:
Enabled: false

Style/HashSyntax:
Enabled: false

##################### Metrics ##################################

Metrics/ClassLength:
Expand Down
15 changes: 15 additions & 0 deletions lib/review/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,21 @@ def headline_prefix(level)
end
private :headline_prefix

## for //firstlinenum[num]
def firstlinenum(num)
@first_line_num = num.to_i
end

def get_line_num
if !@first_line_num
return 1
end
line_num = @first_line_num
@first_line_num = nil

line_num
end

def list(lines, id, caption, lang = nil)
begin
list_header id, caption, lang
Expand Down
1 change: 1 addition & 0 deletions lib/review/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ def inline_defined?(name)
defsingle :tsize, 1
defsingle :include, 1
defsingle :olnum, 1
defsingle :firstlinenum, 1

definline :chapref
definline :chap
Expand Down
13 changes: 9 additions & 4 deletions lib/review/htmlbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def builder_init_file
@column = 0
@sec_counter = SecCounter.new(5, @chapter)
@nonum_counter = 0
@first_line_num = nil
@body_ext = nil
@toc = nil
end
Expand Down Expand Up @@ -492,14 +493,16 @@ def listnum_body(lines, lang)
if highlight?
body = lines.inject(''){|i, j| i + detab(j) + "\n"}
lexer = lang
first_line_number = get_line_num
puts highlight(:body => body, :lexer => lexer, :format => 'html',
:options => {:linenos => 'inline', :nowrap => false})
:options => {:linenos => 'inline', :nowrap => false, :linenostart => first_line_number})
else
class_names = ["list"]
class_names.push("language-#{lang}") unless lang.blank?
print %Q[<pre class="#{class_names.join(" ")}">]
first_line_num = get_line_num
lines.each_with_index do |line, i|
puts detab((i+1).to_s.rjust(2) + ": " + line)
puts detab((i+first_line_num).to_s.rjust(2) + ": " + line)
end
puts '</pre>'
end
Expand Down Expand Up @@ -529,14 +532,16 @@ def emlistnum(lines, caption = nil, lang = nil)
if highlight?
body = lines.inject(''){|i, j| i + detab(j) + "\n"}
lexer = lang
first_line_number = get_line_num
puts highlight(:body => body, :lexer => lexer, :format => 'html',
:options => {:linenos => 'inline', :nowrap => false})
:options => {:linenos => 'inline', :nowrap => false, :linenostart => first_line_number})
else
class_names = ["emlist"]
class_names.push("language-#{lang}") unless lang.blank?
print %Q[<pre class="#{class_names.join(" ")}">]
first_line_num = get_line_num
lines.each_with_index do |line, i|
puts detab((i+1).to_s.rjust(2) + ": " + line)
puts detab((i+first_line_num).to_s.rjust(2) + ": " + line)
end
puts '</pre>'
end
Expand Down
7 changes: 5 additions & 2 deletions lib/review/idgxmlbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def builder_init_file
@column = 0
@noindent = nil
@ol_num = nil
@first_line_num = nil
@rootelement = "doc"
@secttags = nil
@tsize = nil
Expand Down Expand Up @@ -362,23 +363,25 @@ def emlist(lines, caption = nil, lang = nil)

def emlistnum(lines, caption = nil, lang = nil)
_lines = []
first_line_num = get_line_num
lines.each_with_index do |line, i|
_lines << detab("<span type='lineno'>" + (i + 1).to_s.rjust(2) + ": </span>" + line)
_lines << detab("<span type='lineno'>" + (i + first_line_num).to_s.rjust(2) + ": </span>" + line)
end
quotedlist _lines, 'emlistnum', caption
end

def listnum_body(lines, lang)
print %Q(<pre>)
no = 1
first_line_num = get_line_num
lines.each_with_index do |line, i|
unless @book.config["listinfo"].nil?
print "<listinfo line=\"#{no}\""
print " begin=\"1\"" if no == 1
print " end=\"#{no}\"" if no == lines.size
print ">"
end
print detab("<span type='lineno'>" + (i + 1).to_s.rjust(2) + ": </span>" + line)
print detab("<span type='lineno'>" + (i + first_line_num).to_s.rjust(2) + ": </span>" + line)
print "\n"
print "</listinfo>" unless @book.config["listinfo"].nil?
no += 1
Expand Down
36 changes: 25 additions & 11 deletions lib/review/latexbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def builder_init_file
@tsize = nil
@table_caption = nil
@ol_num = nil
@first_line_num = nil
@sec_counter = SecCounter.new(5, @chapter)
initialize_metachars(@book.config["texcommand"])
end
Expand Down Expand Up @@ -261,11 +262,12 @@ def emlist(lines, caption = nil, lang = nil)

def emlistnum(lines, caption = nil, lang = nil)
blank
first_line_num = get_line_num
if highlight_listings?
common_code_block_lst(nil, lines, 'reviewemlistnumlst', 'title', caption, lang)
common_code_block_lst(nil, lines, 'reviewemlistnumlst', 'title', caption, lang, first_line_num: first_line_num)
else
common_code_block(nil, lines, 'reviewemlist', caption, lang) do |line, idx|
detab((idx+1).to_s.rjust(2)+": " + line) + "\n"
detab((idx+first_line_num).to_s.rjust(2)+": " + line) + "\n"
end
end
end
Expand All @@ -283,11 +285,12 @@ def list(lines, id, caption, lang = nil)

## override Builder#listnum
def listnum(lines, id, caption, lang = nil)
first_line_num = get_line_num
if highlight_listings?
common_code_block_lst(id, lines, 'reviewlistnumlst', 'caption', caption, lang)
common_code_block_lst(id, lines, 'reviewlistnumlst', 'caption', caption, lang, first_line_num: first_line_num)
else
common_code_block(id, lines, 'reviewlist', caption, lang) do |line, idx|
detab((idx+1).to_s.rjust(2)+": " + line) + "\n"
detab((idx+first_line_num).to_s.rjust(2)+": " + line) + "\n"
end
end
end
Expand Down Expand Up @@ -325,23 +328,34 @@ def common_code_block(id, lines, command, caption, lang)
blank
end

def common_code_block_lst(id, lines, command, title, caption, lang)
def common_code_block_lst(id, lines, command, title, caption, lang, first_line_num: 1)
if title == "title" && caption.blank?
print "\\vspace{-1.5em}"
end
body = lines.inject(''){|i, j| i + detab(unescape_latex(j)) + "\n"}
args = make_code_block_args(title, caption, lang, first_line_num: first_line_num)
puts "\\begin{"+command+"}[" + args + "]"
print body
puts "\\end{"+ command + "}"
blank
end

def make_code_block_args(title, caption, lang, first_line_num: 1)
caption_str = compile_inline((caption || ""))
if title == "title" && caption_str == ""
caption_str = "\\relax" ## dummy charactor to remove lstname
print "\\vspace{-1.5em}"
end
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
puts "\\end{"+ command + "}"
blank
args = title + "={" + caption_str + "},language={" + lexer + "}"
if first_line_num != 1
args += ",firstnumber=#{first_line_num}"
end
args
end

def source(lines, caption, lang = nil)
Expand Down
72 changes: 72 additions & 0 deletions test/test_htmlbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,36 @@ def foo(a1, a2=:test)
assert_equal expected, actual
end

def test_listnum_linenum
def @chapter.list(id)
Book::ListIndex::Item.new("samplelist",1)
end

@book.config["highlight"] = false
actual = compile_block(<<-EOS)
//firstlinenum[100]
//listnum[samplelist][this is @<b>{test}<&>_][ruby]{
def foo(a1, a2=:test)
(1..3).times{|i| a.include?(:foo)}
return true
end
//}
EOS

expected =<<-EOS
<div class="code">
<p class="caption">リスト1.1: this is <b>test</b>&lt;&amp;&gt;_</p>
<pre class="list language-ruby">100: def foo(a1, a2=:test)
101: (1..3).times{|i| a.include?(:foo)}
102: return true
103: end
</pre>
</div>
EOS

assert_equal expected, actual
end

def test_listnum_pygments_lang
def @chapter.list(id)
Book::ListIndex::Item.new("samplelist",1)
Expand Down Expand Up @@ -682,6 +712,34 @@ def @chapter.list(id)
assert_equal expected, actual
end

def test_listnum_pygments_lang_linenum
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"
actual = compile_block("//firstlinenum[100]\n//listnum[samplelist][this is @<b>{test}<&>_][ruby]{\ndef foo(a1, a2=:test)\n (1..3).times{|i| a.include?(:foo)}\n return true\nend\n\n//}\n")

expected = <<-EOB
<div class=\"code\">
<p class=\"caption\">リスト1.1: this is <b>test</b>&lt;&amp;&gt;_</p>
<div class=\"highlight\" style=\"background: #f8f8f8\"><pre style=\"line-height: 125%\"><span style=\"background-color: #f0f0f0; padding: 0 5px 0 5px\">100</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>)
<span style=\"background-color: #f0f0f0; padding: 0 5px 0 5px\">101</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>)}
<span style=\"background-color: #f0f0f0; padding: 0 5px 0 5px\">102</span> <span style=\"color: #008000; font-weight: bold\">return</span> <span style=\"color: #008000\">true</span>
<span style=\"background-color: #f0f0f0; padding: 0 5px 0 5px\">103</span> <span style=\"color: #008000; font-weight: bold\">end</span>
</pre></div>
</div>
EOB

assert_equal expected, actual
end

def test_listnum_pygments_lang_without_lang
def @chapter.list(id)
Book::ListIndex::Item.new("samplelist",1)
Expand Down Expand Up @@ -772,6 +830,20 @@ def test_emlistnum_lang
assert_equal expected, actual
end

def test_emlistnum_lang_linenum
@book.config["highlight"] = false
actual = compile_block("//firstlinenum[1000]\n//emlistnum[cap][text]{\nlineA\nlineB\n//}\n")
expected =<<-EOS
<div class="emlistnum-code">
<p class="caption">cap</p>
<pre class="emlist language-text">1000: lineA
1001: lineB
</pre>
</div>
EOS
assert_equal expected, actual
end

def test_emlist_with_4tab
@config["tabwidth"] = 4
actual = compile_block("//emlist{\n\tlineA\n\t\tlineB\n\tlineC\n//}\n")
Expand Down
16 changes: 16 additions & 0 deletions test/test_idgxmlbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,22 @@ def @chapter.list(id)
assert_equal %Q|<codelist><caption>リスト1.1 this is <b>test</b>&lt;&amp;&gt;_</caption><pre>test1\ntest1.5\n\ntest<i>2</i>\n</pre></codelist>|, actual
end

def test_listnum
def @chapter.list(id)
Book::ListIndex::Item.new("samplelist",1)
end
actual = compile_block("//listnum[samplelist][this is @<b>{test}<&>_]{\ntest1\ntest1.5\n\ntest@<i>{2}\n//}\n")
assert_equal %Q|<codelist><caption>リスト1.1 this is <b>test</b>&lt;&amp;&gt;_</caption><pre><span type='lineno'> 1: </span>test1\n<span type='lineno'> 2: </span>test1.5\n<span type='lineno'> 3: </span>\n<span type='lineno'> 4: </span>test<i>2</i>\n</pre></codelist>|, actual
end

def test_listnum_linenum
def @chapter.list(id)
Book::ListIndex::Item.new("samplelist",1)
end
actual = compile_block("//firstlinenum[100]\n//listnum[samplelist][this is @<b>{test}<&>_]{\ntest1\ntest1.5\n\ntest@<i>{2}\n//}\n")
assert_equal %Q|<codelist><caption>リスト1.1 this is <b>test</b>&lt;&amp;&gt;_</caption><pre><span type='lineno'>100: </span>test1\n<span type='lineno'>101: </span>test1.5\n<span type='lineno'>102: </span>\n<span type='lineno'>103: </span>test<i>2</i>\n</pre></codelist>|, actual
end

def test_list_listinfo
def @chapter.list(id)
Book::ListIndex::Item.new("samplelist",1)
Expand Down
12 changes: 12 additions & 0 deletions test/test_latexbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -311,13 +311,25 @@ def test_listnum
assert_equal %Q|\\reviewlistcaption{リスト1.1: ruby}\n\\begin{reviewlist}\n 1: class Foo\n 2: def foo\n 3: bar\n 4: \n 5: buz\n 6: end\n 7: end\n\\end{reviewlist}\n|, actual
end

def test_listnum_linenum
actual = compile_block("//firstlinenum[100]\n//listnum[test1][ruby]{\nclass Foo\n def foo\n bar\n\n buz\n end\nend\n//}\n")
assert_equal %Q|\\reviewlistcaption{リスト1.1: ruby}\n\\begin{reviewlist}\n100: class Foo\n101: def foo\n102: bar\n103: \n104: buz\n105: end\n106: end\n\\end{reviewlist}\n|, actual
end

def test_listnum_lst
@book.config["highlight"] = {}
@book.config["highlight"]["latex"] = "listings"
actual = compile_block("//listnum[test1][ruby]{\nclass Foo\n def foo\n bar\n\n buz\n end\nend\n//}\n")
assert_equal %Q|\\begin{reviewlistnumlst}[caption={ruby},language={}]\nclass Foo\n def foo\n bar\n\n buz\n end\nend\n\\end{reviewlistnumlst}\n|, actual
end

def test_listnum_lst_linenum
@book.config["highlight"] = {}
@book.config["highlight"]["latex"] = "listings"
actual = compile_block("//firstlinenum[100]\n//listnum[test1][ruby]{\nclass Foo\n def foo\n bar\n\n buz\n end\nend\n//}\n")
assert_equal %Q|\\begin{reviewlistnumlst}[caption={ruby},language={},firstnumber=100]\nclass Foo\n def foo\n bar\n\n buz\n end\nend\n\\end{reviewlistnumlst}\n|, actual
end

def test_source
actual = compile_block("//source[foo/bar/test.rb]{\nfoo\nbar\n\nbuz\n//}\n")
assert_equal %Q|\\reviewsourcecaption{foo/bar/test.rb}\n\\begin{reviewsource}\nfoo\nbar\n\nbuz\n\\end{reviewsource}\n|, actual
Expand Down

0 comments on commit a30dd66

Please sign in to comment.