From 5c6be7e92d2c22b2019e3c6f177ecd2dac3b30eb Mon Sep 17 00:00:00 2001 From: xyz65535 Date: Fri, 19 Apr 2024 07:25:05 +0200 Subject: [PATCH] reverse_adoc integration cont. --- lib/coradoc/document.rb | 1 + lib/coradoc/document/attribute_list.rb | 40 +++++++++ lib/coradoc/document/audio.rb | 5 +- lib/coradoc/document/author.rb | 15 +++- lib/coradoc/document/block.rb | 83 +++++++++++++++---- lib/coradoc/document/header.rb | 5 +- lib/coradoc/document/inline.rb | 2 + lib/coradoc/document/inline/anchor.rb | 30 +------ lib/coradoc/document/inline/bold.rb | 8 +- .../document/inline/cross_reference.rb | 22 +++++ lib/coradoc/document/inline/link.rb | 26 ++++++ lib/coradoc/document/list.rb | 28 +++++-- lib/coradoc/document/paragraph.rb | 7 +- lib/coradoc/document/revision.rb | 12 +++ lib/coradoc/document/table.rb | 14 ++-- lib/coradoc/document/title.rb | 15 ++-- spec/coradoc/document/attribute_list_spec.rb | 16 ++++ 17 files changed, 256 insertions(+), 73 deletions(-) create mode 100644 lib/coradoc/document/attribute_list.rb create mode 100644 lib/coradoc/document/inline/cross_reference.rb create mode 100644 lib/coradoc/document/inline/link.rb create mode 100644 spec/coradoc/document/attribute_list_spec.rb diff --git a/lib/coradoc/document.rb b/lib/coradoc/document.rb index 4a0a733..cdb7cf2 100644 --- a/lib/coradoc/document.rb +++ b/lib/coradoc/document.rb @@ -2,6 +2,7 @@ require "coradoc/document/block" require "coradoc/document/section" require "coradoc/document/attribute" +require "coradoc/document/attribute_list" require "coradoc/document/admonition" require "coradoc/document/text_element" require "coradoc/document/author" diff --git a/lib/coradoc/document/attribute_list.rb b/lib/coradoc/document/attribute_list.rb new file mode 100644 index 0000000..7fb1429 --- /dev/null +++ b/lib/coradoc/document/attribute_list.rb @@ -0,0 +1,40 @@ +module Coradoc + module Document + class AttributeList + attr_reader :positional, :named + + def initialize(*positional, **named) + @positional = positional || [] + @named = named || {} + end + + def add_positional(attr) + @positional << attr + end + + def add_named(name, value) + @named[name] = value + end + + def empty? + @positional.empty? && @named.empty? + end + + def to_adoc + adoc = "" + adoc << @positional.join(", ") if @positional.any? + adoc << ", " if @positional.any? && @named.any? + adoc << @named.map do |k, v| + v2 = v.to_s + v2 = v2.include?("\"") ? v2.gsub("\"","\\\"") : v2 + if v2.include?(" ") || v2.include?(",") || v2.include?("\"") + v2 = "\"#{v2}\"" + end + [k.to_s, "=", v2].join + end.join(", ") + adoc = "[#{adoc}]" if @positional.any? || @named.any? + adoc + end + end + end +end diff --git a/lib/coradoc/document/audio.rb b/lib/coradoc/document/audio.rb index 87f7266..7253101 100644 --- a/lib/coradoc/document/audio.rb +++ b/lib/coradoc/document/audio.rb @@ -1,17 +1,18 @@ module Coradoc module Document class Audio - attr_reader :id, :title, :src, :options + attr_reader :id, :title, :src, :options, :anchor def initialize(title, options = {}) @title = title @id = options.fetch(:id, nil) + @anchor = Inline::Anchor.new(@id) if @id @src = options.fetch(:src, '') @options = options.fetch(:options, []) end def to_adoc - anchor = @id ? "[[#{@id}]]\n" : "" + anchor = @anchor.nil? ? "" : "#{@anchor.to_adoc}\n" title = ".#{@title}\n" unless @title.empty? opts = "" diff --git a/lib/coradoc/document/author.rb b/lib/coradoc/document/author.rb index 197b9f0..4f8eb68 100644 --- a/lib/coradoc/document/author.rb +++ b/lib/coradoc/document/author.rb @@ -2,10 +2,19 @@ module Coradoc class Document::Author attr_reader :email, :last_name, :first_name - def initialize(first_name, last_name, email) - @email = email - @last_name = last_name + def initialize(first_name, last_name, email, middle_name = nil) @first_name = first_name + @last_name = last_name + @email = email + @middle_name = middle_name + end + + def to_adoc + adoc = "#{@first_name} " + adoc << "#{@middle_name} " if @middle_name + adoc << "#{@last_name}" + adoc << " <#{@email}>" if @email + adoc end end end diff --git a/lib/coradoc/document/block.rb b/lib/coradoc/document/block.rb index 8a9ff57..8476e69 100644 --- a/lib/coradoc/document/block.rb +++ b/lib/coradoc/document/block.rb @@ -11,31 +11,84 @@ def initialize(title, options = {}) @attributes = options.fetch(:attributes, {}) @lang = options.fetch(:lang, nil) @id = options.fetch(:id, nil) + @anchor = @id.nil? ? nil : Inline::Anchor.new(@id) end def type @type ||= defined_type || type_from_delimiter end - def to_adoc - lines = Coradoc::Generator.gen_adoc(@lines) - if type == :quote - "\n\n#{@attributes}____\n" << lines << "\n____\n\n" - elsif type == :source && @lang - anchor = @id ? "[[#{@id}]]\n" : "" - "\n\n#{anchor}[source,#{@lang}]\n----\n" << lines << "\n----\n\n" - elsif type == :literal - anchor = @id ? "[[#{@id}]]\n" : "" - "\n\n#{anchor}....\n" << lines << "\n....\n\n" - elsif type == :side - "\n\n****\n" << lines << "\n****\n\n" - elsif type == :example - anchor = @id ? "[[#{@id}]]\n" : "" + class Side < Block + def initialize(options = {}) + @lines = options.fetch(:lines, []) + end + + def to_adoc + lines = Coradoc::Generator.gen_adoc(@lines) + "\n\n****" << lines << "\n****\n\n" + end + end + + class Example < Block + def initialize(title, options = {}) + @title = title + @id = options.fetch(:id, nil) + @anchor = @id.nil? ? nil : Inline::Anchor.new(@id) + @lines = options.fetch(:lines, []) + end + + def to_adoc + anchor = @anchor.nil? ? "" : "#{@anchor.to_adoc}\n" title = ".#{@title}\n" unless @title.empty? + lines = Coradoc::Generator.gen_adoc(@lines) "\n\n#{anchor}#{title}====\n" << lines << "\n====\n\n" end end + class Quote < Block + def initialize(title, options = {}) + @title = title + @attributes = options.fetch(:attributes, nil) + @lines = options.fetch(:lines, []) + end + + def to_adoc + attrs = @attributes.nil? ? "" : "#{@attributes.to_adoc}\n" + lines = Coradoc::Generator.gen_adoc(@lines) + "\n\n#{attrs}____\n" << lines << "\n____\n\n" + end + end + + class Literal < Block + def initialize(title, options = {}) + @id = options.fetch(:id, nil) + @anchor = @id.nil? ? nil : Inline::Anchor.new(@id) + @lines = options.fetch(:lines, []) + end + + def to_adoc + anchor = @anchor.nil? ? "" : "#{@anchor.to_adoc}\n" + lines = Coradoc::Generator.gen_adoc(@lines) + "\n\n#{anchor}....\n" << lines << "\n....\n\n" + end + end + + class SourceCode < Block + def initialize(title, options = {}) + @id = options.fetch(:id, nil) + @anchor = @id.nil? ? nil : Inline::Anchor.new(@id) + @lang = options.fetch(:lang, '') + @lines = options.fetch(:lines, []) + # super(title, options.merge({type: :literal})) + end + + def to_adoc + anchor = @anchor.nil? ? "" : "#{@anchor.to_adoc}\n" + lines = Coradoc::Generator.gen_adoc(@lines) + "\n\n#{anchor}[source,#{@lang}]\n----\n" << lines << "\n----\n\n" + end + end + private def defined_type @@ -55,6 +108,8 @@ def type_hash "...." => :literal } end + + end end end diff --git a/lib/coradoc/document/header.rb b/lib/coradoc/document/header.rb index 5baa79f..5809e2f 100644 --- a/lib/coradoc/document/header.rb +++ b/lib/coradoc/document/header.rb @@ -9,7 +9,10 @@ def initialize(title, options = {}) end def to_adoc - "= #{title}\n:stem:\n\n" + adoc = "= #{title}\n" + adoc << @author.to_adoc if @author + adoc << @revision.to_adoc if @revision + adoc end end end diff --git a/lib/coradoc/document/inline.rb b/lib/coradoc/document/inline.rb index 9878987..2145856 100644 --- a/lib/coradoc/document/inline.rb +++ b/lib/coradoc/document/inline.rb @@ -1,9 +1,11 @@ require "coradoc/document/inline/anchor" require "coradoc/document/inline/bold" +require "coradoc/document/inline/cross_reference" require "coradoc/document/inline/hard_line_break" require "coradoc/document/inline/highlight" require "coradoc/document/inline/image" require "coradoc/document/inline/italic" +require "coradoc/document/inline/link" require "coradoc/document/inline/monospace" require "coradoc/document/inline/quotation" require "coradoc/document/inline/subscript" diff --git a/lib/coradoc/document/inline/anchor.rb b/lib/coradoc/document/inline/anchor.rb index e735e46..4b18826 100644 --- a/lib/coradoc/document/inline/anchor.rb +++ b/lib/coradoc/document/inline/anchor.rb @@ -2,36 +2,14 @@ module Coradoc module Document module Inline class Anchor - attr_reader :id, :href, :title, :name + attr_reader :id - def initialize(options = {}) - @id = options.fetch(:id,nil) - @href = options.fetch(:href,nil) - @title = options.fetch(:title, nil) - @name = options.fetch(:name,nil) + def initialize(id) + @id = id end def to_adoc - if /^_Toc\d+$|^_GoBack$/.match @id - "" - elsif !@id.nil? && !@id.empty? - "[[#{@id}]]" - elsif @href.to_s.start_with?('#') - @href = @href.sub(/^#/, "").gsub(/\s/, "").gsub(/__+/, "_") - if @name.to_s.empty? - "<<#{@href}>>" - else - "<<#{@href},#{@name}>>" - end - elsif @href.to_s.empty? - @name - else - @name = @title if @name.to_s.empty? - @href = "link:#{@href}" unless @href.to_s =~ URI::DEFAULT_PARSER.make_regexp - link = "#{@href}[#{@name}]" - link.prepend(' ') - link - end + "[[#{@id}]]" end end end diff --git a/lib/coradoc/document/inline/bold.rb b/lib/coradoc/document/inline/bold.rb index 73447a0..aba60e9 100644 --- a/lib/coradoc/document/inline/bold.rb +++ b/lib/coradoc/document/inline/bold.rb @@ -2,13 +2,15 @@ module Coradoc module Document module Inline class Bold - attr_accessor :content - def initialize(content) + attr_accessor :content, :constrained + def initialize(content, constrained = true) @content = content + @constrained = constrained end def to_adoc content = Coradoc::Generator.gen_adoc(@content) - "#{content[/^\s*/]}*#{content.strip}*#{content[/\s*$/]}" + second_char = @constrained ? "" : "*" + "#{content[/^\s*/]}#{second_char}*#{content.strip}*#{second_char}#{content[/\s*$/]}" end end end diff --git a/lib/coradoc/document/inline/cross_reference.rb b/lib/coradoc/document/inline/cross_reference.rb new file mode 100644 index 0000000..a84201d --- /dev/null +++ b/lib/coradoc/document/inline/cross_reference.rb @@ -0,0 +1,22 @@ +module Coradoc + module Document + module Inline + class CrossReference + attr_reader :href, :name + + def initialize(href, name = nil) + @href = href + @name = name + end + + def to_adoc + if @name.to_s.empty? + "<<#{@href}>>" + else + "<<#{@href},#{@name}>>" + end + end + end + end + end +end diff --git a/lib/coradoc/document/inline/link.rb b/lib/coradoc/document/inline/link.rb new file mode 100644 index 0000000..1e522eb --- /dev/null +++ b/lib/coradoc/document/inline/link.rb @@ -0,0 +1,26 @@ +module Coradoc + module Document + module Inline + class Link + attr_reader :path, :title, :name + + def initialize(options = {}) + @path = options.fetch(:path,nil) + @title = options.fetch(:title, nil) + @name = options.fetch(:name,nil) + end + + def to_adoc + link = @path.to_s =~ URI::DEFAULT_PARSER.make_regexp ? @path : "link:#{@path}" + if @name.to_s.empty? + link << "[#{@title}]" + else + link << "[#{@name}]" + end + link.prepend(' ') + link + end + end + end + end +end diff --git a/lib/coradoc/document/list.rb b/lib/coradoc/document/list.rb index 1668fdd..078cd9d 100644 --- a/lib/coradoc/document/list.rb +++ b/lib/coradoc/document/list.rb @@ -1,19 +1,19 @@ module Coradoc module Document class List - attr_reader :items, :prefix, :id, :ol_count + attr_reader :items, :prefix, :id, :ol_count, :anchor def initialize(items, options = {}) @items = items @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, 0) - @anchor = options.fetch(:anchor, nil) @attrs = options.fetch(:attrs, nil) end def to_adoc - content = "" + content = "\n" @items.each do |item| c = Coradoc::Generator.gen_adoc(item) if !c.empty? @@ -21,12 +21,16 @@ def to_adoc content << c end end - "\n#{@anchor}#{@attrs}" + content + anchor = @anchor.nil? ? "" : "#{@anchor.to_adoc}" + attrs = @attrs.nil? ? "" : "#{@attrs.to_adoc}" + "\n#{anchor}#{attrs}" + content end - def prefix - "." * [@ol_count, 0].max + class Ordered < List + def prefix + "." * [@ol_count, 0].max + end end class Unordered < List @@ -35,14 +39,20 @@ def prefix end end + class Definition < List + end + class Item + attr_reader :id def initialize(content, options = {}) @content = content - @anchor = options.fetch(:anchor, '') + @id = options.fetch(:id, nil) + @anchor = @id.nil? ? nil : Inline::Anchor.new(@id) end def to_adoc - content = Coradoc::Generator.gen_adoc(@content) - " #{@anchor}#{content.chomp}\n" + anchor = @anchor.nil? ? "" : "#{@anchor.to_adoc}" + content = Coradoc::Generator.gen_adoc(@content).chomp + " #{anchor}#{content.chomp}\n" end end end diff --git a/lib/coradoc/document/paragraph.rb b/lib/coradoc/document/paragraph.rb index 1364abd..18d5619 100644 --- a/lib/coradoc/document/paragraph.rb +++ b/lib/coradoc/document/paragraph.rb @@ -1,12 +1,13 @@ module Coradoc module Document class Paragraph - attr_reader :content, :id, :tdsinglepara + attr_reader :content, :anchor, :tdsinglepara def initialize(content, options = {}) @content = content @meta = options.fetch(:meta, nil) - @id = options.fetch(:id, nil) + id = options.fetch(:id, nil) + @anchor = Inline::Anchor.new(id) if id @tdsinglepara = options.fetch(:tdsinglepara, nil) end @@ -19,7 +20,7 @@ def texts end def to_adoc - anchor = @id ? "[[#{@id}]]\n" : "" + anchor = @anchor.nil? ? "" : "#{@anchor.to_adoc}\n" if @tdsinglepara "#{anchor}" << Coradoc::Generator.gen_adoc(@content).strip else diff --git a/lib/coradoc/document/revision.rb b/lib/coradoc/document/revision.rb index 74b1dc9..b42735d 100644 --- a/lib/coradoc/document/revision.rb +++ b/lib/coradoc/document/revision.rb @@ -7,5 +7,17 @@ def initialize(number, options = {}) @date = options.fetch(:date, nil) @remark = options.fetch(:remark, nil) end + + def to_adoc + if @date.nil? && @remark.nil? + "v#{@number}\n" + elsif @remark.nil? + "#{@number}, #{@date}\n" + elsif @date.nil? + "#{@number}: #{@remark}\n" + else + "#{@number}, #{@date}: #{@revision}\n" + end + end end end diff --git a/lib/coradoc/document/table.rb b/lib/coradoc/document/table.rb index c885749..90f9df4 100644 --- a/lib/coradoc/document/table.rb +++ b/lib/coradoc/document/table.rb @@ -7,15 +7,16 @@ def initialize(title, rows, options = {}) @rows = rows @title = title @id = options.fetch(:id, nil) + @anchor = @id.nil? ? nil : Inline::Anchor.new(@id) @attrs = options.fetch(:attrs, '') end def to_adoc - anchor = @id ? "[[#{@id}]]\n" : "" - attrs = @attrs - title = @title - content = Coradoc::Generator.gen_adoc(@rows) + anchor = @anchor.nil? ? "" : "#{@anchor.to_adoc}\n" + attrs = @attrs.to_s.empty? ? "" : "#{@attrs.to_adoc}\n" + title = @title.to_s.empty? ? "" : ".#{@title}\n" + content = @rows.map(&:to_adoc).join "\n\n#{anchor}#{attrs}#{title}|===\n" << content << "\n|===\n" end @@ -43,9 +44,10 @@ def underline_for end class Cell - attr_reader :id + attr_reader :anchor def initialize(options = {}) @id = options.fetch(:id, nil) + @anchor = @id.nil? ? nil : Inline::Anchor.new(@id) @colrowattr = options.fetch(:colrowattr, '') @alignattr = options.fetch(:alignattr, '') @style = options.fetch(:style, '') @@ -54,7 +56,7 @@ def initialize(options = {}) end def to_adoc - anchor = @id ? "[[#{@id}]]" : "" + anchor = @anchor.nil? ? "" : "#{@anchor.to_adoc}" content = Coradoc::Generator.gen_adoc(@content) "#{@colrowattr}#{@alignattr}#{@style}| #{anchor}#{content}#{@delim}" end diff --git a/lib/coradoc/document/title.rb b/lib/coradoc/document/title.rb index bfcd18f..20fafd0 100644 --- a/lib/coradoc/document/title.rb +++ b/lib/coradoc/document/title.rb @@ -4,20 +4,23 @@ class Title attr_reader :id, :content, :line_break def initialize(content, level, options = {}) - @level_str = level + @level_int = level + @level_int = level.length if level.is_a?(String) @content = content.to_s - @id = options.fetch(:id, nil).to_s + @id = options.fetch(:id, nil) + @anchor = @id.nil? ? nil : Inline::Anchor.new(@id) @line_break = options.fetch(:line_break, "") - @anchor = options.fetch(:anchor, nil) end def level - @level ||= level_from_string + @level_str ||= level_from_string end def to_adoc + anchor = @anchor.nil? ? "" : "#{@anchor.to_adoc}\n" content = Coradoc::Generator.gen_adoc(@content) - content = ["\n", @anchor, @level_str, ' ', content, "\n"].join("") + level_str = "=" * (@level_int + 1) + content = ["\n", anchor, level_str, ' ', content, "\n"].join("") Coradoc::Generator.gen_adoc(content) end @@ -28,7 +31,7 @@ def to_adoc attr_reader :level_str def level_from_string - case @level_str.length + case @level_int when 2 then :heading_two when 3 then :heading_three when 4 then :heading_four diff --git a/spec/coradoc/document/attribute_list_spec.rb b/spec/coradoc/document/attribute_list_spec.rb new file mode 100644 index 0000000..5000ab9 --- /dev/null +++ b/spec/coradoc/document/attribute_list_spec.rb @@ -0,0 +1,16 @@ +require "spec_helper" + +RSpec.describe Coradoc::Document::AttributeList do + describe ".initialize" do + it "initializes and exposes attributes" do + + positional = ['a','b','c'] + named = {'d':'e','f':'g'} + + attribute_list = Coradoc::Document::AttributeList.new('a','b','c','d':'e','f':'g') + + expect(attribute_list.positional).to eq(positional) + expect(attribute_list.named).to eq(named) + end + end +end