Class: Infoboxer::Templates::Set

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(&definitions) ⇒ Set

Returns a new instance of Set.



15
16
17
18
# File 'lib/infoboxer/templates/set.rb', line 15

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.



155
156
157
158
159
# File 'lib/infoboxer/templates/set.rb', line 155

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.



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/infoboxer/templates/set.rb', line 100

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.



138
139
140
141
142
# File 'lib/infoboxer/templates/set.rb', line 138

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.

Parameters:

  • name

    Definition name.

  • options (defaults to: {})

    Definition options. Currently recognized options are:

    • :match -- regexp or string, which matches template name to add this definition to (if not provided, name param used to match relevant templates);
    • :base -- name of template definition to use as a base class; for example you can do things like:
    # ...inside template set definition...
    template 'Infobox', match: /^Infobox/ do
      # implementation
    end
    
    template 'Infobox cheese', base: 'Infobox' do
    end
    


80
81
82
# File 'lib/infoboxer/templates/set.rb', line 80

def template(name, options = {}, &definition)
  setup_class(name, Base, options, &definition)
end