Skip to content

Commit

Permalink
inline classes, #to_adoc, consistency renames
Browse files Browse the repository at this point in the history
- metanorma#31 added classes for inline elements
- metanorma#10 added #to_adoc to Coradoc::Document classes based upon reverse_adoc code
- renamed numbered/unnumbered to ordered/unordered for consistency with metanorma/asciidoc
  • Loading branch information
xyz65535 committed Apr 17, 2024
1 parent e8bde77 commit 38bff32
Show file tree
Hide file tree
Showing 34 changed files with 490 additions and 62 deletions.
1 change: 1 addition & 0 deletions lib/coradoc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

require "coradoc/parser"
require "coradoc/transformer"
require "coradoc/generator"

module Coradoc
class Error < StandardError; end
Expand Down
5 changes: 5 additions & 0 deletions lib/coradoc/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
require "coradoc/document/table"
require "coradoc/document/list"

require "coradoc/document/inline"
require "coradoc/document/audio"
require "coradoc/document/video"
require "coradoc/document/break"

module Coradoc
module Document
class << self
Expand Down
26 changes: 26 additions & 0 deletions lib/coradoc/document/audio.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module Coradoc
module Document
class Audio
attr_reader :id, :title, :src, :options

def initialize(title, options = {})
@title = title
@id = options.fetch(:id, nil)
@src = options.fetch(:src, '')
@options = options.fetch(:options, [])
end

def to_adoc
anchor = @id ? "[[#{@id}]]\n" : ""
title = ".#{@title}\n" unless @title.empty?

opts = ""
if @options.any?
opts = %{options="#{@options.join(',')}"}
end

[anchor, title, "audio::", @src, "[", opts, "]"].join("")
end
end
end
end
24 changes: 23 additions & 1 deletion lib/coradoc/document/block.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,41 @@
module Coradoc
module Document
class Block
attr_reader :title, :lines, :attributes
attr_reader :title, :lines, :attributes, :lang, :id

def initialize(title, options = {})
@title = title
@lines = options.fetch(:lines, [])
@type_str = options.fetch(:type, nil)
@delimiter = options.fetch(:delimiter, "")
@attributes = options.fetch(:attributes, {})
@lang = options.fetch(:lang, nil)
@id = options.fetch(:id, nil)
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" : ""
title = ".#{@title}\n" unless @title.empty?
"\n\n#{anchor}#{title}====\n" << lines << "\n====\n\n"
end
end

private

def defined_type
Expand All @@ -31,6 +52,7 @@ def type_hash
"****" => :side,
"----" => :source,
"====" => :example,
"...." => :literal
}
end
end
Expand Down
11 changes: 11 additions & 0 deletions lib/coradoc/document/break.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Coradoc
module Document
module Break
class ThematicBreak
def to_adoc
"\n* * *\n"
end
end
end
end
end
4 changes: 4 additions & 0 deletions lib/coradoc/document/header.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,9 @@ def initialize(title, options = {})
@author = options.fetch(:author, nil)
@revision = options.fetch(:revision, nil)
end

def to_adoc
"= #{title}\n:stem:\n\n"
end
end
end
19 changes: 19 additions & 0 deletions lib/coradoc/document/inline.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require "coradoc/document/inline/anchor"
require "coradoc/document/inline/bold"
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/monospace"
require "coradoc/document/inline/quotation"
require "coradoc/document/inline/subscript"
require "coradoc/document/inline/superscript"

module Coradoc
module Document
module Inline
class << self
end
end
end
end
39 changes: 39 additions & 0 deletions lib/coradoc/document/inline/anchor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
module Coradoc
module Document
module Inline
class Anchor
attr_reader :id, :href, :title, :name

