Class: Wikitree::Template

Inherits:
Node
  • Object
show all
Defined in:
lib/wikitree/nodes.rb

Defined Under Namespace

Classes: Param

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, params) ⇒ Template



106
107
108
109
110
111
112
# File 'lib/wikitree/nodes.rb', line 106

def initialize( name, params )
  @name   = name
  @params = []
  params.each_with_index do |param,i|
    @params << Param.new( i+1, param[0], param[1] )
  end
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



56
57
58
# File 'lib/wikitree/nodes.rb', line 56

def name
  @name
end

#paramsObject (readonly)

Returns the value of attribute params.



56
57
58
# File 'lib/wikitree/nodes.rb', line 56

def params
  @params
end

Instance Method Details

#inspectObject



114
115
116
# File 'lib/wikitree/nodes.rb', line 114

def inspect
  "#<template #{@name}: #{@params.inspect}>"
end

#pretty_print(pp) ⇒ Object



118
119
120
121
122
123
124
# File 'lib/wikitree/nodes.rb', line 118

def pretty_print(pp)
    pp.text "#<template "
    pp.text "#{name}: "
    pp.breakable
    pp.pp @params
    pp.text ">"
end

#to_textObject



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/wikitree/nodes.rb', line 126

def to_text
  ## build a template method name (e.g. add _ prefix and change space to _ too)

  ##   and dowcase e.g. Infobox country => infobox_country

  method_name = "_#{@name.downcase.gsub( ' ', '_' )}".to_sym
  if respond_to?( method_name  )
     send( method_name )  ## todo/fix: add args too!!!

  else
    ## rebuild template as string

    buf = String.new('')
    buf << "!!{{"
    buf << "#{@name}"
    @params.each do |param|
      buf << " | "
      if param.name
        buf << param.name
        buf << "="
      end
      buf << param.to_text  ## note. param.to_text ONLY returns value (NOT name incl.)

    end
    buf << "}}"
    buf
  end
end

#to_wikiObject



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/wikitree/nodes.rb', line 150

def to_wiki
  ## rebuild template as string

  buf = String.new('')
  buf << "{{"
  buf << "#{@name}"
  @params.each do |param|
    buf << " | "
    if param.name
      buf << param.name
      buf << "="
    end
    buf << param.to_wiki  ## note. param.to_text ONLY returns value (NOT name incl.)

  end
  buf << "}}"
  buf
end