infoboxer 0.1.2.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +13 -1
- data/README.md +26 -5
- data/bin/infoboxer +45 -0
- data/infoboxer.gemspec +1 -1
- data/lib/infoboxer/definitions/en.wikipedia.org.rb +0 -1
- data/lib/infoboxer/media_wiki/page.rb +13 -5
- data/lib/infoboxer/media_wiki/traits.rb +11 -5
- data/lib/infoboxer/media_wiki.rb +115 -67
- data/lib/infoboxer/navigation/shortcuts.rb +1 -1
- data/lib/infoboxer/parser/context.rb +16 -2
- data/lib/infoboxer/parser/image.rb +1 -1
- data/lib/infoboxer/parser/inline.rb +12 -3
- data/lib/infoboxer/parser/template.rb +3 -4
- data/lib/infoboxer/parser/util.rb +14 -3
- data/lib/infoboxer/tree/image.rb +1 -1
- data/lib/infoboxer/tree/nodes.rb +2 -2
- data/lib/infoboxer/tree/paragraphs.rb +1 -0
- data/lib/infoboxer/tree/table.rb +1 -1
- data/lib/infoboxer/tree/template.rb +9 -0
- data/lib/infoboxer/version.rb +4 -1
- data/lib/infoboxer.rb +87 -35
- data/regression/pages/list_of_countries.wiki +1493 -0
- data/regression/pages/ukrainian_galician_army.wiki +76 -0
- metadata +8 -5
data/lib/infoboxer/tree/nodes.rb
CHANGED
@@ -87,7 +87,7 @@ module Infoboxer
|
|
87
87
|
# @!method fetch
|
88
88
|
# Fetches by name(s) variables for all templates inside.
|
89
89
|
#
|
90
|
-
# See {
|
90
|
+
# See {Tree::Template#fetch} for explanation.
|
91
91
|
|
92
92
|
[
|
93
93
|
:prev_siblings, :next_siblings, :siblings,
|
@@ -101,7 +101,7 @@ module Infoboxer
|
|
101
101
|
# By list of variable names, fetches hashes of `{name => value}`
|
102
102
|
# from all templates inside.
|
103
103
|
#
|
104
|
-
# See {
|
104
|
+
# See {Tree::Template#fetch_hash} for explanation.
|
105
105
|
#
|
106
106
|
# @return [Array<Hash>]
|
107
107
|
def fetch_hashes(*args)
|
data/lib/infoboxer/tree/table.rb
CHANGED
@@ -117,6 +117,15 @@ module Infoboxer
|
|
117
117
|
variables.map{|var| var.to_tree(level+1)}.join
|
118
118
|
end
|
119
119
|
|
120
|
+
# Represents entire template as hash of `String => String`,
|
121
|
+
# where keys are variable names and values are text representation
|
122
|
+
# of variables contents.
|
123
|
+
#
|
124
|
+
# @return [Hash{String => String}]
|
125
|
+
def to_h
|
126
|
+
variables.map{|var| [var.name, var.text]}.to_h
|
127
|
+
end
|
128
|
+
|
120
129
|
# Returns list of template variables with numeric names (which
|
121
130
|
# are treated as "unnamed" variables by MediaWiki templates, see
|
122
131
|
# {Template class docs} for explanation).
|
data/lib/infoboxer/version.rb
CHANGED
data/lib/infoboxer.rb
CHANGED
@@ -35,6 +35,19 @@ require 'backports/2.1.0/array/to_h'
|
|
35
35
|
# * {MediaWiki} client class;
|
36
36
|
# * {Parser} -- which, you know, parses.
|
37
37
|
#
|
38
|
+
# **NB** `Infoboxer` module can also be included in other classes, like
|
39
|
+
# this:
|
40
|
+
#
|
41
|
+
# ```ruby
|
42
|
+
# class MyDataGrabber
|
43
|
+
# include Infoboxer
|
44
|
+
#
|
45
|
+
# def initialize
|
46
|
+
# wikipedia.get('Argentina')
|
47
|
+
# end
|
48
|
+
# end
|
49
|
+
# ```
|
50
|
+
#
|
38
51
|
module Infoboxer
|
39
52
|
private # hiding constants from YARD
|
40
53
|
|
@@ -56,8 +69,15 @@ module Infoboxer
|
|
56
69
|
species: 'species.wikimedia.org',
|
57
70
|
}
|
58
71
|
|
59
|
-
|
72
|
+
public
|
73
|
+
|
74
|
+
# Includeable version of {Infoboxer.wiki}
|
75
|
+
def wiki(api_url, options = {})
|
76
|
+
MediaWiki.new(api_url, options || {})
|
77
|
+
end
|
60
78
|
|
79
|
+
class << self
|
80
|
+
# @!method wiki(api_url, options = {})
|
61
81
|
# Default method for creating MediaWiki API client.
|
62
82
|
#
|
63
83
|
# @param api_url should be URL of api.php for your MediaWiki
|
@@ -70,10 +90,7 @@ module Infoboxer
|
|
70
90
|
# ```ruby
|
71
91
|
# Infoboxer.wiki('some_url').get('Some page title')
|
72
92
|
# ```
|
73
|
-
|
74
|
-
MediaWiki.new(api_url, options || {})
|
75
|
-
end
|
76
|
-
|
93
|
+
|
77
94
|
# @!method wikipedia(lang = 'en', options = {})
|
78
95
|
# Shortcut for creating Wikipedia client.
|
79
96
|
#
|
@@ -128,24 +145,7 @@ module Infoboxer
|
|
128
145
|
# See {wikipedia} for params explanation.
|
129
146
|
# @return [MediaWiki]
|
130
147
|
|
131
|
-
|
132
|
-
define_method name do |lang = 'en', options = {}|
|
133
|
-
if lang.is_a?(Hash)
|
134
|
-
lang, options = 'en', lang
|
135
|
-
end
|
136
|
-
|
137
|
-
wiki("http://#{lang}.#{domain}/w/api.php", options)
|
138
|
-
end
|
139
|
-
end
|
140
|
-
|
141
|
-
alias_method :wp, :wikipedia
|
142
|
-
|
143
|
-
WIKIMEDIA_COMMONS.each do |name, domain|
|
144
|
-
define_method name do |options = {}|
|
145
|
-
wiki("http://#{domain}/w/api.php", options)
|
146
|
-
end
|
147
|
-
end
|
148
|
-
|
148
|
+
# @!method wikia(*domains)
|
149
149
|
# Performs request to wikia.com wikis.
|
150
150
|
#
|
151
151
|
# @overload wikia(*domains)
|
@@ -165,22 +165,74 @@ module Infoboxer
|
|
165
165
|
# (see {wiki} for list of options)
|
166
166
|
#
|
167
167
|
# @return [MediaWiki]
|
168
|
-
|
169
|
-
|
170
|
-
|
168
|
+
end
|
169
|
+
|
170
|
+
WIKIMEDIA_PROJECTS.each do |name, domain|
|
171
|
+
define_method name do |lang = 'en', options = {}|
|
172
|
+
if lang.is_a?(Hash)
|
173
|
+
lang, options = 'en', lang
|
174
|
+
end
|
175
|
+
|
176
|
+
wiki("https://#{lang}.#{domain}/w/api.php", options)
|
171
177
|
end
|
178
|
+
end
|
172
179
|
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
# explicitly.
|
179
|
-
#
|
180
|
-
def user_agent=(ua)
|
181
|
-
MediaWiki.user_agent = ua
|
180
|
+
alias_method :wp, :wikipedia
|
181
|
+
|
182
|
+
WIKIMEDIA_COMMONS.each do |name, domain|
|
183
|
+
define_method name do |options = {}|
|
184
|
+
wiki("https://#{domain}/w/api.php", options)
|
182
185
|
end
|
183
186
|
end
|
187
|
+
|
188
|
+
# @!method wikipedia(lang = 'en', options = {})
|
189
|
+
# Includeable version of {Infoboxer.wikipedia}
|
190
|
+
|
191
|
+
# @!method commons(options = {})
|
192
|
+
# Includeable version of {Infoboxer.commons}
|
193
|
+
|
194
|
+
# @!method wikibooks(lang = 'en', options = {})
|
195
|
+
# Includeable version of {Infoboxer.wikibooks}
|
196
|
+
|
197
|
+
# @!method wikiquote(lang = 'en', options = {})
|
198
|
+
# Includeable version of {Infoboxer.wikiquote}
|
199
|
+
|
200
|
+
# @!method wikiversity(lang = 'en', options = {})
|
201
|
+
# Includeable version of {Infoboxer.wikiversity}
|
202
|
+
|
203
|
+
# @!method wikisource(lang = 'en', options = {})
|
204
|
+
# Includeable version of {Infoboxer.wikisource}
|
205
|
+
|
206
|
+
# @!method wikivoyage(lang = 'en', options = {})
|
207
|
+
# Includeable version of {Infoboxer.wikivoyage}
|
208
|
+
|
209
|
+
# @!method wikinews(lang = 'en', options = {})
|
210
|
+
# Includeable version of {Infoboxer.wikinews}
|
211
|
+
|
212
|
+
# @!method species(options = {})
|
213
|
+
# Includeable version of {Infoboxer.species}
|
214
|
+
|
215
|
+
# @!method wiktionary(lang = 'en', options = {})
|
216
|
+
# Includeable version of {Infoboxer.wiktionary}
|
217
|
+
|
218
|
+
# Includeable version of {Infoboxer.wikia}
|
219
|
+
def wikia(*domains)
|
220
|
+
options = domains.last.is_a?(Hash) ? domains.pop : {}
|
221
|
+
wiki(WIKIA_API_URL % domains.reverse.join('.'), options)
|
222
|
+
end
|
223
|
+
|
224
|
+
# Sets user agent string globally. Default user agent is
|
225
|
+
# {MediaWiki::UA}.
|
226
|
+
#
|
227
|
+
# User agent can also be rewriten as an option to {wiki} method (and
|
228
|
+
# its shortcuts like {wikipedia}), or by using {MediaWiki#initialize}
|
229
|
+
# explicitly.
|
230
|
+
#
|
231
|
+
def Infoboxer.user_agent=(ua)
|
232
|
+
MediaWiki.user_agent = ua
|
233
|
+
end
|
234
|
+
|
235
|
+
extend self
|
184
236
|
end
|
185
237
|
|
186
238
|
require_relative 'infoboxer/version'
|