~ubuntu-branches/ubuntu/wily/tdiary/wily

« back to all changes in this revision

Viewing changes to vendor/rdtool-0.6.33/lib/rd/element.rb

  • Committer: Package Import Robot
  • Author(s): Hideki Yamane
  • Date: 2013-05-19 16:14:01 UTC
  • mfrom: (12.1.1 experimental)
  • Revision ID: package-import@ubuntu.com-20130519161401-hf5oyr8g8a94fsew
Tags: 3.2.2-2
Upload to unstable 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
 
2
 
module RD
3
 
 
4
 
  # abstruct class of node of document tree
5
 
  class Element
6
 
    include Enumerable
7
 
    
8
 
    attr_accessor :parent
9
 
 
10
 
    def initialize
11
 
      @parent = nil
12
 
    end
13
 
    
14
 
    def tree
15
 
      raise RuntimeError, "#{self} doesn't have a parent." unless @parent
16
 
      @parent.tree
17
 
    end
18
 
 
19
 
    def inspect
20
 
      c  = children.collect{|i| indent2(i.inspect)}.join("\n")
21
 
      "<#{self.class.name}>" + (c.empty? ? "" : "\n") + c
22
 
    end
23
 
  end # Element
24
 
 
25
 
  # element which don't have children.
26
 
  module TerminalElement
27
 
    def children
28
 
      []
29
 
    end
30
 
 
31
 
    def each_element
32
 
      yield(self)
33
 
    end
34
 
    alias each each_element
35
 
  end
36
 
 
37
 
  # element which have children.
38
 
  module NonterminalElement
39
 
    def initialize(*arg)
40
 
      @temporary_document_structure = nil
41
 
      super
42
 
    end
43
 
 
44
 
    def children
45
 
      raise NotImplimentedError, "need #{self}#children."
46
 
    end
47
 
 
48
 
    def each_child
49
 
      children.each do |i|
50
 
        yield(i)
51
 
      end
52
 
    end
53
 
 
54
 
    def each_element(&block)
55
 
      yield(self)
56
 
      children.each do |i|
57
 
        i.each_element(&block)
58
 
      end
59
 
    end
60
 
    alias each each_element
61
 
    
62
 
    def add_child(child)
63
 
      add_child_under_document_struct(child, tree.document_struct)
64
 
    end
65
 
 
66
 
    def add_child_under_document_struct(child, document_struct)
67
 
      if document_struct.is_valid?(self, child)
68
 
        push_to_children(child)
69
 
      else
70
 
        raise ArgumentError,
71
 
          "mismatched document structure, #{self} <-/- #{child}."
72
 
      end
73
 
      return self
74
 
    end
75
 
 
76
 
    def add_children(children)
77
 
      add_children_under_document_struct(children, tree.document_struct)
78
 
    end
79
 
 
80
 
    def add_children_under_document_struct(children, document_struct)
81
 
      children.each do |i|
82
 
        add_child_under_document_struct(i, document_struct)
83
 
      end
84
 
      return self
85
 
    end
86
 
 
87
 
    def add_children_without_document_struct(new_children)
88
 
      new_children.each do |i|
89
 
        push_to_children(i)
90
 
      end
91
 
      return self
92
 
    end
93
 
 
94
 
    def push_to_children(child)
95
 
      children.push(child)
96
 
      child.parent = self
97
 
    end
98
 
 
99
 
    attr_accessor :temporary_document_structure
100
 
 
101
 
    def build(document_struct = tree.document_struct, &block)
102
 
      under_temporary_document_structure(document_struct) do
103
 
        self.instance_eval(&block)
104
 
      end
105
 
      self
106
 
    end
107
 
 
108
 
    def make_child(child_class, *args_of_new, &block)
109
 
      child = child_class.new(*args_of_new)
110
 
      if self.temporary_document_structure
111
 
        self.add_child_under_document_struct(child,
112
 
                                             self.temporary_document_structure)
113
 
        child.build(self.temporary_document_structure, &block) if block_given?
114
 
      else
115
 
        self.add_child(child)
116
 
        child.build(&block) if block_given?
117
 
      end
118
 
      child
119
 
    end
120
 
    alias new make_child
121
 
    private :new
122
 
    # NonterminalElement#new, not NonterminalElement.new
123
 
 
124
 
    def under_temporary_document_structure(document_struct)
125
 
      begin
126
 
        self.temporary_document_structure = document_struct
127
 
        yield
128
 
      ensure
129
 
        self.temporary_document_structure = nil
130
 
      end
131
 
    end
132
 
 
133
 
    def indent2(str)
134
 
      buf = ''
135
 
      str.each_line{|i| buf << "  " << i }
136
 
      buf
137
 
    end
138
 
    private :indent2
139
 
  end
140
 
 
141
 
  # root node of document tree
142
 
  class DocumentElement < Element
143
 
    include NonterminalElement
144
 
    attr_reader :blocks
145
 
    
146
 
    def initialize()
147
 
      @blocks = []
148
 
    end
149
 
 
150
 
    def accept(visitor)
151
 
      visitor.visit_DocumentElement(self)
152
 
    end
153
 
 
154
 
    alias each_block each_child
155
 
    
156
 
    def children
157
 
      @blocks
158
 
    end
159
 
  end
160
 
end