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

HTML document work #61

Merged
merged 44 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
1cf71fd
Fixup: coradoc/reverse_adoc
hmdne May 23, 2024
cee010c
Don't die on empty images
hmdne May 23, 2024
3895b1e
Ensure URI is loaded
hmdne May 23, 2024
3cfb2df
Output a correct extension for SVG files
hmdne May 23, 2024
52a5336
Skip empty images - those are used most often for CSS flow etc.
hmdne May 23, 2024
2eee88f
Ensure asciidoc markup for a table cell if it contains images
hmdne May 23, 2024
6c4a059
Set column number if we are not able to provide good syntax
hmdne May 23, 2024
cd84879
Don't add a space before link if constrained.
hmdne May 23, 2024
bdd1dab
Refactor config, add time tracking facility, optimize cleaner
hmdne May 24, 2024
2b55df9
img extraction tempfile - Fix race condition
hmdne May 24, 2024
4e40b00
For adoccell, only consider images that have SRC
hmdne May 24, 2024
1ee70bc
Simplify table cells that contain DIVs
hmdne May 24, 2024
a9f838a
Change logic of generating one-line table rows
hmdne May 24, 2024
b0a5059
Make simplification more aggressive
hmdne May 24, 2024
26d1232
More verbose name for simplification method
hmdne May 24, 2024
3ddfbc1
Ignore image w/h if given with %
hmdne May 24, 2024
f398fdb
Ensure row/column table integrity
hmdne May 24, 2024
c379532
Add a facility to do custom processing
hmdne May 24, 2024
dafb56c
Table display fixup
hmdne May 24, 2024
d75cd54
Link: If name==href, skip name
hmdne May 24, 2024
7ed5b4c
Image: Uncomment a line adding a title
hmdne May 24, 2024
b2e8b96
Plateau: Generate captions for tables and images
hmdne May 24, 2024
4588b6a
Plateau: Headings: Go 1 level down
hmdne May 24, 2024
dd0ad46
Link: Fix the issue with [] being appended to links
hmdne May 25, 2024
fa62615
Plugin architecture: Refactor from processors
hmdne May 25, 2024
432149b
Title: Add style
hmdne May 25, 2024
9c7b545
Plugin architecture: Add hooks; Plateau: use hooks to fix titles
hmdne May 25, 2024
4394ffe
Table: Compute column widths
hmdne May 25, 2024
8d13aa9
Plateau: Align cells to center if they have appropriate CSS class
hmdne May 25, 2024
63c93f3
Plateau: Handle non-semantic lists
hmdne May 25, 2024
67d20a7
Correct table column size computation
hmdne May 25, 2024
1283734
Tests: Add Plugin
hmdne May 25, 2024
29502c5
Tests: Table computation
hmdne May 26, 2024
7c2e301
Tests: Test a href==text case
hmdne May 26, 2024
9fa7e51
Introduce visitor pattern to CoraDoc nodes; generate a section tree
hmdne May 26, 2024
02de9ea
Plugin system: finalize the implementation
hmdne May 27, 2024
83ee91a
Plateau: Correctly handle indentation
hmdne May 27, 2024
e208c72
Move text processing to Coradoc
hmdne May 27, 2024
7550de2
Text: Don't escape things like << ABC >>, because \ is passed thru
hmdne May 27, 2024
0546576
Plateau: Correct a problem after Text node change
hmdne May 27, 2024
0c76c6c
More robust handling for link constrainment
hmdne May 27, 2024
3494176
Fix the " +\n" issue
hmdne May 27, 2024
ed667b2
Section splitting support
hmdne May 27, 2024
d8963e8
Tests: Section splitting
hmdne May 27, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions exe/reverse_adoc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ OptionParser.new do |opts|
Coradoc::ReverseAdoc.config.unknown_tags = v
end

opts.on("-r", "--require RUBYMODULE", "Require additional Ruby file") do |v|
require v
end

opts.on("--track-time", "Track time spent on each step") do
Coradoc::ReverseAdoc.config.track_time = true
end

opts.on("--split-sections LEVEL", "Split sections up to LEVEL") do |i|
Coradoc::ReverseAdoc.config.split_sections = i.to_i
end

opts.on("-v", "--version", "Version information") do |_v|
puts "reverse_adoc: v#{Coradoc::ReverseAdoc::VERSION}"
exit
Expand All @@ -54,6 +66,10 @@ if Coradoc::ReverseAdoc.config.external_images && Coradoc::ReverseAdoc.config.de
raise "The -e | --external-images feature must be used with -o | --output. Exiting."
end

