bade 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/lib/bade/node.rb ADDED
@@ -0,0 +1,116 @@
1
+
2
+ require_relative 'parser'
3
+ require_relative 'ruby_extensions/object'
4
+
5
+
6
+ module Bade
7
+ class Node
8
+ require_relative 'node/key_value_node'
9
+ require_relative 'node/tag_node'
10
+ require_relative 'node/mixin_node'
11
+ require_relative 'node/doctype_node'
12
+
13
+ # @return [Symbol]
14
+ #
15
+ attr_reader :type
16
+
17
+
18
+ # @return [Int] line number
19
+ #
20
+ attr_accessor :lineno
21
+
22
+ # @return [String]
23
+ #
24
+ attr_accessor :data
25
+
26
+
27
+ # @return [Node]
28
+ #
29
+ attr_accessor :parent
30
+
31
+ # @return [Array<Node>]
32
+ #
33
+ attr_accessor :childrens
34
+
35
+ # @return [TrueClass, FalseClass]
36
+ #
37
+ attr_accessor :escaped
38
+
39
+ # @param [Symbol] type
40
+ # @param [Node] parent
41
+ #
42
+ def initialize(type, parent = nil)
43
+ @type = type
44
+ @childrens = []
45
+
46
+ if parent
47
+ parent << self
48
+ end
49
+ end
50
+
51
+ # @param [Node] node
52
+ #
53
+ def << (node)
54
+ node.parent = self
55
+ @childrens << node
56
+
57
+ self
58
+ end
59
+
60
+
61
+
62
+
63
+
64
+ # @return [Hash<Symbol, Class>]
65
+ def self.registered_types
66
+ @@registered_types ||= {}
67
+ end
68
+
69
+ # @param [Symbol] type
70
+ # @param [Class] klass registering class
71
+ #
72
+ def self.register_type(type, klass = self)
73
+ raise StandardError, "Class #{klass} should be subclass of #{self}" unless klass <= Node
74
+
75
+ registered_types[type] = klass
76
+ end
77
+
78
+
79
+
80
+
81
+ def self.create(type, parent)
82
+ klass = registered_types[type]
83
+
84
+ if klass.nil?
85
+ raise Parser::ParserInternalError, "undefined node type #{type.inspect}"
86
+ end
87
+
88
+ klass.new(type, parent)
89
+ end
90
+
91
+ end
92
+
93
+ Node.register_type :text
94
+ Node.register_type :newline
95
+ Node.register_type :ruby_code
96
+
97
+ Node.register_type :comment
98
+ Node.register_type :html_comment
99
+
100
+ TagNode.register_type :tag
101
+ KeyValueNode.register_type :tag_attribute
102
+
103
+ Node.register_type :output
104
+
105
+ Node.register_type :mixin_param
106
+ Node.register_type :mixin_block_param
107
+ Node.register_type :mixin_block
108
+
109
+ MixinCallNode.register_type :mixin_call
110
+ MixinDeclarationNode.register_type :mixin_declaration
111
+ KeyValueNode.register_type :mixin_key_param
112
+
113
+ DoctypeNode.register_type :doctype
114
+
115
+ Node.register_type :import
116
+ end