Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parser work #117

Merged
merged 13 commits into from
Aug 21, 2024
2 changes: 2 additions & 0 deletions coradoc.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +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
5 changes: 3 additions & 2 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 Expand Up @@ -68,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
5 changes: 5 additions & 0 deletions lib/coradoc/element/admonition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ def initialize(content, type, options = {})
@type = type.downcase.to_sym
@line_break = options.fetch(:line_break, "")
end

def to_adoc
content = Coradoc::Generator.gen_adoc(@content)
"#{type.to_s.upcase}: #{content}#{@line_break}"
end
end
end
end
2 changes: 1 addition & 1 deletion lib/coradoc/element/attribute_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 6 additions & 4 deletions lib/coradoc/element/bibliography_entry.rb
Original file line number Diff line number Diff line change
@@ -1,19 +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 << "]]], #{@reference_text}"
adoc << "]]]"
adoc << "#{text}" if @ref_text
adoc << @line_break
adoc
end
Expand Down
2 changes: 2 additions & 0 deletions lib/coradoc/element/block.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +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"
10 changes: 6 additions & 4 deletions lib/coradoc/element/block/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -37,6 +37,7 @@ def gen_title
def gen_attributes
attrs = @attributes.to_adoc(false)
return "#{attrs}\n" if !attrs.empty?

""
end

Expand Down Expand Up @@ -65,6 +66,7 @@ def type_hash
"----" => :source,
"====" => :example,
"...." => :literal,
"++++" => :pass,
}
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/coradoc/element/block/example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
21 changes: 21 additions & 0 deletions lib/coradoc/element/block/pass.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module Coradoc
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 = "+"
@delimiter_len = options.fetch(:delimiter_len, 4)
@lines = options.fetch(:lines, [])
end

def to_adoc
"\n\n#{gen_anchor}#{gen_title}#{gen_attributes}#{gen_delimiter}\n" << gen_lines << "\n#{gen_delimiter}\n\n"
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/coradoc/element/block/quote.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions lib/coradoc/element/block/reviewer_comment.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions lib/coradoc/element/block/sourcecode.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions lib/coradoc/element/document_attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ def to_hash

def to_adoc
to_hash.map do |key, value|
":#{key}: #{value}\n"
end.join("\n") + "\n"
v = value.to_s.empty? ? "" : " #{value}"
":#{key}:#{v}\n"
end.join + "\n"
end
end
end
Expand Down
8 changes: 8 additions & 0 deletions lib/coradoc/element/image/block_image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ 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
[missing, anchor, title, "image", @colons, @src, attrs, @line_break].join("")
end

def validate_named
@attributes.validate_named(VALIDATORS_NAMED, VALIDATORS_NAMED_BLOCK)
end
Expand Down
11 changes: 6 additions & 5 deletions lib/coradoc/element/image/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,24 @@ 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)
@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_macro.to_adoc
[missing, anchor, title, "image", @colons, @src, attrs, @line_break].join("")
end

extend AttributeList::Matchers
Expand Down
3 changes: 1 addition & 2 deletions lib/coradoc/element/include.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ def initialize(path, options = {})
@line_break = options.fetch(:line_break, "\n")
end


def to_adoc
attrs = @attributes.to_adoc(true)
"include::#{@path}#{attrs}"
"include::#{@path}#{attrs}#{@line_break}"
end
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/coradoc/element/inline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
23 changes: 23 additions & 0 deletions lib/coradoc/element/inline/citation.rb
Original file line number Diff line number Diff line change
@@ -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
12 changes: 10 additions & 2 deletions lib/coradoc/element/list/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 do |i|
i.is_a?(Coradoc::Element::ListItem) &&
!i.marker.nil?
end.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

Expand All @@ -30,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
Expand Down
12 changes: 7 additions & 5 deletions lib/coradoc/element/list_item.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@
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, "\n")
end

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,
# 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
end.compact.join("\n+\n")

" #{anchor}#{content.chomp}\n"
" #{anchor}#{content.chomp}#{@line_break}"
end
end
end
Expand Down
9 changes: 6 additions & 3 deletions lib/coradoc/element/paragraph.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ class Paragraph < Base

def initialize(content, options = {})
@content = content
@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

Expand All @@ -22,11 +23,13 @@ def texts
end

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
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}#{attrs}" << Coradoc.strip_unicode(Coradoc::Generator.gen_adoc(@content)) << "\n\n"
end
end
end
Expand Down
Loading
Loading