Class: Coradoc::Input::Html::Converters::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/coradoc/input/html/converters/base.rb

Direct Known Subclasses

A, Aside, Audio, Blockquote, Br, Bypass, Div, Dl, Drop, Figure, H, Head, Hr, Ignore, Img, Li, Markup, Math, Ol, P, PassThrough, Pre, Q, Sub, Sup, Table, Td, Text, Tr, Video

Instance Method Summary collapse

Instance Method Details

#convert(node, state = {}) ⇒ Object

Default implementation to convert a given Nokogiri node to an AsciiDoc script. Can be overriden by subclasses.



9
10
11
# File 'lib/coradoc/input/html/converters/base.rb', line 9

def convert(node, state = {})
  Coradoc::Generator.gen_adoc(to_coradoc(node, state))
end

#extract_leading_trailing_whitespace(node) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/coradoc/input/html/converters/base.rb', line 72

def extract_leading_trailing_whitespace(node)
  node.text =~ /^(\s+)/
  leading_whitespace = $1
  if !leading_whitespace.nil?
    first_text = node.at_xpath("./text()[1]")
    first_text&.replace(first_text.text.lstrip)
    leading_whitespace = " "
  end
  node.text =~ /(\s+)$/
  trailing_whitespace = $1
  if !trailing_whitespace.nil?
    last_text = node.at_xpath("./text()[last()]")
    last_text&.replace(last_text.text.rstrip)
    trailing_whitespace = " "
  end
  [leading_whitespace, trailing_whitespace]
end

#extract_title(node) ⇒ Object



34
35
36
37
38
39
# File 'lib/coradoc/input/html/converters/base.rb', line 34

def extract_title(node)
  title = Coradoc::Element::TextElement.escape_keychars(
    node["title"].to_s,
  )
  title.empty? ? "" : %[ #{title}]
end

#node_has_ancestor?(node, name) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
44
45
46
47
48
# File 'lib/coradoc/input/html/converters/base.rb', line 41

def node_has_ancestor?(node, name)
  case name
  when String
    node.ancestors.map(&:name).include?(name)
  when Array
    (node.ancestors.map(&:name) & name).any?
  end
end

#textnode_after_start_with?(node, str) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
64
65
66
67
68
69
70
# File 'lib/coradoc/input/html/converters/base.rb', line 61

def textnode_after_start_with?(node, str)
  return nil unless [String, Regexp].include?(str.class)
  return nil if str.is_a?(String) && str.empty?

  str = /#{Regexp.escape(str)}/ if str.is_a?(String)
  str = /\A(?:#{str})/

  node2 = node.at_xpath("following-sibling::node()[1]")
  node2.respond_to?(:text) && node2.text.match?(str)
end

#textnode_before_end_with?(node, str) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
53
54
55
56
57
58
59
# File 'lib/coradoc/input/html/converters/base.rb', line 50

def textnode_before_end_with?(node, str)
  return nil unless [String, Regexp].include?(str.class)
  return nil if str.is_a?(String) && str.empty?

  str = /#{Regexp.escape(str)}/ if str.is_a?(String)
  str = /(?:#{str})\z/

  node2 = node.at_xpath("preceding-sibling::node()[1]")
  node2.respond_to?(:text) && node2.text.match?(str)
end

#treat(node, state) ⇒ Object



20
21
22
# File 'lib/coradoc/input/html/converters/base.rb', line 20

def treat(node, state)
  Converters.process(node, state)
end

#treat_children(node, state) ⇒ Object

Note: treat_children won’t run plugin hooks



14
15
16
17
18
# File 'lib/coradoc/input/html/converters/base.rb', line 14

def treat_children(node, state)
  node.children.map do |child|
    treat(child, state)
  end.join
end

#treat_children_coradoc(node, state) ⇒ Object



24
25
26
27
28
# File 'lib/coradoc/input/html/converters/base.rb', line 24

def treat_children_coradoc(node, state)
  node.children.map do |child|
    treat_coradoc(child, state)
  end.flatten.reject { |x| x.to_s.empty? }
end

#treat_coradoc(node, state) ⇒ Object



30
31
32
# File 'lib/coradoc/input/html/converters/base.rb', line 30

def treat_coradoc(node, state)
  Converters.process_coradoc(node, state)
end

#unconstrained_after?(node) ⇒ Boolean

Returns:

  • (Boolean)


98
99
100
101
102
103
# File 'lib/coradoc/input/html/converters/base.rb', line 98

def unconstrained_after?(node)
  after = node.at_xpath("following::node()[1]")

  after && !after.text.strip.empty? &&
    after.text[0]&.match?(/\w|,|;|"|\.\?!/)
end

#unconstrained_before?(node) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
93
94
95
96
# File 'lib/coradoc/input/html/converters/base.rb', line 90

def unconstrained_before?(node)
  before = node.at_xpath("preceding::node()[1]")

  before &&
    !before.text.strip.empty? &&
    before.text[-1]&.match?(/\w/)
end