Module: WaplHelper

Defined in:
lib/wapl_helper.rb

Instance Method Summary collapse

Instance Method Details

#attr_string(attr) ⇒ Object

Converts a hash to attribute string Parameters

Attr

hash - { :name => “test”}



17
18
19
20
21
# File 'lib/wapl_helper.rb', line 17

def attr_string(attr)
  op_str = ""
  attr.each { |k,v|  op_str += %Q{#{k}="#{v}" } }
  return op_str.strip
end

#cdatize(content) ⇒ Object

Wrap string in to cdata marker parameters

content
  • string to be wrapped



39
40
41
42
# File 'lib/wapl_helper.rb', line 39

def cdatize(content)

    content = '<![CDATA[ ' << content << ' ]]>'
end

#chars(content, options = {}) ⇒ Object

  • :cell - cell attribute hash



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/wapl_helper.rb', line 81

def chars(content, options={})
  # defaults
  attributes = options[:attributes] || {}

  value = self.tag('value', content)
  chars = self.tag("chars", value, attributes )

  cell = options[:cell] || {}
  row = options[:row] || {}
  self.row_cell(chars, cell, row)
end

#children_list(children = {}) ⇒ Object

Creates a list of child elements with their values

Children

hash with tags and their contents e.g. { :span => “cool content”}



69
70
71
72
73
# File 'lib/wapl_helper.rb', line 69

def children_list(children={})
  children_str =""
  children.each { |tag, val| children_str +=  self.tag(tag, val)}
  return children_str
end

#css(url = "") ⇒ Object

url
  • url for the external stylesheet



170
171
172
# File 'lib/wapl_helper.rb', line 170

def css(url="")
  self.style_sheet(url)
end

#end_waplObject

closes the root element



151
152
153
# File 'lib/wapl_helper.rb', line 151

def end_wapl
  return self.t_e('wapl')
end

#external_image(url, options = {}, just_element = false) ⇒ Object

just_element

bool - create just the element, without the wrapping <row /><cell />



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

def external_image(url, options={}, just_element=false)
  attributes = options[:attributes] || {}
  children = options[:children] || {}

  children[:url] = url
  child_elements = self.children_list(children)

  img = self.tag('externalImage', child_elements, attributes)

  unless just_element
    cell = options[:cell] || {}
    row = options[:row] || {}
    self.row_cell(img, cell, row)
  else
    return img
  end
end
  • :cell - cell attribute hash



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/wapl_helper.rb', line 125

def external_link(link_label, url, options={})
  attributes = options[:attributes] || {}
  children = options[:children] || {}
  image = options[:image] || {}

 img =""
 unless image.empty?
    img = self.external_image(image[:url], image, true) 
  end

  child_elements = self.children_list(children) << img
  link_label = self.tag('label', link_label)
  url = self.tag('url', url)
  link = self.tag('externalLink', url << link_label << child_elements, attributes)

  cell = options[:cell] || {}
  row = options[:row] || {}
  self.row_cell(link, cell, row)
end

#meta(options = {}) ⇒ Object

Options

hash - a hash of meta properites with value e.g. { :author => “Lukasz Korecki”}



157
158
159
160
161
# File 'lib/wapl_helper.rb', line 157

def meta(options={})
  metaz = ""
  options.each { |k,v|  metaz += "<meta " + self.attr_string({ :name=>k, :content => v} )+ " />" }
  return metaz
end

#row_cell(tag_string, cell = {}, row = {}) ⇒ Object

Inserts a row and a cell with a specified content

tag_string

string - the content that goes into the row/cell

cell

hash - attribute hash for the <cell /> element

row

hash - attribute hash for the <row /> element



62
63
64
65
# File 'lib/wapl_helper.rb', line 62

def row_cell(tag_string, cell={}, row={})
  cell = self.tag('cell',  tag_string, cell)
  self.tag('row', cell, row)
end

#sanitize_html(content) ⇒ Object

Sanitze content Parameters

Content

string



32
33
34
35
# File 'lib/wapl_helper.rb', line 32

def sanitize_html(content)
  require 'cgi'
  CGI.escapeHTML(content)
end

#start_waplObject

XML / root element declaration for use in the layout



147
148
149
# File 'lib/wapl_helper.rb', line 147

def start_wapl
  return '<wapl xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://wapl.wapple.net/wapl.xsd">'
end

#style_sheet(url = "") ⇒ Object

url
  • url for the external stylesheet



164
165
166
# File 'lib/wapl_helper.rb', line 164

def style_sheet(url="")
  self.tag('css', self.tag('url', url))
end

#t_e(tag_name) ⇒ Object

Close the tag Parameters

Tag_name
  • string



25
26
27
# File 'lib/wapl_helper.rb', line 25

def t_e(tag_name)
  %Q{</#{tag_name}>}
end

#t_s(tag_name, attr = "") ⇒ Object

Open a tag Parameters

Tag_name

string - name of the tag

Attribute

string - like name=“test” woop=“woop”



6
7
8
9
10
11
12
# File 'lib/wapl_helper.rb', line 6

def t_s(tag_name, attr="")
  if attr.empty?
    %Q{<#{tag_name}>}
  else
    %Q{<#{tag_name} #{attr}>}
  end
end

#tag(tag_name, content, attributes = {}) ⇒ Object

Returns a tag with optional attributes and content Parameters

Tag_name

string - the tag you wan to create e.g. “value”

Content

string = inner text of the tag

Attributes

hash - hash of attributes (key - val) for the element (optional)



49
50
51
52
53
54
55
56
# File 'lib/wapl_helper.rb', line 49

def tag(tag_name, content,  attributes = {})
  t = self.t_s(tag_name)
  unless attributes.empty?
    t = self.t_s(tag_name, self.attr_string(attributes))
  end

  t << content << self.t_e(tag_name)
end

#title(title = "") ⇒ Object

title

string - title for your page



175
176
177
# File 'lib/wapl_helper.rb', line 175

def title(title="")
  self.tag('title', title)
end