Skip to content

Commit

Permalink
fixes: roundtrip rice-2023
Browse files Browse the repository at this point in the history
  • Loading branch information
xyz65535 committed Aug 6, 2024
1 parent f67ff46 commit 387fd04
Show file tree
Hide file tree
Showing 17 changed files with 113 additions and 19 deletions.
1 change: 1 addition & 0 deletions lib/coradoc/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
require_relative "element/audio"
require_relative "element/video"
require_relative "element/break"
require_relative "element/term"

module Coradoc
class Document
Expand Down
3 changes: 1 addition & 2 deletions lib/coradoc/element/admonition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 5 additions & 4 deletions lib/coradoc/element/bibliography_entry.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/coradoc/element/block/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def type_hash
"----" => :source,
"====" => :example,
"...." => :literal,
"++++" => :pass,
}
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/coradoc/element/inline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
21 changes: 21 additions & 0 deletions lib/coradoc/element/term.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 3 additions & 1 deletion lib/coradoc/parser/asciidoc/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
require_relative "section"
require_relative "table"
# require_relative "include"
require_relative "term"

module Coradoc
module Parser
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/coradoc/parser/asciidoc/bibliography.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 14 additions & 1 deletion lib/coradoc/parser/asciidoc/block.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions lib/coradoc/parser/asciidoc/inline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
3 changes: 2 additions & 1 deletion lib/coradoc/parser/asciidoc/paragraph.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions lib/coradoc/parser/asciidoc/section.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module Section
def contents
(
citation |
term | term2 |
bib_entry |
comment_block |
comment_line |
Expand All @@ -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
Expand Down
23 changes: 23 additions & 0 deletions lib/coradoc/parser/asciidoc/term.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions lib/coradoc/parser/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
require_relative "asciidoc/paragraph"
require_relative "asciidoc/section"
require_relative "asciidoc/table"
require_relative "asciidoc/term"

module Coradoc
module Parser
Expand All @@ -28,13 +29,15 @@ 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
(
# bibliography |
admonition_line |
bib_entry |
term | term2 |
citation |
# attribute_list.as(:attribute_list) |
comment_block |
Expand All @@ -44,6 +47,7 @@ class Base < Parslet::Parser
document_attributes |
section.as(:section) |
list |

paragraph |
header.as(:header) |
empty_line.as(:line_break) |
Expand Down
10 changes: 10 additions & 0 deletions lib/coradoc/reverse_adoc/cleaner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 16 additions & 1 deletion lib/coradoc/transformer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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}
Expand Down
9 changes: 5 additions & 4 deletions utils/round_trip.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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..."
Expand All @@ -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;

0 comments on commit 387fd04

Please sign in to comment.