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.



7
8
9
# File 'lib/coradoc/input/html/converters/base.rb', line 7

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

#extract_leading_trailing_whitespace(node) ⇒ Object



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

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) if first_text
    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) if last_text
    trailing_whitespace = " "
  end
  [leading_whitespace, trailing_whitespace]
end

#extract_title(node) ⇒ Object



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

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)


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

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)


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

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)


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

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



18
19
20
# File 'lib/coradoc/input/html/converters/base.rb', line 18

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

#treat_children(node, state) ⇒ Object

Note: treat_children won’t run plugin hooks



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

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

#treat_children_coradoc(node, state) ⇒ Object



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

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



28
29
30
# File 'lib/coradoc/input/html/converters/base.rb', line 28

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

#unconstrained_after?(node) ⇒ Boolean

Returns:

  • (Boolean)


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

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)


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

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

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