mok-parser 0.3.0

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/mokelement.rb ADDED
@@ -0,0 +1,266 @@
1
+ # -*- encoding: utf-8 -*-
2
+ # Copyright (C) garin <garin54@gmail.com> 2011
3
+ # See the included file COPYING for details.
4
+ # = MokObject
5
+ module Mok
6
+ class HeadIndex
7
+ def initialize
8
+ @head_index = []
9
+ @point_pre = 0
10
+ end
11
+
12
+ def update(level)
13
+ point = level - 2
14
+ if @point_pre > point
15
+ (@point_pre - point).times do
16
+ @head_index.pop
17
+ @point_pre -= 1
18
+ end
19
+ end
20
+ @head_index[point] ||= 0
21
+ @head_index[point] += 1
22
+ @point_pre = point
23
+ end
24
+
25
+ def to_s
26
+ @head_index.map{|n|
27
+ n = 0 if n.nil?
28
+ n
29
+ }.join(".") + "."
30
+ end
31
+
32
+ def clear
33
+ @heade_index = [0,0,0,0,0,0,0]
34
+ end
35
+ end
36
+ end
37
+ module Mok
38
+ class Element
39
+ def initialize(contents = "")
40
+ @contents = Array(contents)
41
+ end
42
+ attr_reader :contents
43
+
44
+ def apply
45
+ @contents
46
+ end
47
+ def add=(content)
48
+ @contents.concat(content.lines)
49
+ end
50
+ end
51
+
52
+ # ----- Blocks
53
+ class WhiteLine < Element
54
+ def apply # noop
55
+ ""
56
+ end
57
+ end
58
+
59
+ class PlainTextBlock < Element
60
+ def apply
61
+ @contents.map {|c| c.apply }
62
+ end
63
+ end
64
+
65
+ class HeadLine < Element
66
+ # @contents = [level, title]
67
+ def apply
68
+ "=" * @contents[0] + " #{@contents[1]}\n"
69
+ end
70
+ end
71
+
72
+ class Paragraph < Element
73
+ def apply
74
+ "#{@contents.map{|c| c.apply}}"
75
+ end
76
+ end
77
+
78
+ class Quote < Element
79
+ def apply
80
+ "<Quote>#{@contents.map{|c|c.apply}}</Quote>\n"
81
+ end
82
+ end
83
+
84
+ class Preformat < Element
85
+ def apply
86
+ "<Preformat>#{@contents}</Preformat>"
87
+ end
88
+ end
89
+
90
+ class ItemList < Element
91
+ def apply
92
+ str = "<ItemList>"
93
+ @contents.map do |c|
94
+ if c == :INDENT
95
+ str += "<INDENT/>"
96
+ elsif c == :DEDENT
97
+ str +="<DEDENT/>"
98
+ else
99
+ str += "<ItemListItem>#{c.apply}</ItemListItem>"
100
+ end
101
+ end
102
+ str += "</ItemList>"
103
+ str
104
+ end
105
+ end
106
+
107
+ class NumList < Element
108
+ def apply
109
+ str = "<NumList>"
110
+ num = 1
111
+ @contents.map do |c|
112
+ if c == :INDENT
113
+ str += "<INDENT/>"
114
+ elsif c == :DEDENT
115
+ str +="<DEDENT/>"
116
+ else
117
+ str += "<NumListItem>#{num}. #{c.apply}</NumListItem>"
118
+ num += 1
119
+ end
120
+ end
121
+ str += "</NumList>"
122
+ str
123
+ end
124
+ end
125
+
126
+ class Desc < Element
127
+ def apply
128
+ ":#{@contents[0]}\n#{@contents[1].map {|c| c.apply} }"
129
+ end
130
+ end
131
+
132
+ class Table < Element
133
+ def apply
134
+ str = ""
135
+ @contents.each do |line|
136
+ line.each do |item|
137
+ if item.split(//)[0] == "*"
138
+ str += "|*#{item.sub(/^\*/, "").sub(/\*$/,"")}"
139
+ else
140
+ str += "|#{item}"
141
+ end
142
+ end
143
+ str += "\n"
144
+ end
145
+ str
146
+ end
147
+ end
148
+
149
+ # -- Blocks end
150
+
151
+ # -- Inlines
152
+ class Empty < Element
153
+ def apply
154
+ ""
155
+ end
156
+ end
157
+
158
+ class Plain < Element
159
+ def apply
160
+ "#{contents}"
161
+ end
162
+ end
163
+
164
+ class Label < Element
165
+ # @contents = [label, title, id]
166
+ def apply
167
+ # "#{@contents[1]}(#{@contents[0]}) mok-label-#{@contents[2]}"
168
+ "#{@contents[1]}(#{@contents[0]}):#{@contents[2]}"
169
+ end
170
+ end
171
+
172
+ class LabelLink < Element
173
+ # @contents = [title, uri]
174
+ def apply
175
+ "<LabelLink>Label:#{@contents[0]},Title:#{@contents[1]}</LabelLink>"
176
+ end
177
+ end
178
+
179
+ class Reference < Element
180
+ # @contents = [title, uri]
181
+ def apply
182
+ "<Reference>Title:#{@contents[0]},Url:#{@contents[1]}</Reference>"
183
+ end
184
+ end
185
+
186
+ class Emphasis < Element
187
+ def apply
188
+ "<Emphasis>#{@contents.map{|c| c.apply}}</Emphasis>"
189
+ end
190
+ end
191
+
192
+ class Italic < Element
193
+ def apply
194
+ "<Italic>#{@contents.map{|c| c.apply}}</Italic>"
195
+ end
196
+ end
197
+
198
+ class Strike < Element
199
+ def apply
200
+ "<Strike>#{@contents.map{|c| c.apply}}</Strike>"
201
+ end
202
+ end
203
+
204
+ class Code < Element
205
+ def apply
206
+ "<Code>#{@contents.map{|c| c.apply}}</Code>"
207
+ end
208
+ end
209
+
210
+ class Kbd < Element
211
+ def apply
212
+ "<Kbd>#{@contents.map{|c| c.apply}}</Kbd>"
213
+ end
214
+ end
215
+
216
+ class Ruby < Element
217
+ def apply
218
+ "<Ruby>Base:#{@contents[0]},Text:#{@contents[1]}</Ruby>"
219
+ end
220
+ end
221
+
222
+ class Variable < Element
223
+ def apply
224
+ "<Variable>#{@contents}</Variable>"
225
+ end
226
+ end
227
+
228
+ class Footnote < Element
229
+ #@contents = [contents, id]
230
+ def apply
231
+ "<Footnote>#{@contents[0].map{|c| c.apply}}</Footnote>"
232
+ end
233
+ end
234
+
235
+ class Media < Element
236
+ def apply
237
+ # @contents = [Name, Mime::MediaType, Mime::SubType]
238
+ "<Media>Name:#{@contents[0]},Mime::MediaType:#{@contents[1]},Mime::SubType:#{@contents[2]}</Media>"
239
+ end
240
+ end
241
+
242
+ class Verb < Element
243
+ def apply
244
+ "<Verb>#{@contents}</Verb>"
245
+ end
246
+ end
247
+
248
+ class Manuedo < Element
249
+ def apply
250
+ "<Manuedo>#{@contents}</Manuedo>"
251
+ end
252
+ end
253
+ end
254
+
255
+ class String
256
+ def to_code
257
+ self.lines.to_a.pack('m').tr("012345679+/=\n", 'abcdefghiPSIQ').strip
258
+ end
259
+ end
260
+
261
+ # support for ruby-2.0.0
262
+ class Array
263
+ def to_s
264
+ self.join
265
+ end
266
+ end