Skip to content

Commit

Permalink
#38
Browse files Browse the repository at this point in the history
- ListItem: added attaching block paragraph/block/admonition
- List: added multiple tests in various combinations of the above e.g. nested lists, attached paragraph, attached admonition, two attached paragraphs, nested list with attached paragraph, nested list with attached multiline paragraph, attached paragraph and nested list

Important improvement: code in Coradoc::Parser::Asciidoc::Base that converts grammar rules written as ruby methods into proper parslet rules, which as it turned out was not the case previously. On top of that, it goes beyond what parslet is capable by also supporting arguments. Due to this improvement time for running all the tests came down (on tested hardware, approximately) from 50 seconds to 10 seconds (5x speedup) and time for running utils/round_trip.rb came down from 2.5 minutes to 3 seconds (50x speedup), which is also realistic input. This improvement makes it possible to iterate on grammar rules much faster, develop parsing inlines and potentially parsing input from various sources, for example test cases from asciidoc, in much more reasonable time.
- Coradoc::Parser::Asciidoc::Base was changed from a module to a class, rules previously contained there were moved to module Coradoc::Parser::Asciidoc::Text

Other improvements:
- Blocks: added missing block types: listing, open. added missing properties in literal block. (based on things noticed with utils/round_trip.rb)
- Grammar rules to be used inside of other rules e.g. line_start?, line_not_text?. Purpose of those rules is making sure rules using them are applied by parser only in places they should be applied.
- Tests for blocks, paragraph, table, two parsing bugs to be fixed
- Minor fixes in tests
  • Loading branch information
xyz65535 committed Nov 5, 2024
1 parent 41438d9 commit 76c1718
Show file tree
Hide file tree
Showing 36 changed files with 1,027 additions and 356 deletions.
4 changes: 3 additions & 1 deletion lib/coradoc/element/block.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ module Block
require_relative "block/core"
require_relative "block/example"
require_relative "block/literal"
require_relative "block/quote"
require_relative "block/listing"
require_relative "block/open"
require_relative "block/pass"
require_relative "block/quote"
require_relative "block/side"
require_relative "block/sourcecode"
require_relative "block/reviewer_comment"
7 changes: 4 additions & 3 deletions lib/coradoc/element/block/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,13 @@ def type_from_delimiter

def type_hash
@type_hash ||= {
"____" => :quote,
"****" => :side,
"----" => :source,
"====" => :example,
"...." => :literal,
"--" => :open,
"++++" => :pass,
"____" => :quote,
"****" => :side,
"----" => :source,
}
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/coradoc/element/block/example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def initialize(title, options = {})
end

def to_adoc
"\n\n#{gen_anchor}#{gen_title}#{gen_delimiter}\n" << gen_lines << "\n#{gen_delimiter}\n\n"
"\n\n#{gen_anchor}#{gen_title}#{gen_attributes}#{gen_delimiter}\n" << gen_lines << "\n#{gen_delimiter}\n\n"
end
end
end
Expand Down
21 changes: 21 additions & 0 deletions lib/coradoc/element/block/listing.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module Coradoc
module Element
module Block
class Listing < Core
def initialize(_title, options = {})
@id = options.fetch(:id, nil)
@anchor = @id.nil? ? nil : Inline::Anchor.new(@id)
@lang = options.fetch(:lang, "")
@attributes = options.fetch(:attributes, AttributeList.new)
@lines = options.fetch(:lines, [])
@delimiter_char = "-"
@delimiter_len = options.fetch(:delimiter_len, 4)
end

def to_adoc
"\n\n#{gen_anchor}#{gen_attributes}\n#{gen_delimiter}\n" << gen_lines << "\n#{gen_delimiter}\n\n"
end
end
end
end
end
6 changes: 4 additions & 2 deletions lib/coradoc/element/block/literal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@ module Coradoc
module Element
module Block
class Literal < Core
def initialize(_title, options = {})
def initialize(title, options = {})
@title = title
@id = options.fetch(:id, nil)
@anchor = @id.nil? ? nil : Inline::Anchor.new(@id)
@attributes = options.fetch(:attributes, AttributeList.new)
@lines = options.fetch(:lines, [])
@delimiter_char = "."
@delimiter_len = options.fetch(:delimiter_len, 4)
end

