infoboxer 0.2.7 → 0.2.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop_todo.yml +1 -0
- data/CHANGELOG.md +6 -0
- data/bin/infoboxer +11 -12
- data/infoboxer.gemspec +3 -2
- data/lib/infoboxer/core_ext.rb +1 -0
- data/lib/infoboxer/definitions/en.wikipedia.org.rb +13 -13
- data/lib/infoboxer/media_wiki/page.rb +4 -3
- data/lib/infoboxer/media_wiki/traits.rb +12 -10
- data/lib/infoboxer/media_wiki.rb +97 -68
- data/lib/infoboxer/navigation/lookup.rb +30 -26
- data/lib/infoboxer/navigation/sections.rb +33 -37
- data/lib/infoboxer/navigation/selector.rb +5 -6
- data/lib/infoboxer/navigation/shortcuts.rb +12 -11
- data/lib/infoboxer/navigation.rb +2 -1
- data/lib/infoboxer/parser/context.rb +12 -13
- data/lib/infoboxer/parser/html.rb +7 -6
- data/lib/infoboxer/parser/image.rb +25 -29
- data/lib/infoboxer/parser/inline.rb +82 -79
- data/lib/infoboxer/parser/paragraphs.rb +34 -37
- data/lib/infoboxer/parser/table.rb +26 -27
- data/lib/infoboxer/parser/template.rb +12 -4
- data/lib/infoboxer/parser/util.rb +11 -16
- data/lib/infoboxer/parser.rb +8 -1
- data/lib/infoboxer/templates/base.rb +3 -3
- data/lib/infoboxer/templates/set.rb +11 -10
- data/lib/infoboxer/tree/compound.rb +7 -6
- data/lib/infoboxer/tree/document.rb +1 -0
- data/lib/infoboxer/tree/html.rb +5 -4
- data/lib/infoboxer/tree/image.rb +8 -7
- data/lib/infoboxer/tree/inline.rb +4 -5
- data/lib/infoboxer/tree/linkable.rb +3 -5
- data/lib/infoboxer/tree/list.rb +15 -16
- data/lib/infoboxer/tree/node.rb +11 -10
- data/lib/infoboxer/tree/nodes.rb +24 -23
- data/lib/infoboxer/tree/paragraphs.rb +3 -2
- data/lib/infoboxer/tree/ref.rb +6 -3
- data/lib/infoboxer/tree/table.rb +13 -13
- data/lib/infoboxer/tree/template.rb +15 -15
- data/lib/infoboxer/tree/text.rb +2 -1
- data/lib/infoboxer/tree/wikilink.rb +9 -8
- data/lib/infoboxer/tree.rb +3 -2
- data/lib/infoboxer/version.rb +2 -1
- data/lib/infoboxer.rb +24 -26
- data/regression/pages/wyoming.wiki +1085 -0
- metadata +8 -21
- data/lib/infoboxer/media_wiki/mediawiktory_patch.rb +0 -23
data/lib/infoboxer/tree/list.rb
CHANGED
@@ -1,13 +1,14 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
+
|
2
3
|
module Infoboxer
|
3
4
|
module Tree
|
4
|
-
# Represents item of ordered or unordered list.
|
5
|
+
# Represents item of ordered or unordered list.
|
5
6
|
class ListItem < BaseParagraph
|
6
7
|
# @private
|
7
8
|
# Internal, used by {Parser}
|
8
9
|
def can_merge?(other)
|
9
10
|
other.class == self.class &&
|
10
|
-
other.children.first.
|
11
|
+
other.children.first.is_a?(List)
|
11
12
|
end
|
12
13
|
|
13
14
|
# @private
|
@@ -21,11 +22,12 @@ module Infoboxer
|
|
21
22
|
end
|
22
23
|
|
23
24
|
def text
|
24
|
-
make_marker +
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
25
|
+
make_marker +
|
26
|
+
if children.last.is_a?(List)
|
27
|
+
children[0..-2].map(&:text).join + "\n" + children.last.text
|
28
|
+
else
|
29
|
+
children.map(&:text).join + "\n"
|
30
|
+
end
|
29
31
|
end
|
30
32
|
|
31
33
|
private
|
@@ -71,7 +73,7 @@ module Infoboxer
|
|
71
73
|
|
72
74
|
# Represents unordered list (list with markers).
|
73
75
|
class UnorderedList < List
|
74
|
-
def make_marker(
|
76
|
+
def make_marker(_item)
|
75
77
|
list_text_indent + '* '
|
76
78
|
end
|
77
79
|
end
|
@@ -87,7 +89,7 @@ module Infoboxer
|
|
87
89
|
# consists of {DTerm}s and {DDefinition}s.
|
88
90
|
#
|
89
91
|
# NB: In fact, at least in English Wikipedia, orphan "definition terms"
|
90
|
-
# are used as a low-level headers, especially in lists of links/references.
|
92
|
+
# are used as a low-level headers, especially in lists of links/references.
|
91
93
|
class DefinitionList < List
|
92
94
|
def make_marker(item)
|
93
95
|
case item
|
@@ -118,7 +120,7 @@ module Infoboxer
|
|
118
120
|
def merge!(other)
|
119
121
|
ochildren = other.children.dup
|
120
122
|
if children.last && ochildren.first &&
|
121
|
-
|
123
|
+
children.last.can_merge?(ochildren.first)
|
122
124
|
|
123
125
|
children.last.merge!(ochildren.shift)
|
124
126
|
end
|
@@ -133,7 +135,7 @@ module Infoboxer
|
|
133
135
|
klass = LISTS[m] or
|
134
136
|
fail("Something went wrong: undefined list marker type #{m}")
|
135
137
|
item_klass = ITEMS[m]
|
136
|
-
|
138
|
+
|
137
139
|
if marker.empty?
|
138
140
|
klass.new(item_klass.new(nodes))
|
139
141
|
else
|
@@ -141,15 +143,13 @@ module Infoboxer
|
|
141
143
|
end
|
142
144
|
end
|
143
145
|
|
144
|
-
private
|
145
|
-
|
146
146
|
# @private
|
147
147
|
LISTS = {
|
148
148
|
';' => DefinitionList,
|
149
149
|
':' => DefinitionList,
|
150
150
|
'*' => UnorderedList,
|
151
151
|
'#' => OrderedList
|
152
|
-
}
|
152
|
+
}.freeze
|
153
153
|
|
154
154
|
# @private
|
155
155
|
ITEMS = {
|
@@ -157,8 +157,7 @@ module Infoboxer
|
|
157
157
|
':' => DDefinition,
|
158
158
|
'*' => ListItem,
|
159
159
|
'#' => ListItem
|
160
|
-
}
|
161
|
-
|
160
|
+
}.freeze
|
162
161
|
end
|
163
162
|
end
|
164
163
|
end
|
data/lib/infoboxer/tree/node.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
+
|
2
3
|
require 'htmlentities'
|
3
4
|
|
4
5
|
module Infoboxer
|
@@ -11,7 +12,7 @@ module Infoboxer
|
|
11
12
|
#
|
12
13
|
class Node
|
13
14
|
include ProcMe
|
14
|
-
|
15
|
+
|
15
16
|
def initialize(params = {})
|
16
17
|
@params = params
|
17
18
|
end
|
@@ -48,12 +49,12 @@ module Infoboxer
|
|
48
49
|
|
49
50
|
# List of siblings before this one
|
50
51
|
def prev_siblings
|
51
|
-
siblings.select{|n| n.index < index}
|
52
|
+
siblings.select { |n| n.index < index }
|
52
53
|
end
|
53
54
|
|
54
55
|
# List of siblings after this one
|
55
56
|
def next_siblings
|
56
|
-
siblings.select{|n| n.index > index}
|
57
|
+
siblings.select { |n| n.index > index }
|
57
58
|
end
|
58
59
|
|
59
60
|
# Node children list
|
@@ -63,7 +64,7 @@ module Infoboxer
|
|
63
64
|
|
64
65
|
# @private
|
65
66
|
# Used only during tree construction in {Parser}.
|
66
|
-
def can_merge?(
|
67
|
+
def can_merge?(_other)
|
67
68
|
false
|
68
69
|
end
|
69
70
|
|
@@ -87,7 +88,7 @@ module Infoboxer
|
|
87
88
|
# # pretty <Italic>
|
88
89
|
# # complicated <Text>
|
89
90
|
# ```
|
90
|
-
#
|
91
|
+
#
|
91
92
|
# Useful for understanding page structure, and Infoboxer's representation
|
92
93
|
# of this structure
|
93
94
|
def to_tree(level = 0)
|
@@ -144,33 +145,33 @@ module Infoboxer
|
|
144
145
|
|
145
146
|
def descr
|
146
147
|
if !params || params.empty?
|
147
|
-
|
148
|
+
clean_class.to_s
|
148
149
|
else
|
149
150
|
"#{clean_class}(#{show_params})"
|
150
151
|
end
|
151
152
|
end
|
152
153
|
|
153
154
|
def show_params(prms = nil)
|
154
|
-
(prms || params).map{|k, v| "#{k}: #{v.inspect}"}.join(', ')
|
155
|
+
(prms || params).map { |k, v| "#{k}: #{v.inspect}" }.join(', ')
|
155
156
|
end
|
156
157
|
|
157
158
|
def indent(level)
|
158
159
|
' ' * level
|
159
160
|
end
|
160
161
|
|
161
|
-
def _eq(
|
162
|
+
def _eq(_other)
|
162
163
|
fail(NotImplementedError, "#_eq should be defined in subclasses (called for #{self.class})")
|
163
164
|
end
|
164
165
|
|
165
166
|
def decode(str)
|
166
167
|
Node.coder.decode(str)
|
167
168
|
end
|
168
|
-
|
169
|
+
|
169
170
|
class << self
|
170
171
|
# Internal: descendandts DSL
|
171
172
|
def def_readers(*keys)
|
172
173
|
keys.each do |k|
|
173
|
-
define_method(k){ params[k] }
|
174
|
+
define_method(k) { params[k] }
|
174
175
|
end
|
175
176
|
end
|
176
177
|
|
data/lib/infoboxer/tree/nodes.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
+
|
2
3
|
module Infoboxer
|
3
4
|
module Tree
|
4
5
|
# List of nodes, which tries to be useful both as array, and as proxy
|
@@ -10,7 +11,7 @@ module Infoboxer
|
|
10
11
|
# like those:
|
11
12
|
#
|
12
13
|
# ```ruby
|
13
|
-
# document.sections. # => Nodes returned,
|
14
|
+
# document.sections. # => Nodes returned,
|
14
15
|
# select{|section| # you can treat them as array, but also...
|
15
16
|
# section.text.length > 1000 #
|
16
17
|
# }. #
|
@@ -22,7 +23,6 @@ module Infoboxer
|
|
22
23
|
# ```
|
23
24
|
#
|
24
25
|
class Nodes < Array
|
25
|
-
|
26
26
|
# @!method select(&block)
|
27
27
|
# Just like Array#select, but returns Nodes
|
28
28
|
|
@@ -41,10 +41,10 @@ module Infoboxer
|
|
41
41
|
# @!method -(other)
|
42
42
|
# Just like Array#-, but returns Nodes
|
43
43
|
|
44
|
-
[
|
45
|
-
define_method(sym)
|
44
|
+
%i[select reject sort_by flatten compact -].each do |sym|
|
45
|
+
define_method(sym) do |*args, &block|
|
46
46
|
Nodes[*super(*args, &block)]
|
47
|
-
|
47
|
+
end
|
48
48
|
end
|
49
49
|
|
50
50
|
# Just like Array#first, but returns Nodes, if provided with `n` of elements.
|
@@ -68,7 +68,7 @@ module Infoboxer
|
|
68
68
|
# Just like Array#map, but returns Nodes, **if** all map results are Node
|
69
69
|
def map
|
70
70
|
res = super
|
71
|
-
if res.all?{|n| n.is_a?(Node) || n.is_a?(Nodes)}
|
71
|
+
if res.all? { |n| n.is_a?(Node) || n.is_a?(Nodes) }
|
72
72
|
Nodes[*res]
|
73
73
|
else
|
74
74
|
res
|
@@ -77,7 +77,7 @@ module Infoboxer
|
|
77
77
|
|
78
78
|
# @!method prev_siblings
|
79
79
|
# Previous siblings (flat list) of all nodes inside.
|
80
|
-
|
80
|
+
|
81
81
|
# @!method next_siblings
|
82
82
|
# Next siblings (flat list) of all nodes inside.
|
83
83
|
|
@@ -88,14 +88,14 @@ module Infoboxer
|
|
88
88
|
# Fetches by name(s) variables for all templates inside.
|
89
89
|
#
|
90
90
|
# See {Tree::Template#fetch} for explanation.
|
91
|
-
|
92
|
-
[
|
93
|
-
|
94
|
-
|
91
|
+
|
92
|
+
%i[
|
93
|
+
prev_siblings next_siblings siblings
|
94
|
+
fetch
|
95
95
|
].each do |sym|
|
96
|
-
define_method(sym)
|
97
|
-
make_nodes
|
98
|
-
|
96
|
+
define_method(sym) do |*args|
|
97
|
+
make_nodes(map { |n| n.send(sym, *args) })
|
98
|
+
end
|
99
99
|
end
|
100
100
|
|
101
101
|
# By list of variable names, fetches hashes of `{name => value}`
|
@@ -105,7 +105,7 @@ module Infoboxer
|
|
105
105
|
#
|
106
106
|
# @return [Array<Hash>]
|
107
107
|
def fetch_hashes(*args)
|
108
|
-
map{|t| t.fetch_hash(*args)}
|
108
|
+
map { |t| t.fetch_hash(*args) }
|
109
109
|
end
|
110
110
|
|
111
111
|
# Just join of all {Node#to_tree Node#to_tree} strings inside.
|
@@ -114,10 +114,11 @@ module Infoboxer
|
|
114
114
|
end
|
115
115
|
|
116
116
|
def inspect
|
117
|
-
'[' +
|
117
|
+
'[' +
|
118
118
|
case
|
119
119
|
when count > MAX_CHILDREN
|
120
|
-
self[0...MAX_CHILDREN].map(&:inspect).join(', ') +
|
120
|
+
self[0...MAX_CHILDREN].map(&:inspect).join(', ') +
|
121
|
+
", ...#{count - MAX_CHILDREN} more nodes"
|
121
122
|
else
|
122
123
|
map(&:inspect).join(', ')
|
123
124
|
end + ']'
|
@@ -138,19 +139,19 @@ module Infoboxer
|
|
138
139
|
# @return [Nodes<MediaWiki::Page>] It is still `Nodes`, so you
|
139
140
|
# still can process them uniformely.
|
140
141
|
def follow
|
141
|
-
links = select{|n| n.respond_to?(:link)}.map(&:link)
|
142
|
+
links = select { |n| n.respond_to?(:link) }.map(&:link)
|
142
143
|
return Nodes[] if links.empty?
|
143
144
|
page = first.lookup_parents(MediaWiki::Page).first or
|
144
|
-
fail(
|
145
|
-
page.client or fail(
|
145
|
+
fail('Not in a page from real source')
|
146
|
+
page.client or fail('MediaWiki client not set')
|
146
147
|
page.client.get(*links)
|
147
148
|
end
|
148
149
|
|
149
150
|
# @private
|
150
151
|
# Internal, used by {Parser}
|
151
152
|
def <<(node)
|
152
|
-
if node.
|
153
|
-
node.each{|n| self << n}
|
153
|
+
if node.is_a?(Array)
|
154
|
+
node.each { |n| self << n }
|
154
155
|
elsif last && last.can_merge?(node)
|
155
156
|
last.merge!(node)
|
156
157
|
else
|
@@ -172,7 +173,7 @@ module Infoboxer
|
|
172
173
|
# @private
|
173
174
|
# Internal, used by {Parser}
|
174
175
|
def flow_templates
|
175
|
-
make_nodes
|
176
|
+
make_nodes(map { |n| n.is_a?(Paragraph) ? n.to_templates? : n })
|
176
177
|
end
|
177
178
|
|
178
179
|
private
|
@@ -1,4 +1,5 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
+
|
2
3
|
module Infoboxer
|
3
4
|
module Tree
|
4
5
|
# Base class for all "paragraph-level" nodes: {Paragraph}, {ListItem},
|
@@ -46,7 +47,7 @@ module Infoboxer
|
|
46
47
|
end
|
47
48
|
end
|
48
49
|
end
|
49
|
-
|
50
|
+
|
50
51
|
# @private
|
51
52
|
class MergeableParagraph < BaseParagraph
|
52
53
|
include Mergeable
|
@@ -68,7 +69,7 @@ module Infoboxer
|
|
68
69
|
# @private
|
69
70
|
# Internal, used by {Parser}
|
70
71
|
def templates_only?
|
71
|
-
children.all?{|c| c.is_a?(Template) || c.is_a?(Text) && c.raw_text.strip.empty?}
|
72
|
+
children.all? { |c| c.is_a?(Template) || c.is_a?(Text) && c.raw_text.strip.empty? }
|
72
73
|
end
|
73
74
|
|
74
75
|
# @private
|
data/lib/infoboxer/tree/ref.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
+
|
2
3
|
module Infoboxer
|
3
4
|
module Tree
|
4
5
|
# Represents footnote.
|
@@ -6,13 +7,15 @@ module Infoboxer
|
|
6
7
|
# Is not rendered in text flow, so, wikitext like
|
7
8
|
#
|
8
9
|
# ```
|
9
|
-
# ...pushed it back into underdevelopment,<ref>...tons of footnote text...</ref> though it
|
10
|
+
# ...pushed it back into underdevelopment,<ref>...tons of footnote text...</ref> though it
|
11
|
+
# nevertheless...
|
10
12
|
# ```
|
11
13
|
# when parsed and {Node#text} called, will return text like:
|
12
14
|
#
|
13
15
|
# ```
|
14
16
|
# ...pushed it back into underdevelopment, though it nevertheless...
|
15
17
|
# ```
|
18
|
+
#
|
16
19
|
# ...which most times is most reasonable thing to do.
|
17
20
|
class Ref < Compound
|
18
21
|
# @!attribute [r] name
|
@@ -24,11 +27,11 @@ module Infoboxer
|
|
24
27
|
# even empty tag should not be dropped!
|
25
28
|
false
|
26
29
|
end
|
27
|
-
|
30
|
+
|
28
31
|
def text
|
29
32
|
# because we want "clean" text,
|
30
33
|
# without references & footnotes messed up in it
|
31
|
-
''
|
34
|
+
''
|
32
35
|
end
|
33
36
|
end
|
34
37
|
end
|
data/lib/infoboxer/tree/table.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
+
|
2
3
|
require 'terminal-table'
|
3
4
|
|
4
5
|
module Infoboxer
|
@@ -25,31 +26,30 @@ module Infoboxer
|
|
25
26
|
#
|
26
27
|
# FIXME: it can easily be several table heading rows
|
27
28
|
def heading_row
|
28
|
-
rows.first && rows.first.children.all?(&call(matches?: TableHeading))
|
29
|
-
rows.first : nil
|
29
|
+
rows.first if rows.first && rows.first.children.all?(&call(matches?: TableHeading))
|
30
30
|
end
|
31
31
|
|
32
32
|
# For now, returns all table rows except {#heading_row}
|
33
33
|
def body_rows
|
34
|
-
rows.first && rows.first.children.all?(&call(matches?: TableHeading))
|
35
|
-
rows[1..-1]
|
34
|
+
if rows.first && rows.first.children.all?(&call(matches?: TableHeading))
|
35
|
+
rows[1..-1]
|
36
|
+
else
|
36
37
|
rows
|
38
|
+
end
|
37
39
|
end
|
38
40
|
|
39
41
|
def text
|
40
42
|
table = Terminal::Table.new
|
41
|
-
if caption
|
42
|
-
|
43
|
-
end
|
44
|
-
|
43
|
+
table.title = caption.text.sub(/\n+\Z/, '') if caption
|
44
|
+
|
45
45
|
if heading_row
|
46
|
-
table.headings = heading_row.children.map(&:text)
|
47
|
-
|
46
|
+
table.headings = heading_row.children.map(&:text)
|
47
|
+
.map(&call(sub: [/\n+\Z/, '']))
|
48
48
|
end
|
49
49
|
|
50
|
-
table.rows = body_rows.map{|r|
|
51
|
-
r.children.map(&:text)
|
52
|
-
|
50
|
+
table.rows = body_rows.map { |r|
|
51
|
+
r.children.map(&:text)
|
52
|
+
.map(&call(sub: [/\n+\Z/, '']))
|
53
53
|
}
|
54
54
|
table.to_s + "\n\n"
|
55
55
|
end
|
@@ -1,4 +1,5 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
+
|
2
3
|
require_relative 'linkable'
|
3
4
|
|
4
5
|
module Infoboxer
|
@@ -40,7 +41,7 @@ module Infoboxer
|
|
40
41
|
# with Infoboxer, you are working with original templates.
|
41
42
|
#
|
42
43
|
# It requires some mastering and understanding, yet allows to do
|
43
|
-
# very poweful things. There are many kinds of them, from pure
|
44
|
+
# very poweful things. There are many kinds of them, from pure
|
44
45
|
# formatting-related (which are typically not more than small bells
|
45
46
|
# and whistles for page outlook, and should be rendered as a text)
|
46
47
|
# to very information-heavy ones, like
|
@@ -107,14 +108,14 @@ module Infoboxer
|
|
107
108
|
|
108
109
|
def initialize(name, variables = Nodes[])
|
109
110
|
super(Nodes[], extract_params(variables))
|
110
|
-
@name
|
111
|
-
@variables = Nodes[*variables].each{|v| v.parent = self}
|
111
|
+
@name = name
|
112
|
+
@variables = Nodes[*variables].each { |v| v.parent = self }
|
112
113
|
end
|
113
114
|
|
114
115
|
# See {Node#to_tree}
|
115
116
|
def to_tree(level = 0)
|
116
117
|
' ' * level + "<#{descr}>\n" +
|
117
|
-
variables.map{|var| var.to_tree(level+1)}.join
|
118
|
+
variables.map { |var| var.to_tree(level + 1) }.join
|
118
119
|
end
|
119
120
|
|
120
121
|
# Represents entire template as hash of `String => String`,
|
@@ -123,7 +124,7 @@ module Infoboxer
|
|
123
124
|
#
|
124
125
|
# @return [Hash{String => String}]
|
125
126
|
def to_h
|
126
|
-
variables.map{|var| [var.name, var.text]}.to_h
|
127
|
+
variables.map { |var| [var.name, var.text] }.to_h
|
127
128
|
end
|
128
129
|
|
129
130
|
# Returns list of template variables with numeric names (which
|
@@ -148,14 +149,14 @@ module Infoboxer
|
|
148
149
|
#
|
149
150
|
# @return [Nodes<Var>]
|
150
151
|
def fetch(*patterns)
|
151
|
-
Nodes[*patterns.map{|p| variables.find(name: p)}.flatten]
|
152
|
+
Nodes[*patterns.map { |p| variables.find(name: p) }.flatten]
|
152
153
|
end
|
153
154
|
|
154
155
|
# Fetches hash `{name => variable}`, by same patterns as {#fetch}.
|
155
156
|
#
|
156
157
|
# @return [Hash<String => Var>]
|
157
158
|
def fetch_hash(*patterns)
|
158
|
-
fetch(*patterns).map{|v| [v.name, v]}.to_h
|
159
|
+
fetch(*patterns).map { |v| [v.name, v] }.to_h
|
159
160
|
end
|
160
161
|
|
161
162
|
# Fetches date by list of variable names containing date components.
|
@@ -175,11 +176,11 @@ module Infoboxer
|
|
175
176
|
def fetch_date(*patterns)
|
176
177
|
components = fetch(*patterns)
|
177
178
|
components.pop while components.last.nil? && !components.empty?
|
178
|
-
|
179
|
+
|
179
180
|
if components.empty?
|
180
181
|
nil
|
181
182
|
else
|
182
|
-
Date.new(*components.map{|v| v.to_s.to_i})
|
183
|
+
Date.new(*components.map { |v| v.to_s.to_i })
|
183
184
|
end
|
184
185
|
end
|
185
186
|
|
@@ -231,16 +232,15 @@ module Infoboxer
|
|
231
232
|
def clean_class
|
232
233
|
"Template[#{name}]"
|
233
234
|
end
|
234
|
-
|
235
|
+
|
235
236
|
def extract_params(vars)
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
map{|v| [v.name, v.children.first.raw_text]}.flatten(1)]
|
237
|
+
vars
|
238
|
+
.select { |v| v.children.count == 1 && v.children.first.is_a?(Text) }
|
239
|
+
.map { |v| [v.name, v.children.first.raw_text] }.to_h
|
240
240
|
end
|
241
241
|
|
242
242
|
def inspect_variables(depth)
|
243
|
-
variables.to_a[0..1].map{|name, var| "#{name}: #{var.inspect(depth+1)}"}.join(', ') +
|
243
|
+
variables.to_a[0..1].map { |name, var| "#{name}: #{var.inspect(depth + 1)}" }.join(', ') +
|
244
244
|
(variables.count > 2 ? ', ...' : '')
|
245
245
|
end
|
246
246
|
end
|
data/lib/infoboxer/tree/text.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
+
|
2
3
|
module Infoboxer
|
3
4
|
module Tree
|
4
5
|
# Represents plain text node.
|
@@ -13,7 +14,7 @@ module Infoboxer
|
|
13
14
|
class Text < Node
|
14
15
|
# Text fragment without decodint of HTML entities.
|
15
16
|
attr_accessor :raw_text
|
16
|
-
|
17
|
+
|
17
18
|
def initialize(text, params = {})
|
18
19
|
super(params)
|
19
20
|
@raw_text = text
|
@@ -1,4 +1,5 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
+
|
2
3
|
require_relative 'linkable'
|
3
4
|
|
4
5
|
module Infoboxer
|
@@ -35,7 +36,7 @@ module Infoboxer
|
|
35
36
|
# The same way, `[Pipe (programming)]` has `topic == 'Pipe'` and
|
36
37
|
# `refinement == 'programming'`
|
37
38
|
attr_reader :topic
|
38
|
-
|
39
|
+
|
39
40
|
# Refinement part of link name.
|
40
41
|
#
|
41
42
|
# See {#topic} for explanation.
|
@@ -57,17 +58,17 @@ module Infoboxer
|
|
57
58
|
|
58
59
|
# @see http://en.wikipedia.org/wiki/Help:Pipe_trick
|
59
60
|
def parse_topic!
|
60
|
-
@topic, @refinement =
|
61
|
-
|
62
|
-
|
63
|
-
[
|
61
|
+
@topic, @refinement =
|
62
|
+
case @name
|
63
|
+
when /^(.+\S)\s*\((.+)\)$/, /^(.+?),\s*(.+)$/
|
64
|
+
[Regexp.last_match(1), Regexp.last_match(2)]
|
64
65
|
else
|
65
66
|
[@name, '']
|
66
67
|
end
|
67
68
|
|
68
|
-
|
69
|
-
|
70
|
-
|
69
|
+
return unless children.count == 1 &&
|
70
|
+
children.first.is_a?(Text) && children.first.raw_text.empty?
|
71
|
+
children.first.raw_text = @topic
|
71
72
|
end
|
72
73
|
end
|
73
74
|
end
|
data/lib/infoboxer/tree.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
+
|
2
3
|
module Infoboxer
|
3
4
|
# Infoboxer provides you with tree structure of the Wikipedia page,
|
4
5
|
# which you can introspect and navigate with ease. This tree structure
|
@@ -62,8 +63,8 @@ module Infoboxer
|
|
62
63
|
require_relative 'tree/nodes'
|
63
64
|
|
64
65
|
%w[text compound inline
|
65
|
-
|
66
|
-
|
66
|
+
image html paragraphs list template table ref math
|
67
|
+
document].each do |type|
|
67
68
|
require_relative "tree/#{type}"
|
68
69
|
end
|
69
70
|
end
|