hparser 0.2.2 → 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/hparser/block/dl.rb +45 -0
- data/lib/hparser/html.rb +20 -0
- data/lib/hparser/text.rb +17 -0
- data/test/test_dl.rb +26 -0
- data/test/test_html.rb +9 -1
- data/test/test_text.rb +12 -0
- metadata +5 -2
@@ -0,0 +1,45 @@
|
|
1
|
+
# Author:: MIZUNO Hiroki (hiroki1124@gmail.com)
|
2
|
+
# Copyright:: Copyright (c) 2006 MIZUNO Hiroki
|
3
|
+
# License:: Distributes under the same terms as Ruby
|
4
|
+
|
5
|
+
require 'hparser/block/collectable'
|
6
|
+
module HParser
|
7
|
+
module Block
|
8
|
+
class Dl
|
9
|
+
include Collectable
|
10
|
+
class Item
|
11
|
+
attr_reader :title,:description
|
12
|
+
def initialize(title,description)
|
13
|
+
@title = title
|
14
|
+
@description = description
|
15
|
+
end
|
16
|
+
|
17
|
+
def ==(o)
|
18
|
+
self.class == o.class and
|
19
|
+
self.title == o.title and
|
20
|
+
self.description == o.description
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.parse(scanner,inlines)
|
25
|
+
items = []
|
26
|
+
while scanner.scan(/\A:/)
|
27
|
+
i = scanner.matched.index(':',1)
|
28
|
+
title = inlines.parse scanner.matched[1...i]
|
29
|
+
description = inlines.parse scanner.matched[i+1..-1]
|
30
|
+
items.push Item.new(title,description)
|
31
|
+
end
|
32
|
+
items == [] ? nil : self.new(*items)
|
33
|
+
end
|
34
|
+
|
35
|
+
attr_reader :items
|
36
|
+
def initialize(*items)
|
37
|
+
@items = items
|
38
|
+
end
|
39
|
+
|
40
|
+
def ==(o)
|
41
|
+
o.class == self.class and o.items == self.items
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
data/lib/hparser/html.rb
CHANGED
@@ -92,6 +92,26 @@ module HParser
|
|
92
92
|
end
|
93
93
|
end
|
94
94
|
|
95
|
+
class Dl
|
96
|
+
include Html
|
97
|
+
|
98
|
+
class Item
|
99
|
+
def to_html
|
100
|
+
dt = self.title.map{|x| x.to_html}.join
|
101
|
+
dd = self.description.map{|x| x.to_html}.join
|
102
|
+
"<dt>#{dt}</dt><dd>#{dd}</dd>"
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
def html_tag()
|
107
|
+
'dl'
|
108
|
+
end
|
109
|
+
|
110
|
+
def html_content
|
111
|
+
@items
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
95
115
|
class TableHeader
|
96
116
|
include Html
|
97
117
|
private
|
data/lib/hparser/text.rb
CHANGED
@@ -84,6 +84,23 @@ module HParser
|
|
84
84
|
end
|
85
85
|
end
|
86
86
|
|
87
|
+
class Dl
|
88
|
+
class Item
|
89
|
+
def to_text
|
90
|
+
dt = self.title.map{|x| x.to_html}.join
|
91
|
+
dd = self.description.map{|x| x.to_html}.join.split("\n").map{|x|
|
92
|
+
' '+x
|
93
|
+
}.join("\n")
|
94
|
+
"#{dt}\n#{dd}"
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
def to_text
|
99
|
+
@items.map{|x| x.to_text}.join("\n")
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
|
87
104
|
class OrderList
|
88
105
|
def to_text
|
89
106
|
i = 0
|
data/test/test_dl.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'hparser/parser'
|
3
|
+
require 'hparser/block/dl'
|
4
|
+
|
5
|
+
class DlTest < Test::Unit::TestCase
|
6
|
+
include HParser::Block
|
7
|
+
include HParser::Inline
|
8
|
+
|
9
|
+
def setup
|
10
|
+
@parser = HParser::Parser.new [Dl]
|
11
|
+
end
|
12
|
+
|
13
|
+
def parse str
|
14
|
+
@parser.parse str
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_dl
|
18
|
+
first = Dl::Item.new([Text.new('foo')],[Text.new('foo is ...')])
|
19
|
+
second = Dl::Item.new([Text.new('bar')],[Text.new('bar is ...')])
|
20
|
+
assert_equal [Dl.new(first,second)],
|
21
|
+
parse(<<-END)
|
22
|
+
:foo:foo is ...
|
23
|
+
:bar:bar is ...
|
24
|
+
END
|
25
|
+
end
|
26
|
+
end
|
data/test/test_html.rb
CHANGED
@@ -7,8 +7,9 @@ class HtmlTest < Test::Unit::TestCase
|
|
7
7
|
include HParser::Block
|
8
8
|
include HParser::Inline
|
9
9
|
def setup
|
10
|
-
@parser = HParser::Parser.new
|
10
|
+
# @parser = HParser::Parser.new
|
11
11
|
end
|
12
|
+
|
12
13
|
def assert_html expect,node
|
13
14
|
assert_equal expect,node.to_html
|
14
15
|
end
|
@@ -44,6 +45,13 @@ class HtmlTest < Test::Unit::TestCase
|
|
44
45
|
Li.new([Text.new('bbb')]))
|
45
46
|
end
|
46
47
|
|
48
|
+
def test_dl
|
49
|
+
expect = '<dl><dt>foo</dt><dd>foo is ...</dd><dt>bar</dt><dd>bar is ...</dd></dl>'
|
50
|
+
first = Dl::Item.new([Text.new('foo')],[Text.new('foo is ...')])
|
51
|
+
second = Dl::Item.new([Text.new('bar')],[Text.new('bar is ...')])
|
52
|
+
assert_html expect,Dl.new(first,second)
|
53
|
+
end
|
54
|
+
|
47
55
|
def th str
|
48
56
|
Th.new [Text.new(str)]
|
49
57
|
end
|
data/test/test_text.rb
CHANGED
@@ -35,6 +35,18 @@ class TextTest < Test::Unit::TestCase
|
|
35
35
|
assert_text ' foobar',Quote.new([Text.new('foobar')])
|
36
36
|
end
|
37
37
|
|
38
|
+
def test_dl
|
39
|
+
expect = <<-END.chomp
|
40
|
+
foo
|
41
|
+
foo is ...
|
42
|
+
bar
|
43
|
+
bar is ...
|
44
|
+
END
|
45
|
+
first = Dl::Item.new([Text.new('foo')],[Text.new('foo is ...')])
|
46
|
+
second = Dl::Item.new([Text.new('bar')],[Text.new('bar is ...')])
|
47
|
+
assert_text expect,Dl.new(first,second)
|
48
|
+
end
|
49
|
+
|
38
50
|
def test_table
|
39
51
|
assert_text "|foo|bar|\n|baz|xyzzy|",
|
40
52
|
Table.new([th('foo'),th('bar')],
|
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.11
|
|
3
3
|
specification_version: 1
|
4
4
|
name: hparser
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.
|
7
|
-
date: 2006-08-
|
6
|
+
version: 0.3.0
|
7
|
+
date: 2006-08-17 00:00:00 +09:00
|
8
8
|
summary: Hatena Format Parser
|
9
9
|
require_paths:
|
10
10
|
- lib
|
@@ -29,6 +29,7 @@ authors:
|
|
29
29
|
- MIZUNO Hiroki
|
30
30
|
files:
|
31
31
|
- test/test_block.rb
|
32
|
+
- test/test_dl.rb
|
32
33
|
- test/test_hatena.rb
|
33
34
|
- test/test_head.rb
|
34
35
|
- test/test_html.rb
|
@@ -47,6 +48,7 @@ files:
|
|
47
48
|
- lib/hparser/text.rb
|
48
49
|
- lib/hparser/block/all.rb
|
49
50
|
- lib/hparser/block/collectable.rb
|
51
|
+
- lib/hparser/block/dl.rb
|
50
52
|
- lib/hparser/block/head.rb
|
51
53
|
- lib/hparser/block/list.rb
|
52
54
|
- lib/hparser/block/p.rb
|
@@ -66,6 +68,7 @@ files:
|
|
66
68
|
- README
|
67
69
|
test_files:
|
68
70
|
- test/test_block.rb
|
71
|
+
- test/test_dl.rb
|
69
72
|
- test/test_hatena.rb
|
70
73
|
- test/test_head.rb
|
71
74
|
- test/test_html.rb
|