Module: HTree::Node
- Includes:
- HTree
- Included in:
- Container, Leaf, TemplateCompiler::TemplateNode
- Defined in:
- lib/htree/modules.rb,
lib/htree/loc.rb,
lib/htree/rexml.rb,
lib/htree/display.rb,
lib/htree/gencode.rb,
lib/htree/raw_string.rb,
lib/htree/raw_string.rb,
lib/htree/extract_text.rb
Overview
:startdoc:
Constant Summary
Constants included from HTree
DefaultContext, ElementContent, ElementExclusions, ElementInclusions, EmptyBindingObject, HTMLContext, NamedCharacters, NamedCharactersPattern, OmittedAttrName
Instance Method Summary collapse
-
#display_html(out = $stdout, encoding = HTree::Encoder.internal_charset) ⇒ Object
HTree::Node#display_html prints the node as HTML.
-
#display_xml(out = $stdout, encoding = HTree::Encoder.internal_charset) ⇒ Object
HTree::Node#display_xml prints the node as XML.
- #eliminate_raw_string ⇒ Object
- #extract_text ⇒ Object
- #generate_xml_output_code(outvar = 'out', contextvar = 'top_context') ⇒ Object
-
#make_loc ⇒ Object
creates a location object which points to self.
-
#raw_string ⇒ Object
raw_string returns a source string recorded by parsing.
-
#subst(pairs) ⇒ Object
subst
substitutes several subtrees at once. -
#subst_internal(pairs) ⇒ Object
:nodoc:.
-
#to_node ⇒ Object
return self.
-
#to_rexml ⇒ Object
convert to REXML tree.
Methods included from HTree
#==, build_node, #check_equality, compile_template, #exact_equal?, #exact_equal_object, expand_template, fix_element, fix_structure_list, frozen_string, #hash, #make_exact_equal_object, #make_usual_equal_object, parse, parse_as, parse_pairs, parse_xml, scan, #usual_equal_object, with_frozen_string_hash
Instance Method Details
#display_html(out = $stdout, encoding = HTree::Encoder.internal_charset) ⇒ Object
HTree::Node#display_html prints the node as HTML.
The first optional argument, out, specifies output target. It should respond to <<
. If it is not specified, $stdout is used.
The second optional argument, encoding, specifies output MIME charset (character encoding). If it is not specified, HTree::Encoder.internal_charset is used.
HTree::Node#display_html returns out.
37 38 39 40 41 42 43 |
# File 'lib/htree/display.rb', line 37 def display_html(out=$stdout, encoding=HTree::Encoder.internal_charset) encoder = HTree::Encoder.new(encoding) encoder.html_output = true self.output(encoder, HTree::HTMLContext) out << encoder.finish out end |
#display_xml(out = $stdout, encoding = HTree::Encoder.internal_charset) ⇒ Object
HTree::Node#display_xml prints the node as XML.
The first optional argument, out, specifies output target. It should respond to <<
. If it is not specified, $stdout is used.
The second optional argument, encoding, specifies output MIME charset (character encoding). If it is not specified, HTree::Encoder.internal_charset is used.
HTree::Node#display_xml returns out.
17 18 19 20 21 22 23 |
# File 'lib/htree/display.rb', line 17 def display_xml(out=$stdout, encoding=HTree::Encoder.internal_charset) encoder = HTree::Encoder.new(encoding) self.output(encoder, HTree::DefaultContext) # don't call finish_with_xmldecl because self already has a xml decl. out << encoder.finish out end |
#eliminate_raw_string ⇒ Object
63 64 65 |
# File 'lib/htree/raw_string.rb', line 63 def eliminate_raw_string raise NotImplementedError end |
#extract_text ⇒ Object
7 8 9 |
# File 'lib/htree/extract_text.rb', line 7 def extract_text raise NotImplementedError end |
#generate_xml_output_code(outvar = 'out', contextvar = 'top_context') ⇒ Object
8 9 10 11 12 13 14 15 |
# File 'lib/htree/gencode.rb', line 8 def generate_xml_output_code(outvar='out', contextvar='top_context') namespaces = HTree::Context::DefaultNamespaces.dup namespaces.default = nil context = Context.new(namespaces) gen = HTree::GenCode.new(outvar, contextvar) output(gen, context) gen.finish end |
#make_loc ⇒ Object
creates a location object which points to self.
8 9 10 |
# File 'lib/htree/loc.rb', line 8 def make_loc self.class::Loc.new(nil, nil, self) end |
#raw_string ⇒ Object
raw_string returns a source string recorded by parsing. It returns nil
if the node is constructed not via parsing.
8 9 10 11 12 13 |
# File 'lib/htree/raw_string.rb', line 8 def raw_string catch(:raw_string_tag) { return raw_string_internal('') } nil end |
#subst(pairs) ⇒ Object
subst
substitutes several subtrees at once.
t = HTree('<r><x/><y/><z/></r>')
l = t.make_loc
t2 = t.subst({
l.get_subnode(0, 'k') => 'v',
l.get_subnode(0, -1) => HTree('<a/>'),
l.get_subnode(0, 1) => nil,
l.get_subnode(0, 2, 0) => HTree('<b/>'),
})
pp t2
# =>
#<HTree::Doc
{elem <r k="v"> {emptyelem <a>} {emptyelem <x>} {elem <z> {emptyelem <b>}}}>
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 |
# File 'lib/htree/loc.rb', line 31 def subst(pairs) pairs = pairs.map {|key, val| key = key.index_list(self) unless Array === val val = [val] end [key, val] } pairs_empty_key, pairs_nonempty_key = pairs.partition {|key, val| key.empty? } if !pairs_empty_key.empty? if !pairs_nonempty_key.empty? raise ArgumentError, "cannot substitute a node under substituting tree." end result = [] pairs_empty_key.each {|key, val| result.concat val } result.compact! if result.length == 1 return result[0] else raise ArgumentError, "cannot substitute top node by multiple nodes: #{nodes.inspect}" end end if pairs_nonempty_key.empty? return self end subst_internal(pairs) end |
#subst_internal(pairs) ⇒ Object
:nodoc:
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/htree/loc.rb', line 62 def subst_internal(pairs) # :nodoc: subnode_pairs = {} pairs.each {|key, val| k = key.pop (subnode_pairs[k] ||= []) << [key, val] } subnode_pairs = subnode_pairs.map {|k, subpairs| s = get_subnode(k) subpairs_empty_key, subpairs_nonempty_key = subpairs.partition {|key, val| key.empty? } if !subpairs_empty_key.empty? if !subpairs_nonempty_key.empty? raise ArgumentError, "cannot substitute a node under substituting tree." end r = [] subpairs_empty_key.each {|key, val| r.concat val } [k, r.compact] elsif subpairs_nonempty_key.empty? [k, s] else [k, s.subst_internal(subpairs)] end } subst_subnode(subnode_pairs) end |
#to_node ⇒ Object
return self.
13 14 15 |
# File 'lib/htree/loc.rb', line 13 def to_node self end |
#to_rexml ⇒ Object
convert to REXML tree.
29 30 31 32 |
# File 'lib/htree/rexml.rb', line 29 def to_rexml require 'rexml/document' to_rexml_internal(nil, DefaultContext) end |