def initialize(options = {})
@id = options.fetch(:id,nil)
@href = options.fetch(:href,nil)
@title = options.fetch(:title, nil)
@name = options.fetch(:name,nil)
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
end
end
end
end
end
16 changes: 16 additions & 0 deletions lib/coradoc/document/inline/bold.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module Coradoc
module Document
module Inline
class Bold
attr_accessor :content
def initialize(content)
@content = content
end
def to_adoc
content = Coradoc::Generator.gen_adoc(@content)
"#{content[/^\s*/]}*#{content.strip}*#{content[/\s*$/]}"
end
end
end
end
end
11 changes: 11 additions & 0 deletions lib/coradoc/document/inline/hard_line_break.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Coradoc
module Document
module Inline
class HardLineBreak
def to_adoc
" +\n"
end
end
end
end
end
16 changes: 16 additions & 0 deletions lib/coradoc/document/inline/highlight.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module Coradoc
module Document
module Inline
class Highlight
attr_accessor :content
def initialize(content)
@content = content
end
def to_adoc
content = Coradoc::Generator.gen_adoc(@content)
"#{content[/^\s*/]}##{content.strip}##{content[/\s*$/]}"
end
end
end
end
end
26 changes: 26 additions & 0 deletions lib/coradoc/document/inline/image.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module Coradoc
module Document
module Inline
class Image
attr_reader :id, :title, :src, :alt, :width, :height
def initialize(options = ())
@id = options.fetch(:id, nil)
@title = options.fetch(:title, nil)
@src = options.fetch(:src, nil)
@alt = options.fetch(:alt, nil)
@width = options.fetch(:width, nil)
@height = options.fetch(:height, nil)
end
def to_adoc
anchor = @id ? "[[#{@id}]]\n" : ""
title = ".#{@title}\n" unless @title.empty?
attrs = @alt
attrs = "\"\"" if (@width || @height) && @alt.nil?
attrs += ",#{@width}" if @width
attrs += ",#{@height}" if @width && @height
[anchor, title, "image::", src, "[", attrs, "]"].join("")
end
end
end
end
end
16 changes: 16 additions & 0 deletions lib/coradoc/document/inline/italic.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module Coradoc
module Document
module Inline
class Italic
attr_accessor :content
def initialize(content)
@content = content
end
def to_adoc
content = Coradoc::Generator.gen_adoc(@content)
"#{content[/^\s*/]}_#{content.strip}_#{content[/\s*$/]}"
end
end
end
end
end
16 changes: 16 additions & 0 deletions lib/coradoc/document/inline/monospace.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module Coradoc
module Document
module Inline
class Monospace
attr_accessor :content
def initialize(content)
@content = content
end
def to_adoc
content = Coradoc::Generator.gen_adoc(@content)
"`#{content}`"
end
end
end
end
end
16 changes: 16 additions & 0 deletions lib/coradoc/document/inline/quotation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module Coradoc
module Document
module Inline
class Quotation
attr_accessor :content
def initialize(content)
@content = content
end
def to_adoc
content = Coradoc::Generator.gen_adoc(@content)
"#{content[/^\s*/]}\"#{content.strip}\"#{content[/\s*$/]}"
end
end
end
end
end
17 changes: 17 additions & 0 deletions lib/coradoc/document/inline/subscript.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module Coradoc
module Document
module Inline
class Subscript
attr_accessor :content
def initialize(content)
@content = content
end

def to_adoc
content = Coradoc::Generator.gen_adoc(@content)
"#{content[/^\s*/]}~#{content.strip}~#{content[/\s*$/]}"
end
end
end
end
end
16 changes: 16 additions & 0 deletions lib/coradoc/document/inline/superscript.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module Coradoc
module Document
module Inline
class Superscript
attr_accessor :content
def initialize(content)
@content = content
end
def to_adoc
content = Coradoc::Generator.gen_adoc(@content)
"#{content[/^\s*/]}^#{content.strip}^#{content[/\s*$/]}"
end
end
end
end
end
42 changes: 39 additions & 3 deletions lib/coradoc/document/list.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,49 @@
module Coradoc
module Document
class List
attr_reader :items
attr_reader :items, :prefix, :id, :ol_count

def initialize(items)
def initialize(items, options = {})
@items = items
@items = [@items] unless @items.is_a?(Array)
@id = options.fetch(:id, nil)
@ol_count = options.fetch(:ol_count, 0)
@anchor = options.fetch(:anchor, nil)
@attrs = options.fetch(:attrs, nil)
end

class Unnumbered < List
def to_adoc
content = ""
@items.each do |item|
c = Coradoc::Generator.gen_adoc(item)
if !c.empty?
content << "#{prefix}"
content << c
end
end
"\n#{@anchor}#{@attrs}" + content
end


def prefix
"." * [@ol_count, 0].max
end

class Unordered < List
def prefix
"*" * [@ol_count, 0].max
end
end

class Item
def initialize(content, options = {})
@content = content
@anchor = options.fetch(:anchor, '')
end
def to_adoc
content = Coradoc::Generator.gen_adoc(@content)
" #{@anchor}#{content.chomp}\n"
end
end
end
end
Expand Down

0 comments on commit 38bff32

Please sign in to comment.