def to_adoc
"\n\n#{gen_anchor}#{gen_delimiter}\n" << gen_lines << "\n#{gen_delimiter}\n\n"
"\n\n#{gen_anchor}#{gen_title}#{gen_attributes}#{gen_delimiter}\n" << gen_lines << "\n#{gen_delimiter}\n\n"
end
end
end
Expand Down
22 changes: 22 additions & 0 deletions lib/coradoc/element/block/open.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module Coradoc
module Element
module Block
class Open < Core
def initialize(title, options = {})
@title = title
@id = options.fetch(:id, nil)
@anchor = @id.nil? ? nil : Inline::Anchor.new(@id)
@lang = options.fetch(:lang, "")
@attributes = options.fetch(:attributes, AttributeList.new)
@lines = options.fetch(:lines, [])
@delimiter_char = "-"
@delimiter_len = options.fetch(:delimiter_len, 2)
end

def to_adoc
"\n\n#{gen_anchor}#{gen_attributes}#{gen_delimiter}\n" << gen_lines << "\n#{gen_delimiter}\n\n"
end
end
end
end
end
4 changes: 2 additions & 2 deletions lib/coradoc/element/list/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ def initialize(items, options = {})
m = @items.select do |i|
i.is_a?(Coradoc::Element::ListItem) &&
!i.marker.nil?
end.first&.marker
@ol_count = m.size if m.is_a?(String)
end.first&.marker.to_s
@ol_count = m.size
end
@ol_count = 1 if @ol_count.nil?
@attrs = options.fetch(:attrs, AttributeList.new)
Expand Down
1 change: 1 addition & 0 deletions lib/coradoc/element/list/ordered.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ def initialize(items, options = {})
end

def prefix
return @marker if @marker
"." * [@ol_count, 1].max
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/coradoc/element/list/unordered.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ def initialize(items, options = {})
end

def prefix
return @marker if @marker
"*" * [@ol_count, 1].max
end
end
Expand Down
18 changes: 13 additions & 5 deletions lib/coradoc/element/list_item.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Coradoc
module Element
class ListItem < Base
attr_accessor :marker, :id, :anchor, :content, :line_break
attr_accessor :marker, :id, :anchor, :content, :subitem, :line_break

declare_children :content, :id, :anchor

Expand All @@ -10,11 +10,14 @@ def initialize(content, options = {})
@id = options.fetch(:id, nil)
@anchor = @id.nil? ? nil : Inline::Anchor.new(@id)
@content = content
@attached = options.fetch(:attached, [])
@nested = options.fetch(:nested, nil)
@line_break = options.fetch(:line_break, "\n")
end

def to_adoc
anchor = @anchor.nil? ? "" : @anchor.to_adoc.to_s
anchor = @anchor.nil? ? "" : " #{@anchor.to_adoc.to_s} "
# text = Coradoc::Generator.gen_adoc(@content)
content = Array(@content).map do |subitem|
next if subitem.is_a? Inline::HardLineBreak

Expand All @@ -24,10 +27,15 @@ def to_adoc
if Coradoc.a_single?(subitem, Coradoc::Element::TextElement)
subcontent = Coradoc.strip_unicode(subcontent)
end
subcontent.chomp
subcontent
end.compact.join("\n+\n")

" #{anchor}#{content.chomp}#{@line_break}"
# attach = Coradoc::Generator.gen_adoc(@attached)
attach = @attached.map do |elem|
"+\n" + Coradoc::Generator.gen_adoc(elem)
end.join
nest = Coradoc::Generator.gen_adoc(@nested)
out = " #{anchor}#{content}#{@line_break}"
out + attach + nest
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/coradoc/element/text_element.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def inspect
str += "(#{@id})" if @id
str += ": "
str += @content.inspect
str += " + #{@line_break.inspect}" unless line_break.empty?
str += " + #{@line_break.inspect}" unless line_break.to_s.empty?
str
end

Expand Down
8 changes: 7 additions & 1 deletion lib/coradoc/parser/asciidoc/attribute_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@ def named_attribute_value
match('[^\],]').repeat(1)
end

def named_key
(str('reviewer') |
match('[a-zA-Z0-9_-]').repeat(1)).as(:named_key)
end

def named_attribute
(match('[a-zA-Z0-9_-]').repeat(1).as(:named_key) >>
( named_key >>
str(' ').maybe >> str("=") >> str(' ').maybe >>
match['a-zA-Z0-9_\- \"'].repeat(1).as(:named_value) >>
str(' ').maybe
Expand Down Expand Up @@ -51,6 +56,7 @@ def positional_zero_or_one
end

def attribute_list(name = :attribute_list)
str('[').present? >>
str('[') >> str("[").absent? >>
( named_many |
positional_one_named_many |
Expand Down
Loading

0 comments on commit 76c1718

Please sign in to comment.