Class: PseudoHiki::HtmlPlugin

Inherits:
Object
  • Object
show all
Defined in:
lib/pseudohiki/htmlplugin.rb

Constant Summary collapse

PLUGIN_PAT =
/^(\w+)([\s\(]+)/
NUMBER_RE =
/(\d+)/

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tag_type, parsed_data) ⇒ HtmlPlugin

Returns a new instance of HtmlPlugin.



45
46
47
48
49
50
# File 'lib/pseudohiki/htmlplugin.rb', line 45

def initialize(tag_type, parsed_data)
  @tag_type = tag_type
  @plugin_name = nil
  @with_paren = nil
  @data = parse(parsed_data.to_s)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missingObject



142
143
144
# File 'lib/pseudohiki/htmlplugin.rb', line 142

def method_missing
  HtmlElement.create(@tag_type, @data, "class" => "plugin")
end

Class Method Details

.add_chemical_formula(chemical_formula = "CO2", en_word = "carbon dioxide") ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/pseudohiki/htmlplugin.rb', line 75

def HtmlPlugin.add_chemical_formula(chemical_formula="CO2", en_word="carbon dioxide")
  eval(<<-End)
  def #{chemical_formula.downcase}
    #(display=":cf",second_display=nil)
    display, second_display = @data.split(",\s")
    display = ":cf" unless display
    return [#{chemical_formula.downcase}(display),
      "(",
      #{chemical_formula.downcase}(second_display),
      ")"].join("") if second_display
    case display
    when ":cf"
      "#{chemical_formula}".gsub(NUMBER_RE, "<sub>\\\\1</sub>")
    when ":en"
      "#{en_word}"
    end
  end
  End
end

Instance Method Details

#anchorObject

def inline

  lines = HtmlElement.decode(@data).split(/\r*\n/o)
  lines.shift if lines.first == ""
  HikiBlockParser.new.parse_lines(lines).join
end


67
68
69
70
71
72
73
# File 'lib/pseudohiki/htmlplugin.rb', line 67

def anchor
  name, anchor_mark = @data.split(/,\s*/o, 2)
  anchor_mark = "_" if (anchor_mark.nil? or anchor_mark.empty?)
  HtmlElement.create("a", anchor_mark,
                     "name" => name,
                     "href" => "#" + name)
end

#applyObject



52
53
54
# File 'lib/pseudohiki/htmlplugin.rb', line 52

def apply
  self.send @plugin_name
end

#c_degreeObject Also known as: oc



120
121
122
# File 'lib/pseudohiki/htmlplugin.rb', line 120

def c_degree
  "&deg;C"
end

#cbObject



111
112
113
114
# File 'lib/pseudohiki/htmlplugin.rb', line 111

def cb
  # I'm wondering if we'd be better to use &sup3; , but...
  "#{@data}<sup>3</sup>"
end

#chemical_formulaObject



124
125
126
# File 'lib/pseudohiki/htmlplugin.rb', line 124

def chemical_formula
  @data.gsub(NUMBER_RE, "<sub>\\1</sub>")
end

#htmlObject



56
57
58
59
# File 'lib/pseudohiki/htmlplugin.rb', line 56

def html
  #    "<div class='raw-html'>"+HtmlElement.decode(@data)+"</div>"
  HtmlElement.decode(@data).to_s
end

#isoObject



128
129
130
131
132
133
134
135
136
137
# File 'lib/pseudohiki/htmlplugin.rb', line 128

def iso
  @data.scan(/\A(\d+)([^\d].*)/o) do |data|
    weight, molecule = data
    if self.respond_to? molecule
      return "<sup>#{weight}</sup>" + HtmlPlugin.new("", molecule).apply
    else
      return "<sup>#{weight}</sup>" + molecule
    end
  end
end

#parse(data) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/pseudohiki/htmlplugin.rb', line 32

def parse(data)
  result = "".freeze
  if PLUGIN_PAT =~ data
    @plugin_name = $1
    @with_paren = true if $2.chomp == "("
    result = data.chomp.sub(PLUGIN_PAT, "")
    result[-1, 1] = "" if @with_paren
  else
    @plugin_name = data.chomp
  end
  result
end

#perObject



116
117
118
# File 'lib/pseudohiki/htmlplugin.rb', line 116

def per
  "#{@data}<sup>-1</sup>"
end

#sqObject



106
107
108
109
# File 'lib/pseudohiki/htmlplugin.rb', line 106

def sq
  # I'm wondering if we'd be better to use &sup2; , but when we search by "km2" for example, we may have problem...
  "#{@data}<sup>2</sup>"
end