Skip to content

Commit

Permalink
reverse_adoc integration cont.
Browse files Browse the repository at this point in the history
  • Loading branch information
xyz65535 authored and ronaldtse committed Apr 23, 2024
1 parent bb941af commit 5c6be7e
Show file tree
Hide file tree
Showing 17 changed files with 256 additions and 73 deletions.
1 change: 1 addition & 0 deletions lib/coradoc/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
40 changes: 40 additions & 0 deletions lib/coradoc/document/attribute_list.rb
Original file line number Diff line number Diff line change
@@ -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
5 changes: 3 additions & 2 deletions lib/coradoc/document/audio.rb
Original file line number Diff line number Diff line change
@@ -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 = ""
Expand Down
15 changes: 12 additions & 3 deletions lib/coradoc/document/author.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
83 changes: 69 additions & 14 deletions lib/coradoc/document/block.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -55,6 +108,8 @@ def type_hash
"...." => :literal
}
end


end
end
end
5 changes: 4 additions & 1 deletion lib/coradoc/document/header.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions lib/coradoc/document/inline.rb
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
30 changes: 4 additions & 26 deletions lib/coradoc/document/inline/anchor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions lib/coradoc/document/inline/bold.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions lib/coradoc/document/inline/cross_reference.rb
Original file line number Diff line number Diff line change
@@ -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
26 changes: 26 additions & 0 deletions lib/coradoc/document/inline/link.rb
Original file line number Diff line number Diff line change
@@ -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
Loading

0 comments on commit 5c6be7e

Please sign in to comment.