org-parse 0.1.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.
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,78 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'racc/parser'
3
+ require ::File.join(OrgParse::LIBPATH , 'org-parse', 'struct-parser.tab.rb')
4
+ require ::File.join(OrgParse::LIBPATH , 'org-parse', 'utils.rb')
5
+ require ::File.join(OrgParse::LIBPATH , 'org-parse', 'inline-parser.rb')
6
+
7
+ module OrgParse
8
+
9
+ # パーサー
10
+ #
11
+ # 構造レベルの構文解析を行う。
12
+ # 各行の中身については、InlineParserを、内部的に呼び出して解析している。
13
+ #
14
+ # ROOT--+-- ブロック要素
15
+ # | +--- ブロック要素
16
+ # +-- ブロック要素
17
+ # の様な形式の構文木を作成する。
18
+ # Node クラスを参照
19
+ #
20
+ class StructParser < Racc::Parser
21
+ include Utils
22
+ include InlineUtils
23
+
24
+ # コンストラクタ
25
+ def initialize(src, title = '(non title)')
26
+ @scanner = StructScanner.new(src, title)
27
+ @variables = []
28
+ @yydebug = true
29
+ set_struct_parser self
30
+ end
31
+
32
+ # 構文解析を実行し、構文木を返す
33
+ def parse
34
+ @scanner.scan
35
+ root = do_parse
36
+ update_nodes root
37
+ root
38
+ end
39
+
40
+ def update_nodes(node, opt = {})
41
+ cnt = 1;
42
+
43
+ section_no = node.section_no if node.kind == :SECTION
44
+ node.children.each do |n|
45
+ if n.is_a? Array
46
+ n.each {|a|
47
+ a.parent = node
48
+ update_nodes a, opt
49
+ }
50
+ next
51
+ end
52
+ n.parent = node
53
+ if n.kind == :SECTION
54
+ if node.kind == :ROOT
55
+ n.section_no = cnt.to_s
56
+ else
57
+ n.section_no = section_no + "." + cnt.to_s
58
+ end
59
+ cnt += 1
60
+ end
61
+ update_nodes n, opt
62
+ end
63
+ end
64
+
65
+ def next_token
66
+ (token, variables) = @scanner.next_token
67
+ @variables = variables unless variables.empty?
68
+ token
69
+ end
70
+
71
+ def variables
72
+ ret = @variables.dup
73
+ @variables.clear
74
+ ret
75
+ end
76
+
77
+ end
78
+ end
@@ -0,0 +1,125 @@
1
+ class OrgParse::StructParser
2
+
3
+ preclow
4
+ nonassoc PLOW
5
+ nonassoc TEXTLINE WHITELINE TABLE_ROW TABLE_SEP QUOTE BLOCK_END EXAMPLE
6
+ prechigh
7
+
8
+ token DOCUMENT_START HEADLINE TEXTLINE WHITELINE VARIABLELINE
9
+ PLOW SEC_END TABLE_ROW TABLE_SEP QUOTE EXAMPLE
10
+ BLOCK_START BLOCK_END UL_START UL_END
11
+ OL_START OL_END DL_START DL_END
12
+ LI_START LI_END P_END FOOTNOTE
13
+
14
+ rule
15
+ document : dummy_section
16
+ | dummy_section sections { result.add val[1] }
17
+
18
+ dummy_section : doc_start items { result.add val[1] }
19
+ | doc_start
20
+
21
+ doc_start : DOCUMENT_START { result = RootNode.new(val[0]) }
22
+
23
+ sections : sections section { result << val[1] }
24
+ | section { result = val }
25
+
26
+ section : section_body SEC_END {
27
+ headline = val[0][0]
28
+ val[0].shift
29
+ result = SectionNode.new(headline, val[0])
30
+ }
31
+
32
+ section_body : headline { result = val }
33
+ | headline sitems { result = [val[0]] + val[1] }
34
+
35
+ headline : HEADLINE { result = HeadlineNode.new(line_parse(val[0][1]), val[0][0]) }
36
+
37
+ sitems : sitems sitem = PLOW { result << val[1] }
38
+ | sitem { result = val }
39
+
40
+ sitem : section
41
+ | textlines = PLOW
42
+ | block
43
+ | table = PLOW
44
+ | lists
45
+ | whitelines = PLOW
46
+ | variable
47
+ | footnote
48
+
49
+ items : items item = PLOW { result << val[1] }
50
+ | item { result = val }
51
+
52
+ item : textlines = PLOW
53
+ | block
54
+ | table = PLOW
55
+ | lists
56
+ | whitelines = PLOW
57
+ | variable
58
+ | footnote
59
+
60
+ footnote : FOOTNOTE { result = Node.new(:FN_DEFINE, line_parse(val[0][1]), val[0][0]) }
61
+
62
+ variable : VARIABLELINE { result = VarNode.new(val[0][0], val[0][1]) }
63
+
64
+ whitelines : whitelines WHITELINE { result.increment }
65
+ | WHITELINE { result = WhitelineNode.new }
66
+
67
+ textlines : textlines textline = TEXTLINE { result.children << val[1] }
68
+ | textline { result = Node.new(:TEXTBLOCK, val, val[0].value) }
69
+
70
+ textline : TEXTLINE { result = TextlineNode.new(line_parse(val[0][0]), val[0]) }
71
+ | html_quote
72
+
73
+ html_quote : QUOTE { result = Node.new(:QUOTE, [], val[0]) }
74
+
75
+ lists : unordered_list
76
+ | ordered_list
77
+ | description_list
78
+
79
+ block : BLOCK_START items BLOCK_END { result = BlockNode.new(val[0], val[1])}
80
+ | examples = PLOW {
81
+ params = ["EXAMPLE", val[0][0].value, val[0][0].indent]
82
+ result = BlockNode.new(params, [Node.new(:TEXTBLOCK, val[0], val[0][0].value)])
83
+ }
84
+
85
+ examples : examples example { result << val[1] }
86
+ | example { result = val }
87
+
88
+ example : EXAMPLE { result = TextlineNode.new(line_parse(val[0][0]), val[0]) }
89
+
90
+
91
+ unordered_list : UL_START list_items UL_END { result = Node.new(:UNORDERED_LIST, val[1], 'dummy') }
92
+
93
+ ordered_list : OL_START list_items OL_END { result = Node.new(:ORDERED_LIST, val[1], 'dummy') }
94
+
95
+ description_list : DL_START list_items DL_END { result = Node.new(:DEFINITION_LIST, val[1], 'dummy') }
96
+
97
+ list_items : list_items list_item { result << val[1] }
98
+ | list_item { result = val }
99
+
100
+ list_item : LI_START items LI_END {
101
+ result = ListitemNode.new(val[0][0], val[1], line_parse(val[0][1]),
102
+ line_parse(val[0][2]))
103
+ }
104
+ | LI_START LI_END {
105
+ result = ListitemNode.new(val[0][0], [], line_parse(val[0][1]),
106
+ line_parse(val[0][2]))
107
+ }
108
+
109
+ table : table_rows table_sep table_rows = PLOW
110
+ { result = TableNode.new(val[0], val[2]) }
111
+ | table_rows = PLOW
112
+ { result = TableNode.new([], val[0]) }
113
+
114
+ table_rows : table_rows table_row { result << val[1] }
115
+ | table_row { result = val }
116
+
117
+ table_sep : TABLE_SEP { result = Node.new(:TABLE_SEP) }
118
+
119
+ table_row : TABLE_ROW {
120
+ cols = []
121
+ val[0].chomp.sub(/^\s*\|/,'').split('|').each do |col|
122
+ cols << line_parse(col)
123
+ end
124
+ result = TableRowNode.new(cols)
125
+ }