org-parse 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (68) hide show
  1. data/.document +5 -0
  2. data/.gitignore +23 -0
  3. data/ChangeLog +4 -0
  4. data/LICENSE +20 -0
  5. data/README.rdoc +68 -0
  6. data/Rakefile +54 -0
  7. data/VERSION.yml +5 -0
  8. data/bin/org-parse +51 -0
  9. data/bin/org-test +74 -0
  10. data/doc/images/org-parse-struct_1ffae50f0c5eb867f9418df6800f40a5cc3d1751.png +0 -0
  11. data/doc/org-parse.html +203 -0
  12. data/doc/org-parse.org +71 -0
  13. data/doc/struct.dot +10 -0
  14. data/doc/struct.png +0 -0
  15. data/examples/body-only.html.erb +1 -0
  16. data/examples/dot.org-parse-rc +21 -0
  17. data/lib/org-parse/inline-parser.output +945 -0
  18. data/lib/org-parse/inline-parser.rb +219 -0
  19. data/lib/org-parse/inline-parser.ry +77 -0
  20. data/lib/org-parse/inline-parser.tab.rb +411 -0
  21. data/lib/org-parse/node.rb +329 -0
  22. data/lib/org-parse/struct-parser.output +1019 -0
  23. data/lib/org-parse/struct-parser.rb +78 -0
  24. data/lib/org-parse/struct-parser.ry +125 -0
  25. data/lib/org-parse/struct-parser.tab.rb +608 -0
  26. data/lib/org-parse/struct-scanner.rb +272 -0
  27. data/lib/org-parse/templates/single.html.erb +118 -0
  28. data/lib/org-parse/textile-visitor.rb +296 -0
  29. data/lib/org-parse/utils.rb +15 -0
  30. data/lib/org-parse/visitor.rb +542 -0
  31. data/lib/org-parse.rb +46 -0
  32. data/org-parse.gemspec +113 -0
  33. data/rakelib/racc.rake +16 -0
  34. data/test/data/blocks.org +67 -0
  35. data/test/data/emphasis.org +7 -0
  36. data/test/data/footnote.html +136 -0
  37. data/test/data/footnote.org +8 -0
  38. data/test/data/html-export.html +1062 -0
  39. data/test/data/html-export.org +342 -0
  40. data/test/data/images.html +179 -0
  41. data/test/data/images.org +30 -0
  42. data/test/data/index.org +242 -0
  43. data/test/data/lily20100228.jpg +0 -0
  44. data/test/data/lily20100228t.jpg +0 -0
  45. data/test/data/link.org +7 -0
  46. data/test/data/list_before_1st_headline.html +119 -0
  47. data/test/data/list_before_1st_headline.org +7 -0
  48. data/test/data/lists.html +284 -0
  49. data/test/data/lists.org +78 -0
  50. data/test/data/no-headline.org +6 -0
  51. data/test/data/one-headline.org +2 -0
  52. data/test/data/paragraph.org +13 -0
  53. data/test/data/quote.org +15 -0
  54. data/test/data/sections.html +173 -0
  55. data/test/data/sections.org +9 -0
  56. data/test/data/simple-list.org +6 -0
  57. data/test/data/skip_t.org +3 -0
  58. data/test/data/structure.org +53 -0
  59. data/test/data/table.org +14 -0
  60. data/test/data/test-list.org +12 -0
  61. data/test/data/text-bef-hl.org +5 -0
  62. data/test/data/text.org +6 -0
  63. data/test/data/title.html +88 -0
  64. data/test/data/title.org +6 -0
  65. data/test/data/verse.org +48 -0
  66. data/test/helper.rb +31 -0
  67. data/test/test_org-parse.rb +148 -0
  68. metadata +134 -0