if Coradoc::ReverseAdoc.config.split_sections && Coradoc::ReverseAdoc.config.destination.nil?
raise "The --split_sections feature must be used with -o | --output. Exiting."
end

# Read from STDIN
adoc_content = Coradoc::ReverseAdoc.convert(input_content)

Expand All @@ -64,7 +80,12 @@ unless Coradoc::ReverseAdoc.config.destination
end

# Write output to Coradoc::ReverseAdoc.config.destination
FileUtils.mkdir_p(File.dirname(Coradoc::ReverseAdoc.config.destination))
File.open(Coradoc::ReverseAdoc.config.destination, "w") do |file|
file.write(adoc_content)
adoc_content = {nil => adoc_content} unless adoc_content.is_a? Hash

adoc_content.each do |file, content|
destination = Coradoc::ReverseAdoc.config.destination
destdir = File.dirname(destination)
filename = file ? "#{destdir}/#{file}" : destination
FileUtils.mkdir_p(File.dirname(filename))
File.write(filename, content)
end
1 change: 1 addition & 0 deletions lib/coradoc/document.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require_relative "element/base"
require_relative "element/title"
require_relative "element/block"
require_relative "element/section"
Expand Down
4 changes: 2 additions & 2 deletions lib/coradoc/element/admonition.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Coradoc
module Element
class Admonition
attr_reader :type, :content, :line_break
class Admonition < Base
attr_accessor :type, :content, :line_break

def initialize(content, type, options = {})
@content = content
Expand Down
4 changes: 2 additions & 2 deletions lib/coradoc/element/attribute.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Coradoc
module Element
class Attribute
attr_reader :key, :value
class Attribute < Base
attr_accessor :key, :value

def initialize(key, value, _options = {})
@key = key.to_s
Expand Down
6 changes: 4 additions & 2 deletions lib/coradoc/element/attribute_list.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
module Coradoc
module Element
class AttributeList
attr_reader :positional, :named, :rejected_positional, :rejected_named
class AttributeList < Base
attr_accessor :positional, :named, :rejected_positional, :rejected_named

declare_children :positional, :named

def initialize(*positional, **named)
@positional = positional || []
Expand Down
6 changes: 4 additions & 2 deletions lib/coradoc/element/audio.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
module Coradoc
module Element
class Audio
attr_reader :id, :title, :src, :options, :anchor
class Audio < Base
attr_accessor :id, :title, :src, :options, :anchor, :attributes

declare_children :id, :title, :anchor, :attributes

def initialize(title, options = {})
@title = title
Expand Down
6 changes: 4 additions & 2 deletions lib/coradoc/element/author.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
module Coradoc
module Element
class Author
attr_reader :email, :last_name, :first_name
class Author < Base
attr_accessor :email, :last_name, :first_name

declare_children :email, :last_name, :first_name

def initialize(first_name, last_name, email, middle_name = nil)
@first_name = first_name
Expand Down
77 changes: 70 additions & 7 deletions lib/coradoc/element/base.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,78 @@
module Coradoc
module Element
class Base
# attr_reader :document_attributes
# The idea here, is that HTML content generators may often introduce
# a lot of unnecessary markup, that only makes sense in the HTML+CSS
# context. The idea is that certain cases can be simplified, making it
# so that the result is equivalent, but much simpler, allowing us to
# generate a nicer AsciiDoc syntax for those cases.
def simplify_block_content(content)
content = Array(content)
collected_content = []
content.each do |i|
case i
when Coradoc::Element::Section
return content unless i.safe_to_collapse?

# def initialize(asciidoc)
# @document_attributes = extract_document_attributes(asciidoc)
# end
simplified = simplify_block_content(i.contents)

# def extract_document_attributes(asciidoc)
# @document_attributes ||= DocumentAttributes.new(asciidoc.attributes)
# end
if simplified && !simplified.empty?
collected_content << simplified
end
else
collected_content << i
end
end

collected_content = collected_content.compact

# We can safely do this optimization only if there's just one other
# element inside this structure.
if collected_content.length <= 1
collected_content
else
content
end
end

def self.declare_children(*children)
@children = (@children || []).dup + children
end

def self.visit(element, &block)
element = yield element, :pre
element = if element.respond_to? :visit
element.visit(&block)
elsif element.is_a? Array
element.map { |child| visit(child, &block) }.flatten.compact
elsif element.is_a? Hash
element.to_h do |k, v|
[visit(k, &block), visit(v, &block)]
end
else
element
end
yield element, :post
end

