Class: Infoboxer::Tree::Template
- Includes:
- Linkable
- Defined in:
- lib/infoboxer/tree/template.rb
Overview
Represents MediaWiki template.
Template is basically a thing with name, some variables and their values. When pages are displayed in browser, templates are rendered in something different by wiki engine; yet, when extracting information with Infoboxer, you are working with original templates.
It requires some mastering and understanding, yet allows to do very poweful things. There are many kinds of them, from pure formatting-related (which are typically not more than small bells and whistles for page outlook, and should be rendered as a text) to very information-heavy ones, like infoboxes, from which Infoboxer borrows its name!
Basically, for information extraction from template you'll list its #variables, and then use #fetch method (and its variants: #fetch_hash/##fetch_date) to extract their values.
On variables naming
MediaWiki templates can contain named and unnamed variables. Example:
{{birth date and age|1953|2|19|df=y}}
This is template with name "birth date and age", three unnamed variables with values "1953", "2" and "19", and one named variable with name "df" and value "y".
For consistency, Infoboxer treats unnamed variables exactly the same way MediaWiki does: they considered to have numeric names, which are started from 1 and stored as a strings. So, for template shown above, the following is correct:
template.fetch('1').text == '1953'
template.fetch('2').text == '2'
template.fetch('3').text == '19'
template.fetch('df').text == 'y'
Note also, that named variables with simple text values are duplicated as a template node Node#params, so, the following is correct also:
template.params['df'] == 'y'
template.params.has_key?('1') == false
For more advanced topics, like subclassing templates by names and converting them to inline text, please read Infoboxer::Templates module's documentation.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#name ⇒ String
readonly
Template name, designating its contents structure.
-
#variables ⇒ Nodes<Var>
readonly
Template variables list.
Attributes inherited from Compound
Attributes inherited from Node
Instance Method Summary collapse
-
#fetch(*patterns) ⇒ Nodes<Var>
Fetches template variable(s) by name(s) or patterns.
-
#fetch_date(*patterns) ⇒ Date
Fetches date by list of variable names containing date components.
-
#fetch_hash(*patterns) ⇒ Hash<String => Var>
Fetches hash
{name => variable}
, by same patterns as #fetch. -
#follow ⇒ MediaWiki::Page
Extracts template source and returns it parsed (or nil, if template not found).
-
#initialize(name, variables = Nodes[]) ⇒ Template
constructor
A new instance of Template.
-
#link ⇒ Object
Wikilink name of this template's source.
-
#to_tree(level = 0) ⇒ Object
See Node#to_tree.
-
#unnamed_variables ⇒ Nodes<Var>
Returns list of template variables with numeric names (which are treated as "unnamed" variables by MediaWiki templates, see class docs for explanation).
Methods included from Linkable
Methods inherited from Compound
Methods inherited from Node
#==, #children, coder, def_readers, #index, #inspect, #next_siblings, #prev_siblings, #siblings, #text, #text_, #to_s
Methods included from Navigation::Sections::Node
Methods included from Navigation::Shortcuts::Node
#bold?, #categories, #external_links, #heading?, #headings, #images, #infoboxes, #italic?, #lists, #paragraphs, #tables, #templates, #wikilinks
Methods included from Navigation::Lookup::Node
#_lookup, #_lookup_children, #_lookup_next_siblings, #_lookup_parents, #_lookup_prev_siblings, #_lookup_siblings, #_matches?, #has_parent?, #lookup, #lookup_children, #lookup_next_siblings, #lookup_parents, #lookup_prev_siblings, #lookup_siblings, #matches?
Constructor Details
#initialize(name, variables = Nodes[]) ⇒ Template
Returns a new instance of Template.
108 109 110 111 112 |
# File 'lib/infoboxer/tree/template.rb', line 108 def initialize(name, variables = Nodes[]) super(Nodes[], extract_params(variables)) @name = name @variables = Nodes[*variables].each{|v| v.parent = self} end |
Instance Attribute Details
#name ⇒ String (readonly)
Template name, designating its contents structure.
See also #url, which you can navigate to read template's definition (and, in Wikipedia and many other projects, its documentation).
99 100 101 |
# File 'lib/infoboxer/tree/template.rb', line 99 def name @name end |
Instance Method Details
#fetch(*patterns) ⇒ Nodes<Var>
Fetches template variable(s) by name(s) or patterns.
Usage:
argentina.infobox.fetch('leader_title_1') # => one Var node
argentina.infobox.fetch('leader_title_1',
'leader_name_1') # => two Var nodes
argentina.infobox.fetch(/leader_title_\d+/) # => several Var nodes
141 142 143 |
# File 'lib/infoboxer/tree/template.rb', line 141 def fetch(*patterns) Nodes[*patterns.map{|p| variables.find(name: p)}.flatten] end |
#fetch_date(*patterns) ⇒ Date
Fetches date by list of variable names containing date components.
(Experimental, subject to change or enchance.)
Explanation: if you have template like
{{birth date and age|1953|2|19|df=y}}
...there is a short way to obtain date from it:
template.fetch_date('1', '2', '3') # => Date.new(1953,2,19)
166 167 168 169 170 171 172 173 174 175 |
# File 'lib/infoboxer/tree/template.rb', line 166 def fetch_date(*patterns) components = fetch(*patterns) components.pop while components.last.nil? && !components.empty? if components.empty? nil else Date.new(*components.map{|v| v.to_s.to_i}) end end |
#fetch_hash(*patterns) ⇒ Hash<String => Var>
Fetches hash {name => variable}
, by same patterns as #fetch.
148 149 150 |
# File 'lib/infoboxer/tree/template.rb', line 148 def fetch_hash(*patterns) fetch(*patterns).map{|v| [v.name, v]}.to_h end |
#follow ⇒ MediaWiki::Page
Extracts template source and returns it parsed (or nil, if template not found).
NB: Infoboxer does NO variable substitution or other template evaluation actions. Moreover, it will almost certainly NOT parse template definitions correctly. You should use this method ONLY for "transclusion" templates (parts of content, which are included into other pages "as is").
Look for example at this page's source: each subtable about some region is just a transclusion of template. This can be processed like:
Infoboxer.wp.get('Tropical and subtropical coniferous forests').
templates(name: /forests^/).
follow.tables #.and_so_on
See also Linkable#follow for general notes on the following links.
|
# File 'lib/infoboxer/tree/template.rb', line 179
|
#link ⇒ Object
Wikilink name of this template's source.
205 206 207 208 |
# File 'lib/infoboxer/tree/template.rb', line 205 def link # FIXME: super-naive for now, doesn't thinks about subpages and stuff. "Template:#{name}" end |
#to_tree(level = 0) ⇒ Object
See Node#to_tree
115 116 117 118 |
# File 'lib/infoboxer/tree/template.rb', line 115 def to_tree(level = 0) ' ' * level + "<#{descr}>\n" + variables.map{|var| var.to_tree(level+1)}.join end |
#unnamed_variables ⇒ Nodes<Var>
Returns list of template variables with numeric names (which are treated as "unnamed" variables by MediaWiki templates, see class docs for explanation).
125 126 127 |
# File 'lib/infoboxer/tree/template.rb', line 125 def unnamed_variables variables.find(name: /^\d+$/) end |