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

ISO-22726-1 compatibility fixes #135

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
14 changes: 13 additions & 1 deletion lib/coradoc/element/attribute_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ def initialize(*positional, **named)
@rejected_named = []
end

def inspect
"AttributeList: " +
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self.class.name?

[
@positional.map(&:inspect).join(", "),
@named.map { |k, v| "#{k}: #{v.inspect}" }.join(", "),
(@rejected_positional.empty? or "rejected: #{@rejected_positional.inspect}"),
(@rejected_positional.empty? or "rejected: #{@rejected_named.inspect}"),
].reject { |i| i == true || i.empty? }.join(", ")
end

def add_positional(*attr)
@positional += attr
end
Expand Down Expand Up @@ -65,7 +75,9 @@ def to_adoc(show_empty = true)

adoc = +""
if [email protected]?
adoc << @positional.map { |p| [nil, ""].include?(p) ? '""' : p }.join(",")
adoc << @positional.map do |p|
[nil, ""].include?(p) ? '""' : p
end.join(",")
end
adoc << "," if @positional.any? && @named.any?
adoc << @named.map do |k, v|
Expand Down
2 changes: 2 additions & 0 deletions lib/coradoc/element/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ def simplify_block_content(content)
when Coradoc::Element::Section
return content unless i.safe_to_collapse?

collected_content << i.anchor if i.anchor

simplified = simplify_block_content(i.contents)

if simplified && !simplified.empty?
Expand Down
4 changes: 2 additions & 2 deletions lib/coradoc/element/section.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Coradoc
module Element
class Section < Base
attr_accessor :id, :title, :attrs, :contents, :sections
attr_accessor :id, :title, :attrs, :contents, :sections, :anchor

declare_children :id, :title, :contents, :sections

Expand Down Expand Up @@ -49,7 +49,7 @@ def to_adoc
# HTML element and if it happens inside some other block element, can be
# safely collapsed.
def safe_to_collapse?
@title.nil? && @id.nil? && @sections.empty?
@title.nil? && @sections.empty?
end

private
Expand Down
9 changes: 9 additions & 0 deletions lib/coradoc/element/text_element.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ def initialize(content, options = {})
end
end

def inspect
str = "TextElement"
str += "(#{@id})" if @id
str += ": "
str += @content.inspect
str += " + #{@line_break.inspect}" unless line_break.empty?
str
end

def to_adoc
Coradoc::Generator.gen_adoc(@content) + @line_break
end
Expand Down
1 change: 1 addition & 0 deletions lib/coradoc/input/html/converters/div.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ def to_coradoc(node, state = {})

register :div, Div.new
register :article, Div.new
register :center, Div.new
end
end
92 changes: 77 additions & 15 deletions lib/coradoc/input/html/postprocessor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ module Coradoc::Input::HTML
# is compatible with what we would get out of Coradoc, if
# it parsed it directly.
class Postprocessor
Element = Coradoc::Element

def self.process(coradoc)
new(coradoc).process
end
Expand All @@ -12,17 +14,74 @@ def initialize(coradoc)
@tree = coradoc
end

# Extracts titles from lists. This happens in HTML files
# generated from DOCX documents by LibreOffice.
#
# We are interested in a particular tree:
# Element::List::Ordered items:
# Element::List::Ordered items: (any depth)
# Element::ListItem content:
# Element::Title
# (any number of other titles of the same scheme)
#
# This tree is flattened into:
# Element::Title
# Element::Title (any number of titles)
def extract_titles_from_lists
@tree = Element::Base.visit(@tree) do |elem, dir|
next elem unless dir == :pre
next elem unless elem.is_a?(Element::List::Ordered)
next elem if elem.items.length != 1

anchors = []
anchors << elem.anchor if elem.anchor

# Extract ListItem from any depth of List::Ordered
processed = elem
while processed.is_a?(Element::List::Ordered)
if processed.items.length != 1
backtrack = true
break
end
anchors << processed.anchor if processed.anchor
processed = processed.items.first
end

# Something went wrong? Anything not matching on the way?
next elem if backtrack
next elem unless processed.is_a?(Element::ListItem)

anchors << processed.anchor if processed.anchor

# Now we must have a title (or titles).
titles = processed.content.flatten

# Don't bother if there's no title in there.
next elem unless titles.any? { |i| i.is_a? Element::Title }

# Ordered is another iteration for our cleanup.
next elem unless titles.all? do |i|
i.is_a?(Element::Title) || i.is_a?(Element::List::Ordered)
end

# We are done now.
titles + anchors
end
end

# Collapse DIVs that only have a title, or nest another DIV.
def collapse_meaningless_sections
@tree = Coradoc::Element::Base.visit(@tree) do |elem, _dir|
if elem.is_a?(Coradoc::Element::Section) && elem.safe_to_collapse?
@tree = Element::Base.visit(@tree) do |elem, _dir|
if elem.is_a?(Element::Section) && elem.safe_to_collapse?
children_classes = Array(elem.contents).map(&:class)
count = children_classes.length
safe_classes = [Coradoc::Element::Section, Coradoc::Element::Title]
safe_classes = [Element::Section, Element::Title]

# Count > 0 because some documents use <div> as a <br>.
if count > 0 && children_classes.all? { |i| safe_classes.include?(i) }
next elem.contents
contents = elem.contents.dup
contents.prepend(elem.anchor) if elem.anchor
next contents
end
end
elem
Expand All @@ -32,12 +91,14 @@ def collapse_meaningless_sections
# tree should now be more cleaned up, so we can progress with
# creating meaningful sections
def generate_meaningful_sections
@tree = Coradoc::Element::Base.visit(@tree) do |elem, dir|
@tree = Element::Base.visit(@tree) do |elem, dir|
# We are searching for an array, that has a title. This
# will be a candidate for our section array.
if dir == :post &&
elem.is_a?(Array) &&
!elem.grep(Coradoc::Element::Title).empty?
!elem.flatten.grep(Element::Title).empty?

elem = elem.flatten

new_array = []
content_array = new_array
Expand All @@ -47,12 +108,12 @@ def generate_meaningful_sections
# all descendant sections into those sections. Otherwise, we push
# an element as content of current section.
elem.each do |e|
if e.is_a? Coradoc::Element::Title
if e.is_a? Element::Title
title = e
content_array = []
section_array = []
level = title.level_int
section = Coradoc::Element::Section.new(
section = Element::Section.new(
title, contents: content_array, sections: section_array
)
# Some documents may not be consistent and eg. follow H4 after
Expand Down Expand Up @@ -82,11 +143,11 @@ def split_sections
previous_sections = {}

determine_section_id = ->(elem) do
if elem.title.style == "appendix"
level = "A"
else
level = 1
end
level = if elem.title.style == "appendix"
"A"
else
1
end

section = previous_sections[elem]
while section
Expand All @@ -102,8 +163,8 @@ def split_sections
style
end

@tree = Coradoc::Element::Base.visit(@tree) do |elem, dir|
title = elem.title if elem.is_a?(Coradoc::Element::Section)
@tree = Element::Base.visit(@tree) do |elem, dir|
title = elem.title if elem.is_a?(Element::Section)

if title && title.level_int <= max_level
if dir == :pre
Expand Down Expand Up @@ -137,6 +198,7 @@ def split_sections
end

def process
extract_titles_from_lists
collapse_meaningless_sections
generate_meaningful_sections
# Do it again to simplify the document further.
Expand Down
Loading