diff --git a/lib/bitclust/rdcompiler.rb b/lib/bitclust/rdcompiler.rb index 531e01e4..e2a49f5b 100644 --- a/lib/bitclust/rdcompiler.rb +++ b/lib/bitclust/rdcompiler.rb @@ -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/ @@ -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\{> @@ -167,6 +167,47 @@ def h(level, label, frag = nil) "#{label}" 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 = "" + when /\A(\s+)\(\d+\)\s/ + open_tag = "
    " + close_tag = "
" + end + if indent + line open_tag + @item_stack.push(close_tag) + end + @f.while_match(/\A(\s+)(?:\*\s|\(\d+\))/) do |line| + string "
  • " + @item_stack.push("
  • ") + 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 ' + +HERE + assert_compiled_source(expected, src) + end + def test_olist src = <<'HERE' (1) hoge1 @@ -422,6 +445,87 @@ def test_olist bar
  • hoge2
  • +HERE + assert_compiled_source(expected, src) + end + + def test_olist_nested + src = < +
  • hoge1
      +
    1. fuga1
    2. +
    +
  • +
  • hoge2
      +
    1. fuga2
    2. +
    +
  • + + +HERE + assert_compiled_source(expected, src) + end + + def test_ulist_olist_nested + src = < +
  • hoge1
      +
    1. fuga1
    2. +
    3. fuga2
    4. +
    +
  • +
  • hoge2
      +
    1. boo1
    2. +
    3. boo2
    4. +
    +
  • + + +HERE + assert_compiled_source(expected, src) + end + + def test_olist_nested_3level + src = < +
  • hoge1
      +
    1. fuga1
        +
      1. boo1
      2. +
      +
    2. +
    3. hoge2
        +
      1. fuga2
          +
        1. boo2
        2. +
        +
      2. +
      +
    4. +
    +
  • + + HERE assert_compiled_source(expected, src) end