raf 0.0.0 → 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/rafelement.rb ADDED
@@ -0,0 +1,219 @@
1
+ # = RafObject
2
+ module Raf
3
+ class HeadIndex
4
+ def initialize
5
+ @head_index = []
6
+ @point_pre = 0
7
+ end
8
+
9
+ def update(level)
10
+ point = level - 2
11
+ if @point_pre > point
12
+ (@point_pre - point).times do
13
+ @head_index.pop
14
+ @point_pre -= 1
15
+ end
16
+ end
17
+ @head_index[point] ||= 0
18
+ @head_index[point] += 1
19
+ @point_pre = point
20
+ end
21
+
22
+ def to_s
23
+ @head_index.map{|n|
24
+ n = 0 if n.nil?
25
+ n
26
+ }.join(".") + "."
27
+ end
28
+
29
+ def clear
30
+ @heade_index = [0,0,0,0,0,0,0]
31
+ end
32
+ end
33
+ end
34
+ module Raf
35
+ class Element
36
+ def initialize(contents = [])
37
+ @contents = contents.to_a
38
+ end
39
+ attr_reader :contents
40
+
41
+ def apply
42
+ @contents
43
+ end
44
+ def add=(content)
45
+ @contents.concat(content.to_a)
46
+ end
47
+ end
48
+
49
+ # ----- Blocks
50
+ class WhiteLine < Element
51
+ def apply # noop
52
+ ""
53
+ end
54
+ end
55
+
56
+ class PlainTextBlock < Element
57
+ def apply
58
+ @contents.map {|c| c.apply }
59
+ end
60
+ end
61
+
62
+ class HeadLine < Element
63
+ # @contents = [level, title]
64
+ def apply
65
+ "=" * @contents[0] + " #{@contents[1]}\n"
66
+ end
67
+ end
68
+
69
+ class Paragraph < Element
70
+ def apply
71
+ "#{@contents.map{|c| c.apply}}"
72
+ end
73
+ end
74
+
75
+ class Quote < Element
76
+ def apply
77
+ "#{@contents}"
78
+ end
79
+ end
80
+
81
+ class ItemList < Element
82
+ def apply
83
+ str = "<ItemList>"
84
+ @contents.map do |c|
85
+ if c == :INDENT
86
+ str += "<INDENT/>"
87
+ elsif c == :DEDENT
88
+ str +="<DEDENT/>"
89
+ else
90
+ str += "<ItemListItem>#{c.apply}</ItemListItem>"
91
+ end
92
+ end
93
+ str += "</ItemList>"
94
+ str
95
+ end
96
+ end
97
+
98
+ class NumList < Element
99
+ def apply
100
+ str = "<NumList>"
101
+ num = 1
102
+ @contents.map do |c|
103
+ if c == :INDENT
104
+ str += "<INDENT/>"
105
+ elsif c == :DEDENT
106
+ str +="<DEDENT/>"
107
+ else
108
+ str += "<NumListItem>#{num}. #{c.apply}</NumListItem>"
109
+ num += 1
110
+ end
111
+ end
112
+ str += "</NumList>"
113
+ str
114
+ end
115
+ end
116
+
117
+ class Desc < Element
118
+ def apply
119
+ ":#{@contents[0]}\n#{@contents[1].map {|c| c.apply} }"
120
+ end
121
+ end
122
+
123
+ class Table < Element
124
+ def apply
125
+ str = ""
126
+ @contents.each do |line|
127
+ line.each do |item|
128
+ if item.split(//)[0] == "*"
129
+ str += "|*#{item.sub(/^\*/, "").sub(/\*$/,"")}"
130
+ else
131
+ str += "|#{item}"
132
+ end
133
+ end
134
+ str += "\n"
135
+ end
136
+ str
137
+ end
138
+ end
139
+
140
+ # -- Blocks end
141
+
142
+ # -- Inlines
143
+ class Plain < Element
144
+ def apply
145
+ "#{contents}"
146
+ end
147
+ end
148
+
149
+ class Label < Element
150
+ # @contents = [label, title, id]
151
+ def apply
152
+ # "#{@contents[1]}(#{@contents[0]}) raf-label-#{@contents[2]}"
153
+ "#{@contents[1]}(#{@contents[0]}):#{@contents[2]}"
154
+ end
155
+ end
156
+
157
+ class Reference < Element
158
+ # @contents = [title, uri]
159
+ def apply
160
+ "#{@contents}"
161
+ end
162
+ end
163
+
164
+ class Emphasis < Element
165
+ def apply
166
+ "<Emphasis>#{@contents.map{|c| c.apply}}</Emphasis>"
167
+ end
168
+ end
169
+
170
+ class Italic < Element
171
+ def apply
172
+ "<Italic>#{@contents.map{|c| c.apply}}</Italic>"
173
+ end
174
+ end
175
+
176
+ class Strike < Element
177
+ def apply
178
+ "<Strike>#{@contents.map{|c| c.apply}}</Strike>"
179
+ end
180
+ end
181
+
182
+ class Ruby < Element
183
+ def apply
184
+ "<Ruby>Base:#{@contents[0]},Text:#{@contents[1]}</Ruby>"
185
+ end
186
+ end
187
+
188
+ class Footnote < Element
189
+ #@contents = [contents, id]
190
+ def apply
191
+ "<Footnote>#{@contents[0].map{|c| c.apply}}</Footnote>"
192
+ end
193
+ end
194
+
195
+ class Image < Element
196
+ def apply
197
+ "<Image>#{@contents}</Image>"
198
+ end
199
+ end
200
+
201
+ class Verb < Element
202
+ def apply
203
+ "<Verb>#{@contents}</Verb>"
204
+ end
205
+ end
206
+
207
+ class Manuedo < Element
208
+ def apply
209
+ "<Manuedo>#{@contents}</Manuedo>"
210
+ end
211
+ end
212
+ end
213
+
214
+
215
+ class String
216
+ def to_code
217
+ self.to_a.pack('m').tr("012345679+/=\n", 'abcdefghiPSIQ').strip
218
+ end
219
+ end