def self.children_accessors
@children || []
end

def children_accessors
self.class.children_accessors
end

def visit(&block)
children_accessors.each do |accessor|
child = public_send(accessor)
result = self.class.visit(child, &block)
if result != child
public_send(:"#{accessor}=", result)
end
end
self
end
end
end
end
6 changes: 4 additions & 2 deletions lib/coradoc/element/block/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
module Coradoc
module Element
module Block
class Core
attr_reader :title, :lines, :attributes, :lang, :id
class Core < Base
attr_accessor :title, :lines, :attributes, :lang, :id

declare_children :title, :lines, :attributes, :lang, :id

def initialize(title, options = {})
@title = title
Expand Down
2 changes: 1 addition & 1 deletion lib/coradoc/element/break.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Coradoc
module Element
module Break
class ThematicBreak
class ThematicBreak < Base
def to_adoc
"\n* * *\n"
end
Expand Down
6 changes: 4 additions & 2 deletions lib/coradoc/element/document_attributes.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
module Coradoc
module Element
class DocumentAttributes
attr_reader :data
class DocumentAttributes < Base
attr_accessor :data

declare_children :data

def initialize(data = {}, options = {})
@data = data
Expand Down
6 changes: 4 additions & 2 deletions lib/coradoc/element/header.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
module Coradoc
module Element
class Header
attr_reader :title, :author, :revision
class Header < Base
attr_accessor :title, :author, :revision

declare_children :title

def initialize(title, options = {})
@title = title
Expand Down
4 changes: 2 additions & 2 deletions lib/coradoc/element/image/block_image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ module Coradoc
module Element
module Image
class BlockImage < Core
def initialize(title, id, src, options = ())
super(title, id, src, options)
def initialize(title, id, src, options = {})
super
@colons = "::"
end

Expand Down
8 changes: 5 additions & 3 deletions lib/coradoc/element/image/core.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
module Coradoc
module Element
module Image
class Core
attr_reader :title, :id, :src, :attributes
class Core < Base
attr_accessor :title, :id, :src, :attributes

def initialize(title, id, src, options = ())
declare_children :id, :src, :title, :attributes

def initialize(title, id, src, options = {})
@title = title
@id = id
@anchor = @id.nil? ? nil : Coradoc::Element::Inline::Anchor.new(@id)
Expand Down
4 changes: 2 additions & 2 deletions lib/coradoc/element/image/inline_image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ module Coradoc
module Element
module Image
class InlineImage < Core
def initialize(title, id, src, options = ())
super(title, id, src, options)
def initialize(title, id, src, options = {})
super
@colons = ":"
end
end
Expand Down
6 changes: 4 additions & 2 deletions lib/coradoc/element/inline/anchor.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
module Coradoc
module Element
module Inline
class Anchor
attr_reader :id
class Anchor < Base
attr_accessor :id

declare_children :id

def initialize(id)
@id = id
Expand Down
4 changes: 3 additions & 1 deletion lib/coradoc/element/inline/bold.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
module Coradoc
module Element
module Inline
class Bold
class Bold < Base
attr_accessor :content, :unconstrained

declare_children :content

def initialize(content, unconstrained: true)
@content = content
@unconstrained = unconstrained
Expand Down
6 changes: 4 additions & 2 deletions lib/coradoc/element/inline/cross_reference.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
module Coradoc
module Element
module Inline
class CrossReference
attr_reader :href, :name
class CrossReference < Base
attr_accessor :href, :name

declare_children :href, :name

def initialize(href, name = nil)
@href = href
Expand Down
2 changes: 1 addition & 1 deletion lib/coradoc/element/inline/hard_line_break.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Coradoc
module Element
module Inline
class HardLineBreak
class HardLineBreak < Base
def to_adoc
" +\n"
end
Expand Down
4 changes: 3 additions & 1 deletion lib/coradoc/element/inline/highlight.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
module Coradoc
module Element
module Inline
class Highlight
class Highlight < Base
attr_accessor :content, :unconstrained

declare_children :content

def initialize(content, unconstrained: true)
@content = content
@unconstrained = unconstrained
Expand Down
26 changes: 0 additions & 26 deletions lib/coradoc/element/inline/image.rb

This file was deleted.

4 changes: 3 additions & 1 deletion lib/coradoc/element/inline/italic.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
module Coradoc
module Element
module Inline
class Italic
class Italic < Base
attr_accessor :content, :unconstrained

declare_children :content

def initialize(content, unconstrained: true)
@content = content
@unconstrained = unconstrained
Expand Down
Loading
Loading