From d1f097d6648d43a130b7a4cdfe58e9120dc33a63 Mon Sep 17 00:00:00 2001 From: xyz65535 Date: Thu, 5 Sep 2024 19:05:23 +0200 Subject: [PATCH] #38 list continuations --- lib/coradoc/element/list.rb | 1 + lib/coradoc/element/list/continuation.rb | 18 ++++++ lib/coradoc/parser/asciidoc/block.rb | 65 ++++++++++++++++++-- lib/coradoc/parser/asciidoc/content.rb | 12 ++-- lib/coradoc/parser/asciidoc/list.rb | 51 ++++++++++++--- lib/coradoc/parser/asciidoc/section.rb | 2 +- lib/coradoc/parser/base.rb | 2 +- lib/coradoc/transformer.rb | 27 +++----- spec/coradoc/parser/asciidoc/content_spec.rb | 6 +- spec/coradoc/parser/asciidoc/section_spec.rb | 6 +- spec/coradoc/parser_spec.rb | 4 +- 11 files changed, 148 insertions(+), 46 deletions(-) create mode 100644 lib/coradoc/element/list/continuation.rb diff --git a/lib/coradoc/element/list.rb b/lib/coradoc/element/list.rb index e55c8a4..a387938 100644 --- a/lib/coradoc/element/list.rb +++ b/lib/coradoc/element/list.rb @@ -7,6 +7,7 @@ module List require_relative "list_item" require_relative "list_item_definition" +require_relative "list/continuation" require_relative "list/core" require_relative "list/ordered" require_relative "list/unordered" diff --git a/lib/coradoc/element/list/continuation.rb b/lib/coradoc/element/list/continuation.rb new file mode 100644 index 0000000..5f39775 --- /dev/null +++ b/lib/coradoc/element/list/continuation.rb @@ -0,0 +1,18 @@ +require_relative "../inline/anchor" +require_relative "core" + +module Coradoc + module Element + module List + class Continuation < Base + + def initialize() + end + + def to_adoc + "\n+\n" + end + end + end + end +end diff --git a/lib/coradoc/parser/asciidoc/block.rb b/lib/coradoc/parser/asciidoc/block.rb index 2607fee..71ae1ef 100644 --- a/lib/coradoc/parser/asciidoc/block.rb +++ b/lib/coradoc/parser/asciidoc/block.rb @@ -4,11 +4,12 @@ module Asciidoc module Block def block - sidebar_block | - example_block | - source_block | - quote_block | - pass_block + ( sidebar_block | + example_block | + source_block | + quote_block | + pass_block + ).as(:block) end def source_block @@ -27,6 +28,40 @@ def quote_block block_style("_") end + def block_nh + ( sidebar_block_nh | + example_block_nh | + source_block_nh | + quote_block_nh | + pass_block_nh + ).as(:block) + end + + def sidebar_block_nh + block_style_nh("*") + end + + def example_block_nh + block_style_nh("=") + end + + def source_block_nh + block_style_nh("-", 2) + end + + def pass_block_nh + block_style_nh("+", 4, :pass) + end + + def source_block_nh + block_style_nh("-", 2) + end + + def quote_block_nh + block_style_nh("_") + end + + def block_content(n_deep = 2) c = block_image | list | @@ -36,6 +71,16 @@ def block_content(n_deep = 2) c.repeat(1) end + def block_content_nh(n_deep = 2) + c = block_image | + # list | + text_line | + empty_line.as(:line_break) + # c = c | block_content(n_deep - 1) if (n_deep > 0) + c.repeat(1) + end + + def sidebar_block block_style("*") end @@ -60,6 +105,16 @@ def block_id str("[#") >> keyword.as(:id) >> str("]")) >> newline end + def block_style_nh(delimiter = "*", repeater = 4, type = nil) + str(delimiter).repeat(repeater).as(:delimiter) >> newline >> + # if type == :pass + # (text_line | empty_line.as(:line_break)).repeat(1).as(:lines) + # else + block_content_nh(0).as(:lines) >> + # end >> + str(delimiter).repeat(repeater) >> newline + end + def block_style(delimiter = "*", repeater = 4, type = nil) block_id.maybe >> block_title.maybe >> diff --git a/lib/coradoc/parser/asciidoc/content.rb b/lib/coradoc/parser/asciidoc/content.rb index 0039fc7..22483d4 100644 --- a/lib/coradoc/parser/asciidoc/content.rb +++ b/lib/coradoc/parser/asciidoc/content.rb @@ -26,13 +26,17 @@ def literal_space? end # Text - def text_line(many_breaks = false) + def text_line(has_break = true, many_breaks = false) tl = (asciidoc_char_with_id.absent? | text_id) >> literal_space? >> text.as(:text) - if many_breaks - tl >> line_ending.repeat(1).as(:line_break) + if has_break + if many_breaks + tl >> line_ending.repeat(1).as(:line_break) + else + tl >> line_ending.as(:line_break) + end else - tl >> line_ending.as(:line_break) + tl end end diff --git a/lib/coradoc/parser/asciidoc/list.rb b/lib/coradoc/parser/asciidoc/list.rb index 8a8529a..c28f739 100644 --- a/lib/coradoc/parser/asciidoc/list.rb +++ b/lib/coradoc/parser/asciidoc/list.rb @@ -10,9 +10,13 @@ def list ).as(:list) end + def list_continuation + str("\n+\n")#.as(:list_continuation) + end + def ordered_list(nesting_level = 1) attrs = (attribute_list >> newline).maybe - r = olist_item(nesting_level) + r = olist_item(nesting_level, true) if nesting_level <= 8 r = r | ordered_list(nesting_level + 1) end @@ -21,7 +25,7 @@ def ordered_list(nesting_level = 1) def unordered_list(nesting_level = 1) attrs = (attribute_list >> newline).maybe - r = ulist_item(nesting_level) + r = ulist_item(nesting_level, true) if nesting_level <= 8 r = r | unordered_list(nesting_level + 1) end @@ -34,23 +38,54 @@ def definition_list(delimiter = "::") dlist_item(delimiter).absent? end - def olist_item(nesting_level = 1) + + def olist_item(nesting_level = 1, has_break = true) nl2 = nesting_level - 1 marker = match(/^\./) marker = marker >> str(".").repeat(nl2, nl2) if nl2 > 0 - str("").as(:list_item) >> + ( marker.as(:marker) >> str(".").absent? >> - match("\n").absent? >> space >> text_line(true) + match("\n").absent? >> space >> + + ( (text_line(has_break, true) >> + str("+\n").absent?) | + + (text_line(false).repeat(1,1) >> + (list_continuation.repeat(1,1) >> + (text_line(true)) ).repeat(1) + ).as(:text) | + (text_line(false).repeat(1,1) >> + (list_continuation.repeat(1,1) >> + (text_line(true) | block_nh) ).repeat(1) + ).as(:text) + + ) + ).as(:list_item) end - def ulist_item(nesting_level = 1) + def ulist_item(nesting_level = 1, has_break = true) nl2 = nesting_level - 1 marker = match(/^\*/) marker = marker >> str("*").repeat(nl2, nl2) if nl2 > 0 - str("").as(:list_item) >> + ( marker.as(:marker) >> str("*").absent? >> str(' [[[').absent? >> - match("\n").absent? >> space >> text_line(true) + match("\n").absent? >> space >> + + ( (text_line(has_break, true) >> + str("+\n").absent?) | + + (text_line(false).repeat(1,1) >> + (list_continuation.repeat(1,1) >> + (text_line(true)) ).repeat(1) + ).as(:text) | + + (text_line(false).repeat(1,1) >> + (list_continuation.repeat(1,1) >> + (text_line(true) | block_nh) ).repeat(1) + ).as(:text) + ) + ).as(:list_item) end def dlist_delimiter diff --git a/lib/coradoc/parser/asciidoc/section.rb b/lib/coradoc/parser/asciidoc/section.rb index 8f3c5fb..94d7a7c 100644 --- a/lib/coradoc/parser/asciidoc/section.rb +++ b/lib/coradoc/parser/asciidoc/section.rb @@ -14,7 +14,7 @@ def contents comment_line | include_directive | admonition_line | - block.as(:block) | + block | table.as(:table) | highlight.as(:highlight) | glossaries.as(:glossaries) | diff --git a/lib/coradoc/parser/base.rb b/lib/coradoc/parser/base.rb index 6bbe4a6..66765e2 100644 --- a/lib/coradoc/parser/base.rb +++ b/lib/coradoc/parser/base.rb @@ -43,7 +43,7 @@ class Base < Parslet::Parser tag | comment_block | comment_line | - block.as(:block) | + block | section.as(:section) | include_directive | document_attributes | diff --git a/lib/coradoc/transformer.rb b/lib/coradoc/transformer.rb index 44e7115..35b8c75 100644 --- a/lib/coradoc/transformer.rb +++ b/lib/coradoc/transformer.rb @@ -379,28 +379,17 @@ class NamedAttribute < Struct.new(:key, :value); end Element::Table.new(title, rows, opts) end - rule(list_item: simple(:list_item), - marker: simple(:marker), - text: simple(:text), - line_break: simple(:line_break)) do - Element::ListItem.new( - text, - marker: marker.to_s, - line_break: line_break - ) + rule(list_continuation: simple(:list_continuation)) do + Element::List::Continuation.new end - rule(list_item: simple(:list_item), - marker: simple(:marker), - id: simple(:id), - text: simple(:text), - line_break: simple(:line_break)) do + rule(list_item: subtree(:list_item)) do + marker = list_item[:marker] + id = list_item[:id] + text = list_item[:text] + line_break = list_item[:line_break] Element::ListItem.new( - text, - id: id, - marker: marker.to_s, - line_break: line_break - ) + text, id:, marker:, line_break: ) end diff --git a/spec/coradoc/parser/asciidoc/content_spec.rb b/spec/coradoc/parser/asciidoc/content_spec.rb index 017fc82..a0d54fa 100644 --- a/spec/coradoc/parser/asciidoc/content_spec.rb +++ b/spec/coradoc/parser/asciidoc/content_spec.rb @@ -217,9 +217,9 @@ list_items = ast[0][:list][:unordered] expect(list_items.count).to eq(3) - expect(list_items[0][:text]).to eq("Unordered list item 1") - expect(list_items[2][:id]).to eq("list_item_id") - expect(list_items[2][:text]).to eq("Unordered list item 3") + expect(list_items[0][:list_item][:text]).to eq("Unordered list item 1") + expect(list_items[2][:list_item][:id]).to eq("list_item_id") + expect(list_items[2][:list_item][:text]).to eq("Unordered list item 3") end it "parses the table block" do diff --git a/spec/coradoc/parser/asciidoc/section_spec.rb b/spec/coradoc/parser/asciidoc/section_spec.rb index c728cb0..467e223 100644 --- a/spec/coradoc/parser/asciidoc/section_spec.rb +++ b/spec/coradoc/parser/asciidoc/section_spec.rb @@ -149,9 +149,9 @@ expect(section[:id]).to eq("section_id") expect(section[:title][:text]).to eq("Section title") - expect(list_items[0][:text]).to eq("List item one") - expect(list_items[1][:id]).to eq("list_item_id") - expect(list_items[1][:text]).to eq("List item two") + expect(list_items[0][:list_item][:text]).to eq("List item one") + expect(list_items[1][:list_item][:id]).to eq("list_item_id") + expect(list_items[1][:list_item][:text]).to eq("List item two") end it "parses blocks with different types" do diff --git a/spec/coradoc/parser_spec.rb b/spec/coradoc/parser_spec.rb index 2a85af5..d7f8bb6 100644 --- a/spec/coradoc/parser_spec.rb +++ b/spec/coradoc/parser_spec.rb @@ -44,8 +44,8 @@ list_one_items = guidance[:contents][2][:list][:unordered] expect(list_one_items.count).to eq(3) - expect(list_one_items[0][:text]).not_to be_nil - expect(list_one_items[0][:id]).to eq("guidance_5.1_part_2_1") + expect(list_one_items[0][:list_item][:text]).not_to be_nil + expect(list_one_items[0][:list_item][:id]).to eq("guidance_5.1_part_2_1") expect(guidance[:contents][3][:paragraph][:lines][0][:id]).not_to be_nil expect(guidance[:contents][4][:list][:unordered].count).to eq(7)