Class: Infoboxer::Templates::Set
- Inherits:
-
Object
- Object
- Infoboxer::Templates::Set
- Defined in:
- lib/infoboxer/templates/set.rb
Overview
Base class for defining set of templates, used for some site/domain.
Currently only can be plugged in via MediaWiki::Traits.templates.
Template set defines a DSL for creating new template definitions -- also simplest ones and very complicated.
You can look at implementation of English Wikipedia common templates set in Infoboxer's repo.
Instance Method Summary collapse
-
#initialize(&definitions) ⇒ Set
constructor
A new instance of Set.
-
#literal(*names) ⇒ Object
Define list of "literally rendered templates".
-
#replace(*replacements) ⇒ Object
Define list of "replacements": templates, which text should be replaced with arbitrary value.
-
#show(*names) ⇒ Object
Define list of "show children" templates.
-
#template(name, options = {}, &definition) ⇒ Object
Most common form of template definition.
Constructor Details
#initialize(&definitions) ⇒ Set
Returns a new instance of Set.
17 18 19 20 |
# File 'lib/infoboxer/templates/set.rb', line 17 def initialize(&definitions) @templates = [] define(&definitions) if definitions end |
Instance Method Details
#literal(*names) ⇒ Object
Define list of "literally rendered templates". It means, when rendering text, template is replaced with just its name.
Explanation: in
MediaWiki, there are contexts (deeply in other templates and
tables), when you can't just type something like ","
and not
have it interpreted. So, wikis oftenly define wrappers around
those templates, looking like {{,}}
-- so, while rendering texts,
such templates can be replaced with their names.
Expected to be used inside Set definition block.
157 158 159 160 161 |
# File 'lib/infoboxer/templates/set.rb', line 157 def literal(*names) names.each do |name| setup_class(name, Literal) end end |
#replace(*replacements) ⇒ Object
Define list of "replacements": templates, which text should be replaced with arbitrary value.
Example:
# ...inside template set definition...
replace(
'!!' => '||',
'!(' => '['
)
Now, all templates with name !!
will render as ||
when you
call their (or their parents') Infoboxer::Tree::Node#text.
Expected to be used inside Set definition block.
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/infoboxer/templates/set.rb', line 102 def replace(*replacements) case when replacements.count == 2 && replacements.all? { |r| r.is_a?(String) } name, what = *replacements setup_class(name, Replace) do define_method(:replace) do what end end when replacements.count == 1 && replacements.first.is_a?(Hash) replacements.first.each do |nm, rep| replace(nm, rep) end else fail(ArgumentError, "Can't call :replace with #{replacements.join(', ')}") end end |
#show(*names) ⇒ Object
Define list of "show children" templates. Those ones, when rendered as text, just provide join of their children text (space-separated).
Example:
#...in template set definition...
show 'Small'
Now, wikitext paragraph looking like...
This is {{small|text}} in template
...before this template definition had rendered like
"This is in template"
(template contents ommitted), and after
this definition it will render like "This is text in template"
(template contents rendered as is).
Expected to be used inside Set definition block.
140 141 142 143 144 |
# File 'lib/infoboxer/templates/set.rb', line 140 def show(*names) names.each do |name| setup_class(name, Show) end end |
#template(name, options = {}, &definition) ⇒ Object
Most common form of template definition.
Can be used like:
template 'Age' do
def from
fetch_date('1', '2', '3')
end
def to
fetch_date('4', '5', '6') || Date.today
end
def value
(to - from).to_i / 365 # FIXME: obviously
end
def text
"#{value} years"
end
end
Expected to be used inside Set definition block.
82 83 84 |
# File 'lib/infoboxer/templates/set.rb', line 82 def template(name, = {}, &definition) setup_class(name, Base, , &definition) end |