Skip to content

Commit

Permalink
#38 list continuations
Browse files Browse the repository at this point in the history
  • Loading branch information
xyz65535 committed Sep 5, 2024
1 parent b0d50b6 commit d1f097d
Show file tree
Hide file tree
Showing 11 changed files with 148 additions and 46 deletions.
1 change: 1 addition & 0 deletions lib/coradoc/element/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
18 changes: 18 additions & 0 deletions lib/coradoc/element/list/continuation.rb
Original file line number Diff line number Diff line change
@@ -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
65 changes: 60 additions & 5 deletions lib/coradoc/parser/asciidoc/block.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 |
Expand All @@ -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
Expand All @@ -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 >>
Expand Down
12 changes: 8 additions & 4 deletions lib/coradoc/parser/asciidoc/content.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
51 changes: 43 additions & 8 deletions lib/coradoc/parser/asciidoc/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/coradoc/parser/asciidoc/section.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) |
Expand Down
2 changes: 1 addition & 1 deletion lib/coradoc/parser/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class Base < Parslet::Parser
tag |
comment_block |
comment_line |
block.as(:block) |
block |
section.as(:section) |
include_directive |
document_attributes |
Expand Down
27 changes: 8 additions & 19 deletions lib/coradoc/transformer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
6 changes: 3 additions & 3 deletions spec/coradoc/parser/asciidoc/content_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions spec/coradoc/parser/asciidoc/section_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions spec/coradoc/parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit d1f097d

Please sign in to comment.