From a6273d77ebfa47b5ffa79212f4b3f4340515da1c Mon Sep 17 00:00:00 2001 From: hmdne <54514036+hmdne@users.noreply.github.com> Date: Wed, 18 Sep 2024 10:11:07 +0200 Subject: [PATCH] converters/h: Work around an issue and clean up the code Ref: #128 --- lib/coradoc/input/html/converters/base.rb | 12 ++++++------ lib/coradoc/input/html/converters/h.rb | 12 +++++++----- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/lib/coradoc/input/html/converters/base.rb b/lib/coradoc/input/html/converters/base.rb index 7dee25e..b361091 100644 --- a/lib/coradoc/input/html/converters/base.rb +++ b/lib/coradoc/input/html/converters/base.rb @@ -10,9 +10,9 @@ def convert(node, state = {}) # Note: treat_children won't run plugin hooks def treat_children(node, state) - node.children.inject("") do |memo, child| - memo << treat(child, state) - end + node.children.map do |child| + treat(child, state) + end.join end def treat(node, state) @@ -20,9 +20,9 @@ def treat(node, state) end def treat_children_coradoc(node, state) - node.children.inject([]) do |memo, child| - memo << treat_coradoc(child, state) - end.flatten.reject { |x| x == "" || x.nil? } + node.children.map do |child| + treat_coradoc(child, state) + end.flatten.reject { |x| x.to_s.empty? } end def treat_coradoc(node, state) diff --git a/lib/coradoc/input/html/converters/h.rb b/lib/coradoc/input/html/converters/h.rb index 583a771..a78f228 100644 --- a/lib/coradoc/input/html/converters/h.rb +++ b/lib/coradoc/input/html/converters/h.rb @@ -6,7 +6,9 @@ def to_coradoc(node, state = {}) internal_anchor = treat_children_anchors(node, state) if id.to_s.empty? && internal_anchor.size.positive? - id = internal_anchor.first.id + if internal_anchor.first.respond_to? :id + id = internal_anchor.first.id + end end level = node.name[/\d/].to_i @@ -16,14 +18,14 @@ def to_coradoc(node, state = {}) end def treat_children_no_anchors(node, state) - node.children.reject { |a| a.name == "a" }.inject([]) do |memo, child| - memo << treat_coradoc(child, state) + node.children.reject { |a| a.name == "a" }.map do |child| + treat_coradoc(child, state) end end def treat_children_anchors(node, state) - node.children.select { |a| a.name == "a" }.inject([]) do |memo, child| - memo << treat_coradoc(child, state) + node.children.select { |a| a.name == "a" }.map do |child| + treat_coradoc(child, state) end end end