Class: DOMtempl
- Inherits:
-
Object
- Object
- DOMtempl
- Defined in:
- lib/domtempl.rb
Constant Summary collapse
- FRAGMENT =
0x00000001- PRETTIFY =
0x00000002
Instance Attribute Summary collapse
-
#vars ⇒ Object
Returns the value of attribute vars.
Instance Method Summary collapse
- #assign(path, var) ⇒ Object
- #dump ⇒ Object
- #dumpXML ⇒ Object
- #error(str) ⇒ Object
- #expand_path(node, base, path = nil) ⇒ Object
-
#initialize(input, flags = 0) ⇒ DOMtempl
constructor
A new instance of DOMtempl.
- #out ⇒ Object
- #outXML ⇒ Object
- #parse ⇒ Object
- #parse_vars_node(root) ⇒ Object
- #read_var(path) ⇒ Object
- #reflow ⇒ Object
- #replace_vars(root) ⇒ Object
- #replace_vars_node(node, clean) ⇒ Object
- #safe_clone(elem, after = nil) ⇒ Object
- #safe_remove(node) ⇒ Object
- #write_var(path, val, no_overwrite = false) ⇒ Object
Constructor Details
#initialize(input, flags = 0) ⇒ DOMtempl
Returns a new instance of DOMtempl.
11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/domtempl.rb', line 11 def initialize(input, flags = 0) @vars = { } @var_iters = { } if (!(flags & FRAGMENT).zero?) @_document = Nokogiri::XML::DocumentFragment.parse(input) else @_document = Nokogiri::HTML(open(input)) end parse() end |
Instance Attribute Details
#vars ⇒ Object
Returns the value of attribute vars.
9 10 11 |
# File 'lib/domtempl.rb', line 9 def vars @vars end |
Instance Method Details
#assign(path, var) ⇒ Object
342 343 344 |
# File 'lib/domtempl.rb', line 342 def assign(path, var) write_var(path, var) end |
#dump ⇒ Object
346 347 348 349 |
# File 'lib/domtempl.rb', line 346 def dump() reflow() return @_document.to_html() end |
#dumpXML ⇒ Object
355 356 357 358 |
# File 'lib/domtempl.rb', line 355 def dumpXML() reflow() return @_document.toxml() end |
#error(str) ⇒ Object
24 25 26 |
# File 'lib/domtempl.rb', line 24 def error( str) puts "Error:" + str end |
#expand_path(node, base, path = nil) ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/domtempl.rb', line 142 def (node, base, path = nil) if (path == nil) then path = node.get_attribute(base) end top = node.parent while path[0] != '/' top_path = ''; if (!top) then path = '/' + path; break; end if (top.type != Nokogiri::XML::Node::ELEMENT_NODE) top = top.parent next; end if (top.has_attribute?('data-from')) top_path = top.get_attribute('data-from') + '.'; elsif (top.has_attribute?('data-each')) top_path = top.get_attribute('data-each') + '/'; elsif (top.has_attribute?('data-same')) top_path = top.get_attribute('data-same') + '/'; elsif (top.has_attribute?('data-when')) top_path = top.get_attribute('data-when') + '.'; end path = top_path + path; top = top.parent end return path end |
#out ⇒ Object
351 352 353 |
# File 'lib/domtempl.rb', line 351 def out() print dump() end |
#outXML ⇒ Object
360 361 362 |
# File 'lib/domtempl.rb', line 360 def outXML() print dumpXML() end |
#parse ⇒ Object
338 339 340 |
# File 'lib/domtempl.rb', line 338 def parse() parse_vars_node(@_document) end |
#parse_vars_node(root) ⇒ Object
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/domtempl.rb', line 169 def parse_vars_node(root) for node in root.children if (node.type == Nokogiri::XML::Node::ELEMENT_NODE and node.attributes()) if node.has_attribute?('data-each') @var_iters[ (node, 'data-each') ] = 0 end if node.has_attribute?('data-same') @var_iters[ (node, 'data-same') ] += 1 end #for attr in node.get_attributes: # print attr for i in 0..node.attributes.length-1 attr = node.attribute_nodes[i] if (attr.name.include?('data-attr-')) key = attr.name['data-attr'.length..-1] write_var( (node, '', not(attr.value) ? key : attr.value), node.get_attribute(key).to_s ) end end if node.has_attribute?('data-var') write_var( (node, 'data-var'), node.inner_text() ) end if node.has_attribute?('data-when') write_var( (node, 'data-when'), true, 1 ) end end if node.children parse_vars_node(node) end end end |
#read_var(path) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/domtempl.rb', line 28 def read_var(path) if (path[0] == '/') then path = path[1..-1] end walk = path.split(/(\.|\/)/, -1) #, -1, PREG_SPLIT_DELIM_CAPTURE); #puts "let's r-walk [" + path + "] {" #puts walk #puts " } " #puts @vars cpath = '/' ptr = @vars last = walk[ walk.length - 1 ] (0..walk.length-2).step(2).each do |i| step = walk[i] mod = walk[i+1] cpath += step if (ptr[step].nil?) error('undefined array "' + step + '" of path ' + path) return nil; end ptr = ptr[step]; if (mod == '/') n = @var_iters[cpath]; #puts sprintf("Iterator of `%s` is %d", cpath, n) if (ptr[n].nil?) error(('cant iterate through "%d"' % n) + ' of path ' + path); return nil; end ptr = ptr[n]; if (last == '*' and i == walk.length - 3) then return n; end # Hack -- iterator itself if (last == '' and i == walk.length - 3) then break; end end cpath += mod; end if (last == '') return ptr; end if (ptr[last].nil?) error('undefined variable "'+last+'" of path "' + path + '"'); return nil; end return ptr[ last ]; end |
#reflow ⇒ Object
326 327 328 329 330 331 332 333 334 335 336 |
# File 'lib/domtempl.rb', line 326 def reflow() # Reset iteration counters if (@var_iters) for i in @var_iters.keys @var_iters[i] = 0; end end # Reflow all variables replace_vars(@_document); end |
#replace_vars(root) ⇒ Object
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 |
# File 'lib/domtempl.rb', line 264 def replace_vars(root) for i in 0..root.children.length-1 if i >= root.children.length # because range/xrange doesn't change :(( break end node = root.children[i]; clean = [] if (node.type == Nokogiri::XML::Node::ELEMENT_NODE and node.attributes()) if node.has_attribute?('data-when') if not( read_var((node, 'data-when')) ) i -= safe_remove(node); next; end clean.push( 'data-when' ); end if (node.has_attribute?('data-same')) clean.push( 'data-same' ); next; end if (node.has_attribute?('data-each')) clean.push( 'data-each' ); path = (node, 'data-each'); arr = read_var(path); # Kill marked siblings kill = node.next_sibling; while (kill) _next = kill.next_sibling; if (kill.type == Nokogiri::XML::Node::ELEMENT_NODE and kill.has_attribute('data-same')) safe_remove(kill); end kill = _next; end #print "Cloning time with" #print arr if (arr.length) # Clone new siblings last = nil; (1..arr.length-1).each do |j| #puts sprintf("Doing clone #%d, setting var iter of /%s", j, path); @var_iters[path] = j; nod = safe_clone(node, last); last = nod; nod.remove_attribute('data-each'); nod.set_attribute('data-same', path); replace_vars_node(nod, ['data-same']); end @var_iters[path] = 0; end #print "CLONE COMPLETE" end replace_vars_node( node, clean ); end end end |
#replace_vars_node(node, clean) ⇒ Object
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/domtempl.rb', line 217 def replace_vars_node(node, clean) stop_here = false; #hack, for speed if (node.attributes) if (node.type == Nokogiri::XML::Node::ELEMENT_NODE) j = -1 #for (j = 0; j < node.get_attributes.length; j++): while j < node.attributes.length - 1 j += 1 attr = node.attribute_nodes[j]; if (attr.name.include?('data-attr-')) key = attr.name['data-attr-'.length..-1] path = (node, '', (not(attr.value) ? key : attr.value)); val = read_var(path); if (val != false) node.set_attribute(key, val); end clean.push( attr.name ); end end if (node.has_attribute?('data-var')) #print "\nReplacing data-var for `" #print node #puts "` using the path `" + expand_path(node, 'data-var') + "`" clean.push( 'data-var' ) node.inner_html = read_var((node, 'data-var')) ; #print "Inner html is now:" + node.toxml() #print "\n" stop_here = true; # do not traverse children of inserted node end end end if (node.children and not(stop_here)) # stop here if 'data-var' was used replace_vars(node); end for attr in clean node.remove_attribute(attr); end end |
#safe_clone(elem, after = nil) ⇒ Object
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 |
# File 'lib/domtempl.rb', line 382 def safe_clone(elem, after = nil) if after == nil then after = elem; end #print "Must clone " #puts elem #print " after " #puts after #if (elem.cloneNode) orig = elem.previous_sibling; ident = nil; if (orig != nil \ and orig.type == Nokogiri::XML::Node::TEXT_NODE \ and orig.inner_text.strip() == '' #and orig.isWhitespaceInElementContent ) ident = orig.clone(); #print "Found ident (" #print ident.inner_text.length #print ident #print ")" #puts "" end cln = elem.clone(); # Note: because Nokogiri has different DOM-append methods, # we do not need appendChild/insertBefore wraps after.after(cln); if (ident) then cln.before(ident); end return cln; #end #return nil; end |
#safe_remove(node) ⇒ Object
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 |
# File 'lib/domtempl.rb', line 364 def safe_remove(node) ident = node.previous_sibling; #print "and ws is " #print ident.isWhitespaceInElementContent r = 0; if (ident != nil \ and ident.type == Nokogiri::XML::Node::TEXT_NODE \ and ident.inner_text.strip() == '' #and ident.isWhitespaceInElementContent ) ident.remove(); r = 1; end node.remove() return r end |
#write_var(path, val, no_overwrite = false) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/domtempl.rb', line 83 def write_var(path, val, no_overwrite = false) if (path[0] == '/') then path = path[1..path.length]; end walk = path.split(/(\.|\/)/, -1); #, -1, PREG_SPLIT_DELIM_CAPTURE); cpath = '/'; ptr = @vars; last = walk[ walk.length - 1 ]; #puts "Lets w-walk [ " + path + "] { " #puts walk #puts "}" (0..walk.length-2).step(2).each do |i| step = walk[i] mod = walk[i+1] cpath += step if (mod == '/') n = 0; if (ptr[step].nil? or ptr[step] == true) ptr[step] = [ ] @var_iters[cpath] = 0 else n = @var_iters[cpath] end ptr = ptr[step] if (last == '' and i == walk.length - 3) then break; end if (ptr[n].nil?) ptr[n] = { } end ptr = ptr[n] end if (mod == '.') if (ptr[step].nil? or ptr[step] == true) ptr[step] = { } end ptr = ptr[step] end cpath += mod end if (last == '') #print ptr #if isinstance(ptr, dict) # ptr[ ptr.length ] = val; #else ptr.push( val ); #end return; end if (no_overwrite and not(ptr[last].nil?)) then return; end # puts "Setting [" + last + "]" # puts "in" # puts ptr ptr[ last ] = val; end |