Skip to content

Commit

Permalink
Support nested list (ol, ul). refs #2498
Browse files Browse the repository at this point in the history
  • Loading branch information
okkez committed Sep 6, 2013
1 parent 30df129 commit 7ee740d
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 8 deletions.
57 changes: 49 additions & 8 deletions lib/bitclust/rdcompiler.rb
Expand Up @@ -76,10 +76,10 @@ def library_file
entry_chunk
when /\A=+/
headline @f.gets
when /\A\s+\*\s/
ulist
when /\A\s+\(\d+\)\s/
olist
when /\A(\s+)\*\s/, /\A(\s+)\(\d+\)\s/
@item_stack = []
item_list($1.size)
raise "@item_stack should be empty. #{@item_stack.inspect}" unless @item_stack.empty?
when %r<\A//emlist\{>
emlist
when /\A:\s/
Expand Down Expand Up @@ -127,10 +127,10 @@ def entry_chunk
end
when /\A---/
break
when /\A\s+\*\s/
ulist
when /\A\s+\(\d+\)\s/
olist
when /\A(\s+)\*\s/, /\A(\s+)\(\d+\)\s/
@item_stack = []
item_list($1.size)
raise "@item_stack should be empty. #{@item_stack.inspect}" unless @item_stack.empty?
when /\A:\s/
dlist
when %r<\A//emlist\{>
Expand Down Expand Up @@ -167,6 +167,47 @@ def h(level, label, frag = nil)
"<h#{level} #{name}>#{label}</h#{level}>"
end

def item_list(level = 0, indent = true)
open_tag = nil
close_tag = nil
pattern = nil
case @f.peek
when /\A(\s+)\*\s/
open_tag = "<ul>"
close_tag = "</ul>"
when /\A(\s+)\(\d+\)\s/
open_tag = "<ol>"
close_tag = "</ol>"
end
if indent
line open_tag
@item_stack.push(close_tag)
end
@f.while_match(/\A(\s+)(?:\*\s|\(\d+\))/) do |line|
string "<li>"
@item_stack.push("</li>")
string compile_text(line.sub(/\A(\s+)(?:\*|\(\d+\))/, '').strip)
if /\A(\s+)(?!\*\s|\(\d+\))\S/ =~ @f.peek
@f.while_match(/\A\s+(?!\*\s|\(\d+\))\S/) do |cont|
nl
string compile_text(cont.strip)
end
line @item_stack.pop # current level li
elsif /\A(\s+)(?:\*\s|\(\d+\))/ =~ @f.peek and level < $1.size
item_list($1.size)
line @item_stack.pop # current level ul or ol
elsif /\A(\s+)(?:\*\s|\(\d+\))/ =~ @f.peek and level > $1.size
line @item_stack.pop # current level li
line @item_stack.pop # current level ul or ol
line @item_stack.pop # previous level li
item_list($1.size, false)
else
line @item_stack.pop # current level li
end
end
line @item_stack.pop unless @item_stack.empty?
end

def ulist
@out.puts '<ul>'
@f.while_match(/\A\s+\*\s/) do |line|
Expand Down
104 changes: 104 additions & 0 deletions test/test_rdcompiler.rb
Expand Up @@ -410,6 +410,29 @@ def test_ulist_continuous_line

end

def test_ulist_nested
src = <<HERE
* hoge1
* fuga1
* hoge2
* fuga2
HERE
expected = <<HERE
<ul>
<li>hoge1<ul>
<li>fuga1</li>
</ul>
</li>
<li>hoge2<ul>
<li>fuga2</li>
</ul>
</li>
</ul>
HERE
assert_compiled_source(expected, src)
end

def test_olist
src = <<'HERE'
(1) hoge1
Expand All @@ -422,6 +445,87 @@ def test_olist
bar</li>
<li>hoge2</li>
</ol>
HERE
assert_compiled_source(expected, src)
end

def test_olist_nested
src = <<HERE
(1) hoge1
(11) fuga1
(2) hoge2
(12) fuga2
HERE
expected = <<HERE
<ol>
<li>hoge1<ol>
<li>fuga1</li>
</ol>
</li>
<li>hoge2<ol>
<li>fuga2</li>
</ol>
</li>
</ol>
HERE
assert_compiled_source(expected, src)
end

def test_ulist_olist_nested
src = <<HERE
* hoge1
(1) fuga1
(2) fuga2
* hoge2
(1) boo1
(2) boo2
HERE
expected = <<HERE
<ul>
<li>hoge1<ol>
<li>fuga1</li>
<li>fuga2</li>
</ol>
</li>
<li>hoge2<ol>
<li>boo1</li>
<li>boo2</li>
</ol>
</li>
</ul>
HERE
assert_compiled_source(expected, src)
end

def test_olist_nested_3level
src = <<HERE
(1) hoge1
(11) fuga1
(111) boo1
(2) hoge2
(22) fuga2
(222) boo2
HERE
expected = <<HERE
<ol>
<li>hoge1<ol>
<li>fuga1<ol>
<li>boo1</li>
</ol>
</li>
<li>hoge2<ol>
<li>fuga2<ol>
<li>boo2</li>
</ol>
</li>
</ol>
</li>
</ol>
</li>
</ol>
HERE
assert_compiled_source(expected, src)
end
Expand Down

0 comments on commit 7ee740d

Please sign in to comment.