Class: Deba::Extractor

Inherits:
Object
  • Object
show all
Defined in:
lib/deba/extractor.rb

Constant Summary collapse

HEADING_TAGS =
%w(h1 h2 h3 h4 h5 h6)
BLOCK_INITIATING_TAGS =
%w(
address
article
aside
body
blockquote
div
dd
dl
dt
figure
footer
header
li
main
nav
ol
p
pre
section
td
th
ul)
ENHANCERS =
{ %w(b strong) => "*", %w(i em) => "_" }
SKIP_TAGS =
%w(
  head
  style
  script
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(doc, options = {}) ⇒ Extractor



35
36
37
38
# File 'lib/deba/extractor.rb', line 35

def initialize(doc, options = {})
  @node = doc.root
  @options = options
end

Instance Attribute Details

#blocksObject (readonly)

Returns the value of attribute blocks.



33
34
35
# File 'lib/deba/extractor.rb', line 33

def blocks
  @blocks
end

Instance Method Details

#extractObject



40
41
42
43
44
45
46
47
48
# File 'lib/deba/extractor.rb', line 40

def extract
  @just_appended_br = false
  @in_blockquote = false
  @document = Deba::Document.new(self)

  process(@node)

  @document.content.chomp("\n")
end

#in_blockquote?Boolean



155
156
157
# File 'lib/deba/extractor.rb', line 155

def in_blockquote?
  @in_blockquote
end

#process(node) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/deba/extractor.rb', line 50

def process(node)
  if @options.key?(:exclude)
    return if Array(@options[:exclude]).any? { |selector| node.matches?(selector) }
  end

  node_name = node.name.downcase

  return if SKIP_TAGS.include?(node_name)

  #Handle repeated brs by making a paragraph break
  if node_name == 'br'
    if @just_appended_br
      @just_appended_br = false

      @document.break(Deba::Paragraph)

      return
    else
      @just_appended_br = true
    end
  elsif @just_appended_br
    @just_appended_br = false

    @document << "\n"
  end

  if node.text?
    @document << Deba::Span.new(node.inner_text) if Deba::Utils.present?(node.inner_text)

    return
  end

  if ENHANCERS.keys.flatten.include?(node_name)
    ENHANCERS.each_pair do |tags, nsf_rep|
      if tags.include?(node_name)
        @document << nsf_rep
        node.children.each { |n| process(n) }
        @document << nsf_rep
      end
    end

    return
  end

  if node_name == 'blockquote'
    @in_blockquote = true

    @document.break(Deba::Paragraph)
    node.children.each { |n| process(n) }
    @document.break(Deba::Paragraph)

    @in_blockquote = false

    return
  end

  if node_name == 'li'
    last_item = node.xpath('count(following-sibling::li)').to_i == 0
    index = node.xpath('boolean(ancestor::ol)') ? (node.xpath('count(preceding-sibling::li)').to_i + 1) : nil

    @document.break(Deba::ListItem, last_item, index)
    node.children.each { |n| process(n) }
    @document.break(Deba::Paragraph)

    return
  end

  if node_name == 'dt'
    @document.break(Deba::DefinitionTerm)
    node.children.each { |n| process(n) }
    @document.break(Deba::Paragraph)

    return
  end

  if node_name == 'dd'
    last_item = node.xpath('count(following-sibling::dd)').to_i == 0
    @document.break(Deba::DefinitionDescription, last_item)
    node.children.each { |n| process(n) }
    @document.break(Deba::Paragraph)

    return
  end

  #These tags terminate the current paragraph, if present, and start a new paragraph
  if BLOCK_INITIATING_TAGS.include?(node_name)
    @document.break(Deba::Paragraph)
    node.children.each { |n| process(n) }
    @document.break(Deba::Paragraph)

    return
  end

  if HEADING_TAGS.include?(node_name)
    @document.break(Deba::Heading, node_name[1..-1].to_i)
    node.children.each { |n| process(n) }
    @document.break(Deba::Paragraph)

    return
  end

  #Pretend that the children of this node were siblings of this node (move them one level up the tree)
  node.children.each { |n| process(n) }
end