Class: RML
- Inherits:
-
Object
- Object
- RML
- Defined in:
- lib/rml.rb
Instance Method Summary collapse
-
#add_attr(*args) ⇒ Object
args - [0] args [0] Hash or String [1] Hash or String [1] hash => params.
- #allow_dirty_code(d) ⇒ Object
- #data(t) ⇒ Object
- #dirty(&block) ⇒ Object
- #doctype! ⇒ Object
- #get_attrs(args) ⇒ Object
- #get_content(args) ⇒ Object
- #headfix(*attrs, &block) ⇒ Object
- #html(*attrs, &block) ⇒ Object
-
#initialize ⇒ RML
constructor
A new instance of RML.
- #markup(&proc) ⇒ Object
- #markup_file(file) ⇒ Object
- #method_missing(sym, *args, &block) ⇒ Object
- #p(*attrs, &block) ⇒ Object
- #process_tag(name, attrs, &block) ⇒ Object
- #reinit ⇒ Object
- #rml(file) ⇒ Object
-
#rsm(file, &block) ⇒ Object
todo add cdata to script and style.
- #text(t) ⇒ Object
- #to_s ⇒ Object
- #use_bound(b) ⇒ Object
Constructor Details
#initialize ⇒ RML
Returns a new instance of RML.
105 106 107 108 |
# File 'lib/rml.rb', line 105 def initialize @dirty = false reinit end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args, &block) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/rml.rb', line 125 def method_missing sym, *args, &block args.flatten! if @tag_css @css_classes << sym if not args.empty? or not block.nil? tag = @tag_css @tag_css = nil classes = @css_classes.join(' ') @css_classes = [] method_missing tag, add_attr(args, :class => classes), &block end elsif Tags.keys.include? sym if args.empty? and block.nil? and not Tags[sym][:tags].empty? @tag_css = sym else process_tag sym, args, &block end elsif Tags.keys.include? sym.to_s.split('_')[0].to_sym if args.empty? and block.nil? and not Tags[sym.to_s.split('_')[0].to_sym][:tags].empty? @tag_css = sym else process_tag sym.to_s.split('_')[0].to_sym, add_attr(args, :id => sym.to_s.split('_')[1]), &block end else raise NoMethodError, "Undefined method '#{sym}'" end self end |
Instance Method Details
#add_attr(*args) ⇒ Object
args - [0] args [0] Hash or String [1] Hash or String [1] hash => params
198 199 200 201 202 203 204 205 206 |
# File 'lib/rml.rb', line 198 def add_attr *args i = 0 i = 1 if args[0][0].class == String args[0][i] = {} if args[0][i].nil? or args[0][i].class != Hash args[1].each_pair do |k,v| args[0][i][k] = v end args[0] end |
#allow_dirty_code(d) ⇒ Object
117 118 119 |
# File 'lib/rml.rb', line 117 def allow_dirty_code d @dirty = d end |
#data(t) ⇒ Object
248 249 250 |
# File 'lib/rml.rb', line 248 def data t @html += t end |
#dirty(&block) ⇒ Object
224 225 226 227 228 229 |
# File 'lib/rml.rb', line 224 def dirty &block @dirty = true instance_eval &block @dirty = false self end |
#doctype! ⇒ Object
252 253 254 255 256 |
# File 'lib/rml.rb', line 252 def doctype! @html += '<?xml version="1.0" encoding="utf-8" ?>'+"\r\n" @html += '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">' + "\r\n" end |
#get_attrs(args) ⇒ Object
216 217 218 219 220 221 222 |
# File 'lib/rml.rb', line 216 def get_attrs args i = -1 i = 0 if args[0].class == Hash i = 1 if args[1].class == Hash return args[i] if i >= 0 nil end |
#get_content(args) ⇒ Object
208 209 210 211 212 213 214 |
# File 'lib/rml.rb', line 208 def get_content args i = -1 i = 0 if args[0].class == String i = 1 if args[1].class == String return CGI.escapeHTML(args[i]) if i >= 0 '' end |
#headfix(*attrs, &block) ⇒ Object
239 240 241 |
# File 'lib/rml.rb', line 239 def headfix *attrs, &block method_missing :head, attrs, &block end |
#html(*attrs, &block) ⇒ Object
231 232 233 |
# File 'lib/rml.rb', line 231 def html *attrs, &block method_missing :html, add_attr(attrs, :xmlns => "http://www.w3.org/1999/xhtml"), &block end |
#markup(&proc) ⇒ Object
268 269 270 271 |
# File 'lib/rml.rb', line 268 def markup &proc instance_eval &proc self end |
#markup_file(file) ⇒ Object
258 259 260 261 262 263 264 265 266 |
# File 'lib/rml.rb', line 258 def markup_file file tmpl = '' begin tmpl = File.open(file).read rescue raise "Can't open file: #{file}" end instance_eval tmpl end |
#p(*attrs, &block) ⇒ Object
235 236 237 |
# File 'lib/rml.rb', line 235 def p *attrs, &block method_missing :p, attrs, &block end |
#process_tag(name, attrs, &block) ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/rml.rb', line 154 def process_tag name, attrs, &block raise TagNotAllowedException, "Tag <#{@tag_stack.last}> should't contain child tag <#{name}>" unless @tag_stack.empty? or Tags[@tag_stack.last][:tags].include? name or @dirty css = '' content = '' attr_str = '' attributes = get_attrs(attrs) content = get_content(attrs) if not attributes.nil? attributes.keys.each do |a| raise AttrNotAllowedException, "Attribute '#{a}' not allowed for tag <#{name}>" unless Tags[name][:attrs].include? a or @dirty end Tags[name][:required_attrs].each do |a| raise AttrRequiredException, "Attribute '#{a}' required for tag <#{name}>" unless attributes.include? a or @dirty end attributes.each_pair do |k,v| attr_str += " #{k}=\"#{CGI.escapeHTML(v.to_s)}\"" end else raise AttrRequiredException, "Attributes '#{Tags[name][:required_attrs].join(', ')}' required for tag <#{name}>" unless Tags[name][:required_attrs].empty? or @dirty end if block @html += "<#{name.to_s+attr_str}>#{content}\r\n".tab(@tag_stack.length) @tag_stack << name instance_eval &block @html += "</#{@tag_stack.pop}>\r\n".tab(@tag_stack.length) else if Tags[name][:tags].empty? or content.empty? @html += "<#{name.to_s+attr_str} />\r\n".tab(@tag_stack.length) else @html += "<#{name.to_s+attr_str}>#{content}</#{name}>\r\n".tab(@tag_stack.length) end end self end |
#reinit ⇒ Object
110 111 112 113 114 115 |
# File 'lib/rml.rb', line 110 def reinit @tag_stack = [] @html = '' @tag_css = nil @css_classes = [] end |
#rml(file) ⇒ Object
277 278 279 |
# File 'lib/rml.rb', line 277 def rml file markup_file file.to_s + ".rml" end |
#rsm(file, &block) ⇒ Object
todo add cdata to script and style
281 282 283 284 285 286 287 288 289 |
# File 'lib/rml.rb', line 281 def rsm file, &block s = RSM.new if not file.nil? s.markup File.open(file.to_s + ".rsm").read else s.markup &block end process_tag :style, [s.to_s, {:type => 'text/html'}] end |
#text(t) ⇒ Object
243 244 245 246 |
# File 'lib/rml.rb', line 243 def text t raise TextNotAllowedException, "Tag <#{@tag_stack.last}> should't contain text" unless Tags[@tag_stack.last][:tags].include?(:pcdata) or @dirty @html += CGI.escapeHTML(t).tab(@tag_stack.length)+"\r\n" end |
#to_s ⇒ Object
273 274 275 |
# File 'lib/rml.rb', line 273 def to_s @html end |
#use_bound(b) ⇒ Object
121 122 123 |
# File 'lib/rml.rb', line 121 def use_bound b @bound = b end |