@@ -0,0 +1,329 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ module OrgParse
4
+
5
+ # syntax tree node
6
+ #
7
+ # _children_ is Node array
8
+ #
9
+ # === Node kind
10
+ # - _ROOT_ :: RootNode
11
+ #
12
+ # lines (StructParser)
13
+ # - _SECTION_ :: SectionNode
14
+ # - _HEADLINE_ :: HeadlineNode
15
+ # - _FN_DEFINE_ :: footnote definition
16
+ # - _VARIABLE_ :: VarNode
17
+ # - _WHITELINE_ :: WhitelineNode
18
+ # - _TEXTBLOCK_ :: TextlineNode's list will become paragraph
19
+ # - _TEXTLINE_ :: TextlineNode
20
+ # - _QUOTE_ :: HTML quoted text
21
+ # - _BLOCK_ :: BlockNode (verse, example, src, html, ...)
22
+ # - _UNORDERED_LIST_ :: Unorderd list <UL>
23
+ # - _ENUMLIST_ :: Ordered list <OL>
24
+ # - _DESCLIST_ :: Description list <DL>
25
+ # - _LISTITEM_ :: ListitemNode
26
+ # - _TABLE_ :: TableNode
27
+ # - _TABLE_SEP_ :: separator between th and td
28
+ # - _TABLE_ROW_ :: TableRowNode
29
+ #
30
+ # inlines (InlineParser)
31
+ # - _BOLD_ :: bold
32
+ # - _ITALIC_ :: italic
33
+ # - _UNDER_LINE_ :: under line
34
+ # - _VERBATIM_ :: verbatim
35
+ # - _STRIKE_THROUGH_ :: strike through
36
+ # - _CODE_ :: code
37
+ # - _LINK_ :: LinkNode 参照
38
+ # - _QUOTE_ :: @<br/> , #+HTML ... 等
39
+ #
40
+ class Node
41
+ # Node type
42
+ attr_accessor :kind
43
+ # child nodes
44
+ attr_accessor :children
45
+ # Node value
46
+ attr_accessor :value
47
+ # Parent node
48
+ attr_accessor :parent
49
+
50
+ def initialize(kind = nil, children = [], value = nil)
51
+ @kind = kind
52
+ @children = children
53
+ @value = value
54
+ @done = false
55
+ @parent = nil
56
+ @verse = false
57
+ @example = false
58
+ @html = false
59
+ @src = false
60
+ end
61
+
62
+ def set_verse
63
+ @verse = true
64
+ end
65
+
66
+ def verse?
67
+ @verse
68
+ end
69
+
70
+ def set_example
71
+ @example = true
72
+ end
73
+
74
+ def example?
75
+ @example
76
+ end
77
+
78
+ def set_src
79
+ @src = true
80
+ end
81
+
82
+ def src?
83
+ @src
84
+ end
85
+
86
+ def set_html
87
+ @html = true
88
+ end
89
+
90
+ def html?
91
+ @html
92
+ end
93
+
94
+ def done?
95
+ @done
96
+ end
97
+
98
+ def done
99
+ @done = true
100
+ end
101
+
102
+ def is_leaf?
103
+ @children.empty?
104
+ end
105
+
106
+ #def is_container?
107
+ # [:SECTION, :TEXTBLOCK, :LISTITEM, :UNORDERED_LIST, :ENUMLIST, :DESCLIST, :BLOCK].include? @kind
108
+ #end
109
+
110
+ def inspect
111
+ c = ''
112
+ c = @children.collect{|i| indent2(i.inspect)}.join("\n") if @children
113
+ val = @value ? @value.to_s : '(nil)'
114
+ val = val.is_a?( Array) ? val.join(",") : val.gsub("\n", ' ')
115
+ "<#{self.class.name} #{@kind}:#{val}>" + (c.empty? ? "" : "\n") + c
116
+ end
117
+
118
+ def section_no_array
119
+ if @section_no
120
+ @section_no.split('.')
121
+ else
122
+ nil
123
+ end
124
+ end
125
+
126
+ def set_to_descendant(method, val=nil)
127
+ @children.each{|node|
128
+ if val
129
+ node.send method, *val
130
+ else
131
+ node.send method
132
+ end
133
+ node.set_to_descendant method, val
134
+ }
135
+ end
136
+
137
+ private
138
+
139
+ def indent2(str)
140
+ buf = ''
141
+ str.each_line{|i| buf << " " << i }
142
+ buf
143
+ end
144
+ end
145
+
146
+ # Root
147
+ #
148
+ #
149
+ class RootNode < Node
150
+ # options
151
+ attr_reader :options
152
+ attr_accessor :section_no
153
+
154
+ def initialize(opts)
155
+ super(:ROOT)
156
+ @options = opts
157
+ @section_no = "0"
158
+ end
159
+
160
+ def add(nodes)
161
+ @children += nodes
162
+ end
163
+ end
164
+
165
+ # セクション保持用
166
+ #
167
+ # _children[0]_ には、必ず Headline Nodeが入る
168
+ #
169
+ class SectionNode < Node
170
+ attr_accessor :section_no
171
+
172
+ # コンストラクタ
173
+ # [_headline_] セクション開始の Headline
174
+ # [_bodyitems_] セクションに含まれるブロック要素
175
+ def initialize(headline, bodyitems)
176
+ @section_no = "0"
177
+ headline.parent = self
178
+ super(:SECTION, bodyitems, headline)
179
+ end
180
+
181
+ # Headline のノードを返す
182
+ def headline
183
+ @value
184
+ end
185
+ end
186
+
187
+ # Headline を保持するクラス
188
+ class HeadlineNode < Node
189
+ attr_reader :is_comment, :level
190
+
191
+ def initialize(children, level)
192
+ @is_comment = false
193
+ @level = level.size
194
+ super(:HEADLINE, children)
195
+ end
196
+ end
197
+
198
+ # Whiteline
199
+ class WhitelineNode < Node
200
+ attr_reader :count
201
+ def initialize
202
+ super(:WHITELINE)
203
+ @value = 1
204
+ end
205
+ def increment
206
+ @value += 1
207
+ end
208
+ def count
209
+ @value
210
+ end
211
+ end
212
+
213
+
214
+ class TextlineNode < Node
215
+ attr_reader :indent
216
+
217
+ def initialize(children, vals)
218
+ @indent = vals[1]
219
+ super(:TEXTLINE, children, vals[0])
220
+ end
221
+
222
+ end
223
+
224
+ # リストアイテム
225
+ class ListitemNode < Node
226
+ attr_reader :dt, :type
227
+
228
+ def initialize(type, children, value, dt)
229
+ @type = type
230
+ @dt = dt
231
+ super(:LIST_ITEM, children, value)
232
+ end
233
+ end
234
+
235
+ # BLOCK の情報を保持
236
+ class BlockNode < Node
237
+ attr_reader :indent, :block_name, :syntax, :syntax_theme
238
+ def initialize(vals, children)
239
+ @block_name = vals[0].upcase
240
+ @indent = vals[2]
241
+ @syntax = ''
242
+ @syntax_theme = ''
243
+ super(:BLOCK, children, vals[1])
244
+ case @block_name
245
+ when 'VERSE'
246
+ set_to_descendant :set_verse
247
+ when 'EXAMPLE'
248
+ set_to_descendant :set_example
249
+ when 'SRC'
250
+ set_to_descendant :set_src
251
+ if vals[1] =~ /SRC\s*([^\s]+)\s+([^\s]+)\s*$/i
252
+ @syntax = $1.downcase
253
+ @syntax_theme = $2.downcase
254
+ elsif vals[1] =~ /SRC\s*(.+)\s*$/i
255
+ @syntax = $1.downcase
256
+ end
257
+ when 'HTML'
258
+ set_to_descendant :set_html
259
+ end
260
+ end
261
+
262
+ def line
263
+ @value
264
+ end
265
+ end
266
+
267
+ class TableNode < Node
268
+ attr_reader :hrows
269
+
270
+ def initialize(hrows, drows)
271
+ hrows.each{|h| h.is_head = true }
272
+ @hrows = hrows
273
+ super(:TABLE, drows)
274
+ end
275
+ end
276
+
277
+ # table row
278
+ class TableRowNode < Node
279
+ def initialize(cols, head = false)
280
+ super(:TABLE_ROW, cols, head)
281
+ end
282
+
283
+ def is_head?
284
+ @value
285
+ end
286
+
287
+ def is_head=(val)
288
+ @value = val
289
+ end
290
+ end
291
+
292
+ # Link保持
293
+ #
294
+ class LinkNode < Node
295
+ attr_reader :html_options, :caption
296
+
297
+ def initialize(uri, children = [], vars = [])
298
+ @html_options = {}
299
+ @caption = nil
300
+ vars.each do |v|
301
+ if v =~ /^CAPTION:(.+)$/
302
+ @caption = $1.chomp
303
+ else
304
+ while v =~ /([^ =]+)=("[^"]+")/
305
+ @html_options[$1] = $2
306
+ v = $'
307
+ end
308
+ end
309
+ end
310
+ super(:LINK, children, uri)
311
+ end
312
+
313
+ # href
314
+ def uri
315
+ @value
316
+ end
317
+ end
318
+
319
+ # オプション変数
320
+ class VarNode < Node
321
+ attr_reader :name
322
+
323
+ def initialize(name, val)
324
+ @name = val
325
+ super(:VARIABLE, [], val)
326
+ end
327
+ end
328
+
329
+ end