pseudohikiparser 0.0.0.4.develop

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.
@@ -0,0 +1,313 @@
1
+ #/usr/bin/env ruby
2
+
3
+ require 'test/unit'
4
+ require 'lib/pseudohiki/blockparser'
5
+
6
+ class TC_BlockLeaf < Test::Unit::TestCase
7
+ include PseudoHiki::BlockParser::BlockElement
8
+
9
+ def test_block_when_descnode
10
+ assert_equal(DescNode, DescLeaf.new.block)
11
+ end
12
+
13
+ def test_block_when_headingleaf
14
+ assert_equal(HeadingNode, HeadingLeaf.new.block)
15
+ end
16
+
17
+ def test_block_head_re_when_verbatimleaf
18
+ assert_equal(/\A(\s)/o, VerbatimLeaf.new.head_re)
19
+ end
20
+
21
+ def test_block_head_re_when_tableleaf
22
+ assert_equal(/\A(\|\|)/o, TableLeaf.new.head_re)
23
+ end
24
+
25
+ def test_block_head_re_when_commentoutleaf
26
+ assert_equal(/\A(\/\/)/o.to_s, CommentOutLeaf.new.head_re.to_s)
27
+ end
28
+
29
+ def test_block_head_re_when_commentoutleaf2
30
+ assert_equal(/\A(\/\/)/o, /\A(\/\/)/o)
31
+ end
32
+
33
+ def test_block_head_re_when_headingleaf
34
+ assert_equal(/\A(!)+/o, HeadingLeaf.new.head_re)
35
+ end
36
+
37
+ def test_leaf_create
38
+ parser = PseudoHiki::BlockParser.new
39
+ paragraph_line = "This is a paragraph."
40
+ paragraph = parser.select_leaf_type(paragraph_line).create(paragraph_line)
41
+ assert_equal([paragraph_line], paragraph)
42
+ assert_equal(nil, paragraph.nominal_level)
43
+ end
44
+
45
+ def test_nestedleaf_create
46
+ parser = PseudoHiki::BlockParser.new
47
+ level1_heading_line = "!This is a level1 heading."
48
+ heading1 = parser.select_leaf_type(level1_heading_line).create(level1_heading_line)
49
+ assert_equal([["This is a level1 heading."]], heading1)
50
+ assert_equal(1, heading1.nominal_level)
51
+
52
+ level2_heading_line = "!!This is a level2 heading."
53
+ heading2 = parser.select_leaf_type(level2_heading_line).create(level2_heading_line)
54
+ assert_equal([["This is a level2 heading."]], heading2)
55
+ assert_equal(2, heading2.nominal_level)
56
+ end
57
+
58
+ def test_select_leaf_type
59
+ parser = PseudoHiki::BlockParser.new
60
+ assert_equal(ParagraphLeaf, parser.select_leaf_type("This is a paragraph type line."))
61
+ assert_equal(DescLeaf, parser.select_leaf_type(': This is a desc type line.'))
62
+ assert_equal(VerbatimLeaf, parser.select_leaf_type(" This is a verbatim type line."))
63
+ assert_equal(QuoteLeaf, parser.select_leaf_type('"" This is a quote type line.'))
64
+ assert_equal(TableLeaf, parser.select_leaf_type('|| This is a table type line.'))
65
+ assert_equal(CommentOutLeaf, parser.select_leaf_type('// This is a commentout type line.'))
66
+ assert_equal(HeadingLeaf, parser.select_leaf_type("!This is a heading type line."))
67
+ assert_equal(ListLeaf, parser.select_leaf_type("*This is a list type line."))
68
+ assert_equal(ListLeaf, parser.select_leaf_type("**This is another list type line."))
69
+ assert_equal(EnumLeaf, parser.select_leaf_type("#This is a list type line."))
70
+ assert_equal(EnumLeaf, parser.select_leaf_type("##This is another list type line."))
71
+ assert_equal(HrLeaf, parser.select_leaf_type("----"))
72
+ assert_equal(BlockNodeEnd, parser.select_leaf_type("\n"))
73
+ assert_equal(BlockNodeEnd, parser.select_leaf_type("\r\n"))
74
+ end
75
+
76
+ def create_leaf(str)
77
+ parser = PseudoHiki::BlockParser.new
78
+ parser.select_leaf_type(str).create(str)
79
+ end
80
+
81
+ def test_push_leaf
82
+ paragraph_str = "This is a paragraph line."
83
+ another_paragraph_str = "This is another paragraph line."
84
+ stack = PseudoHiki::BlockParser.new.stack
85
+ stack.push create_leaf(paragraph_str)
86
+ paragraph_tree = stack.tree
87
+ assert_equal([[[paragraph_str]]],paragraph_tree)
88
+ assert_equal(nil,paragraph_tree.first.nominal_level)
89
+ another_paragraph = create_leaf(another_paragraph_str)
90
+ stack.push another_paragraph
91
+ assert_equal([[[paragraph_str,
92
+ another_paragraph_str]]],paragraph_tree)
93
+
94
+ heading_str = "This is a heading line."
95
+ stack = PseudoHiki::BlockParser.new.stack
96
+ stack.push create_leaf("!"+heading_str)
97
+ heading_tree = stack.tree
98
+ # heading_tree = push_leaf_on_stack("!"+heading_str).tree
99
+ assert_equal(1,heading_tree.first.nominal_level)
100
+ assert_equal(HeadingNode, heading_tree.first.class)
101
+ assert_equal(HeadingLeaf, heading_tree.first.first.class)
102
+ stack.push create_leaf("!!"+heading_str)
103
+ assert_equal([[[[heading_str]],
104
+ [[[heading_str]]]]], heading_tree)
105
+
106
+ stack = PseudoHiki::BlockParser.new.stack
107
+ stack.push create_leaf("!!"+heading_str)
108
+ heading2_tree = stack.tree
109
+ assert_equal([[[[heading_str]]]], heading2_tree)
110
+ assert_equal(2,heading2_tree.first.nominal_level)
111
+
112
+ stack = PseudoHiki::BlockParser.new.stack
113
+ stack.push create_leaf("!"+heading_str)
114
+ stack.push create_leaf(another_paragraph_str)
115
+ assert_equal([[[[heading_str]],
116
+ [[another_paragraph_str]]]],stack.tree)
117
+ end
118
+
119
+ def test_paragraph_breakable?
120
+ paragraph_leaf = create_leaf("paragraph leaf")
121
+ blocknode_end = create_leaf("\n")
122
+ heading1_leaf = create_leaf("!heading1 leaf")
123
+ heading2_leaf = create_leaf("!!heaindg2 leaf")
124
+
125
+ parser = PseudoHiki::BlockParser.new
126
+ parser.stack.push paragraph_leaf
127
+ assert_equal(true, parser.breakable?(heading1_leaf))
128
+ assert_equal(false, parser.breakable?(paragraph_leaf))
129
+ assert_equal(true, parser.breakable?(blocknode_end))
130
+ end
131
+
132
+ def test_heading1_breakable?
133
+ paragraph_leaf = create_leaf("paragraph leaf")
134
+ blocknode_end = create_leaf("\n")
135
+ heading1_leaf = create_leaf("!heading1 leaf")
136
+ heading2_leaf = create_leaf("!!heaindg2 leaf")
137
+
138
+ parser = PseudoHiki::BlockParser.new
139
+ parser.stack.push heading1_leaf
140
+ assert_equal(true, parser.breakable?(heading1_leaf))
141
+ assert_equal(false, parser.breakable?(heading2_leaf))
142
+ assert_equal(false, parser.breakable?(blocknode_end))
143
+ end
144
+
145
+ def test_heading2_breakable?
146
+ paragraph_leaf = create_leaf("paragraph leaf")
147
+ blocknode_end = create_leaf("\n")
148
+ heading1_leaf = create_leaf("!heading1 leaf")
149
+ heading2_leaf = create_leaf("!!heaindg2 leaf")
150
+
151
+ parser = PseudoHiki::BlockParser.new
152
+ parser.stack.push heading2_leaf
153
+ assert_equal(true, parser.breakable?(heading1_leaf))
154
+ assert_equal(true, parser.breakable?(heading2_leaf))
155
+ assert_equal(false, parser.breakable?(blocknode_end))
156
+ end
157
+
158
+ def test_list1_breakale?
159
+ paragraph_leaf = create_leaf("paragraph leaf")
160
+ blocknode_end = create_leaf("\n")
161
+ list1_leaf = create_leaf("*list1 leaf")
162
+ list2_leaf = create_leaf("**list2 leaf")
163
+
164
+ parser = PseudoHiki::BlockParser.new
165
+ parser.stack.push list1_leaf
166
+ assert_equal(true, parser.breakable?(blocknode_end))
167
+ assert_equal(true, parser.breakable?(list1_leaf))
168
+ assert_equal(false, parser.breakable?(list2_leaf))
169
+ parser.stack.pop
170
+ assert_equal(false, parser.breakable?(list1_leaf))
171
+ end
172
+
173
+ def test_list2_breakale?
174
+ paragraph_leaf = create_leaf("paragraph leaf")
175
+ blocknode_end = create_leaf("\n")
176
+ list1_leaf = create_leaf("*list1 leaf")
177
+ list2_leaf = create_leaf("**list2 leaf")
178
+
179
+ parser = PseudoHiki::BlockParser.new
180
+ parser.stack.push list2_leaf
181
+ current_node_superclass = parser.stack.current_node.class.superclass
182
+ leaf_superclass = list1_leaf.block.superclass
183
+ assert_equal(2, parser.stack.current_node.nominal_level)
184
+ assert_equal(ListNode, list1_leaf.block)
185
+ assert_equal(1, list1_leaf.nominal_level)
186
+ assert_equal(ListNode, list2_leaf.block)
187
+ assert_equal(2, list2_leaf.nominal_level)
188
+ assert_equal(ListWrapNode, parser.stack.current_node.class)
189
+ # assert_equal(true, parser.stack.current_node.class.kind_of?(PseudoHiki::BlockParser::ListTypeBlockNode))
190
+ assert_equal(PseudoHiki::BlockParser::ListLeafNode, current_node_superclass)
191
+ assert_equal(PseudoHiki::BlockParser::ListTypeBlockNode, leaf_superclass)
192
+ assert_equal(false, current_node_superclass == leaf_superclass)
193
+ assert_equal(true, PseudoHiki::BlockParser::ListTypeBlockNode == leaf_superclass)
194
+ assert_equal(true, parser.breakable?(blocknode_end))
195
+ assert_equal(false, parser.stack.current_node.nominal_level <= list1_leaf.nominal_level)
196
+ assert_equal(true, parser.stack.current_node.nominal_level <= list2_leaf.nominal_level)
197
+ assert_equal(true, parser.breakable?(list1_leaf))
198
+ assert_equal(true, parser.breakable?(list2_leaf))
199
+ parser.stack.pop
200
+ assert_equal(ListNode, parser.stack.current_node.class)
201
+ assert_equal(true, parser.stack.current_node.class.superclass == leaf_superclass)
202
+ assert_equal(true, parser.breakable?(list1_leaf))
203
+ assert_equal(false, parser.breakable?(list2_leaf))
204
+ end
205
+
206
+ def test_parse
207
+ text = <<TEXT
208
+ !heading1
209
+
210
+ paragraph1.
211
+ paragraph2.
212
+ paragraph3.
213
+ ""citation1
214
+ paragraph4.
215
+
216
+ *list1
217
+ *list1-1
218
+ **list2
219
+ **list2-2
220
+ *list3
221
+
222
+ paragraph5.
223
+
224
+ !!heading2
225
+
226
+ paragraph6.
227
+ paragraph7.
228
+
229
+ paragraph8.
230
+
231
+ !heading3
232
+
233
+ paragraph9.
234
+ TEXT
235
+
236
+ tree = PseudoHiki::BlockParser.parse(text.split(/\r?\n/o))
237
+ assert_equal([[[["heading1"]],
238
+ [[["paragraph1.paragraph2.paragraph3."]]],
239
+ [[[[["citation1"]]]]],
240
+ [[["paragraph4."]]],
241
+ [[[["list1"]]],
242
+ [[["list1-1"]],
243
+ [[[["list2"]]],
244
+ [[["list2-2"]]]]],
245
+ [[["list3"]]]],
246
+ [[["paragraph5."]]],
247
+ [[["heading2"]],
248
+ [[["paragraph6.paragraph7."]]],
249
+ [[["paragraph8."]]]]],
250
+ [[["heading3"]],
251
+ [[["paragraph9."]]]]],tree)
252
+ end
253
+
254
+ def test_parse_with_inline_elements
255
+ text = <<TEXT
256
+ !heading
257
+
258
+ paragraph with a [[link|http://www.example.org/]].
259
+
260
+ ||col||col
261
+ :item:description
262
+ TEXT
263
+
264
+ tree = PseudoHiki::BlockParser.parse(text.split(/\r?\n/o))
265
+ assert_equal([[[["heading"]],
266
+ [[["paragraph with a "],
267
+ [["link"],
268
+ ["|"],
269
+ ["http"], [":"], ["//www.example.org/"]],
270
+ ["."]]],
271
+ [[[["col"]],[["col"]]]],
272
+ [[["item"], [":"], ["description"]]]]], tree)
273
+ end
274
+
275
+ def test_parse_table_with_inline_elements
276
+ text =<<TEXT
277
+ ||!col||![[link|http://www.example.org/]]||col
278
+ TEXT
279
+
280
+ parsed_cells = [[[["col"]],
281
+ [[""],
282
+ [["link"],
283
+ ["|"],
284
+ ["http"], [":"], ["//www.example.org/"]]],
285
+ [["col"]]]]
286
+
287
+ tablenode = PseudoHiki::BlockParser.parse(text.split(/\r?\n/o)).shift
288
+ assert_equal(parsed_cells, tablenode)
289
+ assert_equal(TableNode, tablenode.class)
290
+ end
291
+
292
+ def test_assign_node_id
293
+ text = <<TEXT
294
+ *[lst]list
295
+ TEXT
296
+ list_leaf = ListLeaf.new
297
+ list_leaf.push "[lst]list"
298
+ list_node = ListNode.new
299
+ assert_equal(["[lst]list"], list_leaf)
300
+ PseudoHiki::BlockParser.assign_node_id(list_leaf,list_node)
301
+ assert_equal(["list"], list_leaf)
302
+ assert_equal("lst", list_node.node_id)
303
+ parsed = PseudoHiki::BlockParser.parse(text.split(/\r?\n/o))
304
+ assert_equal([[[[["list"]]]]],parsed)
305
+
306
+ text2 = <<TEXT
307
+ !!![hd]heading
308
+ TEXT
309
+
310
+ parsed = PseudoHiki::BlockParser.parse(text2.split(/\r?\n/o))
311
+ assert_equal([[[["heading"]]]],parsed)
312
+ end
313
+ end
@@ -0,0 +1,73 @@
1
+ #/usr/bin/env ruby
2
+
3
+ require 'test/unit'
4
+ require 'lib/htmlelement'
5
+
6
+ class TC_HtmlElement < Test::Unit::TestCase
7
+
8
+ def test_format_attributes
9
+ a = HtmlElement.create("a")
10
+ a['href'] = "http://www.example.net/example.cgi&param=value"
11
+ assert_equal('<a href="http://www.example.net/example.cgi&amp;param=value"></a>', a.to_s)
12
+ end
13
+
14
+ def test_empty_elements
15
+ xhtml_img = XhtmlElement.create("img")
16
+ assert_equal('<img />'+$/, xhtml_img.to_s)
17
+
18
+ img = HtmlElement.create("img")
19
+ assert_equal('<img>'+$/, img.to_s)
20
+
21
+ xhtml_img = XhtmlElement.create("img")
22
+ assert_equal('<img />'+$/, xhtml_img.to_s)
23
+
24
+ img = HtmlElement.create("img")
25
+ assert_equal('<img>'+$/, img.to_s)
26
+ end
27
+
28
+ def test_doc_type
29
+ html_doctype = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
30
+ "http://www.w3.org/TR/html4/loose.dtd">'.split(/\r?\n/o).join($/)+"#{$/}"
31
+
32
+ assert_equal(html_doctype, HtmlElement.doctype("EUC-JP"))
33
+
34
+
35
+ xhtml_doctype = '<?xml version="1.0" encoding="EUC-JP"?>
36
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
37
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'.split(/\r?\n/o).join($/)+"#{$/}"
38
+
39
+ assert_equal(xhtml_doctype, XhtmlElement.doctype("EUC-JP"))
40
+
41
+ xhtml_default_doctype = '<?xml version="1.0" encoding="UTF-8"?>
42
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
43
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'.split(/\r?\n/o).join($/)+"#{$/}"
44
+
45
+ assert_equal(xhtml_default_doctype, XhtmlElement.doctype)
46
+
47
+ html5_doctype = '<?xml version="1.0" encoding="UTF-8"?>
48
+ <!DOCTYPE html>'.split(/\r?\n/o).join($/)+"#{$/}"
49
+
50
+ assert_equal(html5_doctype, Xhtml5Element.doctype)
51
+ end
52
+
53
+ def test_html5_elements
54
+ html_section = <<SECTION
55
+ <div class="section">
56
+ <!-- end of section -->
57
+ </div>
58
+ SECTION
59
+
60
+ html_section = html_section.split(/\r?\n/o).join($/)+"#{$/}"
61
+
62
+ assert_equal(html_section, HtmlElement.create("section").to_s)
63
+
64
+ html5_section = <<SECTION
65
+ <section>
66
+ </section>
67
+ SECTION
68
+
69
+ html5_section = html5_section.split(/\r?\n/o).join($/)+"#{$/}"
70
+
71
+ assert_equal(html5_section, Xhtml5Element.create("section").to_s)
72
+ end
73
+ end