Class: HTree::Doc

Inherits:
Object
  • Object
show all
Includes:
Container, Trav
Defined in:
lib/htree/loc.rb,
lib/htree/doc.rb,
lib/htree/rexml.rb,
lib/htree/output.rb,
lib/htree/inspect.rb,
lib/htree/modules.rb,
lib/htree/modules.rb,
lib/htree/modules.rb,
lib/htree/equality.rb,
lib/htree/raw_string.rb,
lib/htree/raw_string.rb

Overview

:stopdoc:

Defined Under Namespace

Modules: Trav Classes: Loc

Constant Summary

Constants included from HTree

DefaultContext, ElementContent, ElementExclusions, ElementInclusions, EmptyBindingObject, HTMLContext, NamedCharacters, NamedCharactersPattern, OmittedAttrName

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Trav

#author, #has_xmldecl?, #root, #title, #traverse_all_element, #traverse_some_element

Methods included from Container::Trav

#each_child, #each_child_with_index, #each_hyperlink, #each_hyperlink_uri, #each_uri, #filter, #find_element, #traverse_element, #traverse_text_internal

Methods included from Traverse

#bogusetag?, #comment?, #doc?, #doctype?, #elem?, #get_subnode, #procins?, #text?, #traverse_text, #xmldecl?

Methods included from Container

#children, #extract_text, #find_loc_step

Methods included from Node

#display_html, #display_xml, #extract_text, #generate_xml_output_code, #make_loc, #raw_string, #subst, #subst_internal, #to_node, #to_rexml

Methods included from HTree

#==, build_node, #check_equality, compile_template, #exact_equal?, 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, with_frozen_string_hash

Constructor Details

#initialize(children = []) ⇒ Doc

:notnew:



56
57
58
59
60
61
62
63
# File 'lib/htree/doc.rb', line 56

def initialize(children=[]) # :notnew:
  @children = children.dup.freeze
  unless @children.all? {|c| c.kind_of?(HTree::Node) and !c.kind_of?(HTree::Doc) }
    unacceptable = @children.reject {|c| c.kind_of?(HTree::Node) and !c.kind_of?(HTree::Doc) }
    unacceptable = unacceptable.map {|uc| uc.inspect }.join(', ')
    raise TypeError, "Unacceptable document child: #{unacceptable}"
  end
end

Class Method Details

.new(*args) ⇒ Object

The arguments should be a sequence of follows.

String object

specified string is converted to HTree::Text.

HTree::Node object

used as a child.

HTree::Doc object

used as children. It is expanded except HTree::XMLDecl and HTree::DocType objects.

Array of String, HTree::Node and HTree::Doc

used as children.



20
21
22
23
24
25
26
27
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
# File 'lib/htree/doc.rb', line 20

def Doc.new(*args)
  children = []
  args.each {|arg|
    arg = arg.to_node if HTree::Location === arg
    case arg
    when Array
      arg.each {|a|
        a = a.to_node if HTree::Location === a
        case a
        when HTree::Doc
          children.concat(a.children.reject {|c|
            HTree::XMLDecl === c || HTree::DocType === c
          })
        when HTree::Node
          children << a
        when String
          children << Text.new(a)
        else
          raise TypeError, "unexpected argument: #{arg.inspect}"
        end
      }
    when HTree::Doc
      children.concat(arg.children.reject {|c|
        HTree::XMLDecl === c || HTree::DocType === c
      })
    when HTree::Node
      children << arg
    when String
      children << Text.new(arg)
    else
      raise TypeError, "unexpected argument: #{arg.inspect}"
    end
  }
  new!(children)
end

.new!Object



8
# File 'lib/htree/doc.rb', line 8

alias new! new

Instance Method Details

#eliminate_raw_stringObject



71
72
73
# File 'lib/htree/raw_string.rb', line 71

def eliminate_raw_string
  Doc.new(@children.map {|c| c.eliminate_raw_string })
end

#get_subnode_internal(index) ⇒ Object

:nodoc:



65
66
67
68
69
70
71
72
73
74
# File 'lib/htree/doc.rb', line 65

def get_subnode_internal(index) # :nodoc:
  unless Integer === index
    raise TypeError, "invalid index: #{index.inspect}"
  end
  if index < 0 || @children.length <= index
    nil
  else
    @children[index]
  end
end

#node_test_stringObject



90
# File 'lib/htree/loc.rb', line 90

def node_test_string() 'doc()' end

#output(out, context) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/htree/output.rb', line 62

def output(out, context)
  xmldecl = false
  @children.each {|n|
    if n.respond_to? :output_prolog_xmldecl
      n.output_prolog_xmldecl(out, context) unless xmldecl # xxx: encoding?
      xmldecl = true
    else
      n.output(out, context)
    end
  }
end

#pretty_print(q) ⇒ Object



12
13
14
# File 'lib/htree/inspect.rb', line 12

def pretty_print(q)
  q.object_group(self) { @children.each {|elt| q.breakable; q.pp elt } }
end

#raw_string_internal(result) ⇒ Object



18
19
20
21
22
# File 'lib/htree/raw_string.rb', line 18

def raw_string_internal(result)
  @children.each {|n|
    n.raw_string_internal(result)
  }
end

#subst_subnode(pairs) ⇒ Object

doc.subst_subnode(pairs) -> doc

The argument pairs should be a hash or an assocs. Its key should be an integer which means an index for children.

Its value should be one of follows.

HTree::Node object

specified object is used as is.

String object

specified string is converted to HTree::Text

Array of above

specified HTree::Node and String is used in that order.

nil

delete corresponding node.

d = HTree('<a/><b/><c/>')        
p d.subst_subnode({0=>HTree('<x/>'), 2=>HTree('<z/>')})
p d.subst_subnode([[0,HTree('<x/>')], [2,HTree('<z/>')]])
# =>
#<HTree::Doc {emptyelem <x>} {emptyelem <b>} {emptyelem <z>}>
#<HTree::Doc {emptyelem <x>} {emptyelem <b>} {emptyelem <z>}>


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
141
142
143
144
145
146
147
# File 'lib/htree/doc.rb', line 94

def subst_subnode(pairs)
  hash = {}
  pairs.each {|index, value|
    unless Integer === index
      raise TypeError, "invalid index: #{index.inspect}"
    end
    value = value.to_node if HTree::Location === value
    case value
    when Node
      value = [value]
    when String
      value = [value]
    when Array
      value = value.dup
    when nil
      value = []
    else
      raise TypeError, "invalid value: #{value.inspect}"
    end
    value.map! {|v|
      v = v.to_node if HTree::Location === v
      case v
      when Node
        v
      when String
        Text.new(v)
      else
        raise TypeError, "invalid value: #{v.inspect}"
      end
    }
    if !hash.include?(index)
      hash[index] = []
    end
    hash[index].concat value
  }

  children_left = []
  children = @children.dup
  children_right = []

  hash.keys.sort.each {|index|
    value = hash[index]
    if index < 0
      children_left << value
    elsif children.length <= index
      children_right << value
    else
      children[index] = value
    end
  }

  children = [children_left, children, children_right].flatten.compact
  Doc.new(children)
end

#to_rexml_internal(parent, context) ⇒ Object

Raises:

  • (ArgumentError)


38
39
40
41
42
43
44
45
# File 'lib/htree/rexml.rb', line 38

def to_rexml_internal(parent, context)
  raise ArgumentError, "parent must be nil" if parent != nil
  result = REXML::Document.new
  self.children.each {|c|
    c.to_rexml_internal(result, context)
  }
  result
end