From 477a0ebfe948dc096cf101f1592ced30375ede80 Mon Sep 17 00:00:00 2001 From: xyz65535 Date: Sun, 28 Jul 2024 20:46:03 +0200 Subject: [PATCH 01/13] adoc parser: rule dependence graph tool --- utils/parser_analyzer.rb | 65 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 utils/parser_analyzer.rb diff --git a/utils/parser_analyzer.rb b/utils/parser_analyzer.rb new file mode 100644 index 0000000..e13082a --- /dev/null +++ b/utils/parser_analyzer.rb @@ -0,0 +1,65 @@ +require "rubocop" + +def ast_from(string) + RuboCop::ProcessedSource.new(string, RUBY_VERSION.to_f).ast +end + +def is_def?(arr) + if Array === arr + if arr[0] == :def + $defs << arr + else + arr.each do |e| + is_def?(e) + end + end + end +end + +path = "lib/coradoc/parser/asciidoc/" +class_files = Dir.entries(path).select{|x| File.file?(path+x)} + +$all_defs = {} + +class_files.each do |cf| + a = ast_from(File.open(path+cf).read) + next if a.nil? + sexp = a.to_sexp_array + $defs = [] + is_def?(sexp) + $all_defs[cf] = $defs +end; + +relevant_names = $all_defs.map{|fn, defs| defs.map{|d| d[1]}}.flatten; + +require 'graphviz' + +g = GraphViz.new( :G, :type => :digraph ); + +g[:fontsize] = 8 +g[:rankdir] = "LR" +g[:concentrate] = true + +nodes = {} + +$all_defs.each do |file_name, defs| + + defs.each do |ast_def| + calls = ast_def[2..-1].flatten & relevant_names + node_name = ast_def[1] + nodes[node_name] = g.add_nodes( "#{node_name}\n#{file_name}" ) + end +end; + +$all_defs.each do |file_name, defs| + defs.each do |ast_def| + calls = ast_def[2..-1].flatten & relevant_names + node_name = ast_def[1] + calls.each do |cl| + w = g.add_edges(nodes[node_name], nodes[cl]) + w[:weight] = 1.5 + end + end +end; + +g.output( :png => "utils/parser_graph.png_#{Time.now.to_i}" ); From ec8b516dfe3b6af4993dae589ecdad06cb1b3015 Mon Sep 17 00:00:00 2001 From: xyz65535 Date: Sun, 28 Jul 2024 20:46:51 +0200 Subject: [PATCH 02/13] adoc parser: round trip tools --- utils/round_trip.rb | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 utils/round_trip.rb diff --git a/utils/round_trip.rb b/utils/round_trip.rb new file mode 100644 index 0000000..943260f --- /dev/null +++ b/utils/round_trip.rb @@ -0,0 +1,37 @@ +$LOAD_PATH.unshift("../coradoc/lib"); + +require "coradoc" + + +rice_path = "../mn-samples-iso/sources/international-standard/rice-2023/" + +if !Dir.exist?(rice_path) + puts "pleas set path to rice-2023" + exit +end + +adoc_files = Dir.glob("#{rice_path}**/*adoc"); + +adoc_files.each do |file_path| + puts file_path + file_path_rt = "#{file_path}.roundtrip" + file_path_diff = "#{file_path}.roundtrip.diff" + FileUtils.rm(file_path_rt) if File.exist?(file_path_rt) + FileUtils.rm(file_path_diff) if File.exist?(file_path_diff) + begin + adoc_file = File.open(file_path).read; + puts "parsing..." + ast = Coradoc::Parser::Base.new.parse(adoc_file); + puts "transforming..." + doc = Coradoc::Transformer.transform(ast[:document]) + #puts doc.inspect + # doc = Coradoc::Document.from_adoc(sample_file) + + puts "generating..." + generated_adoc = Coradoc::Generator.gen_adoc(doc) + File.open("#{file_path}.roundtrip","w"){|f| f.write(generated_adoc)} + `diff #{file_path} #{file_path}.roundtrip > #{file_path}.roundtrip.diff` + rescue + puts "unsuccessful..." + end +end; From f3966a979102bccb73da1089d19fa6630f47faf5 Mon Sep 17 00:00:00 2001 From: xyz65535 Date: Sun, 28 Jul 2024 20:48:49 +0200 Subject: [PATCH 03/13] fixes: list, added nesting --- lib/coradoc/element/list/core.rb | 10 +++++++++- lib/coradoc/element/list_item.rb | 6 ++++-- lib/coradoc/parser/asciidoc/list.rb | 28 +++++++++++++++++++++------- 3 files changed, 34 insertions(+), 10 deletions(-) diff --git a/lib/coradoc/element/list/core.rb b/lib/coradoc/element/list/core.rb index 5637eb2..587d2e4 100644 --- a/lib/coradoc/element/list/core.rb +++ b/lib/coradoc/element/list/core.rb @@ -14,7 +14,15 @@ def initialize(items, options = {}) @items = [@items] unless @items.is_a?(Array) @id = options.fetch(:id, nil) @anchor = @id.nil? ? nil : Inline::Anchor.new(@id) - @ol_count = options.fetch(:ol_count, 1) + @ol_count = options.fetch(:ol_count, nil) + if @ol_count.nil? + m = @items.select{ |i| + i.is_a?(Coradoc::Element::ListItem) && + !i.marker.nil? + }.first&.marker + @ol_count = m.size if m.is_a?(String) + end + @ol_count = 1 if @ol_count.nil? @attrs = options.fetch(:attrs, AttributeList.new) end diff --git a/lib/coradoc/element/list_item.rb b/lib/coradoc/element/list_item.rb index 910cdae..0a9ec52 100644 --- a/lib/coradoc/element/list_item.rb +++ b/lib/coradoc/element/list_item.rb @@ -1,14 +1,16 @@ module Coradoc module Element class ListItem < Base - attr_accessor :id, :content, :anchor + attr_accessor :marker, :id, :anchor, :content, :line_break declare_children :content, :id, :anchor def initialize(content, options = {}) - @content = content + @marker = options.fetch(:marker, nil) @id = options.fetch(:id, nil) @anchor = @id.nil? ? nil : Inline::Anchor.new(@id) + @content = content + @line_break = options.fetch(:line_break, "") end def to_adoc diff --git a/lib/coradoc/parser/asciidoc/list.rb b/lib/coradoc/parser/asciidoc/list.rb index f9b4ccb..dd20a4e 100644 --- a/lib/coradoc/parser/asciidoc/list.rb +++ b/lib/coradoc/parser/asciidoc/list.rb @@ -4,7 +4,7 @@ module Asciidoc module List def list - (unordered_list.as(:unordered) | + (unordered_list | ordered_list.as(:ordered) # definition_list | ).as(:list) end @@ -13,8 +13,12 @@ def ordered_list (olist_item >> newline.maybe).repeat(1) end - def unordered_list - (ulist_item >> newline.maybe).repeat(1) + def unordered_list(nesting_level = 1) + r = ulist_item(nesting_level) >> newline.maybe + if nesting_level <= 8 + r = r | unordered_list(nesting_level + 1) + end + r.repeat(1).as(:unordered) end def definition_list(delimiter = "::") @@ -22,12 +26,22 @@ def definition_list(delimiter = "::") dlist_item(delimiter).absent? end - def olist_item - match("^\\.") >> match("\n").absent? >> space >> text_line + def olist_item(nesting_level = 1) + nl2 = nesting_level - 1 + r = str("").as(:list_item) + r = r >> match("^\\.") + r = r >> str(".").repeat(nl2, nl2) if nl2 > 0 + r = (r).as(:marker) >> str(".").absent? >> + match("\n").absent? >> space >> text_line end - def ulist_item - match("^\\*") >> match("\n").absent? >> space >> text_line + def ulist_item(nesting_level = 1) + nl2 = nesting_level - 1 + r = str("").as(:list_item) + r = match("^\\*") + r = r >> str("*").repeat(nl2, nl2) if nl2 > 0 + r = (r).as(:marker) >> str("*").absent? >> + match("\n").absent? >> space >> text_line end def dlist_delimiter From b912f6a4094b57725783c6e2b476a72996fccdc2 Mon Sep 17 00:00:00 2001 From: xyz65535 Date: Sun, 28 Jul 2024 20:52:23 +0200 Subject: [PATCH 04/13] fixes: document, paragraph, section --- lib/coradoc/element/document_attributes.rb | 3 +- lib/coradoc/element/include.rb | 2 +- lib/coradoc/parser/asciidoc/base.rb | 5 +- lib/coradoc/parser/asciidoc/content.rb | 4 +- lib/coradoc/parser/asciidoc/paragraph.rb | 5 +- lib/coradoc/parser/asciidoc/section.rb | 7 ++- lib/coradoc/transformer.rb | 50 ++++++++++++------- .../asciidoc/document_attributes_spec.rb | 6 ++- spec/coradoc/parser/asciidoc/section_spec.rb | 2 +- 9 files changed, 54 insertions(+), 30 deletions(-) diff --git a/lib/coradoc/element/document_attributes.rb b/lib/coradoc/element/document_attributes.rb index a61a983..796b301 100644 --- a/lib/coradoc/element/document_attributes.rb +++ b/lib/coradoc/element/document_attributes.rb @@ -18,7 +18,8 @@ def to_hash def to_adoc to_hash.map do |key, value| - ":#{key}: #{value}\n" + v = value.to_s.empty? ? "" : " #{value}" + ":#{key}:#{v}\n" end.join("\n") + "\n" end end diff --git a/lib/coradoc/element/include.rb b/lib/coradoc/element/include.rb index aa23db3..3698373 100644 --- a/lib/coradoc/element/include.rb +++ b/lib/coradoc/element/include.rb @@ -12,7 +12,7 @@ def initialize(path, options = {}) def to_adoc attrs = @attributes.to_adoc(true) - "include::#{@path}#{attrs}" + "include::#{@path}#{attrs}#{@line_break}" end end end diff --git a/lib/coradoc/parser/asciidoc/base.rb b/lib/coradoc/parser/asciidoc/base.rb index afb3a8f..3673108 100644 --- a/lib/coradoc/parser/asciidoc/base.rb +++ b/lib/coradoc/parser/asciidoc/base.rb @@ -34,7 +34,8 @@ def space? end def space - match('\s').repeat(1) + str(' ').repeat(1) + # match('\s').repeat(1) end def text @@ -116,7 +117,7 @@ def include_directive (str("include::") >> file_path.as(:path) >> attribute_list >> - (line_ending) + (newline | str("")).as(:line_break) ).as(:include) end diff --git a/lib/coradoc/parser/asciidoc/content.rb b/lib/coradoc/parser/asciidoc/content.rb index d6e673c..dc8b01c 100644 --- a/lib/coradoc/parser/asciidoc/content.rb +++ b/lib/coradoc/parser/asciidoc/content.rb @@ -31,7 +31,7 @@ def text_line comment_line.absent? >> include_directive.absent?) >> (asciidoc_char_with_id.absent? | text_id) >> literal_space? >> - text.as(:text) >> line_ending.as(:break) + text.as(:text) >> line_ending.as(:line_break) end def asciidoc_char @@ -49,7 +49,7 @@ def text_id def glossary keyword.as(:key) >> str("::") >> (str(" ") | newline) >> - text.as(:value) >> line_ending.as(:break) + text.as(:value) >> line_ending.as(:line_break) end def glossaries diff --git a/lib/coradoc/parser/asciidoc/paragraph.rb b/lib/coradoc/parser/asciidoc/paragraph.rb index 0196960..7f61a26 100644 --- a/lib/coradoc/parser/asciidoc/paragraph.rb +++ b/lib/coradoc/parser/asciidoc/paragraph.rb @@ -26,9 +26,10 @@ def paragraph list.absent? >> admonition_line.absent? >> - ((attribute_list >> newline).maybe >> + ( block_title.maybe >> + (attribute_list >> newline).maybe >> (paragraph_text_line.repeat(1,1) >> any.absent? | - (paragraph_text_line >> newline_single.as(:break)).repeat(1) >> + (paragraph_text_line >> newline_single.as(:line_break)).repeat(1) >> (paragraph_text_line.repeat(1,1)).repeat(0,1) ).as(:lines) >> newline.repeat(0) diff --git a/lib/coradoc/parser/asciidoc/section.rb b/lib/coradoc/parser/asciidoc/section.rb index c5a2949..6bfa457 100644 --- a/lib/coradoc/parser/asciidoc/section.rb +++ b/lib/coradoc/parser/asciidoc/section.rb @@ -5,6 +5,7 @@ module Section def contents ( + citation | comment_block | comment_line | include_directive | @@ -19,6 +20,10 @@ def contents ).repeat(1) end + def citation + (str("[.source]\n") >> cross_reference.as(:reference) ).as(:citation) + end + def section_block(level = 2) section_id.maybe >> section_title(level).as(:title) >> @@ -34,7 +39,7 @@ def section_id # Heading def section_title(level = 2, max_level = 8) match("=").repeat(level, max_level).as(:level) >> - space? >> text.as(:text) >> endline.as(:break) + space? >> text.as(:text) >> endline.as(:line_break) end # section diff --git a/lib/coradoc/transformer.rb b/lib/coradoc/transformer.rb index 6c1afbc..07e7505 100644 --- a/lib/coradoc/transformer.rb +++ b/lib/coradoc/transformer.rb @@ -67,21 +67,27 @@ class NamedAttribute < Struct.new(:key, :value); end # Include rule(include: { path: simple(:path), - attribute_list: simple(:attribute_obj)} + attribute_list: simple(:attribute_list), + line_break: simple(:line_break)} ) { Element::Include.new( path.to_s, - attributes: attribute_obj) + attributes: attribute_list, + line_break: line_break) } # Text Element rule(text: simple(:text)) { - Element::TextElement.new(text) + Element::TextElement.new(text.to_s) + } + + rule(text: simple(:text), line_break: simple(:line_break)) { + Element::TextElement.new(text.to_s, line_break: line_break) } rule(id: simple(:id), text: simple(:text)) do - Element::TextElement.new(text.to_s, id: id) + Element::TextElement.new(text.to_s, id: id.to_s) end rule(text: sequence(:text)) { @@ -90,7 +96,7 @@ class NamedAttribute < Struct.new(:key, :value); end rule( text: simple(:text), - break: simple(:line_break) + line_break: simple(:line_break) ) do Element::TextElement.new( text.to_s, @@ -176,33 +182,30 @@ class NamedAttribute < Struct.new(:key, :value); end # Paragraph - # rule(paragraph: simple(:paragraph)) { paragraph } - # rule(lines: sequence(:lines)) { Element::Paragraph.new(lines) } - # rule(meta: simple(:meta), lines: sequence(:lines)) do - # Element::Paragraph.new(lines, meta: meta) - # end - - # Paragraph - rule(paragraph: simple(:paragraph)) { paragraph } - rule(lines: sequence(:lines)) { Element::Paragraph.new(lines) } - rule(attribute_list: simple(:attribute_list), lines: sequence(:lines)) do - Element::Paragraph.new(lines, meta: attribute_list) + rule(paragraph: subtree(:paragraph)) do + Element::Paragraph.new( + paragraph[:lines], + meta: paragraph[:attribute_list], + title: paragraph[:title] + ) end + + # Title Element rule( level: simple(:level), text: simple(:text), - break: simple(:line_break), + line_break: simple(:line_break), ) do - Element::Title.new(text, level, line_break: line_break) + Element::Title.new(text, level.to_s, line_break: line_break) end rule( name: simple(:name), level: simple(:level), text: simple(:text), - break: simple(:line_break), + line_break: simple(:line_break), ) do Element::Title.new(text, level, line_break: line_break, id: name) end @@ -227,6 +230,15 @@ class NamedAttribute < Struct.new(:key, :value); end Element::Section.new(title, id: id, sections: sections) end + rule( + id: simple(:id), + title: simple(:title), + contents: sequence(:contents), + ) do + Element::Section.new(title, id: id, contents: contents) + end + + rule( title: simple(:title), contents: sequence(:contents), diff --git a/spec/coradoc/parser/asciidoc/document_attributes_spec.rb b/spec/coradoc/parser/asciidoc/document_attributes_spec.rb index c5fa2ea..8aa5745 100644 --- a/spec/coradoc/parser/asciidoc/document_attributes_spec.rb +++ b/spec/coradoc/parser/asciidoc/document_attributes_spec.rb @@ -11,11 +11,12 @@ :remarks: OSCAL from ISO27002:2022 :mn-output-extensions: xml,html,doc,html_alt :title-main-fr: Spécification et méthodes d'essai + :local-cache-only: DOC ast = Asciidoc::DocumentAttributesTester.parse(document_attributes).first - expect(ast[:document_attributes].count).to eq(7) + expect(ast[:document_attributes].count).to eq(8) expect(ast[:document_attributes][0][:key]).to eq("published") expect(ast[:document_attributes][0][:value]).to eq("'2023-03-08T09:51:08+08:00'") @@ -30,6 +31,9 @@ expect(ast[:document_attributes][6][:key]).to eq("title-main-fr") expect(ast[:document_attributes][6][:value]).to eq("Spécification et méthodes d'essai") + + expect(ast[:document_attributes][7][:key]).to eq("local-cache-only") + expect(ast[:document_attributes][7][:value]).to eq("") end end diff --git a/spec/coradoc/parser/asciidoc/section_spec.rb b/spec/coradoc/parser/asciidoc/section_spec.rb index a5ff2c7..5b9c913 100644 --- a/spec/coradoc/parser/asciidoc/section_spec.rb +++ b/spec/coradoc/parser/asciidoc/section_spec.rb @@ -180,7 +180,7 @@ contents = section[:contents] expect(contents.count).to eq(3) - expect(contents[0][:block][:type]).to eq("example") + expect(contents[0][:block][:attribute_list][:attribute_array][0][:positional]).to eq("example") expect(contents[1][:block][:delimiter]).to eq("====") expect(section[:title][:text]).to eq("Basic block with perimeters") expect(contents[2][:block][:lines][0][:text]).to eq("Renders in monospace") From 005c3b8ab48e083279c35ec1ef9888300a764e49 Mon Sep 17 00:00:00 2001 From: xyz65535 Date: Sun, 28 Jul 2024 20:55:03 +0200 Subject: [PATCH 05/13] fixes: block, added reviewer comment --- lib/coradoc/element/block.rb | 1 + lib/coradoc/element/block/core.rb | 8 ++-- lib/coradoc/element/block/reviewer_comment.rb | 19 +++++++++ lib/coradoc/parser/asciidoc/block.rb | 17 ++++++-- lib/coradoc/transformer.rb | 39 +++++++++++++++---- spec/coradoc/parser/asciidoc/content_spec.rb | 10 ++--- 6 files changed, 73 insertions(+), 21 deletions(-) create mode 100644 lib/coradoc/element/block/reviewer_comment.rb diff --git a/lib/coradoc/element/block.rb b/lib/coradoc/element/block.rb index 6ffb3ce..861a11e 100644 --- a/lib/coradoc/element/block.rb +++ b/lib/coradoc/element/block.rb @@ -11,3 +11,4 @@ module Block require_relative "block/quote" require_relative "block/side" require_relative "block/sourcecode" +require_relative "block/reviewer_comment" \ No newline at end of file diff --git a/lib/coradoc/element/block/core.rb b/lib/coradoc/element/block/core.rb index 1ec42a5..b72cb93 100644 --- a/lib/coradoc/element/block/core.rb +++ b/lib/coradoc/element/block/core.rb @@ -10,13 +10,13 @@ class Core < Base def initialize(title, options = {}) @title = title - @lines = options.fetch(:lines, []) + @id = options.fetch(:id, nil) + @anchor = @id.nil? ? nil : Inline::Anchor.new(@id) @type_str = options.fetch(:type, nil) - @delimiter = options.fetch(:delimiter, "") @attributes = options.fetch(:attributes, AttributeList.new) + @delimiter = options.fetch(:delimiter, "") @lang = options.fetch(:lang, nil) - @id = options.fetch(:id, nil) - @anchor = @id.nil? ? nil : Inline::Anchor.new(@id) + @lines = options.fetch(:lines, []) end def type diff --git a/lib/coradoc/element/block/reviewer_comment.rb b/lib/coradoc/element/block/reviewer_comment.rb new file mode 100644 index 0000000..299a651 --- /dev/null +++ b/lib/coradoc/element/block/reviewer_comment.rb @@ -0,0 +1,19 @@ +module Coradoc + module Element + module Block + class ReviewerComment < Core + def initialize(options = {}) + @title = options.fetch(:title, "") + @attributes = options.fetch(:attributes, AttributeList.new) + @delimiter_char = "*" + @delimiter_len = options.fetch(:delimiter_len, 4) + @lines = options.fetch(:lines, []) + end + + def to_adoc + "\n\n#{gen_title}#{gen_attributes}#{gen_delimiter}\n" << gen_lines << "\n#{gen_delimiter}\n\n" + end + end + end + end +end diff --git a/lib/coradoc/parser/asciidoc/block.rb b/lib/coradoc/parser/asciidoc/block.rb index b64c480..4246eb7 100644 --- a/lib/coradoc/parser/asciidoc/block.rb +++ b/lib/coradoc/parser/asciidoc/block.rb @@ -4,7 +4,10 @@ module Asciidoc module Block def block - sidebar_block | example_block | source_block | quote_block + sidebar_block | + example_block | + source_block | + quote_block end def source_block @@ -19,6 +22,12 @@ def quote_block block_style("_") end + def block_content + (text_line | + list + ).repeat(1) #>> newline + end + def sidebar_block block_style("*") end @@ -28,7 +37,7 @@ def example_block end def block_title - str(".") >> text.as(:title) >> newline + str(".") >> space.absent? >> text.as(:title) >> newline end def block_type(type) @@ -40,9 +49,9 @@ def block_type(type) def block_style(delimiter = "*", repeater = 4, type = "") block_title.maybe >> newline.maybe >> - block_type(type).maybe >> + (attribute_list >> newline ).maybe >> str(delimiter).repeat(repeater).as(:delimiter) >> newline >> - text_line.repeat(1).as(:lines) >> + block_content.as(:lines) >> str(delimiter).repeat(repeater) >> newline end diff --git a/lib/coradoc/transformer.rb b/lib/coradoc/transformer.rb index 07e7505..fc49e0f 100644 --- a/lib/coradoc/transformer.rb +++ b/lib/coradoc/transformer.rb @@ -309,15 +309,38 @@ class NamedAttribute < Struct.new(:key, :value); end ) } - rule(block: { - delimiter: simple(:delimiter), - lines: sequence(:lines) - }) { + rule(block: subtree(:block) + # { + # title: simple(:title), + # attribute_list: simple(:attribute_list), + # delimiter: simple(:delimiter), + # lines: sequence(:lines) + # } + ) { + title = block[:title] + attribute_list = block[:attribute_list] + delimiter = block[:delimiter] + lines = block[:lines] + + opts = {title: title, + attributes: attribute_list, + delimiter_len: delimiter.size, + lines: lines} if delimiter == "****" - Element::Block::Side.new( - title: nil, - lines: lines - ) + # puts attribute_lisp.inspect + if (attribute_list.positional == [] && + attribute_list.named.keys[0] == "reviewer") + Element::Block::ReviewerComment.new( + opts + ) + elsif (attribute_list.positional[0] == "sidebar" && + attribute_list.named == {}) + Element::Block::Side.new( + opts + ) + end + elsif delimiter == "____" + Element::Block::Quote.new end } diff --git a/spec/coradoc/parser/asciidoc/content_spec.rb b/spec/coradoc/parser/asciidoc/content_spec.rb index a925732..218f9c6 100644 --- a/spec/coradoc/parser/asciidoc/content_spec.rb +++ b/spec/coradoc/parser/asciidoc/content_spec.rb @@ -29,7 +29,7 @@ ast = Asciidoc::ContentTester.parse(content) block = ast.first[:block] - expect(block[:type]).to eq("sidebar") + expect(block[:attribute_list][:attribute_array][0][:positional]).to eq("sidebar") expect(block[:delimiter]).to eq("****") expect(block[:title]).to eq("Side blocks (open block syntax)") expect(block[:lines].first[:text]).to eq("This renders in the side.") @@ -48,7 +48,7 @@ ast = Asciidoc::ContentTester.parse(content) block = ast.first[:block] - expect(block[:type]).to eq("sidebar") + expect(block[:attribute_list][:attribute_array][0][:positional]).to eq("sidebar") expect(block[:delimiter]).to eq("*****") expect(block[:title]).to eq("Side blocks (open block syntax)") expect(block[:lines].first[:text]).to eq("This renders in the side.") @@ -87,7 +87,7 @@ block_one = ast.first[:block] block_two = ast.last[:block] - expect(block_one[:type]).to eq("example") + expect(block_one[:attribute_list][:attribute_array][0][:positional]).to eq("example") expect(block_one[:delimiter]).to eq("====") expect(block_two[:delimiter]).to eq("======") expect(block_two[:lines][0][:text]).to eq("Example text with permiter") @@ -112,7 +112,7 @@ block_one = ast.first[:block] block_two = ast.last[:block] - expect(block_one[:type]).to eq("source") + expect(block_one[:attribute_list][:attribute_array][0][:positional]).to eq("source") expect(block_one[:delimiter]).to eq("--") expect(block_two[:delimiter]).to eq("----") expect(block_one[:lines][0][:text]).to eq("This renders in monospace.") @@ -137,7 +137,7 @@ block_one = ast.first[:block] block_two = ast.last[:block] - expect(block_one[:type]).to eq("quote") + expect(block_one[:attribute_list][:attribute_array][0][:positional]).to eq("quote") expect(block_one[:delimiter]).to eq("--") expect(block_two[:delimiter]).to eq("____") expect(block_one[:lines][0][:text]).to eq("This is quote type text") From 5a90413be428f18f5565127d8d67b52a76a24687 Mon Sep 17 00:00:00 2001 From: xyz65535 Date: Sun, 28 Jul 2024 20:55:33 +0200 Subject: [PATCH 06/13] fixes: table --- lib/coradoc/element/table.rb | 2 +- lib/coradoc/parser/asciidoc/table.rb | 3 +- lib/coradoc/reverse_adoc/converters/table.rb | 4 +- lib/coradoc/transformer.rb | 55 +++++++++++++++++--- spec/coradoc/element/title_spec.rb | 6 +-- 5 files changed, 55 insertions(+), 15 deletions(-) diff --git a/lib/coradoc/element/table.rb b/lib/coradoc/element/table.rb index a4cb3f8..0ea28d1 100644 --- a/lib/coradoc/element/table.rb +++ b/lib/coradoc/element/table.rb @@ -10,7 +10,7 @@ def initialize(title, rows, options = {}) @title = title @id = options.fetch(:id, nil) @anchor = @id.nil? ? nil : Inline::Anchor.new(@id) - @attrs = options.fetch(:attrs, "") + @attrs = options.fetch(:attributes, "") end def to_adoc diff --git a/lib/coradoc/parser/asciidoc/table.rb b/lib/coradoc/parser/asciidoc/table.rb index 2f8deb0..99a4fc7 100644 --- a/lib/coradoc/parser/asciidoc/table.rb +++ b/lib/coradoc/parser/asciidoc/table.rb @@ -5,7 +5,8 @@ module Table # include Coradoc::Parser::Asciidoc::Base def table - block_title >> + (attribute_list >> newline).maybe >> + block_title.maybe >> str("|===") >> line_ending >> table_row.repeat(1).as(:rows) >> str("|===") >> line_ending diff --git a/lib/coradoc/reverse_adoc/converters/table.rb b/lib/coradoc/reverse_adoc/converters/table.rb index 3437034..c0890dd 100644 --- a/lib/coradoc/reverse_adoc/converters/table.rb +++ b/lib/coradoc/reverse_adoc/converters/table.rb @@ -4,9 +4,9 @@ class Table < Base def to_coradoc(node, state = {}) id = node["id"] title = extract_title(node) - attrs = style(node) + attributes = style(node) content = treat_children_coradoc(node, state) - Coradoc::Element::Table.new(title, content, { id: id, attrs: attrs }) + Coradoc::Element::Table.new(title, content, { id: id, attributes: attributes }) end def extract_title(node) diff --git a/lib/coradoc/transformer.rb b/lib/coradoc/transformer.rb index fc49e0f..4105cf9 100644 --- a/lib/coradoc/transformer.rb +++ b/lib/coradoc/transformer.rb @@ -106,7 +106,7 @@ class NamedAttribute < Struct.new(:key, :value); end rule( id: simple(:id), text: simple(:text), - break: simple(:line_break) + line_break: simple(:line_break) ) do Element::TextElement.new( text.to_s, @@ -117,7 +117,7 @@ class NamedAttribute < Struct.new(:key, :value); end rule( id: simple(:id), text: sequence(:text), - break: simple(:line_break) + line_break: simple(:line_break) ) do Element::TextElement.new( text, @@ -129,7 +129,7 @@ class NamedAttribute < Struct.new(:key, :value); end rule(text: sequence(:text), - break: simple(:line_break) + line_break: simple(:line_break) ) do Element::TextElement.new( text, @@ -279,6 +279,10 @@ class NamedAttribute < Struct.new(:key, :value); end sections: sections) end + + + + rule(example: sequence(:example)) do Element::Core.new("", type: "example", lines: example) end @@ -380,7 +384,7 @@ class NamedAttribute < Struct.new(:key, :value); end end rule(key: simple(:key), value: simple(:value), - break: simple(:line_break)) do + line_break: simple(:line_break)) do Element::Attribute.new(key, value, line_break: line_break) end @@ -391,18 +395,53 @@ class NamedAttribute < Struct.new(:key, :value); end end # Table - rule(table: simple(:table)) { table } - rule(cols: sequence(:cols)) { Element::Table::Row.new(cols) } - rule(title: simple(:title), rows: sequence(:rows)) do - Element::Table.new(title, rows) + + rule(cols: sequence(:cols)) { + cells = cols.map{|c| Element::Table::Cell.new(content: c)} + Element::Table::Row.new(cells) + } + + rule(table: subtree(:table)) do + title = table[:title] || nil + rows = table[:rows] || [] + opts = {attributes: table[:attribute_list] || ""} + Element::Table.new(title, rows, opts) + end + + + + rule(marker: simple(:marker), + text: simple(:text), + line_break: simple(:line_break)) do + Element::ListItem.new( + text, + marker: marker.to_s, + line_break: line_break + ) + end + + rule(marker: simple(:marker), + id: simple(:id), + text: simple(:text), + line_break: simple(:line_break)) do + Element::ListItem.new( + text, + id: id, + marker: marker.to_s, + line_break: line_break + ) end + # List rule(list: simple(:list)) { list } rule(unordered: sequence(:list_items)) do Element::List::Unordered.new(list_items) end + + + # rule(list: simple(:list)) { list } rule(ordered: sequence(:list_items)) do Element::List::Ordered.new(list_items) diff --git a/spec/coradoc/element/title_spec.rb b/spec/coradoc/element/title_spec.rb index df481db..7c704cc 100644 --- a/spec/coradoc/element/title_spec.rb +++ b/spec/coradoc/element/title_spec.rb @@ -5,13 +5,13 @@ it "initializes instance and exposes attributes" do title = Coradoc::Element::Title.new( ast[:title], - ast[:level], + ast[:level_int], id: ast[:id], line_break: ast[:line_break], ) expect(title.id).to eq(ast[:id]) - expect(title.level).to eq(:heading_two) + expect(title.level_int).to eq(1) expect(title.content).to eq(ast[:title]) expect(title.line_break).to eq(ast[:line_break]) end @@ -19,7 +19,7 @@ def ast @ast ||= { - level: "==", + level_int: 1, id: "dummy-id", title: "Heading two", line_break: "\n\n", From b17f053fd2c79535a33d16497ae22bd3a06132a5 Mon Sep 17 00:00:00 2001 From: xyz65535 Date: Thu, 1 Aug 2024 18:39:19 +0200 Subject: [PATCH 07/13] fixes: roundtrip rice-2023 --- coradoc.gemspec | 1 + lib/coradoc/element/admonition.rb | 6 ++++ lib/coradoc/element/document_attributes.rb | 2 +- lib/coradoc/element/list_item.rb | 4 +-- lib/coradoc/element/paragraph.rb | 6 ++-- lib/coradoc/element/text_element.rb | 6 +++- lib/coradoc/element/title.rb | 2 +- lib/coradoc/parser/asciidoc/admonition.rb | 10 +++++- lib/coradoc/parser/asciidoc/attribute_list.rb | 8 ++--- lib/coradoc/parser/asciidoc/base.rb | 10 ++++-- lib/coradoc/parser/asciidoc/block.rb | 6 ++-- lib/coradoc/parser/asciidoc/content.rb | 13 +++---- lib/coradoc/parser/asciidoc/inline.rb | 12 +++---- lib/coradoc/parser/asciidoc/list.rb | 36 +++++++++++-------- lib/coradoc/parser/asciidoc/paragraph.rb | 24 ++++++------- lib/coradoc/parser/asciidoc/section.rb | 2 ++ lib/coradoc/reverse_adoc/converters/text.rb | 2 +- lib/coradoc/transformer.rb | 34 ++++++++++++------ .../parser/asciidoc/admonition_spec.rb | 22 +++++++++--- 19 files changed, 136 insertions(+), 70 deletions(-) diff --git a/coradoc.gemspec b/coradoc.gemspec index 45a4421..24748a8 100644 --- a/coradoc.gemspec +++ b/coradoc.gemspec @@ -44,5 +44,6 @@ Gem::Specification.new do |spec| spec.add_development_dependency "rubocop" spec.add_development_dependency "rubocop-performance" spec.add_development_dependency "simplecov" + spec.add_development_dependency "stackprof" # spec.add_runtime_dependency "thor" end diff --git a/lib/coradoc/element/admonition.rb b/lib/coradoc/element/admonition.rb index 08cb682..ce06800 100644 --- a/lib/coradoc/element/admonition.rb +++ b/lib/coradoc/element/admonition.rb @@ -8,6 +8,12 @@ def initialize(content, type, options = {}) @type = type.downcase.to_sym @line_break = options.fetch(:line_break, "") end + + def to_s + content = Coradoc::Generator.gen_adoc(@content) + "#{type.to_s.upcase}: #{content}" + end + end end end diff --git a/lib/coradoc/element/document_attributes.rb b/lib/coradoc/element/document_attributes.rb index 796b301..dd9bf92 100644 --- a/lib/coradoc/element/document_attributes.rb +++ b/lib/coradoc/element/document_attributes.rb @@ -20,7 +20,7 @@ def to_adoc to_hash.map do |key, value| v = value.to_s.empty? ? "" : " #{value}" ":#{key}:#{v}\n" - end.join("\n") + "\n" + end.join + "\n" end end end diff --git a/lib/coradoc/element/list_item.rb b/lib/coradoc/element/list_item.rb index 0a9ec52..9cf1bbf 100644 --- a/lib/coradoc/element/list_item.rb +++ b/lib/coradoc/element/list_item.rb @@ -10,7 +10,7 @@ def initialize(content, options = {}) @id = options.fetch(:id, nil) @anchor = @id.nil? ? nil : Inline::Anchor.new(@id) @content = content - @line_break = options.fetch(:line_break, "") + @line_break = options.fetch(:line_break, "\n") end def to_adoc @@ -27,7 +27,7 @@ def to_adoc subcontent.chomp end.compact.join("\n+\n") - " #{anchor}#{content.chomp}\n" + " #{anchor}#{content.chomp}#{@line_break}" end end end diff --git a/lib/coradoc/element/paragraph.rb b/lib/coradoc/element/paragraph.rb index f38b958..7b7a5f2 100644 --- a/lib/coradoc/element/paragraph.rb +++ b/lib/coradoc/element/paragraph.rb @@ -7,6 +7,7 @@ class Paragraph < Base def initialize(content, options = {}) @content = content + @title = options.fetch(:title, nil) @meta = options.fetch(:meta, nil) @id = options.fetch(:id, nil) @anchor = Inline::Anchor.new(@id) if @id @@ -22,11 +23,12 @@ def texts end def to_adoc + title = @title.nil? ? "" : ".#{Coradoc::Generator.gen_adoc(@title)}\n" anchor = @anchor.nil? ? "" : "#{@anchor.to_adoc}\n" if @tdsinglepara - anchor.to_s << Coradoc.strip_unicode(Coradoc::Generator.gen_adoc(@content)) + "#{title}#{anchor}" << Coradoc.strip_unicode(Coradoc::Generator.gen_adoc(@content)) else - "\n\n#{anchor}" << Coradoc.strip_unicode(Coradoc::Generator.gen_adoc(@content)) << "\n\n" + "\n\n#{title}#{anchor}" << Coradoc.strip_unicode(Coradoc::Generator.gen_adoc(@content)) << "\n\n" end end end diff --git a/lib/coradoc/element/text_element.rb b/lib/coradoc/element/text_element.rb index 958ff92..8b8490d 100644 --- a/lib/coradoc/element/text_element.rb +++ b/lib/coradoc/element/text_element.rb @@ -9,10 +9,14 @@ def initialize(content, options = {}) @content = content # .to_s @id = options.fetch(:id, nil) @line_break = options.fetch(:line_break, "") + @html_cleanup = options.fetch(:html_cleanup, false) + if @html_cleanup + @content = treat_text_to_adoc(@content) + end end def to_adoc - Coradoc::Generator.gen_adoc(treat_text_to_adoc(@content)) + Coradoc::Generator.gen_adoc(@content) + @line_break end def treat_text_to_adoc(text) diff --git a/lib/coradoc/element/title.rb b/lib/coradoc/element/title.rb index 8e64e9f..31605c3 100644 --- a/lib/coradoc/element/title.rb +++ b/lib/coradoc/element/title.rb @@ -7,7 +7,7 @@ class Title < Base def initialize(content, level, options = {}) @level_int = level - @level_int = level.length if level.is_a?(String) + # @level_int = level.length - 1 if level.is_a?(String) @content = content @id = options.fetch(:id, nil) @anchor = @id.nil? ? nil : Inline::Anchor.new(@id) diff --git a/lib/coradoc/parser/asciidoc/admonition.rb b/lib/coradoc/parser/asciidoc/admonition.rb index 2785fae..ea3bbcf 100644 --- a/lib/coradoc/parser/asciidoc/admonition.rb +++ b/lib/coradoc/parser/asciidoc/admonition.rb @@ -12,7 +12,15 @@ def admonition_type end def admonition_line admonition_type.as(:admonition_type) >> str(': ') >> - text.as(:text) >> line_ending.as(:line_break) + # (text_line.repeat(1).as(:content) | + # text_line.as(:text) >> line_ending.repeat(2).as(:line_break) | + # # (text.as(:text) >> line + # text_line.as(:text) + # # ) + # ) + ( + text_line(1).repeat(1) + ).as(:content) end end end diff --git a/lib/coradoc/parser/asciidoc/attribute_list.rb b/lib/coradoc/parser/asciidoc/attribute_list.rb index 7bea2c2..6aad044 100644 --- a/lib/coradoc/parser/asciidoc/attribute_list.rb +++ b/lib/coradoc/parser/asciidoc/attribute_list.rb @@ -12,10 +12,10 @@ def named_attribute_value end def named_attribute - (match['a-zA-Z0-9_-'].repeat(1).as(:named_key) >> - space? >> str("=") >> space? >> + (match('[a-zA-Z0-9_-]').repeat(1).as(:named_key) >> + str(' ').maybe >> str("=") >> str(' ').maybe >> match['a-zA-Z0-9_-'].repeat(1).as(:named_value) >> - space? + str(' ').maybe ).as(:named) end @@ -51,7 +51,7 @@ def positional_zero_or_one end def attribute_list - str("[") >> + match('^\[') >> str("[").absent? >> ( named_many | positional_one_named_many | positional_many_named_many | diff --git a/lib/coradoc/parser/asciidoc/base.rb b/lib/coradoc/parser/asciidoc/base.rb index 3673108..c88c076 100644 --- a/lib/coradoc/parser/asciidoc/base.rb +++ b/lib/coradoc/parser/asciidoc/base.rb @@ -31,6 +31,9 @@ module Base def space? space.maybe + # str(' ') >> str(' ').absent? | + # str(' ') >> str(' ').absent? | + # space.maybe end def space @@ -43,7 +46,7 @@ def text end def line_ending - match("[\n]") + str("\n") end def endline @@ -52,11 +55,11 @@ def endline def newline # match["\r\n"].repeat(1) - (match("\n") | match("\r\n")).repeat(1) + (str("\n") | str("\r\n")).repeat(1) end def newline_single - (match("\n") | match("\r\n")) + (str("\n") | str("\r\n")) end def keyword @@ -77,6 +80,7 @@ def digits def word match("[a-zA-Z0-9_-]").repeat(1) + # match(/[a-zA-Z0-9_-]+/) end def words diff --git a/lib/coradoc/parser/asciidoc/block.rb b/lib/coradoc/parser/asciidoc/block.rb index 4246eb7..f3488bc 100644 --- a/lib/coradoc/parser/asciidoc/block.rb +++ b/lib/coradoc/parser/asciidoc/block.rb @@ -37,11 +37,13 @@ def example_block end def block_title - str(".") >> space.absent? >> text.as(:title) >> newline + match("^\\.") >> space.absent? >> text.as(:title) >> newline end def block_type(type) - (str("[") >> str(type).as(:type) >> str("]")) | + (match("^[") >> str("[").absent? >> + str(type).as(:type) >> + str("]")) | (str("[") >> keyword.as(:type) >> str("]") ) >> newline end diff --git a/lib/coradoc/parser/asciidoc/content.rb b/lib/coradoc/parser/asciidoc/content.rb index dc8b01c..12cac08 100644 --- a/lib/coradoc/parser/asciidoc/content.rb +++ b/lib/coradoc/parser/asciidoc/content.rb @@ -26,12 +26,13 @@ def literal_space? end # Text - def text_line - (comment_block.absent? >> - comment_line.absent? >> - include_directive.absent?) >> + def text_line( n_line_breaks = 1) + # (comment_block.absent? >> + # comment_line.absent? >> + # include_directive.absent?) >> (asciidoc_char_with_id.absent? | text_id) >> literal_space? >> - text.as(:text) >> line_ending.as(:line_break) + text.as(:text) >> + line_ending.repeat(n_line_breaks).as(:line_break) end def asciidoc_char @@ -43,7 +44,7 @@ def asciidoc_char_with_id end def text_id - str("[[") >> keyword.as(:id) >> str("]]") | + str("[[") >> str('[').absent? >> keyword.as(:id) >> str("]]") | str("[#") >> keyword.as(:id) >> str("]") end diff --git a/lib/coradoc/parser/asciidoc/inline.rb b/lib/coradoc/parser/asciidoc/inline.rb index d73d302..0d417b7 100644 --- a/lib/coradoc/parser/asciidoc/inline.rb +++ b/lib/coradoc/parser/asciidoc/inline.rb @@ -10,42 +10,42 @@ def cross_reference end def bold_constrained - (str('*') >> str('*').absent? >> + (str('*') >> match("[^*]").repeat(1).as(:text).repeat(1,1) >> str('*') >> str('*').absent? ).as(:bold_constrained) end def bold_unconstrained - (str('**') >> str('*').absent? >> + (str('**') >> match("[^*\n]").repeat(1).as(:text).repeat(1,1) >> str('**') ).as(:bold_unconstrained) end def highlight_constrained - (str('#') >> str('#').absent? >> + (str('#') >> match('[^#]').repeat(1).as(:text).repeat(1,1) >> str('#') >> str('#').absent? ).as(:highlight_constrained) end def highlight_unconstrained - (str('##') >> str('#').absent? >> + (str('##') >> match('[^#]').repeat(1).as(:text).repeat(1,1) >> str('##') ).as(:highlight_unconstrained) end def italic_constrained - (str('_') >> str('_').absent? >> + (str('_') >> match('[^_]').repeat(1).as(:text).repeat(1,1) >> str('_') >> str('_').absent? ).as(:italic_constrained) end def italic_unconstrained - (str('__') >> str('_').absent? >> + (str('__') >> match('[^_]').repeat(1).as(:text).repeat(1,1) >> str('__') ).as(:italic_unconstrained) diff --git a/lib/coradoc/parser/asciidoc/list.rb b/lib/coradoc/parser/asciidoc/list.rb index dd20a4e..eeb2430 100644 --- a/lib/coradoc/parser/asciidoc/list.rb +++ b/lib/coradoc/parser/asciidoc/list.rb @@ -4,43 +4,51 @@ module Asciidoc module List def list - (unordered_list | - ordered_list.as(:ordered) # definition_list | + ( + unordered_list | + ordered_list # definition_list | ).as(:list) end - def ordered_list - (olist_item >> newline.maybe).repeat(1) + def ordered_list(nesting_level = 1) + attrs = (attribute_list >> newline).maybe + r = olist_item(nesting_level) + if nesting_level <= 8 + r = r | ordered_list(nesting_level + 1) + end + attrs >> r.repeat(1).as(:ordered) end def unordered_list(nesting_level = 1) - r = ulist_item(nesting_level) >> newline.maybe + attrs = (attribute_list >> newline).maybe + r = ulist_item(nesting_level) if nesting_level <= 8 r = r | unordered_list(nesting_level + 1) end - r.repeat(1).as(:unordered) + attrs >> r.repeat(1).as(:unordered) end def definition_list(delimiter = "::") + (attribute_list >> newline).maybe >> dlist_item(delimiter).as(:definition_list).repeat(1) >> dlist_item(delimiter).absent? end def olist_item(nesting_level = 1) nl2 = nesting_level - 1 - r = str("").as(:list_item) - r = r >> match("^\\.") - r = r >> str(".").repeat(nl2, nl2) if nl2 > 0 - r = (r).as(:marker) >> str(".").absent? >> + 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 end def ulist_item(nesting_level = 1) nl2 = nesting_level - 1 - r = str("").as(:list_item) - r = match("^\\*") - r = r >> str("*").repeat(nl2, nl2) if nl2 > 0 - r = (r).as(:marker) >> str("*").absent? >> + 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 end diff --git a/lib/coradoc/parser/asciidoc/paragraph.rb b/lib/coradoc/parser/asciidoc/paragraph.rb index 7f61a26..ac4252b 100644 --- a/lib/coradoc/parser/asciidoc/paragraph.rb +++ b/lib/coradoc/parser/asciidoc/paragraph.rb @@ -4,13 +4,13 @@ module Asciidoc module Paragraph def paragraph_text_line - include_directive.absent? >> - comment_block.absent? >> - comment_line.absent? >> - list.absent? >> - admonition_line.absent? >> + # include_directive.absent? >> + # comment_block.absent? >> + # comment_line.absent? >> + # list.absent? >> + # admonition_line.absent? >> - block.absent? >> + # block.absent? >> (asciidoc_char_with_id.absent? | text_id ) >> literal_space? >> (text_formatted.as(:text) # >> @@ -19,12 +19,12 @@ def paragraph_text_line end def paragraph - block.absent? >> - include_directive.absent? >> - comment_block.absent? >> - comment_line.absent? >> - list.absent? >> - admonition_line.absent? >> + # block.absent? >> + # include_directive.absent? >> + # comment_block.absent? >> + # comment_line.absent? >> + # list.absent? >> + # admonition_line.absent? >> ( block_title.maybe >> (attribute_list >> newline).maybe >> diff --git a/lib/coradoc/parser/asciidoc/section.rb b/lib/coradoc/parser/asciidoc/section.rb index 6bfa457..84180c3 100644 --- a/lib/coradoc/parser/asciidoc/section.rb +++ b/lib/coradoc/parser/asciidoc/section.rb @@ -26,6 +26,7 @@ def citation def section_block(level = 2) section_id.maybe >> + (attribute_list >> newline).maybe >> section_title(level).as(:title) >> contents.as(:contents).maybe end @@ -39,6 +40,7 @@ def section_id # Heading def section_title(level = 2, max_level = 8) match("=").repeat(level, max_level).as(:level) >> + str('=').absent? >> space? >> text.as(:text) >> endline.as(:line_break) end diff --git a/lib/coradoc/reverse_adoc/converters/text.rb b/lib/coradoc/reverse_adoc/converters/text.rb index 8375259..f949f2e 100644 --- a/lib/coradoc/reverse_adoc/converters/text.rb +++ b/lib/coradoc/reverse_adoc/converters/text.rb @@ -4,7 +4,7 @@ class Text < Base def to_coradoc(node, state = {}) return treat_empty(node, state) if node.text.strip.empty? - Coradoc::Element::TextElement.new(node.text) + Coradoc::Element::TextElement.new(node.text, html_cleanup: true) end private diff --git a/lib/coradoc/transformer.rb b/lib/coradoc/transformer.rb index 4105cf9..7be8f33 100644 --- a/lib/coradoc/transformer.rb +++ b/lib/coradoc/transformer.rb @@ -198,7 +198,7 @@ class NamedAttribute < Struct.new(:key, :value); end text: simple(:text), line_break: simple(:line_break), ) do - Element::Title.new(text, level.to_s, line_break: line_break) + Element::Title.new(text, level.size - 1, line_break: line_break) end rule( @@ -207,7 +207,7 @@ class NamedAttribute < Struct.new(:key, :value); end text: simple(:text), line_break: simple(:line_break), ) do - Element::Title.new(text, level, line_break: line_break, id: name) + Element::Title.new(text, level.size - 1, line_break: line_break, id: name) end # Section @@ -349,11 +349,14 @@ class NamedAttribute < Struct.new(:key, :value); end } # # Admonition - # rule(admonition: simple(:admonition)) { admonition } - # rule(type: simple(:type), text: simple(:text), break: simple(:line_break)) do - # Element::Admonition.new(text, type.to_s, line_break: line_break) - # end - # + # rule(admonition_type: simple(:admonition)) { admonition } + rule(admonition_type: simple(:admonition_type), + content: simple(:content), + # line_break: simple(:line_break) + ) do + Element::Admonition.new(content, admonition_type.to_s) + end + # # Block # rule(title: simple(:title), lines: sequence(:lines)) do # Element::Block.new(title, lines: lines) @@ -410,7 +413,8 @@ class NamedAttribute < Struct.new(:key, :value); end - rule(marker: simple(:marker), + rule(list_item: simple(:list_item), + marker: simple(:marker), text: simple(:text), line_break: simple(:line_break)) do Element::ListItem.new( @@ -420,7 +424,8 @@ class NamedAttribute < Struct.new(:key, :value); end ) end - rule(marker: simple(:marker), + rule(list_item: simple(:list_item), + marker: simple(:marker), id: simple(:id), text: simple(:text), line_break: simple(:line_break)) do @@ -438,7 +443,11 @@ class NamedAttribute < Struct.new(:key, :value); end rule(unordered: sequence(:list_items)) do Element::List::Unordered.new(list_items) end - + rule(attribute_list: simple(:attribute_list), + unordered: sequence(:list_items) + ) do + Element::List::Unordered.new(list_items, attrs: attribute_list) + end @@ -447,6 +456,11 @@ class NamedAttribute < Struct.new(:key, :value); end Element::List::Ordered.new(list_items) end + rule(attribute_list: simple(:attribute_list), + ordered: sequence(:list_items) + ) do + Element::List::Ordered.new(list_items, attrs: attribute_list) + end rule(terms: simple(:terms), definition: simple(:definition)) do diff --git a/spec/coradoc/parser/asciidoc/admonition_spec.rb b/spec/coradoc/parser/asciidoc/admonition_spec.rb index 66714aa..6f8bc69 100644 --- a/spec/coradoc/parser/asciidoc/admonition_spec.rb +++ b/spec/coradoc/parser/asciidoc/admonition_spec.rb @@ -2,15 +2,29 @@ RSpec.describe "Coradoc::Parser::Asciidoc::Admonition" do describe ".parse" do - it "parses admonitions" do + it "parses one line admonition" do parser = Asciidoc::AdmonitionTester ast = parser.parse("NOTE: some text\n") - exp = [{:admonition_type=>"NOTE", - :text => "some text", - :line_break => "\n"}] + exp = [{:admonition_type => "NOTE", + :content => [ + {:text => "some text", :line_break => "\n"} + ] + }] expect(ast).to eq(exp) + end + it "parses multi line admonition" do + parser = Asciidoc::AdmonitionTester + ast = parser.parse("NOTE: some text\ncontinued\n") + exp = [{:admonition_type => "NOTE", + :content => [ + {:text => "some text", :line_break => "\n"}, + {:text => "continued", :line_break => "\n"} + ] + }] + + expect(ast).to eq(exp) end end end From 489a9bdfecb8ac428c8cc7050cc3fab4528115a2 Mon Sep 17 00:00:00 2001 From: xyz65535 Date: Fri, 2 Aug 2024 17:45:57 +0200 Subject: [PATCH 08/13] cleanup: section --- lib/coradoc/element/section.rb | 6 +- lib/coradoc/parser/asciidoc/list.rb | 1 + lib/coradoc/parser/asciidoc/section.rb | 35 +++------- lib/coradoc/transformer.rb | 73 +++----------------- spec/coradoc/parser/asciidoc/section_spec.rb | 61 ++++++++-------- spec/coradoc/parser_spec.rb | 12 ++-- 6 files changed, 61 insertions(+), 127 deletions(-) diff --git a/lib/coradoc/element/section.rb b/lib/coradoc/element/section.rb index ea1cc21..74af3a7 100644 --- a/lib/coradoc/element/section.rb +++ b/lib/coradoc/element/section.rb @@ -1,7 +1,7 @@ module Coradoc module Element class Section < Base - attr_accessor :id, :title, :contents, :sections + attr_accessor :id, :title, :attrs, :contents, :sections declare_children :id, :title, :contents, :sections @@ -10,6 +10,7 @@ def initialize(title, options = {}) @id = options.fetch(:id, nil) @id = nil if @id == "" @anchor = @id.nil? ? nil : Inline::Anchor.new(@id) + @attrs = options.fetch(:attribute_list, "") @contents = options.fetch(:contents, []) @sections = options.fetch(:sections, []) end @@ -27,6 +28,7 @@ def content def to_adoc anchor = @anchor.nil? ? "" : "#{@anchor.to_adoc}\n" title = Coradoc::Generator.gen_adoc(@title) + attrs = @attrs.to_s.empty? ? "" : "#{@attrs.to_adoc}\n" content = Coradoc::Generator.gen_adoc(@contents) sections = Coradoc::Generator.gen_adoc(@sections) @@ -40,7 +42,7 @@ def to_adoc content = Coradoc.strip_unicode(content) end - "\n#{anchor}" << title << content << sections << "\n" + "\n#{anchor}" << attrs << title << content << sections << "\n" end # Check for cases when Section is simply an equivalent of an empty
diff --git a/lib/coradoc/parser/asciidoc/list.rb b/lib/coradoc/parser/asciidoc/list.rb index eeb2430..95498a1 100644 --- a/lib/coradoc/parser/asciidoc/list.rb +++ b/lib/coradoc/parser/asciidoc/list.rb @@ -49,6 +49,7 @@ def ulist_item(nesting_level = 1) 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 end diff --git a/lib/coradoc/parser/asciidoc/section.rb b/lib/coradoc/parser/asciidoc/section.rb index 84180c3..be1f02b 100644 --- a/lib/coradoc/parser/asciidoc/section.rb +++ b/lib/coradoc/parser/asciidoc/section.rb @@ -25,6 +25,7 @@ def citation end def section_block(level = 2) + return nil if level > 8 section_id.maybe >> (attribute_list >> newline).maybe >> section_title(level).as(:title) >> @@ -45,33 +46,19 @@ def section_title(level = 2, max_level = 8) end # section - def section - section_block >> second_level_section.repeat(0).as(:sections) - end - - def sub_section(level) - newline.maybe >> section_block(level) - end - - def second_level_section - sub_section(3) >> third_level_section.repeat(0).as(:sections) - end + def section(level = 2) + r = section_block(level) + if level < 8 + r = r >> section(level + 1).as(:section).repeat(0).as(:sections) + end + if level == 2 + (r).as(:section) + else + r + end - def third_level_section - sub_section(4) >> fourth_level_section.repeat(0).as(:sections) end - def fourth_level_section - sub_section(5) >> fifth_level_section.repeat(0).as(:sections) - end - - def fifth_level_section - sub_section(6) >> sixth_level_section.repeat(0).as(:sections) - end - - def sixth_level_section - sub_section(7) >> sub_section(8).repeat(0).as(:sections) - end end end end diff --git a/lib/coradoc/transformer.rb b/lib/coradoc/transformer.rb index 7be8f33..281e6cc 100644 --- a/lib/coradoc/transformer.rb +++ b/lib/coradoc/transformer.rb @@ -215,73 +215,16 @@ class NamedAttribute < Struct.new(:key, :value); end # # rule(id: simple(:id), title: simple(:title), content:) - rule( - title: simple(:title), - sections: sequence(:sections), - ) do - Element::Section.new(title, sections: sections) - end - - rule( - id: simple(:id), - title: simple(:title), - sections: sequence(:sections), - ) do - Element::Section.new(title, id: id, sections: sections) - end - - rule( - id: simple(:id), - title: simple(:title), - contents: sequence(:contents), - ) do - Element::Section.new(title, id: id, contents: contents) - end - - - rule( - title: simple(:title), - contents: sequence(:contents), - sections: sequence(:sections), - ) do - Element::Section.new( - title, - contents: contents, - sections: sections) + rule(section: subtree(:section)) do + id = section[:id] || nil + title = section[:title] || nil + attribute_list = section[:attribute_list] || nil + contents = section[:contents] || [] + sections = section[:sections] + opts = {id:,attribute_list:,contents:,sections: } + Element::Section.new(title, opts) end - rule( - id: simple(:id), - title: simple(:title), - contents: sequence(:contents), - sections: simple(:sections), - ) do - Element::Section.new(title, id: id, contents: contents, - sections: sections) - end - - rule( - id: simple(:id), - title: simple(:title), - contents: sequence(:contents), - sections: sequence(:sections), - ) do - Element::Section.new(title, id: id, contents: contents, - sections: sections) - end - - rule( - title: simple(:title), - contents: sequence(:contents), - sections: simple(:sections), - ) do - Element::Section.new(title, contents: contents, - sections: sections) - end - - - - rule(example: sequence(:example)) do Element::Core.new("", type: "example", lines: example) diff --git a/spec/coradoc/parser/asciidoc/section_spec.rb b/spec/coradoc/parser/asciidoc/section_spec.rb index 5b9c913..c728cb0 100644 --- a/spec/coradoc/parser/asciidoc/section_spec.rb +++ b/spec/coradoc/parser/asciidoc/section_spec.rb @@ -9,10 +9,10 @@ TEXT ast = Asciidoc::SectionTester.parse(section) - paragraph = ast.first[:contents][0][:paragraph] + paragraph = ast.first[:section][:contents][0][:paragraph] - expect(ast.first[:title][:level]).to eq("==") - expect(ast.first[:title][:text]).to eq("Section title") + expect(ast.first[:section][:title][:level]).to eq("==") + expect(ast.first[:section][:title][:text]).to eq("Section title") expect(paragraph[:lines][0][:text]).to eq("Section content") end @@ -24,11 +24,11 @@ TEXT ast = Asciidoc::SectionTester.parse(section) - paragraph = ast.first[:contents][0][:paragraph] + paragraph = ast.first[:section][:contents][0][:paragraph] - expect(ast.first[:id]).to eq("section_id") - expect(ast.first[:title][:level]).to eq("==") - expect(ast.first[:title][:text]).to eq("Section title") + expect(ast.first[:section][:id]).to eq("section_id") + expect(ast.first[:section][:title][:level]).to eq("==") + expect(ast.first[:section][:title][:text]).to eq("Section title") expect(paragraph[:lines][0][:text]).to eq("Section content") end @@ -45,10 +45,10 @@ ast = Asciidoc::SectionTester.parse(section) - expect(ast.first[:id]).to eq("section_id") - expect(ast.first[:title][:level]).to eq("==") - expect(ast.first[:title][:text]).to eq("Section title") - expect(ast.first[:sections].first[:id]).to eq("section_id_5.1") + expect(ast.first[:section][:id]).to eq("section_id") + expect(ast.first[:section][:title][:level]).to eq("==") + expect(ast.first[:section][:title][:text]).to eq("Section title") + expect(ast.first[:section][:sections].first[:section][:id]).to eq("section_id_5.1") end it "it parses nested sub sections" do @@ -74,33 +74,33 @@ ast = Asciidoc::SectionTester.parse(section) - expect(ast[0][:title][:level]).to eq("==") - expect(ast[1][:title][:level]).to eq("==") + expect(ast[0][:section][:title][:level]).to eq("==") + expect(ast[1][:section][:title][:level]).to eq("==") - expect(ast[0][:title][:text]).to eq("Section title") - expect(ast[1][:title][:text]).to eq("Another section title") + expect(ast[0][:section][:title][:text]).to eq("Section title") + expect(ast[1][:section][:title][:text]).to eq("Another section title") - level_two = ast[0][:sections].first + level_two = ast[0][:section][:sections].first[:section] expect(level_two[:title][:level]).to eq("===") expect(level_two[:title][:text]).to eq("Level 2 clause heading") - level_three = level_two[:sections].first + level_three = level_two[:sections].first[:section] expect(level_three[:title][:level]).to eq("====") expect(level_three[:title][:text]).to eq("Level 3 clause heading") - level_four = level_three[:sections].first + level_four = level_three[:sections].first[:section] expect(level_four[:title][:level]).to eq("=====") expect(level_four[:title][:text]).to eq("Level 4 clause heading") - level_five = level_four[:sections].first + level_five = level_four[:sections].first[:section] expect(level_five[:title][:level]).to eq("======") expect(level_five[:title][:text]).to eq("Level 5 clause heading") - level_six = level_five[:sections].first + level_six = level_five[:sections].first[:section] expect(level_six[:title][:level]).to eq("=======") expect(level_six[:title][:text]).to eq("Level 6 clause heading") - level_seven = level_six[:sections].first + level_seven = level_six[:sections].first[:section] expect(level_seven[:title][:level]).to eq("========") expect(level_seven[:title][:text]).to eq("Level 7 clause heading") end @@ -118,18 +118,19 @@ TEXT ast = Asciidoc::SectionTester.parse(section) - contents = ast.first[:contents] - expect(ast.first[:id]).to eq("section_id") - expect(ast.first[:title][:level]).to eq("==") - expect(ast.first[:title][:text]).to eq("Section title") + contents = ast.first[:section][:contents] + + expect(ast.first[:section][:id]).to eq("section_id") + expect(ast.first[:section][:title][:level]).to eq("==") + expect(ast.first[:section][:title][:text]).to eq("Section title") expect(contents[0][:paragraph][:lines][0][:text]).to eq("Section content") expect(contents[1][:paragraph][:lines][0][:id]).to eq("inline_id") expect(contents[1][:paragraph][:lines][0][:text]).to eq("This is inline id") - sub_sections = ast.first[:sections] - expect(sub_sections[0][:id]).to eq("section_id_two") - expect(sub_sections[0][:title][:text]).to eq("This is another section id") + sub_sections = ast.first[:section][:sections] + expect(sub_sections[0][:section][:id]).to eq("section_id_two") + expect(sub_sections[0][:section][:title][:text]).to eq("This is another section id") end it "it parses section with inline id" do @@ -142,7 +143,7 @@ TEXT ast = Asciidoc::SectionTester.parse(section) - section = ast[0] + section = ast[0][:section] list_items = section[:contents].first[:list][:unordered] expect(section[:id]).to eq("section_id") @@ -176,7 +177,7 @@ TEXT ast = Asciidoc::SectionTester.parse(section) - section = ast.first + section = ast.first[:section] contents = section[:contents] expect(contents.count).to eq(3) diff --git a/spec/coradoc/parser_spec.rb b/spec/coradoc/parser_spec.rb index 9b271cf..045ffcd 100644 --- a/spec/coradoc/parser_spec.rb +++ b/spec/coradoc/parser_spec.rb @@ -16,8 +16,8 @@ expect(ast[1][:document_attributes][0][:key]).to eq("published") expect(ast[1][:document_attributes][0][:value]).to eq("'2023-03-08T09:51:08+08:00'") - section = ast[3][:section] - clause_5_1 = section[:sections][0] + section = ast[3][:section][:section] #TODO fix it + clause_5_1 = section[:sections][0][:section] content = clause_5_1[:contents].first expect(section[:title][:text]).to eq("Organizational controls") @@ -26,18 +26,18 @@ expect(content[:glossaries][6][:key]).to eq("Domain") expect(content[:glossaries][6][:value]).to eq("Governance_and_Ecosystem, Resilience") - control_section = clause_5_1[:sections][0] + control_section = clause_5_1[:sections][0][:section] expect(control_section[:id]).to eq("control_5.1") expect(control_section[:title][:text]).to eq("Control") expect(control_section[:contents].count).to eq(1) expect(control_section[:contents][0][:paragraph]).not_to be_nil - purpose_section = clause_5_1[:sections][1] + purpose_section = clause_5_1[:sections][1][:section] expect(purpose_section[:id]).to eq("purpose_5.1") expect(purpose_section[:title][:text]).to eq("Purpose") expect(purpose_section[:contents][0][:paragraph]).not_to be_nil - guidance = clause_5_1[:sections][2] + guidance = clause_5_1[:sections][2][:section] expect(guidance[:contents].count).to eq(16) expect(guidance[:contents][0][:paragraph][:lines][0][:id]).to eq("guidance_5.1_part_1") expect(guidance[:contents][1][:paragraph][:lines][0][:id]).to eq("guidance_5.1_part_2") @@ -58,7 +58,7 @@ expect(diff_table[:rows][0][:cols][0][:text]).to eq(" ") expect(diff_table[:rows][1][:cols][0][:text]).to eq("*Level of detail*") - purpose_5_9 = section[:sections][8][:sections][1] + purpose_5_9 = section[:sections][8][:section][:sections][1][:section] highlight_5_9 = purpose_5_9[:contents][1][:highlight] expect(highlight_5_9[:id]).to eq("scls_5-9") expect(highlight_5_9[:text]).to eq("Inventory") From f67ff4659560b71ef95d0a3e1c3fb15c8796032c Mon Sep 17 00:00:00 2001 From: xyz65535 Date: Tue, 6 Aug 2024 06:29:56 +0200 Subject: [PATCH 09/13] fixes: roundtrip rice-2023 --- lib/coradoc/element/admonition.rb | 2 +- lib/coradoc/element/attribute_list.rb | 2 +- lib/coradoc/element/bibliography_entry.rb | 3 ++- lib/coradoc/element/inline.rb | 1 + lib/coradoc/element/inline/citation.rb | 23 +++++++++++++++++++ lib/coradoc/parser/asciidoc/admonition.rb | 5 +++- lib/coradoc/parser/asciidoc/attribute_list.rb | 2 +- lib/coradoc/parser/asciidoc/bibliography.rb | 5 ++-- lib/coradoc/parser/asciidoc/content.rb | 1 + lib/coradoc/parser/asciidoc/inline.rb | 9 ++++++++ lib/coradoc/parser/asciidoc/paragraph.rb | 2 ++ lib/coradoc/parser/asciidoc/section.rb | 8 +++---- lib/coradoc/parser/base.rb | 7 ++++-- lib/coradoc/transformer.rb | 12 +++++++++- spec/coradoc/parser_spec.rb | 2 +- utils/round_trip.rb | 4 +++- 16 files changed, 71 insertions(+), 17 deletions(-) create mode 100644 lib/coradoc/element/inline/citation.rb diff --git a/lib/coradoc/element/admonition.rb b/lib/coradoc/element/admonition.rb index ce06800..b2f25e3 100644 --- a/lib/coradoc/element/admonition.rb +++ b/lib/coradoc/element/admonition.rb @@ -11,7 +11,7 @@ def initialize(content, type, options = {}) def to_s content = Coradoc::Generator.gen_adoc(@content) - "#{type.to_s.upcase}: #{content}" + "#{type.to_s.upcase}: #{content}#{@line_break}" end end diff --git a/lib/coradoc/element/attribute_list.rb b/lib/coradoc/element/attribute_list.rb index 7c31162..e3646cb 100644 --- a/lib/coradoc/element/attribute_list.rb +++ b/lib/coradoc/element/attribute_list.rb @@ -71,7 +71,7 @@ def to_adoc(show_empty = true) adoc << @named.map do |k, v| if v.is_a?(String) v = v.gsub("\"", "\\\"") - if v.include?(" ") || v.include?(",") || v.include?('"') + if v.include?(",") || v.include?('"') v = "\"#{v}\"" end elsif v.is_a?(Array) diff --git a/lib/coradoc/element/bibliography_entry.rb b/lib/coradoc/element/bibliography_entry.rb index 2b13777..ebe6215 100644 --- a/lib/coradoc/element/bibliography_entry.rb +++ b/lib/coradoc/element/bibliography_entry.rb @@ -13,7 +13,8 @@ def initialize(options = {}) def to_adoc adoc = "* [[[#{@anchor_name}" adoc << ",#{@document_id}" if @document_id - adoc << "]]], #{@reference_text}" + adoc << "]]]" + adoc << "#{@reference_text}" if @reference_text adoc << @line_break adoc end diff --git a/lib/coradoc/element/inline.rb b/lib/coradoc/element/inline.rb index 8a9149b..4c038c3 100644 --- a/lib/coradoc/element/inline.rb +++ b/lib/coradoc/element/inline.rb @@ -8,6 +8,7 @@ module Inline require_relative "inline/anchor" require_relative "inline/bold" require_relative "inline/cross_reference" +# require_relative "inline/citation" require_relative "inline/hard_line_break" require_relative "inline/highlight" require_relative "inline/italic" diff --git a/lib/coradoc/element/inline/citation.rb b/lib/coradoc/element/inline/citation.rb new file mode 100644 index 0000000..80266cb --- /dev/null +++ b/lib/coradoc/element/inline/citation.rb @@ -0,0 +1,23 @@ +module Coradoc + module Element + module Inline + class Citation < Base + attr_accessor :cross_reference, :comment + + declare_children :cross_reference, :comment + + def initialize(cross_reference, comment = nil) + @cross_reference = cross_reference + @comment = comment + end + + def to_adoc + adoc = "[.source]\n" + adoc << @cross_reference.to_adoc + adoc << Coradoc::Generator.gen_adoc(@comment) if @comment + adoc + end + end + end + end +end diff --git a/lib/coradoc/parser/asciidoc/admonition.rb b/lib/coradoc/parser/asciidoc/admonition.rb index ea3bbcf..808522e 100644 --- a/lib/coradoc/parser/asciidoc/admonition.rb +++ b/lib/coradoc/parser/asciidoc/admonition.rb @@ -19,7 +19,10 @@ def admonition_line # # ) # ) ( - text_line(1).repeat(1) + # text_line(1).repeat(1) + (text.as(:text) >> + line_ending.as(:line_break) + ).repeat(1) ).as(:content) end end diff --git a/lib/coradoc/parser/asciidoc/attribute_list.rb b/lib/coradoc/parser/asciidoc/attribute_list.rb index 6aad044..bf940c4 100644 --- a/lib/coradoc/parser/asciidoc/attribute_list.rb +++ b/lib/coradoc/parser/asciidoc/attribute_list.rb @@ -14,7 +14,7 @@ def named_attribute_value def named_attribute (match('[a-zA-Z0-9_-]').repeat(1).as(:named_key) >> str(' ').maybe >> str("=") >> str(' ').maybe >> - match['a-zA-Z0-9_-'].repeat(1).as(:named_value) >> + match['a-zA-Z0-9_\- '].repeat(1).as(:named_value) >> str(' ').maybe ).as(:named) end diff --git a/lib/coradoc/parser/asciidoc/bibliography.rb b/lib/coradoc/parser/asciidoc/bibliography.rb index 32d4264..b23641d 100644 --- a/lib/coradoc/parser/asciidoc/bibliography.rb +++ b/lib/coradoc/parser/asciidoc/bibliography.rb @@ -12,14 +12,15 @@ def bibliography end def bib_entry - (str('* [[[') >> match('[^,\]\n]').repeat(1).as(:anchor_name) >> + (match("^*") >> str(' [[[') >> + match('[^,\[\]\n]').repeat(1).as(:anchor_name) >> ( str(",") >> match('[^\]\n]').repeat(1).as(:document_id) ).maybe >> str("]]]") >> (text_line.repeat(0,1) >> text_line.repeat(0) - ).as(:reference_text) >> + ).as(:reference_text).maybe >> line_ending.repeat(1).as(:line_break).maybe ).as(:bibliography_entry) end diff --git a/lib/coradoc/parser/asciidoc/content.rb b/lib/coradoc/parser/asciidoc/content.rb index 12cac08..f261287 100644 --- a/lib/coradoc/parser/asciidoc/content.rb +++ b/lib/coradoc/parser/asciidoc/content.rb @@ -30,6 +30,7 @@ def text_line( n_line_breaks = 1) # (comment_block.absent? >> # comment_line.absent? >> # include_directive.absent?) >> + # attribute_list.absent? >> (asciidoc_char_with_id.absent? | text_id) >> literal_space? >> text.as(:text) >> line_ending.repeat(n_line_breaks).as(:line_break) diff --git a/lib/coradoc/parser/asciidoc/inline.rb b/lib/coradoc/parser/asciidoc/inline.rb index 0d417b7..40d3a22 100644 --- a/lib/coradoc/parser/asciidoc/inline.rb +++ b/lib/coradoc/parser/asciidoc/inline.rb @@ -9,6 +9,13 @@ def cross_reference str('>>') end + def citation + (str("[.source]\n") >> + cross_reference.as(:cross_reference) >> + (match['[^\n]'].repeat(1)).as(:comment).maybe >> + newline).as(:citation) + end + def bold_constrained (str('*') >> match("[^*]").repeat(1).as(:text).repeat(1,1) >> @@ -65,7 +72,9 @@ def text_unformatted end def text_formatted + attribute_list.absent? >> (asciidoc_char_with_id.absent?| text_id) >> + (attribute_list >> newline).absent? >> # literal_space? >> ((cross_reference | bold_unconstrained | bold_constrained | diff --git a/lib/coradoc/parser/asciidoc/paragraph.rb b/lib/coradoc/parser/asciidoc/paragraph.rb index ac4252b..e095bd2 100644 --- a/lib/coradoc/parser/asciidoc/paragraph.rb +++ b/lib/coradoc/parser/asciidoc/paragraph.rb @@ -9,8 +9,10 @@ def paragraph_text_line # comment_line.absent? >> # list.absent? >> # admonition_line.absent? >> + # (match("^") >> attribute_list >> newline).absent? >> # block.absent? >> + # match('^\[').absent? >> (asciidoc_char_with_id.absent? | text_id ) >> literal_space? >> (text_formatted.as(:text) # >> diff --git a/lib/coradoc/parser/asciidoc/section.rb b/lib/coradoc/parser/asciidoc/section.rb index be1f02b..50f5941 100644 --- a/lib/coradoc/parser/asciidoc/section.rb +++ b/lib/coradoc/parser/asciidoc/section.rb @@ -6,6 +6,7 @@ module Section def contents ( citation | + bib_entry | comment_block | comment_line | include_directive | @@ -20,14 +21,10 @@ def contents ).repeat(1) end - def citation - (str("[.source]\n") >> cross_reference.as(:reference) ).as(:citation) - end - def section_block(level = 2) return nil if level > 8 + (attribute_list >> newline).maybe >> section_id.maybe >> - (attribute_list >> newline).maybe >> section_title(level).as(:title) >> contents.as(:contents).maybe end @@ -56,6 +53,7 @@ def section(level = 2) else r end + # r end diff --git a/lib/coradoc/parser/base.rb b/lib/coradoc/parser/base.rb index d6b92c4..6193585 100644 --- a/lib/coradoc/parser/base.rb +++ b/lib/coradoc/parser/base.rb @@ -32,7 +32,10 @@ class Base < Parslet::Parser root :document rule(:document) do ( - bibliography | + # bibliography | + admonition_line | + bib_entry | + citation | # attribute_list.as(:attribute_list) | comment_block | comment_line | @@ -40,8 +43,8 @@ class Base < Parslet::Parser include_directive | document_attributes | section.as(:section) | - paragraph | list | + paragraph | header.as(:header) | empty_line.as(:line_break) | any.as(:unparsed) diff --git a/lib/coradoc/transformer.rb b/lib/coradoc/transformer.rb index 281e6cc..fd11f0c 100644 --- a/lib/coradoc/transformer.rb +++ b/lib/coradoc/transformer.rb @@ -256,6 +256,16 @@ class NamedAttribute < Struct.new(:key, :value); end ) } + rule(citation: {cross_reference: simple(:cross_reference), + comment: simple(:comment)}){ + Element::Inline::Citation.new(cross_reference, comment) + } + + rule(citation: {cross_reference: simple(:cross_reference)}){ + Element::Inline::Citation.new(cross_reference) + } + + rule(block: subtree(:block) # { # title: simple(:title), @@ -294,7 +304,7 @@ class NamedAttribute < Struct.new(:key, :value); end # # Admonition # rule(admonition_type: simple(:admonition)) { admonition } rule(admonition_type: simple(:admonition_type), - content: simple(:content), + content: sequence(:content), # line_break: simple(:line_break) ) do Element::Admonition.new(content, admonition_type.to_s) diff --git a/spec/coradoc/parser_spec.rb b/spec/coradoc/parser_spec.rb index 045ffcd..3dae1f7 100644 --- a/spec/coradoc/parser_spec.rb +++ b/spec/coradoc/parser_spec.rb @@ -16,7 +16,7 @@ expect(ast[1][:document_attributes][0][:key]).to eq("published") expect(ast[1][:document_attributes][0][:value]).to eq("'2023-03-08T09:51:08+08:00'") - section = ast[3][:section][:section] #TODO fix it + section = ast[3][:section][:section] clause_5_1 = section[:sections][0][:section] content = clause_5_1[:contents].first diff --git a/utils/round_trip.rb b/utils/round_trip.rb index 943260f..0b3b1dc 100644 --- a/utils/round_trip.rb +++ b/utils/round_trip.rb @@ -1,6 +1,7 @@ $LOAD_PATH.unshift("../coradoc/lib"); require "coradoc" +require "coradoc/reverse_adoc" rice_path = "../mn-samples-iso/sources/international-standard/rice-2023/" @@ -29,7 +30,8 @@ puts "generating..." generated_adoc = Coradoc::Generator.gen_adoc(doc) - File.open("#{file_path}.roundtrip","w"){|f| f.write(generated_adoc)} + cleaned_adoc = Coradoc::ReverseAdoc.cleaner.tidy(generated_adoc) + File.open("#{file_path}.roundtrip","w"){|f| f.write(cleaned_adoc)} `diff #{file_path} #{file_path}.roundtrip > #{file_path}.roundtrip.diff` rescue puts "unsuccessful..." From 387fd04793f854f94de1dfb34e9af0ae2980e871 Mon Sep 17 00:00:00 2001 From: xyz65535 Date: Tue, 6 Aug 2024 13:21:14 +0200 Subject: [PATCH 10/13] fixes: roundtrip rice-2023 --- lib/coradoc/document.rb | 1 + lib/coradoc/element/admonition.rb | 3 +-- lib/coradoc/element/bibliography_entry.rb | 9 ++++---- lib/coradoc/element/block/core.rb | 1 + lib/coradoc/element/inline.rb | 2 +- lib/coradoc/element/term.rb | 21 +++++++++++++++++++ lib/coradoc/parser/asciidoc/base.rb | 4 +++- lib/coradoc/parser/asciidoc/bibliography.rb | 2 +- lib/coradoc/parser/asciidoc/block.rb | 15 +++++++++++++- lib/coradoc/parser/asciidoc/inline.rb | 6 +++--- lib/coradoc/parser/asciidoc/paragraph.rb | 3 ++- lib/coradoc/parser/asciidoc/section.rb | 2 ++ lib/coradoc/parser/asciidoc/term.rb | 23 +++++++++++++++++++++ lib/coradoc/parser/base.rb | 4 ++++ lib/coradoc/reverse_adoc/cleaner.rb | 10 +++++++++ lib/coradoc/transformer.rb | 17 ++++++++++++++- utils/round_trip.rb | 9 ++++---- 17 files changed, 113 insertions(+), 19 deletions(-) create mode 100644 lib/coradoc/element/term.rb create mode 100644 lib/coradoc/parser/asciidoc/term.rb diff --git a/lib/coradoc/document.rb b/lib/coradoc/document.rb index 4efc400..cabf4f8 100644 --- a/lib/coradoc/document.rb +++ b/lib/coradoc/document.rb @@ -23,6 +23,7 @@ require_relative "element/audio" require_relative "element/video" require_relative "element/break" +require_relative "element/term" module Coradoc class Document diff --git a/lib/coradoc/element/admonition.rb b/lib/coradoc/element/admonition.rb index b2f25e3..256a489 100644 --- a/lib/coradoc/element/admonition.rb +++ b/lib/coradoc/element/admonition.rb @@ -9,11 +9,10 @@ def initialize(content, type, options = {}) @line_break = options.fetch(:line_break, "") end - def to_s + def to_adoc content = Coradoc::Generator.gen_adoc(@content) "#{type.to_s.upcase}: #{content}#{@line_break}" end - end end end diff --git a/lib/coradoc/element/bibliography_entry.rb b/lib/coradoc/element/bibliography_entry.rb index ebe6215..dbfd84d 100644 --- a/lib/coradoc/element/bibliography_entry.rb +++ b/lib/coradoc/element/bibliography_entry.rb @@ -1,20 +1,21 @@ module Coradoc module Element class BibliographyEntry < Base - attr_accessor :anchor_name, :document_id, :reference_text, :line_break + attr_accessor :anchor_name, :document_id, :ref_text, :line_break def initialize(options = {}) @anchor_name = options.fetch(:anchor_name, nil) @document_id = options.fetch(:document_id, nil) - @reference_text = options.fetch(:reference_text, nil) - @line_break = options.fetch(:line_break, nil) + @ref_text = options.fetch(:ref_text, nil) + @line_break = options.fetch(:line_break, "") end def to_adoc + text = Coradoc::Generator.gen_adoc(@ref_text) if @ref_text adoc = "* [[[#{@anchor_name}" adoc << ",#{@document_id}" if @document_id adoc << "]]]" - adoc << "#{@reference_text}" if @reference_text + adoc << "#{text}" if @ref_text adoc << @line_break adoc end diff --git a/lib/coradoc/element/block/core.rb b/lib/coradoc/element/block/core.rb index b72cb93..aa64740 100644 --- a/lib/coradoc/element/block/core.rb +++ b/lib/coradoc/element/block/core.rb @@ -65,6 +65,7 @@ def type_hash "----" => :source, "====" => :example, "...." => :literal, + "++++" => :pass, } end end diff --git a/lib/coradoc/element/inline.rb b/lib/coradoc/element/inline.rb index 4c038c3..c99ce75 100644 --- a/lib/coradoc/element/inline.rb +++ b/lib/coradoc/element/inline.rb @@ -8,7 +8,7 @@ module Inline require_relative "inline/anchor" require_relative "inline/bold" require_relative "inline/cross_reference" -# require_relative "inline/citation" +require_relative "inline/citation" require_relative "inline/hard_line_break" require_relative "inline/highlight" require_relative "inline/italic" diff --git a/lib/coradoc/element/term.rb b/lib/coradoc/element/term.rb new file mode 100644 index 0000000..ba92f17 --- /dev/null +++ b/lib/coradoc/element/term.rb @@ -0,0 +1,21 @@ +module Coradoc + module Element + class Term < Base + attr_accessor :term, :options + + declare_children :term, :options + + def initialize(term, options = {}) + @term = term + @type = options.fetch(:type, nil) + @lang = options.fetch(:lang, :en) + @line_break = options.fetch(:line_break, "") + end + + def to_adoc + return "#{@type.to_s}:[#{@term}]#{@line_break}" if @lang == :en + return "[#{@type.to_s}]##{@term}]##{@line_break}" if @lang == :fr + end + end + end +end diff --git a/lib/coradoc/parser/asciidoc/base.rb b/lib/coradoc/parser/asciidoc/base.rb index c88c076..c938c33 100644 --- a/lib/coradoc/parser/asciidoc/base.rb +++ b/lib/coradoc/parser/asciidoc/base.rb @@ -11,6 +11,7 @@ require_relative "section" require_relative "table" # require_relative "include" +require_relative "term" module Coradoc module Parser @@ -28,6 +29,7 @@ module Base include Coradoc::Parser::Asciidoc::Paragraph include Coradoc::Parser::Asciidoc::Section include Coradoc::Parser::Asciidoc::Table + include Coradoc::Parser::Asciidoc::Term def space? space.maybe @@ -63,7 +65,7 @@ def newline_single end def keyword - (match("[a-zA-Z0-9_-]") | str(".")).repeat(1) + (match('[a-zA-Z0-9_\-.,]') | str(".")).repeat(1) end def empty_line diff --git a/lib/coradoc/parser/asciidoc/bibliography.rb b/lib/coradoc/parser/asciidoc/bibliography.rb index b23641d..284ade7 100644 --- a/lib/coradoc/parser/asciidoc/bibliography.rb +++ b/lib/coradoc/parser/asciidoc/bibliography.rb @@ -20,7 +20,7 @@ def bib_entry str("]]]") >> (text_line.repeat(0,1) >> text_line.repeat(0) - ).as(:reference_text).maybe >> + ).as(:ref_text).maybe >> line_ending.repeat(1).as(:line_break).maybe ).as(:bibliography_entry) end diff --git a/lib/coradoc/parser/asciidoc/block.rb b/lib/coradoc/parser/asciidoc/block.rb index f3488bc..3fb40eb 100644 --- a/lib/coradoc/parser/asciidoc/block.rb +++ b/lib/coradoc/parser/asciidoc/block.rb @@ -7,13 +7,18 @@ def block sidebar_block | example_block | source_block | - quote_block + quote_block | + pass_block end def source_block block_style("-", 2) end + def pass_block + block_style("+") + end + def source_block block_style("-", 2) end @@ -48,10 +53,18 @@ def block_type(type) ) >> newline end + def block_id + (str("[[") >> keyword.as(:id) >> str("]]") | + str("[#") >> keyword.as(:id) >> str("]")) >> newline + end + + def block_style(delimiter = "*", repeater = 4, type = "") block_title.maybe >> newline.maybe >> (attribute_list >> newline ).maybe >> + block_id.maybe >> + (attribute_list >> newline ).maybe >> str(delimiter).repeat(repeater).as(:delimiter) >> newline >> block_content.as(:lines) >> str(delimiter).repeat(repeater) >> newline diff --git a/lib/coradoc/parser/asciidoc/inline.rb b/lib/coradoc/parser/asciidoc/inline.rb index 40d3a22..6396b92 100644 --- a/lib/coradoc/parser/asciidoc/inline.rb +++ b/lib/coradoc/parser/asciidoc/inline.rb @@ -72,9 +72,9 @@ def text_unformatted end def text_formatted - attribute_list.absent? >> - (asciidoc_char_with_id.absent?| text_id) >> - (attribute_list >> newline).absent? >> + # attribute_list.absent? >> + # (asciidoc_char_with_id.absent?| text_id) >> + # (attribute_list >> newline).absent? >> # literal_space? >> ((cross_reference | bold_unconstrained | bold_constrained | diff --git a/lib/coradoc/parser/asciidoc/paragraph.rb b/lib/coradoc/parser/asciidoc/paragraph.rb index e095bd2..67a732a 100644 --- a/lib/coradoc/parser/asciidoc/paragraph.rb +++ b/lib/coradoc/parser/asciidoc/paragraph.rb @@ -17,7 +17,8 @@ def paragraph_text_line literal_space? >> (text_formatted.as(:text) # >> # newline_single.as(:break).maybe - )#.as(:paragraph_text_line) + ) | term | term2 + #.as(:paragraph_text_line) end def paragraph diff --git a/lib/coradoc/parser/asciidoc/section.rb b/lib/coradoc/parser/asciidoc/section.rb index 50f5941..e35f680 100644 --- a/lib/coradoc/parser/asciidoc/section.rb +++ b/lib/coradoc/parser/asciidoc/section.rb @@ -6,6 +6,7 @@ module Section def contents ( citation | + term | term2 | bib_entry | comment_block | comment_line | @@ -25,6 +26,7 @@ def section_block(level = 2) return nil if level > 8 (attribute_list >> newline).maybe >> section_id.maybe >> + (attribute_list >> newline).maybe >> section_title(level).as(:title) >> contents.as(:contents).maybe end diff --git a/lib/coradoc/parser/asciidoc/term.rb b/lib/coradoc/parser/asciidoc/term.rb new file mode 100644 index 0000000..fbad156 --- /dev/null +++ b/lib/coradoc/parser/asciidoc/term.rb @@ -0,0 +1,23 @@ +module Coradoc + module Parser + module Asciidoc + module Term + def term_type + (str("alt") | str("deprecated") | str("domain")).as(:term_type) + end + + def term + term_type >> str(':[') >> + match('[^\]]').repeat(1).as(:term) >> + str("]") >> str("\n").repeat(1).as(:line_break) + end + + def term2 + match('^\[') >> term_type >> str(']#') >> + match('[^\#]').repeat(1).as(:term2) >> str('#') >> + str("]") >> str("\n").repeat(1).as(:line_break) + end + end + end + end +end diff --git a/lib/coradoc/parser/base.rb b/lib/coradoc/parser/base.rb index 6193585..60de1c7 100644 --- a/lib/coradoc/parser/base.rb +++ b/lib/coradoc/parser/base.rb @@ -13,6 +13,7 @@ require_relative "asciidoc/paragraph" require_relative "asciidoc/section" require_relative "asciidoc/table" +require_relative "asciidoc/term" module Coradoc module Parser @@ -28,6 +29,7 @@ class Base < Parslet::Parser include Coradoc::Parser::Asciidoc::Paragraph include Coradoc::Parser::Asciidoc::Section include Coradoc::Parser::Asciidoc::Table + include Coradoc::Parser::Asciidoc::Term root :document rule(:document) do @@ -35,6 +37,7 @@ class Base < Parslet::Parser # bibliography | admonition_line | bib_entry | + term | term2 | citation | # attribute_list.as(:attribute_list) | comment_block | @@ -44,6 +47,7 @@ class Base < Parslet::Parser document_attributes | section.as(:section) | list | + paragraph | header.as(:header) | empty_line.as(:line_break) | diff --git a/lib/coradoc/reverse_adoc/cleaner.rb b/lib/coradoc/reverse_adoc/cleaner.rb index 96a731b..92455dc 100644 --- a/lib/coradoc/reverse_adoc/cleaner.rb +++ b/lib/coradoc/reverse_adoc/cleaner.rb @@ -16,6 +16,16 @@ def tidy(string) result = HtmlConverter.track_time "Cleaning punctuation characters" do clean_punctuation_characters(result) end + result = remove_block_leading_newlines(result) + result = remove_section_attribute_newlines(result) + end + + def remove_block_leading_newlines(string) + string.gsub("]\n****\n\n","]\n****\n") + end + + def remove_section_attribute_newlines(string) + string.gsub("]\n\n==","]\n==") end def remove_newlines(string) diff --git a/lib/coradoc/transformer.rb b/lib/coradoc/transformer.rb index fd11f0c..83e8fb7 100644 --- a/lib/coradoc/transformer.rb +++ b/lib/coradoc/transformer.rb @@ -266,6 +266,19 @@ class NamedAttribute < Struct.new(:key, :value); end } + rule(term_type: simple(:term_type), + term: simple(:term), + line_break: simple(:line_break)){ + Coradoc::Element::Term.new(term, type: term_type, line_break: line_break, lang: :en) + } + + rule(term_type: simple(:term_type), + term2: simple(:term2), + line_break: simple(:line_break)){ + Coradoc::Element::Term.new(term2, type: term_type, line_break: line_break, lang: :fr) + } + + rule(block: subtree(:block) # { # title: simple(:title), @@ -274,12 +287,14 @@ class NamedAttribute < Struct.new(:key, :value); end # lines: sequence(:lines) # } ) { + id = block[:id] title = block[:title] attribute_list = block[:attribute_list] delimiter = block[:delimiter] lines = block[:lines] - opts = {title: title, + opts = {id: id, + title: title, attributes: attribute_list, delimiter_len: delimiter.size, lines: lines} diff --git a/utils/round_trip.rb b/utils/round_trip.rb index 0b3b1dc..d11ed41 100644 --- a/utils/round_trip.rb +++ b/utils/round_trip.rb @@ -19,8 +19,9 @@ file_path_diff = "#{file_path}.roundtrip.diff" FileUtils.rm(file_path_rt) if File.exist?(file_path_rt) FileUtils.rm(file_path_diff) if File.exist?(file_path_diff) - begin + # begin adoc_file = File.open(file_path).read; + next if adoc_file.size == 0 puts "parsing..." ast = Coradoc::Parser::Base.new.parse(adoc_file); puts "transforming..." @@ -33,7 +34,7 @@ cleaned_adoc = Coradoc::ReverseAdoc.cleaner.tidy(generated_adoc) File.open("#{file_path}.roundtrip","w"){|f| f.write(cleaned_adoc)} `diff #{file_path} #{file_path}.roundtrip > #{file_path}.roundtrip.diff` - rescue - puts "unsuccessful..." - end + # rescue + # puts "unsuccessful..." + # end end; From a1b818c0f3ac74b92a9629c886903788d9c079ee Mon Sep 17 00:00:00 2001 From: xyz65535 Date: Wed, 7 Aug 2024 13:22:47 +0200 Subject: [PATCH 11/13] removing development dependency "stackprof" --- coradoc.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/coradoc.gemspec b/coradoc.gemspec index 24748a8..ef77d61 100644 --- a/coradoc.gemspec +++ b/coradoc.gemspec @@ -44,6 +44,6 @@ Gem::Specification.new do |spec| spec.add_development_dependency "rubocop" spec.add_development_dependency "rubocop-performance" spec.add_development_dependency "simplecov" - spec.add_development_dependency "stackprof" + # spec.add_development_dependency "stackprof" # spec.add_runtime_dependency "thor" end From 45b3c6752a45300b6c714ad3d143e48323cdb639 Mon Sep 17 00:00:00 2001 From: xyz65535 Date: Thu, 8 Aug 2024 21:28:52 +0200 Subject: [PATCH 12/13] fixes: roundtrip rice-2023 --- lib/coradoc/element/block.rb | 1 + lib/coradoc/element/block/example.rb | 1 + lib/coradoc/element/block/pass.rb | 19 +++++++ lib/coradoc/element/block/quote.rb | 2 +- lib/coradoc/element/block/sourcecode.rb | 1 + lib/coradoc/element/image/block_image.rb | 13 +++++ lib/coradoc/element/image/core.rb | 13 +++-- lib/coradoc/element/list_item.rb | 2 +- lib/coradoc/element/paragraph.rb | 7 +-- lib/coradoc/element/table.rb | 2 +- lib/coradoc/element/term.rb | 2 +- lib/coradoc/parser/asciidoc/attribute_list.rb | 18 +++---- lib/coradoc/parser/asciidoc/base.rb | 13 ++++- lib/coradoc/parser/asciidoc/block.rb | 29 +++++++---- lib/coradoc/parser/asciidoc/content.rb | 2 +- lib/coradoc/parser/asciidoc/header.rb | 2 +- lib/coradoc/parser/asciidoc/paragraph.rb | 3 +- lib/coradoc/parser/asciidoc/section.rb | 1 + lib/coradoc/parser/asciidoc/table.rb | 2 + lib/coradoc/parser/asciidoc/term.rb | 2 +- lib/coradoc/parser/base.rb | 6 ++- lib/coradoc/transformer.rb | 50 +++++++++++++++---- 22 files changed, 142 insertions(+), 49 deletions(-) create mode 100644 lib/coradoc/element/block/pass.rb diff --git a/lib/coradoc/element/block.rb b/lib/coradoc/element/block.rb index 861a11e..4be04ec 100644 --- a/lib/coradoc/element/block.rb +++ b/lib/coradoc/element/block.rb @@ -9,6 +9,7 @@ module Block require_relative "block/example" require_relative "block/literal" require_relative "block/quote" +require_relative "block/pass" require_relative "block/side" require_relative "block/sourcecode" require_relative "block/reviewer_comment" \ No newline at end of file diff --git a/lib/coradoc/element/block/example.rb b/lib/coradoc/element/block/example.rb index 0d2e856..ffc3b19 100644 --- a/lib/coradoc/element/block/example.rb +++ b/lib/coradoc/element/block/example.rb @@ -8,6 +8,7 @@ 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) diff --git a/lib/coradoc/element/block/pass.rb b/lib/coradoc/element/block/pass.rb new file mode 100644 index 0000000..12c8502 --- /dev/null +++ b/lib/coradoc/element/block/pass.rb @@ -0,0 +1,19 @@ +module Coradoc + module Element + module Block + class Pass < Core + def initialize(options = {}) + @title = options.fetch(:title, "") + @attributes = options.fetch(:attributes, AttributeList.new) + @delimiter_char = "+" + @delimiter_len = options.fetch(:delimiter_len, 4) + @lines = options.fetch(:lines, []) + end + + def to_adoc + "\n\n#{gen_title}#{gen_attributes}#{gen_delimiter}\n" << gen_lines << "\n#{gen_delimiter}\n\n" + end + end + end + end +end diff --git a/lib/coradoc/element/block/quote.rb b/lib/coradoc/element/block/quote.rb index ef004bc..18f537f 100644 --- a/lib/coradoc/element/block/quote.rb +++ b/lib/coradoc/element/block/quote.rb @@ -11,7 +11,7 @@ def initialize(title, options = {}) end def to_adoc - "\n\n#{gen_attributes}#{gen_delimiter}\n" << gen_lines << "\n#{gen_delimiter}\n\n" + "\n\n#{gen_title}#{gen_attributes}#{gen_delimiter}\n" << gen_lines << "\n#{gen_delimiter}\n\n" end end end diff --git a/lib/coradoc/element/block/sourcecode.rb b/lib/coradoc/element/block/sourcecode.rb index 62d67f2..e59f8f4 100644 --- a/lib/coradoc/element/block/sourcecode.rb +++ b/lib/coradoc/element/block/sourcecode.rb @@ -6,6 +6,7 @@ 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) diff --git a/lib/coradoc/element/image/block_image.rb b/lib/coradoc/element/image/block_image.rb index 8045792..b09f6f2 100644 --- a/lib/coradoc/element/image/block_image.rb +++ b/lib/coradoc/element/image/block_image.rb @@ -7,6 +7,19 @@ def initialize(title, id, src, options = {}) @colons = "::" end + def to_adoc + missing = "// FIXME: Missing image: #{@annotate_missing}\n" if @annotate_missing + anchor = @anchor.nil? ? "" : "#{@anchor.to_adoc}\n" + title = ".#{@title}\n" unless @title.to_s.empty? + # attrs = @attributes.to_adoc + attrs = @attributes.to_adoc + [missing, anchor, title, "image", @colons, @src, attrs, @line_break].join("") + end + + # def to_adoc + + # end + def validate_named @attributes.validate_named(VALIDATORS_NAMED, VALIDATORS_NAMED_BLOCK) end diff --git a/lib/coradoc/element/image/core.rb b/lib/coradoc/element/image/core.rb index 8d7242a..690c5cf 100644 --- a/lib/coradoc/element/image/core.rb +++ b/lib/coradoc/element/image/core.rb @@ -9,23 +9,26 @@ class Core < Base def initialize(title, id, src, options = {}) @title = title @id = id - @anchor = @id.nil? ? nil : Coradoc::Element::Inline::Anchor.new(@id) + @anchor = @id.nil? ? nil : Inline::Anchor.new(@id) @src = src + # @attributes = options.fetch(:attributes, AttributeList.new) @attributes = options.fetch(:attributes, AttributeList.new) - @annotate_missing = options.fetch(:annotate_missing) - @title = options.fetch(:title, nil) + @annotate_missing = options.fetch(:annotate_missing, nil) + @title = options.fetch(:title, nil) unless @title if @attributes.any? @attributes.validate_positional(VALIDATORS_POSITIONAL) @attributes.validate_named(VALIDATORS_NAMED) end + @line_break = options.fetch(:line_break, "") end def to_adoc missing = "// FIXME: Missing image: #{@annotate_missing}\n" if @annotate_missing anchor = @anchor.nil? ? "" : "#{@anchor.to_adoc}\n" title = ".#{@title}\n" unless @title.to_s.empty? - attrs = @attributes.to_adoc - [missing, anchor, title, "image", @colons, @src, attrs].join("") + # attrs = @attributes.to_adoc + attrs = @attributes_macro.to_adoc + [missing, anchor, title, "image", @colons, @src, attrs, @line_break].join("") end extend AttributeList::Matchers diff --git a/lib/coradoc/element/list_item.rb b/lib/coradoc/element/list_item.rb index 9cf1bbf..cbf8b8c 100644 --- a/lib/coradoc/element/list_item.rb +++ b/lib/coradoc/element/list_item.rb @@ -16,7 +16,7 @@ def initialize(content, options = {}) def to_adoc anchor = @anchor.nil? ? "" : @anchor.to_adoc.to_s content = Array(@content).map do |subitem| - next if subitem.is_a? Coradoc::Element::Inline::HardLineBreak + next if subitem.is_a? Inline::HardLineBreak subcontent = Coradoc::Generator.gen_adoc(subitem) # Only try to postprocess elements that are text, diff --git a/lib/coradoc/element/paragraph.rb b/lib/coradoc/element/paragraph.rb index 7b7a5f2..22e3e5e 100644 --- a/lib/coradoc/element/paragraph.rb +++ b/lib/coradoc/element/paragraph.rb @@ -7,10 +7,10 @@ class Paragraph < Base def initialize(content, options = {}) @content = content - @title = options.fetch(:title, nil) - @meta = options.fetch(:meta, nil) @id = options.fetch(:id, nil) @anchor = Inline::Anchor.new(@id) if @id + @title = options.fetch(:title, nil) + @attributes = options.fetch(:attributes, nil) @tdsinglepara = options.fetch(:tdsinglepara, nil) end @@ -25,10 +25,11 @@ def texts def to_adoc title = @title.nil? ? "" : ".#{Coradoc::Generator.gen_adoc(@title)}\n" anchor = @anchor.nil? ? "" : "#{@anchor.to_adoc}\n" + attrs = @attributes.nil? ? "" : "#{@attributes.to_adoc}\n" if @tdsinglepara "#{title}#{anchor}" << Coradoc.strip_unicode(Coradoc::Generator.gen_adoc(@content)) else - "\n\n#{title}#{anchor}" << Coradoc.strip_unicode(Coradoc::Generator.gen_adoc(@content)) << "\n\n" + "\n\n#{title}#{anchor}#{attrs}" << Coradoc.strip_unicode(Coradoc::Generator.gen_adoc(@content)) << "\n\n" end end end diff --git a/lib/coradoc/element/table.rb b/lib/coradoc/element/table.rb index 0ea28d1..60f6f51 100644 --- a/lib/coradoc/element/table.rb +++ b/lib/coradoc/element/table.rb @@ -10,7 +10,7 @@ def initialize(title, rows, options = {}) @title = title @id = options.fetch(:id, nil) @anchor = @id.nil? ? nil : Inline::Anchor.new(@id) - @attrs = options.fetch(:attributes, "") + @attrs = options.fetch(:attributes, nil) end def to_adoc diff --git a/lib/coradoc/element/term.rb b/lib/coradoc/element/term.rb index ba92f17..a939699 100644 --- a/lib/coradoc/element/term.rb +++ b/lib/coradoc/element/term.rb @@ -14,7 +14,7 @@ def initialize(term, options = {}) def to_adoc return "#{@type.to_s}:[#{@term}]#{@line_break}" if @lang == :en - return "[#{@type.to_s}]##{@term}]##{@line_break}" if @lang == :fr + return "[#{@type.to_s}]##{@term}##{@line_break}" if @lang == :fr end end end diff --git a/lib/coradoc/parser/asciidoc/attribute_list.rb b/lib/coradoc/parser/asciidoc/attribute_list.rb index bf940c4..a0799c0 100644 --- a/lib/coradoc/parser/asciidoc/attribute_list.rb +++ b/lib/coradoc/parser/asciidoc/attribute_list.rb @@ -14,50 +14,50 @@ def named_attribute_value def named_attribute (match('[a-zA-Z0-9_-]').repeat(1).as(:named_key) >> str(' ').maybe >> str("=") >> str(' ').maybe >> - match['a-zA-Z0-9_\- '].repeat(1).as(:named_value) >> + match['a-zA-Z0-9_\- \"'].repeat(1).as(:named_value) >> str(' ').maybe ).as(:named) end def positional_attribute - (match['a-zA-Z0-9_-'].repeat(1) >> + (match['a-zA-Z0-9_\-%'].repeat(1) >> str("=").absent? ).as(:positional) end def named_many (named_attribute.repeat(1,1) >> - (str(",") >> named_attribute).repeat(0)) + (str(",") >> space.maybe >> named_attribute).repeat(0)) end def positional_one_named_many (positional_attribute.repeat(1,1) >> - (str(",") >> named_attribute).repeat(1)) + (str(",") >> space.maybe >> named_attribute).repeat(1)) end def positional_many_named_many (positional_attribute.repeat(1,1) >> - (str(",") >> positional_attribute).repeat(1) >> - (str(",") >> named_attribute).repeat(1)) + (str(",") >> space.maybe >> positional_attribute).repeat(1) >> + (str(",") >> space.maybe>> named_attribute).repeat(1)) end def positional_many (positional_attribute.repeat(1,1) >> - (str(",") >> positional_attribute).repeat(0)) + (str(",") >> space.maybe >> positional_attribute).repeat(0)) end def positional_zero_or_one positional_attribute.repeat(0,1) end - def attribute_list + def attribute_list(name = :attribute_list) match('^\[') >> str("[").absent? >> ( named_many | positional_one_named_many | positional_many_named_many | positional_many | positional_zero_or_one - ).as(:attribute_array).as(:attribute_list) >> + ).as(:attribute_array).as(name) >> str("]") end diff --git a/lib/coradoc/parser/asciidoc/base.rb b/lib/coradoc/parser/asciidoc/base.rb index c938c33..e4f0980 100644 --- a/lib/coradoc/parser/asciidoc/base.rb +++ b/lib/coradoc/parser/asciidoc/base.rb @@ -102,7 +102,7 @@ def email end def special_character - match("^[*_:=-]") | str("[#") | str("[[") + match("^[*:=-]") | str("[#") | str("[[") end def date @@ -135,6 +135,17 @@ def inline_image ).as(:inline_image) end + def block_image + (block_id.maybe >> + block_title.maybe >> + (attribute_list >> newline).maybe >> + match('^i') >> str("mage::") >> + file_path.as(:path) >> + attribute_list(:attribute_list_macro) >> + newline.as(:line_break) + ).as(:block_image) + end + def comment_line (str('//') >> str("/").absent? >> space? >> diff --git a/lib/coradoc/parser/asciidoc/block.rb b/lib/coradoc/parser/asciidoc/block.rb index 3fb40eb..ce9cde6 100644 --- a/lib/coradoc/parser/asciidoc/block.rb +++ b/lib/coradoc/parser/asciidoc/block.rb @@ -16,7 +16,7 @@ def source_block end def pass_block - block_style("+") + block_style("+", 4, :pass) end def source_block @@ -27,10 +27,13 @@ def quote_block block_style("_") end - def block_content - (text_line | - list - ).repeat(1) #>> newline + def block_content(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) #>> newline end def sidebar_block @@ -46,27 +49,31 @@ def block_title end def block_type(type) - (match("^[") >> str("[").absent? >> + (match('^\[') >> str("[").absent? >> str(type).as(:type) >> str("]")) | - (str("[") >> keyword.as(:type) >> str("]") - ) >> newline + (match('^\[') >> keyword.as(:type) >> str("]")) >> newline end def block_id - (str("[[") >> keyword.as(:id) >> str("]]") | + (match('^\[') >> str("[") >> str('[').absent? >> keyword.as(:id) >> str("]]") | str("[#") >> keyword.as(:id) >> str("]")) >> newline end - def block_style(delimiter = "*", repeater = 4, type = "") + def block_style(delimiter = "*", repeater = 4, type = nil) + block_id.maybe >> block_title.maybe >> newline.maybe >> (attribute_list >> newline ).maybe >> block_id.maybe >> (attribute_list >> newline ).maybe >> str(delimiter).repeat(repeater).as(:delimiter) >> newline >> - block_content.as(:lines) >> + if type == :pass + (text_line | empty_line.as(:line_break)).repeat(1).as(:lines) + else + block_content.as(:lines) + end >> str(delimiter).repeat(repeater) >> newline end diff --git a/lib/coradoc/parser/asciidoc/content.rb b/lib/coradoc/parser/asciidoc/content.rb index f261287..72793a8 100644 --- a/lib/coradoc/parser/asciidoc/content.rb +++ b/lib/coradoc/parser/asciidoc/content.rb @@ -37,7 +37,7 @@ def text_line( n_line_breaks = 1) end def asciidoc_char - match("^[*_:=-]") + match('^[*_:=\-+]') end def asciidoc_char_with_id diff --git a/lib/coradoc/parser/asciidoc/header.rb b/lib/coradoc/parser/asciidoc/header.rb index 557760f..a0568fb 100644 --- a/lib/coradoc/parser/asciidoc/header.rb +++ b/lib/coradoc/parser/asciidoc/header.rb @@ -10,7 +10,7 @@ def header end def header_title - match("=") >> space? >> text.as(:title) >> newline + match("^=") >> str('=').absent? >> space? >> text.as(:title) >> newline end def author diff --git a/lib/coradoc/parser/asciidoc/paragraph.rb b/lib/coradoc/parser/asciidoc/paragraph.rb index 67a732a..b7185e1 100644 --- a/lib/coradoc/parser/asciidoc/paragraph.rb +++ b/lib/coradoc/parser/asciidoc/paragraph.rb @@ -29,7 +29,8 @@ def paragraph # list.absent? >> # admonition_line.absent? >> - ( block_title.maybe >> + ( block_id.maybe >> + block_title.maybe >> (attribute_list >> newline).maybe >> (paragraph_text_line.repeat(1,1) >> any.absent? | (paragraph_text_line >> newline_single.as(:line_break)).repeat(1) >> diff --git a/lib/coradoc/parser/asciidoc/section.rb b/lib/coradoc/parser/asciidoc/section.rb index e35f680..c154543 100644 --- a/lib/coradoc/parser/asciidoc/section.rb +++ b/lib/coradoc/parser/asciidoc/section.rb @@ -8,6 +8,7 @@ def contents citation | term | term2 | bib_entry | + block_image | comment_block | comment_line | include_directive | diff --git a/lib/coradoc/parser/asciidoc/table.rb b/lib/coradoc/parser/asciidoc/table.rb index 99a4fc7..6df2122 100644 --- a/lib/coradoc/parser/asciidoc/table.rb +++ b/lib/coradoc/parser/asciidoc/table.rb @@ -5,8 +5,10 @@ module Table # include Coradoc::Parser::Asciidoc::Base def table + block_id.maybe >> (attribute_list >> newline).maybe >> block_title.maybe >> + (attribute_list >> newline).maybe >> str("|===") >> line_ending >> table_row.repeat(1).as(:rows) >> str("|===") >> line_ending diff --git a/lib/coradoc/parser/asciidoc/term.rb b/lib/coradoc/parser/asciidoc/term.rb index fbad156..74fbdde 100644 --- a/lib/coradoc/parser/asciidoc/term.rb +++ b/lib/coradoc/parser/asciidoc/term.rb @@ -15,7 +15,7 @@ def term def term2 match('^\[') >> term_type >> str(']#') >> match('[^\#]').repeat(1).as(:term2) >> str('#') >> - str("]") >> str("\n").repeat(1).as(:line_break) + str("\n").repeat(1).as(:line_break) end end end diff --git a/lib/coradoc/parser/base.rb b/lib/coradoc/parser/base.rb index 60de1c7..5ffafd6 100644 --- a/lib/coradoc/parser/base.rb +++ b/lib/coradoc/parser/base.rb @@ -37,17 +37,19 @@ class Base < Parslet::Parser # bibliography | admonition_line | bib_entry | + block_image | term | term2 | citation | # attribute_list.as(:attribute_list) | comment_block | comment_line | + section.as(:section) | block.as(:block) | include_directive | document_attributes | - section.as(:section) | - list | + list | + table.as(:table) | paragraph | header.as(:header) | empty_line.as(:line_break) | diff --git a/lib/coradoc/transformer.rb b/lib/coradoc/transformer.rb index 83e8fb7..033e02b 100644 --- a/lib/coradoc/transformer.rb +++ b/lib/coradoc/transformer.rb @@ -185,7 +185,7 @@ class NamedAttribute < Struct.new(:key, :value); end rule(paragraph: subtree(:paragraph)) do Element::Paragraph.new( paragraph[:lines], - meta: paragraph[:attribute_list], + attributes: paragraph[:attribute_list], title: paragraph[:title] ) end @@ -290,15 +290,16 @@ class NamedAttribute < Struct.new(:key, :value); end id = block[:id] title = block[:title] attribute_list = block[:attribute_list] - delimiter = block[:delimiter] + delimiter = block[:delimiter].to_s + delimiter_c = delimiter[0] lines = block[:lines] opts = {id: id, title: title, - attributes: attribute_list, delimiter_len: delimiter.size, lines: lines} - if delimiter == "****" + opts[:attributes] = attribute_list if attribute_list + if delimiter_c == "*" # puts attribute_lisp.inspect if (attribute_list.positional == [] && attribute_list.named.keys[0] == "reviewer") @@ -311,8 +312,16 @@ class NamedAttribute < Struct.new(:key, :value); end opts ) end - elsif delimiter == "____" - Element::Block::Quote.new + elsif delimiter_c == "=" + Element::Block::Example.new(title, opts) + elsif delimiter_c == "+" + Element::Block::Pass.new(opts) + elsif delimiter_c == "-" + if (attribute_list.positional[0] == "quote") + Element::Block::Quote.new(title, opts) + end + elsif delimiter_c == "_" + Element::Block::Quote.new(title, opts) end } @@ -324,7 +333,8 @@ class NamedAttribute < Struct.new(:key, :value); end ) do Element::Admonition.new(content, admonition_type.to_s) end - + + # # Block # rule(title: simple(:title), lines: sequence(:lines)) do # Element::Block.new(title, lines: lines) @@ -348,7 +358,22 @@ class NamedAttribute < Struct.new(:key, :value); end # rule(attributes: simple(:attributes), lines: sequence(:lines)) do # Element::Block.new(nil, lines: lines, attributes: attributes) # end - # + + # TODO block_image + rule(block_image: subtree(:block_image)) do + id = block_image[:id] + title = block_image[:title] + path = block_image[:path] + opts = { + # attributes: block_image[:attribute_list], + attributes: block_image[:attribute_list_macro], + line_break: block_image[:line_break] + } + Element::Image::BlockImage.new(title, id, path, opts) + end + + + # Attribute rule(key: simple(:key), value: simple(:value)) do Element::Attribute.new(key, value) @@ -359,7 +384,9 @@ class NamedAttribute < Struct.new(:key, :value); end Element::Attribute.new(key, value, line_break: line_break) end - rule(line_break: simple(:line_break)) { Element::LineBreak.new(line_break) } + rule(line_break: simple(:line_break)) { + Element::LineBreak.new(line_break) + } rule(document_attributes: sequence(:document_attributes)) do Element::DocumentAttributes.new(document_attributes) @@ -375,7 +402,10 @@ class NamedAttribute < Struct.new(:key, :value); end rule(table: subtree(:table)) do title = table[:title] || nil rows = table[:rows] || [] - opts = {attributes: table[:attribute_list] || ""} + opts = { + id: table[:id] || nil, + attributes: table[:attribute_list] || nil + } Element::Table.new(title, rows, opts) end From e4248c54451dec866285ca1fcc888dd685bd4f04 Mon Sep 17 00:00:00 2001 From: xyz65535 Date: Sat, 17 Aug 2024 17:52:41 +0200 Subject: [PATCH 13/13] cleanup: comments + some rubocop --- coradoc.gemspec | 1 + lib/coradoc/document.rb | 4 +- lib/coradoc/element/block/core.rb | 1 + lib/coradoc/element/block/pass.rb | 4 +- lib/coradoc/element/image/block_image.rb | 5 - lib/coradoc/element/image/core.rb | 2 - lib/coradoc/element/include.rb | 1 - lib/coradoc/element/list/core.rb | 8 +- lib/coradoc/element/list_item.rb | 2 +- lib/coradoc/element/section.rb | 2 +- lib/coradoc/element/table.rb | 2 +- lib/coradoc/element/term.rb | 4 +- lib/coradoc/parser/asciidoc/admonition.rb | 7 -- lib/coradoc/parser/asciidoc/base.rb | 11 +-- lib/coradoc/parser/asciidoc/block.rb | 5 +- lib/coradoc/parser/asciidoc/content.rb | 6 +- lib/coradoc/parser/asciidoc/inline.rb | 8 +- lib/coradoc/parser/asciidoc/paragraph.rb | 18 ---- lib/coradoc/parser/asciidoc/section.rb | 2 - lib/coradoc/parser/base.rb | 5 +- lib/coradoc/reverse_adoc.rb | 25 ++--- lib/coradoc/reverse_adoc/cleaner.rb | 4 +- lib/coradoc/reverse_adoc/config.rb | 114 +++++++++++----------- lib/coradoc/reverse_adoc/converters.rb | 88 +++++++++-------- lib/coradoc/reverse_adoc/errors.rb | 14 +-- lib/coradoc/reverse_adoc/plugin.rb | 4 +- lib/coradoc/reverse_adoc/postprocessor.rb | 2 +- lib/coradoc/transformer.rb | 73 ++------------ lib/coradoc/util.rb | 2 +- lib/reverse_adoc.rb | 4 +- spec/coradoc/element/list_spec.rb | 1 - utils/parser_analyzer.rb | 3 +- utils/round_trip.rb | 15 ++- 33 files changed, 170 insertions(+), 277 deletions(-) diff --git a/coradoc.gemspec b/coradoc.gemspec index ef77d61..63200ee 100644 --- a/coradoc.gemspec +++ b/coradoc.gemspec @@ -44,6 +44,7 @@ Gem::Specification.new do |spec| spec.add_development_dependency "rubocop" spec.add_development_dependency "rubocop-performance" spec.add_development_dependency "simplecov" + # spec.add_development_dependency "parallel_tests" # spec.add_development_dependency "stackprof" # spec.add_runtime_dependency "thor" end diff --git a/lib/coradoc/document.rb b/lib/coradoc/document.rb index cabf4f8..1238538 100644 --- a/lib/coradoc/document.rb +++ b/lib/coradoc/document.rb @@ -69,8 +69,8 @@ def initialize(options = {}) def to_adoc Coradoc::Generator.gen_adoc(@header) + - Coradoc::Generator.gen_adoc(@document_attributes) + - Coradoc::Generator.gen_adoc(@sections) + Coradoc::Generator.gen_adoc(@document_attributes) + + Coradoc::Generator.gen_adoc(@sections) end end end diff --git a/lib/coradoc/element/block/core.rb b/lib/coradoc/element/block/core.rb index aa64740..f4c4275 100644 --- a/lib/coradoc/element/block/core.rb +++ b/lib/coradoc/element/block/core.rb @@ -37,6 +37,7 @@ def gen_title def gen_attributes attrs = @attributes.to_adoc(false) return "#{attrs}\n" if !attrs.empty? + "" end diff --git a/lib/coradoc/element/block/pass.rb b/lib/coradoc/element/block/pass.rb index 12c8502..94cfce0 100644 --- a/lib/coradoc/element/block/pass.rb +++ b/lib/coradoc/element/block/pass.rb @@ -3,6 +3,8 @@ module Element module Block class Pass < Core def initialize(options = {}) + @id = options.fetch(:id, nil) + @anchor = @id.nil? ? nil : Inline::Anchor.new(@id) @title = options.fetch(:title, "") @attributes = options.fetch(:attributes, AttributeList.new) @delimiter_char = "+" @@ -11,7 +13,7 @@ def initialize(options = {}) end def to_adoc - "\n\n#{gen_title}#{gen_attributes}#{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 diff --git a/lib/coradoc/element/image/block_image.rb b/lib/coradoc/element/image/block_image.rb index b09f6f2..d8347b6 100644 --- a/lib/coradoc/element/image/block_image.rb +++ b/lib/coradoc/element/image/block_image.rb @@ -11,15 +11,10 @@ def to_adoc missing = "// FIXME: Missing image: #{@annotate_missing}\n" if @annotate_missing anchor = @anchor.nil? ? "" : "#{@anchor.to_adoc}\n" title = ".#{@title}\n" unless @title.to_s.empty? - # attrs = @attributes.to_adoc attrs = @attributes.to_adoc [missing, anchor, title, "image", @colons, @src, attrs, @line_break].join("") end - # def to_adoc - - # end - def validate_named @attributes.validate_named(VALIDATORS_NAMED, VALIDATORS_NAMED_BLOCK) end diff --git a/lib/coradoc/element/image/core.rb b/lib/coradoc/element/image/core.rb index 690c5cf..aa432a4 100644 --- a/lib/coradoc/element/image/core.rb +++ b/lib/coradoc/element/image/core.rb @@ -11,7 +11,6 @@ def initialize(title, id, src, options = {}) @id = id @anchor = @id.nil? ? nil : Inline::Anchor.new(@id) @src = src - # @attributes = options.fetch(:attributes, AttributeList.new) @attributes = options.fetch(:attributes, AttributeList.new) @annotate_missing = options.fetch(:annotate_missing, nil) @title = options.fetch(:title, nil) unless @title @@ -26,7 +25,6 @@ def to_adoc missing = "// FIXME: Missing image: #{@annotate_missing}\n" if @annotate_missing anchor = @anchor.nil? ? "" : "#{@anchor.to_adoc}\n" title = ".#{@title}\n" unless @title.to_s.empty? - # attrs = @attributes.to_adoc attrs = @attributes_macro.to_adoc [missing, anchor, title, "image", @colons, @src, attrs, @line_break].join("") end diff --git a/lib/coradoc/element/include.rb b/lib/coradoc/element/include.rb index 3698373..8063080 100644 --- a/lib/coradoc/element/include.rb +++ b/lib/coradoc/element/include.rb @@ -9,7 +9,6 @@ def initialize(path, options = {}) @line_break = options.fetch(:line_break, "\n") end - def to_adoc attrs = @attributes.to_adoc(true) "include::#{@path}#{attrs}#{@line_break}" diff --git a/lib/coradoc/element/list/core.rb b/lib/coradoc/element/list/core.rb index 587d2e4..d0c5791 100644 --- a/lib/coradoc/element/list/core.rb +++ b/lib/coradoc/element/list/core.rb @@ -16,10 +16,10 @@ def initialize(items, options = {}) @anchor = @id.nil? ? nil : Inline::Anchor.new(@id) @ol_count = options.fetch(:ol_count, nil) if @ol_count.nil? - m = @items.select{ |i| + m = @items.select do |i| i.is_a?(Coradoc::Element::ListItem) && - !i.marker.nil? - }.first&.marker + !i.marker.nil? + end.first&.marker @ol_count = m.size if m.is_a?(String) end @ol_count = 1 if @ol_count.nil? @@ -38,7 +38,7 @@ def to_adoc # See: https://github.com/metanorma/coradoc/issues/96 unless item.is_a? List::Core content << prefix.to_s - content << " " if c[0]!=" " + content << " " if c[0] != " " end content << c end diff --git a/lib/coradoc/element/list_item.rb b/lib/coradoc/element/list_item.rb index cbf8b8c..c97a241 100644 --- a/lib/coradoc/element/list_item.rb +++ b/lib/coradoc/element/list_item.rb @@ -21,7 +21,7 @@ def to_adoc subcontent = Coradoc::Generator.gen_adoc(subitem) # Only try to postprocess elements that are text, # otherwise we could strip markup. - if Coradoc.is_a_single?(subitem, Coradoc::Element::TextElement) + if Coradoc.a_single?(subitem, Coradoc::Element::TextElement) subcontent = Coradoc.strip_unicode(subcontent) end subcontent.chomp diff --git a/lib/coradoc/element/section.rb b/lib/coradoc/element/section.rb index 74af3a7..7813264 100644 --- a/lib/coradoc/element/section.rb +++ b/lib/coradoc/element/section.rb @@ -38,7 +38,7 @@ def to_adoc # Only try to postprocess elements that are text, # otherwise we could strip markup. - if Coradoc.is_a_single?(@contents, Coradoc::Element::TextElement) + if Coradoc.a_single?(@contents, Coradoc::Element::TextElement) content = Coradoc.strip_unicode(content) end diff --git a/lib/coradoc/element/table.rb b/lib/coradoc/element/table.rb index 60f6f51..e6f85fe 100644 --- a/lib/coradoc/element/table.rb +++ b/lib/coradoc/element/table.rb @@ -78,7 +78,7 @@ def to_adoc content = Coradoc::Generator.gen_adoc(content) # Only try to postprocess elements that are text, # otherwise we could strip markup. - if Coradoc.is_a_single?(@content, Coradoc::Element::TextElement) + if Coradoc.a_single?(@content, Coradoc::Element::TextElement) content = Coradoc.strip_unicode(content) end "#{@colrowattr}#{@alignattr}#{@style}| #{anchor}#{content}" diff --git a/lib/coradoc/element/term.rb b/lib/coradoc/element/term.rb index a939699..9f8fbe3 100644 --- a/lib/coradoc/element/term.rb +++ b/lib/coradoc/element/term.rb @@ -13,8 +13,8 @@ def initialize(term, options = {}) end def to_adoc - return "#{@type.to_s}:[#{@term}]#{@line_break}" if @lang == :en - return "[#{@type.to_s}]##{@term}##{@line_break}" if @lang == :fr + return "#{@type}:[#{@term}]#{@line_break}" if @lang == :en + "[#{@type}]##{@term}##{@line_break}" end end end diff --git a/lib/coradoc/parser/asciidoc/admonition.rb b/lib/coradoc/parser/asciidoc/admonition.rb index 808522e..1538b9d 100644 --- a/lib/coradoc/parser/asciidoc/admonition.rb +++ b/lib/coradoc/parser/asciidoc/admonition.rb @@ -12,14 +12,7 @@ def admonition_type end def admonition_line admonition_type.as(:admonition_type) >> str(': ') >> - # (text_line.repeat(1).as(:content) | - # text_line.as(:text) >> line_ending.repeat(2).as(:line_break) | - # # (text.as(:text) >> line - # text_line.as(:text) - # # ) - # ) ( - # text_line(1).repeat(1) (text.as(:text) >> line_ending.as(:line_break) ).repeat(1) diff --git a/lib/coradoc/parser/asciidoc/base.rb b/lib/coradoc/parser/asciidoc/base.rb index e4f0980..850dc41 100644 --- a/lib/coradoc/parser/asciidoc/base.rb +++ b/lib/coradoc/parser/asciidoc/base.rb @@ -10,7 +10,6 @@ require_relative "paragraph" require_relative "section" require_relative "table" -# require_relative "include" require_relative "term" module Coradoc @@ -33,14 +32,10 @@ module Base def space? space.maybe - # str(' ') >> str(' ').absent? | - # str(' ') >> str(' ').absent? | - # space.maybe end def space str(' ').repeat(1) - # match('\s').repeat(1) end def text @@ -56,7 +51,6 @@ def endline end def newline - # match["\r\n"].repeat(1) (str("\n") | str("\r\n")).repeat(1) end @@ -82,7 +76,6 @@ def digits def word match("[a-zA-Z0-9_-]").repeat(1) - # match(/[a-zA-Z0-9_-]+/) end def words @@ -116,7 +109,6 @@ def attr_name def file_path match('[^\[]').repeat(1) - # match("[a-zA-Z0-9_-]").repeat(1) end def include_directive @@ -149,8 +141,7 @@ def block_image def comment_line (str('//') >> str("/").absent? >> space? >> - text.as(:comment_text) #>> - # newline.as(:line_break) + text.as(:comment_text) ).as(:comment_line) end diff --git a/lib/coradoc/parser/asciidoc/block.rb b/lib/coradoc/parser/asciidoc/block.rb index ce9cde6..2607fee 100644 --- a/lib/coradoc/parser/asciidoc/block.rb +++ b/lib/coradoc/parser/asciidoc/block.rb @@ -33,7 +33,7 @@ def block_content(n_deep = 2) text_line | empty_line.as(:line_break) c = c | block_content(n_deep - 1) if (n_deep > 0) - c.repeat(1) #>> newline + c.repeat(1) end def sidebar_block @@ -45,7 +45,7 @@ def example_block end def block_title - match("^\\.") >> space.absent? >> text.as(:title) >> newline + match('^\\.') >> space.absent? >> text.as(:title) >> newline end def block_type(type) @@ -60,7 +60,6 @@ def block_id str("[#") >> keyword.as(:id) >> str("]")) >> 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 72793a8..122a48f 100644 --- a/lib/coradoc/parser/asciidoc/content.rb +++ b/lib/coradoc/parser/asciidoc/content.rb @@ -27,10 +27,6 @@ def literal_space? # Text def text_line( n_line_breaks = 1) - # (comment_block.absent? >> - # comment_line.absent? >> - # include_directive.absent?) >> - # attribute_list.absent? >> (asciidoc_char_with_id.absent? | text_id) >> literal_space? >> text.as(:text) >> line_ending.repeat(n_line_breaks).as(:line_break) @@ -41,7 +37,7 @@ def asciidoc_char end def asciidoc_char_with_id - asciidoc_char | str("[#") | str("[[") + asciidoc_char | str('[#') | str('[[') end def text_id diff --git a/lib/coradoc/parser/asciidoc/inline.rb b/lib/coradoc/parser/asciidoc/inline.rb index 6396b92..e340d85 100644 --- a/lib/coradoc/parser/asciidoc/inline.rb +++ b/lib/coradoc/parser/asciidoc/inline.rb @@ -68,19 +68,15 @@ def text_unformatted italic_unconstrained.absent? | italic_constrained.absent?) >> match('[^\n]').repeat(1) - )#.as(:text_unformatted) + ) end def text_formatted - # attribute_list.absent? >> - # (asciidoc_char_with_id.absent?| text_id) >> - # (attribute_list >> newline).absent? >> - # literal_space? >> ((cross_reference | bold_unconstrained | bold_constrained | highlight_unconstrained | highlight_constrained | italic_unconstrained | italic_constrained )| - text_unformatted).repeat(1)#.as(:text_formatted) + text_unformatted).repeat(1) end end diff --git a/lib/coradoc/parser/asciidoc/paragraph.rb b/lib/coradoc/parser/asciidoc/paragraph.rb index b7185e1..8d37f8c 100644 --- a/lib/coradoc/parser/asciidoc/paragraph.rb +++ b/lib/coradoc/parser/asciidoc/paragraph.rb @@ -4,31 +4,13 @@ module Asciidoc module Paragraph def paragraph_text_line - # include_directive.absent? >> - # comment_block.absent? >> - # comment_line.absent? >> - # list.absent? >> - # admonition_line.absent? >> - # (match("^") >> attribute_list >> newline).absent? >> - - # block.absent? >> - # match('^\[').absent? >> (asciidoc_char_with_id.absent? | text_id ) >> literal_space? >> (text_formatted.as(:text) # >> - # newline_single.as(:break).maybe ) | term | term2 - #.as(:paragraph_text_line) end def paragraph - # block.absent? >> - # include_directive.absent? >> - # comment_block.absent? >> - # comment_line.absent? >> - # list.absent? >> - # admonition_line.absent? >> - ( block_id.maybe >> block_title.maybe >> (attribute_list >> newline).maybe >> diff --git a/lib/coradoc/parser/asciidoc/section.rb b/lib/coradoc/parser/asciidoc/section.rb index c154543..e5ece0d 100644 --- a/lib/coradoc/parser/asciidoc/section.rb +++ b/lib/coradoc/parser/asciidoc/section.rb @@ -56,8 +56,6 @@ def section(level = 2) else r end - # r - end end diff --git a/lib/coradoc/parser/base.rb b/lib/coradoc/parser/base.rb index 5ffafd6..f930edd 100644 --- a/lib/coradoc/parser/base.rb +++ b/lib/coradoc/parser/base.rb @@ -34,20 +34,17 @@ class Base < Parslet::Parser root :document rule(:document) do ( - # bibliography | admonition_line | bib_entry | block_image | term | term2 | citation | - # attribute_list.as(:attribute_list) | comment_block | comment_line | - section.as(:section) | block.as(:block) | + section.as(:section) | include_directive | document_attributes | - list | table.as(:table) | paragraph | diff --git a/lib/coradoc/reverse_adoc.rb b/lib/coradoc/reverse_adoc.rb index aa4a3ad..64cde2b 100644 --- a/lib/coradoc/reverse_adoc.rb +++ b/lib/coradoc/reverse_adoc.rb @@ -12,19 +12,20 @@ require_relative "reverse_adoc/plugin" require_relative "reverse_adoc/postprocessor" +module Coradoc + module ReverseAdoc + def self.convert(input, options = {}) + Coradoc::ReverseAdoc::HtmlConverter.convert(input, options) + end -module Coradoc::ReverseAdoc - def self.convert(input, options = {}) - Coradoc::ReverseAdoc::HtmlConverter.convert(input, options) - end - - def self.config - @config ||= Config.new - yield @config if block_given? - @config - end + def self.config + @config ||= Config.new + yield @config if block_given? + @config + end - def self.cleaner - @cleaner ||= Cleaner.new + def self.cleaner + @cleaner ||= Cleaner.new + end end end diff --git a/lib/coradoc/reverse_adoc/cleaner.rb b/lib/coradoc/reverse_adoc/cleaner.rb index 92455dc..6fab2c8 100644 --- a/lib/coradoc/reverse_adoc/cleaner.rb +++ b/lib/coradoc/reverse_adoc/cleaner.rb @@ -21,11 +21,11 @@ def tidy(string) end def remove_block_leading_newlines(string) - string.gsub("]\n****\n\n","]\n****\n") + string.gsub("]\n****\n\n", "]\n****\n") end def remove_section_attribute_newlines(string) - string.gsub("]\n\n==","]\n==") + string.gsub("]\n\n==", "]\n==") end def remove_newlines(string) diff --git a/lib/coradoc/reverse_adoc/config.rb b/lib/coradoc/reverse_adoc/config.rb index a48ea28..4fc7061 100644 --- a/lib/coradoc/reverse_adoc/config.rb +++ b/lib/coradoc/reverse_adoc/config.rb @@ -1,73 +1,75 @@ require "tmpdir" -module Coradoc::ReverseAdoc - class Config - def initialize - @unknown_tags = :pass_through - @input_format = :html - @mathml2asciimath = false - @external_images = false +module Coradoc + module ReverseAdoc + class Config + def initialize + @unknown_tags = :pass_through + @input_format = :html + @mathml2asciimath = false + @external_images = false - # Destination to save file and images - @destination = nil + # Destination to save file and images + @destination = nil - # Source of HTML - # @sourcedir = nil + # Source of HTML + # @sourcedir = nil - # Image counter, assuming there are max 999 images - @image_counter = 1 - # pad with 0s - @image_counter_pattern = "%03d" + # Image counter, assuming there are max 999 images + @image_counter = 1 + # pad with 0s + @image_counter_pattern = "%03d" - @em_delimiter = "_".freeze - @strong_delimiter = "*".freeze - @inline_options = {} - @tag_border = " ".freeze + @em_delimiter = "_".freeze + @strong_delimiter = "*".freeze + @inline_options = {} + @tag_border = " ".freeze - @split_sections = nil + @split_sections = nil - # Document width - used to compute table sizes. - # This is an assumption for screen size in input document. - # If column widths are specified in absolute values, then we - # have to convert them to relative values, as AsciiDoc only - # supports those. - @doc_width = 1000 + # Document width - used to compute table sizes. + # This is an assumption for screen size in input document. + # If column widths are specified in absolute values, then we + # have to convert them to relative values, as AsciiDoc only + # supports those. + @doc_width = 1000 - # Plugin system - @plugins = [] + # Plugin system + @plugins = [] - # Debugging options - @track_time = false - end + # Debugging options + @track_time = false + end - def with(options = {}) - old_options = @inline_options - @inline_options = options - result = yield - @inline_options = old_options - result - end + def with(options = {}) + old_options = @inline_options + @inline_options = options + result = yield + @inline_options = old_options + result + end + + def self.declare_option(option) + define_method(option) do + @inline_options[option] || instance_variable_get(:"@#{option}") + end - def self.declare_option(option) - define_method(option) do - @inline_options[option] || instance_variable_get(:"@#{option}") + attr_writer option end - attr_writer option + declare_option :unknown_tags + declare_option :tag_border + declare_option :mathml2asciimath + declare_option :external_images + declare_option :destination + declare_option :sourcedir + declare_option :image_counter + declare_option :image_counter_pattern + declare_option :input_format + declare_option :split_sections + declare_option :doc_width + declare_option :plugins + declare_option :track_time end - - declare_option :unknown_tags - declare_option :tag_border - declare_option :mathml2asciimath - declare_option :external_images - declare_option :destination - declare_option :sourcedir - declare_option :image_counter - declare_option :image_counter_pattern - declare_option :input_format - declare_option :split_sections - declare_option :doc_width - declare_option :plugins - declare_option :track_time end end diff --git a/lib/coradoc/reverse_adoc/converters.rb b/lib/coradoc/reverse_adoc/converters.rb index 9067eea..6c8b813 100644 --- a/lib/coradoc/reverse_adoc/converters.rb +++ b/lib/coradoc/reverse_adoc/converters.rb @@ -1,54 +1,56 @@ -module Coradoc::ReverseAdoc - module Converters - def self.register(tag_name, converter) - @@converters ||= {} - @@converters[tag_name.to_sym] = converter - end +module Coradoc + module ReverseAdoc + module Converters + def self.register(tag_name, converter) + @@converters ||= {} + @@converters[tag_name.to_sym] = converter + end - def self.unregister(tag_name) - @@converters.delete(tag_name.to_sym) - end + def self.unregister(tag_name) + @@converters.delete(tag_name.to_sym) + end - def self.lookup(tag_name) - converter = @@converters[tag_name.to_sym] || default_converter(tag_name) - converter = converter.new if converter.respond_to? :new - converter - end + def self.lookup(tag_name) + converter = @@converters[tag_name.to_sym] || default_converter(tag_name) + converter = converter.new if converter.respond_to? :new + converter + end - # Note: process won't run plugin hooks - def self.process(node, state) - node = node.to_a if node.is_a? Nokogiri::XML::NodeSet - return node.map { |i| process(i, state) }.join if node.is_a? Array + # Note: process won't run plugin hooks + def self.process(node, state) + node = node.to_a if node.is_a? Nokogiri::XML::NodeSet + return node.map { |i| process(i, state) }.join if node.is_a? Array - lookup(node.name).convert(node, state) - end + lookup(node.name).convert(node, state) + end - def self.process_coradoc(node, state) - node = node.to_a if node.is_a? Nokogiri::XML::NodeSet - return node.map { |i| process_coradoc(i, state) } if node.is_a? Array + def self.process_coradoc(node, state) + node = node.to_a if node.is_a? Nokogiri::XML::NodeSet + return node.map { |i| process_coradoc(i, state) } if node.is_a? Array - plugins = state[:plugin_instances] || {} - process = proc { lookup(node.name).to_coradoc(node, state) } - plugins.each do |i| - prev_process = process - process = proc { i.html_tree_run_hooks(node, state, &prev_process) } + plugins = state[:plugin_instances] || {} + process = proc { lookup(node.name).to_coradoc(node, state) } + plugins.each do |i| + prev_process = process + process = proc { i.html_tree_run_hooks(node, state, &prev_process) } + end + process.(node, state) end - process.(node, state) - end - def self.default_converter(tag_name) - case Coradoc::ReverseAdoc.config.unknown_tags.to_sym - when :pass_through - Coradoc::ReverseAdoc::Converters::PassThrough.new - when :drop - Coradoc::ReverseAdoc::Converters::Drop.new - when :bypass - Coradoc::ReverseAdoc::Converters::Bypass.new - when :raise - raise UnknownTagError, "unknown tag: #{tag_name}" - else - raise InvalidConfigurationError, - "unknown value #{Coradoc::ReverseAdoc.config.unknown_tags.inspect} for Coradoc::ReverseAdoc.config.unknown_tags" + def self.default_converter(tag_name) + case Coradoc::ReverseAdoc.config.unknown_tags.to_sym + when :pass_through + Coradoc::ReverseAdoc::Converters::PassThrough.new + when :drop + Coradoc::ReverseAdoc::Converters::Drop.new + when :bypass + Coradoc::ReverseAdoc::Converters::Bypass.new + when :raise + raise UnknownTagError, "unknown tag: #{tag_name}" + else + raise InvalidConfigurationError, + "unknown value #{Coradoc::ReverseAdoc.config.unknown_tags.inspect} for Coradoc::ReverseAdoc.config.unknown_tags" + end end end end diff --git a/lib/coradoc/reverse_adoc/errors.rb b/lib/coradoc/reverse_adoc/errors.rb index 26c5c86..c2429f9 100644 --- a/lib/coradoc/reverse_adoc/errors.rb +++ b/lib/coradoc/reverse_adoc/errors.rb @@ -1,10 +1,12 @@ -module Coradoc::ReverseAdoc - class Error < StandardError - end +module Coradoc + module ReverseAdoc + class Error < StandardError + end - class UnknownTagError < Error - end + class UnknownTagError < Error + end - class InvalidConfigurationError < Error + class InvalidConfigurationError < Error + end end end diff --git a/lib/coradoc/reverse_adoc/plugin.rb b/lib/coradoc/reverse_adoc/plugin.rb index 99a394c..5b7f8f6 100644 --- a/lib/coradoc/reverse_adoc/plugin.rb +++ b/lib/coradoc/reverse_adoc/plugin.rb @@ -53,11 +53,11 @@ def html_tree_replace_with_children_by_css(css) end end - def html_tree_process_to_coradoc(tree, state={}) + def html_tree_process_to_coradoc(tree, state = {}) Coradoc::ReverseAdoc::Converters.process_coradoc(tree, state) end - def html_tree_process_to_adoc(tree, state={}) + def html_tree_process_to_adoc(tree, state = {}) Coradoc::ReverseAdoc::Converters.process(tree, state) end diff --git a/lib/coradoc/reverse_adoc/postprocessor.rb b/lib/coradoc/reverse_adoc/postprocessor.rb index 098794e..50065d9 100644 --- a/lib/coradoc/reverse_adoc/postprocessor.rb +++ b/lib/coradoc/reverse_adoc/postprocessor.rb @@ -111,7 +111,7 @@ def split_sections # we can compute numbers previous_sections[elem] = parent_sections[title.level_int] parent_sections[title.level_int] = elem - parent_sections[(title.level_int+1)..nil] = nil + parent_sections[(title.level_int + 1)..nil] = nil elem else diff --git a/lib/coradoc/transformer.rb b/lib/coradoc/transformer.rb index 033e02b..9c2abfc 100644 --- a/lib/coradoc/transformer.rb +++ b/lib/coradoc/transformer.rb @@ -125,9 +125,6 @@ class NamedAttribute < Struct.new(:key, :value); end line_break: line_break) end - - - rule(text: sequence(:text), line_break: simple(:line_break) ) do @@ -136,11 +133,6 @@ class NamedAttribute < Struct.new(:key, :value); end line_break: line_break) end - # rule(text_unformatted: simple(:text)) { text } - # rule(text: sequence({text_unformatted: simple(:t)})) { t.to_s } - - - rule(href: simple(:href)){ Element::Inline::CrossReference.new( href.to_s @@ -211,10 +203,6 @@ class NamedAttribute < Struct.new(:key, :value); end end # Section - # rule(title: simple(:title)) { Element::Section.new(title) } - # - # rule(id: simple(:id), title: simple(:title), content:) - rule(section: subtree(:section)) do id = section[:id] || nil title = section[:title] || nil @@ -230,26 +218,16 @@ class NamedAttribute < Struct.new(:key, :value); end Element::Core.new("", type: "example", lines: example) end - # rule(title: simple(:title), paragraphs: sequence(:paragraphs)) do - # Element::Section.new(title, paragraphs: paragraphs) - # end - # - # rule(title: simple(:title), blocks: sequence(:blocks)) do - # Element::Section.new(title, blocks: blocks) - # end - # - rule(bibliography_entry: subtree(:bib_entry) ){ Element::BibliographyEntry.new(bib_entry) } - rule( #bibliography: subtree(:bib_data)){ + rule( id: simple(:id), title: simple(:title), entries: sequence(:entries) ){ Element::Bibliography.new( - # bib_data id: id, title: title, entries: entries @@ -280,27 +258,24 @@ class NamedAttribute < Struct.new(:key, :value); end rule(block: subtree(:block) - # { - # title: simple(:title), - # attribute_list: simple(:attribute_list), - # delimiter: simple(:delimiter), - # lines: sequence(:lines) - # } ) { + id = block[:id] title = block[:title] attribute_list = block[:attribute_list] delimiter = block[:delimiter].to_s delimiter_c = delimiter[0] lines = block[:lines] + ordering = block.keys.select{|k| + [:id, :title, :attribute_list, :attribute_list2].include?(k)} opts = {id: id, title: title, delimiter_len: delimiter.size, - lines: lines} + lines: lines, + ordering: ordering} opts[:attributes] = attribute_list if attribute_list if delimiter_c == "*" - # puts attribute_lisp.inspect if (attribute_list.positional == [] && attribute_list.named.keys[0] == "reviewer") Element::Block::ReviewerComment.new( @@ -325,47 +300,18 @@ class NamedAttribute < Struct.new(:key, :value); end end } - # # Admonition - # rule(admonition_type: simple(:admonition)) { admonition } + # Admonition rule(admonition_type: simple(:admonition_type), content: sequence(:content), - # line_break: simple(:line_break) ) do Element::Admonition.new(content, admonition_type.to_s) end - - # # Block - # rule(title: simple(:title), lines: sequence(:lines)) do - # Element::Block.new(title, lines: lines) - # end - # - # rule( - # title: simple(:title), - # delimiter: simple(:delimiter), - # lines: sequence(:lines)) do - # Element::Block.new(title, lines: lines, delimiter: delimiter) - # end - # - # rule( - # type: simple(:type), - # title: simple(:title), - # delimiter: simple(:delimiter), - # lines: sequence(:lines)) do - # Element::Block.new(title, lines: lines, delimiter: delimiter, type: type) - # end - # - # rule(attributes: simple(:attributes), lines: sequence(:lines)) do - # Element::Block.new(nil, lines: lines, attributes: attributes) - # end - - # TODO block_image rule(block_image: subtree(:block_image)) do id = block_image[:id] title = block_image[:title] path = block_image[:path] opts = { - # attributes: block_image[:attribute_list], attributes: block_image[:attribute_list_macro], line_break: block_image[:line_break] } @@ -409,8 +355,6 @@ 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), @@ -447,9 +391,6 @@ class NamedAttribute < Struct.new(:key, :value); end Element::List::Unordered.new(list_items, attrs: attribute_list) end - - - # rule(list: simple(:list)) { list } rule(ordered: sequence(:list_items)) do Element::List::Ordered.new(list_items) end diff --git a/lib/coradoc/util.rb b/lib/coradoc/util.rb index e9ede72..b85218e 100644 --- a/lib/coradoc/util.rb +++ b/lib/coradoc/util.rb @@ -3,7 +3,7 @@ def self.strip_unicode(str) str.gsub(/\A[[:space:]]+|[[:space:]]+\z/, "") end - def self.is_a_single?(obj, klass) + def self.a_single?(obj, klass) obj.is_a?(klass) || (obj.is_a?(Array) && obj.length == 1 && obj.first.is_a?(klass)) end diff --git a/lib/reverse_adoc.rb b/lib/reverse_adoc.rb index e3b1c80..084caef 100644 --- a/lib/reverse_adoc.rb +++ b/lib/reverse_adoc.rb @@ -1,4 +1,4 @@ -warn <<~END +warn <<~WARN Deprecated: reverse_adoc has been merged into coradoc gem. | Please update your references from: | require 'reverse_adoc' @@ -13,7 +13,7 @@ | | Please also ensure that you replace all references to ReverseAdoc in your code | with Coradoc::ReverseAdoc. -END +WARN require "coradoc/reverse_adoc" diff --git a/spec/coradoc/element/list_spec.rb b/spec/coradoc/element/list_spec.rb index 313f8f7..4ff4f52 100644 --- a/spec/coradoc/element/list_spec.rb +++ b/spec/coradoc/element/list_spec.rb @@ -28,7 +28,6 @@ expect(list.to_adoc).to eq("\n\n* Item 1\n* Item 2\nsecond line\nthird line\n") end it "handles definition list" do - # items = Coradoc::Element::ListItem.new(["Item1", "Item 2b", "Item 2c"]) item = Coradoc::Element::ListItemDefinition.new("Coffee","Black hot drink") items = [item] diff --git a/utils/parser_analyzer.rb b/utils/parser_analyzer.rb index e13082a..a05c6c1 100644 --- a/utils/parser_analyzer.rb +++ b/utils/parser_analyzer.rb @@ -38,7 +38,8 @@ def is_def?(arr) g[:fontsize] = 8 g[:rankdir] = "LR" -g[:concentrate] = true +g[:overlap] = false +g[:splines] = false nodes = {} diff --git a/utils/round_trip.rb b/utils/round_trip.rb index d11ed41..473efcd 100644 --- a/utils/round_trip.rb +++ b/utils/round_trip.rb @@ -1,4 +1,4 @@ -$LOAD_PATH.unshift("../coradoc/lib"); +$LOAD_PATH.unshift("../coradoc/lib") require "coradoc" require "coradoc/reverse_adoc" @@ -11,7 +11,7 @@ exit end -adoc_files = Dir.glob("#{rice_path}**/*adoc"); +adoc_files = Dir.glob("#{rice_path}**/*adoc") adoc_files.each do |file_path| puts file_path @@ -20,21 +20,18 @@ FileUtils.rm(file_path_rt) if File.exist?(file_path_rt) FileUtils.rm(file_path_diff) if File.exist?(file_path_diff) # begin - adoc_file = File.open(file_path).read; + adoc_file = File.open(file_path).read next if adoc_file.size == 0 puts "parsing..." - ast = Coradoc::Parser::Base.new.parse(adoc_file); + ast = Coradoc::Parser::Base.new.parse(adoc_file) puts "transforming..." doc = Coradoc::Transformer.transform(ast[:document]) - #puts doc.inspect - # doc = Coradoc::Document.from_adoc(sample_file) - puts "generating..." generated_adoc = Coradoc::Generator.gen_adoc(doc) cleaned_adoc = Coradoc::ReverseAdoc.cleaner.tidy(generated_adoc) File.open("#{file_path}.roundtrip","w"){|f| f.write(cleaned_adoc)} - `diff #{file_path} #{file_path}.roundtrip > #{file_path}.roundtrip.diff` + `diff -B #{file_path} #{file_path}.roundtrip > #{file_path}.roundtrip.diff` # rescue # puts "unsuccessful..." # end -end; +end