hparser 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -16,23 +16,32 @@ module HParser
16
16
  # In short,some format is different in begining/ending string.
17
17
  # So this class have basic structure for that format.
18
18
  class Pair
19
- # make parser by begin/end-ing string
20
- def self.spliter(from,to)
19
+ def self.get(scanner,from,to)
21
20
  from_q = Regexp.quote from
22
21
  to_q = Regexp.quote to
22
+ if scanner.scan(/\A#{from_q}(.*)#{to_q}\Z/) then
23
+ content = scanner.matched
24
+ return content[from.length...-to.length].strip
25
+ elsif scanner.scan(/\A#{from_q}/)
26
+ content = scanner.matched[from.length..-1]
27
+ until scanner.scan(/#{to_q}\Z/) do
28
+ content += "\n"+ scanner.scan(/./)
29
+ end
30
+ content += "\n"+scanner.matched[0...-to.length]
31
+ return content.strip
32
+ end
33
+ return nil
34
+ end
23
35
 
36
+ # make parser by begin/end-ing string
37
+ def self.spliter(from,to)
24
38
  module_eval <<-"END"
25
39
  def self.parse(scanner,inlines)
26
- if scanner.scan(/\\A#{from_q}(.*)#{to_q}\\Z/) then
27
- content = scanner.matched
28
- self.new inlines.parse(content[#{from.length}...-#{to.length}].strip)
29
- elsif scanner.scan(/\\A#{from_q}/)
30
- content = scanner.matched[#{from.length}..-1]
31
- until scanner.scan(/#{to_q}\\Z/) do
32
- content += "\n"+ scanner.scan(/./)
33
- end
34
- content += "\n"+scanner.matched[0...-#{to.length}]
35
- self.new inlines.parse(content.strip)
40
+ content = get(scanner,"#{from}","#{to}")
41
+ if content then
42
+ self.new inlines.parse(content)
43
+ else
44
+ nil
36
45
  end
37
46
  end
38
47
  END
@@ -5,7 +5,13 @@ module HParser
5
5
  # Super pre parser.
6
6
  class SuperPre < Pair
7
7
  include Collectable
8
- spliter '>||','||<'
8
+
9
+ def self.parse scanner,inlines
10
+ content = get scanner,'>||','||<'
11
+ if content then
12
+ SuperPre.new content
13
+ end
14
+ end
9
15
 
10
16
  def self.<=>(o)
11
17
  -1
@@ -0,0 +1,138 @@
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
+ module HParser
6
+ # This module provide +to_hatena+ method.
7
+ # This method is intended to convert hatena format node to hatena format.
8
+ #
9
+ # For example:
10
+ # Hatena::Parser.parse('*foo').to_html # -> *foo
11
+ # Hatena::Parser.parse('>|bar|<').to_html # -> >|bar|<
12
+ #
13
+ # Please see also HParser::Text
14
+ module Hatena
15
+ def to_hatena
16
+ content = hatena_content
17
+ if content.class == Array then
18
+ content = content.map{|x| x.to_hatena}.join
19
+ end
20
+ if self.respond_to? :hatena_filter,true
21
+ content = hatena_filter content
22
+ end
23
+ content+"\n"
24
+ end
25
+ end
26
+
27
+ module Block
28
+ class P
29
+ include Hatena
30
+ private
31
+ alias_method :hatena_content,:content
32
+ end
33
+
34
+ class Empty
35
+ include Hatena
36
+ private
37
+ def hatena_content
38
+ ""
39
+ end
40
+ end
41
+
42
+ class Head
43
+ include Hatena
44
+ private
45
+ def hatena_content
46
+ self.content
47
+ end
48
+
49
+ def hatena_filter content
50
+ '*'*self.level + content
51
+ end
52
+ end
53
+
54
+ class Quote
55
+ include Hatena
56
+ alias_method :hatena_content,:content
57
+ def hatena_filter c
58
+ '>>'+c+'<<'
59
+ end
60
+ end
61
+
62
+ class Pre
63
+ include Hatena
64
+ alias_method :hatena_content,:content
65
+ def hatena_filter c
66
+ '>|'+c+'|<'
67
+ end
68
+ end
69
+
70
+ class SuperPre
71
+ include Hatena
72
+ alias_method :hatena_content,:content
73
+ def hatena_filter c
74
+ '>||'+c+'||<'
75
+ end
76
+ end
77
+
78
+ class Table
79
+ def to_hatena
80
+ map_row{|row|
81
+ '|'+ row.map{|cell| cell.to_hatena.chomp}.join('|')+'|'
82
+ }.join("\n")+"\n"
83
+ end
84
+ end
85
+
86
+ class TableHeader
87
+ include Hatena
88
+ alias_method :hatena_content,:content
89
+ def hatena_filter c
90
+ '*'+c
91
+ end
92
+ end
93
+
94
+ class TableCell
95
+ include Hatena
96
+ alias_method :hatena_content,:content
97
+ end
98
+
99
+ class UnorderList
100
+ def to_hatena(level=0,label=nil)
101
+ @items.map{|li| li.to_hatena(level+1,'-').chomp}.join("\n")+"\n"
102
+ end
103
+ end
104
+
105
+ class OrderList
106
+ def to_hatena(level=0,label=nil)
107
+ @items.map{|li|
108
+ li.to_hatena(level+1,'+').chomp
109
+ }.join("\n")+"\n"
110
+ end
111
+ end
112
+
113
+ class ListItem
114
+ include Hatena
115
+ alias_method :hatena_content,:content
116
+ alias_method :_to_hatena_,:to_hatena
117
+ def to_hatena(level,label)
118
+ label*level + _to_hatena_
119
+ end
120
+ end
121
+ end
122
+
123
+ module Inline
124
+ class Text
125
+ alias_method :to_hatena,:text
126
+ end
127
+
128
+ class Url
129
+ alias_method :to_hatena,:url
130
+ end
131
+
132
+ class HatenaId
133
+ def to_hatena
134
+ "id:#{self.name}"
135
+ end
136
+ end
137
+ end
138
+ end
data/lib/hparser/html.rb CHANGED
@@ -87,12 +87,25 @@ module HParser
87
87
  class Table
88
88
  def to_html
89
89
  '<table>'+self.map_row{|tr|
90
- '<tr>'+tr.map{|cell| tag = cell.class == Th ? 'th' : 'td'
91
- "<#{tag}>#{cell.content.map{|x| x.to_html}.join}</#{tag}>"}.join+'</tr>'
90
+ '<tr>'+tr.map{|cell| cell.to_html }.join + '</tr>'
92
91
  }.join+'</table>'
93
92
  end
94
93
  end
95
94
 
95
+ class TableHeader
96
+ include Html
97
+ private
98
+ def html_tag() 'th' end
99
+ alias_method :html_content,:content
100
+ end
101
+
102
+ class TableCell
103
+ include Html
104
+ private
105
+ alias_method :html_content,:content
106
+ def html_tag() 'td' end
107
+ end
108
+
96
109
  class UnorderList
97
110
  include Html
98
111
  private
@@ -0,0 +1,120 @@
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
+ module HParser
6
+ # This module provide +to_text+ method.
7
+ # This method is intended to convert hatena format to text.
8
+ #
9
+ # For example:
10
+ # Hatena::Parser.parse('*foo').to_html # -> foo
11
+ # Hatena::Parser.parse('>|bar|<').to_html # -> bar
12
+ #
13
+ # Please see also HParser::Hatena
14
+ module Text
15
+ def to_text
16
+ content = text_content
17
+ if content.class == Array then
18
+ content = content.map{|x| x.to_text}.join
19
+ end
20
+ content
21
+ end
22
+ end
23
+
24
+ module Block
25
+ class Head
26
+ include Text
27
+ alias_method :text_content,:content
28
+ end
29
+
30
+ class P
31
+ include Text
32
+ alias_method :text_content,:content
33
+ end
34
+
35
+ class Empty
36
+ include Text
37
+ def text_content
38
+ "\n"
39
+ end
40
+ end
41
+
42
+ module Indent
43
+ include Text
44
+ def text_content
45
+ self.content
46
+ end
47
+ alias_method :_to_text_,:to_text
48
+ def to_text
49
+ self._to_text_.split("\n").map{|line| ' '+line}.join("\n")
50
+ end
51
+ end
52
+
53
+ class Pre
54
+ include Indent
55
+ end
56
+ class SuperPre
57
+ include Indent
58
+ end
59
+ class Quote
60
+ include Indent
61
+ end
62
+
63
+ class Table
64
+ def to_text
65
+ map_row do |tr|
66
+ '|'+tr.map{|cell| cell.to_text}.join('|')+'|'
67
+ end.join("\n")
68
+ end
69
+ end
70
+
71
+ class TableHeader
72
+ include Text
73
+ alias_method :text_content,:content
74
+ end
75
+
76
+ class TableCell
77
+ include Text
78
+ alias_method :text_content,:content
79
+ end
80
+
81
+ class UnorderList
82
+ def to_text
83
+ @items.map{|li| '-'+li.to_text}.join("\n")
84
+ end
85
+ end
86
+
87
+ class OrderList
88
+ def to_text
89
+ i = 0
90
+ @items.map{|li|
91
+ i += 1
92
+ "#{i}." +li.to_text
93
+ }.join("\n")
94
+ end
95
+ end
96
+
97
+ class ListItem
98
+ include Text
99
+ def text_content
100
+ [HParser::Inline::Text.new(' '),self.content].flatten
101
+ end
102
+ end
103
+ end
104
+
105
+ module Inline
106
+ class Text
107
+ alias_method :to_text,:text
108
+ end
109
+
110
+ class Url
111
+ alias_method :to_text,:url
112
+ end
113
+
114
+ class HatenaId
115
+ def to_text
116
+ "id:#{self.name}"
117
+ end
118
+ end
119
+ end
120
+ end
data/test/test_block.rb CHANGED
@@ -61,7 +61,6 @@ END
61
61
  end
62
62
 
63
63
  def test_spre
64
- p HParser::Parser.default_parser
65
64
  assert_equal [SuperPre.new('a')],parse(<<-END)
66
65
  >||
67
66
  a
@@ -0,0 +1,60 @@
1
+ require 'test/unit'
2
+ require 'hparser'
3
+ require 'hparser/hatena'
4
+
5
+ class HatenaTest < Test::Unit::TestCase
6
+ include HParser::Block
7
+ include HParser::Inline
8
+ def setup
9
+ @parser = HParser::Parser.new
10
+ end
11
+
12
+ def assert_hatena expect,node
13
+ assert_equal expect+"\n",node.to_hatena
14
+ end
15
+
16
+ def test_head
17
+ assert_hatena "*foo",Head.new(1,[Text.new('foo')])
18
+ end
19
+
20
+ def test_p
21
+ assert_hatena "foobar",P.new([Text.new('foobar')])
22
+ end
23
+
24
+ def test_empty
25
+ assert_hatena "",Empty.new
26
+ end
27
+
28
+ def test_pre
29
+ assert_hatena '>|foobar|<',Pre.new([Text.new('foobar')])
30
+ assert_hatena '>||foobar||<',SuperPre.new('foobar')
31
+ assert_hatena ">|foobar\nbar|<",Pre.new([Text.new("foobar\nbar")])
32
+ end
33
+
34
+ def test_quote
35
+ assert_hatena '>>foobar<<',Quote.new([Text.new('foobar')])
36
+ end
37
+
38
+ def test_table
39
+ assert_hatena "|*foo|*bar|\n|baz|xyzzy|",
40
+ Table.new([th('foo'),th('bar')],
41
+ [td('baz') ,td('xyzzy')])
42
+ end
43
+
44
+ def test_list
45
+ assert_hatena "-aaa\n-bbb",Ul.new(Li.new([Text.new('aaa')]),Li.new([Text.new('bbb')]))
46
+ assert_hatena "+aaa\n+bbb",Ol.new(Li.new([Text.new('aaa')]),Li.new([Text.new('bbb')]))
47
+ assert_hatena "+aaa\n--bbb",Ol.new(Li.new([Text.new('aaa')]),
48
+ Ul.new(Li.new([Text.new('bbb')])))
49
+ assert_hatena "-aaa\n--bbb",Ul.new(Li.new([Text.new('aaa')]),
50
+ Ul.new(Li.new([Text.new('bbb')])))
51
+ end
52
+
53
+ def th str
54
+ Th.new [Text.new(str)]
55
+ end
56
+
57
+ def td str
58
+ Td.new [Text.new(str)]
59
+ end
60
+ end
data/test/test_pair.rb CHANGED
@@ -27,8 +27,4 @@ class QuoteTest < Test::Unit::TestCase
27
27
  def test_pre
28
28
  assert_pair ">|","|<",Pre
29
29
  end
30
-
31
- def test_spre
32
- assert_pair ">||","||<",SuperPre
33
- end
34
30
  end
data/test/test_text.rb ADDED
@@ -0,0 +1,60 @@
1
+ require 'test/unit'
2
+ require 'hparser'
3
+ require 'hparser/text'
4
+
5
+ class TextTest < Test::Unit::TestCase
6
+ include HParser::Block
7
+ include HParser::Inline
8
+ def setup
9
+ @parser = HParser::Parser.new
10
+ end
11
+
12
+ def assert_text expect,node
13
+ assert_equal expect,node.to_text
14
+ end
15
+
16
+ def test_head
17
+ assert_text 'foo',Head.new(1,[Text.new('foo')])
18
+ end
19
+
20
+ def test_p
21
+ assert_text 'foobar',P.new([Text.new('foobar')])
22
+ end
23
+
24
+ def test_empty
25
+ assert_text "\n",Empty.new
26
+ end
27
+
28
+ def test_pre
29
+ assert_text ' foobar',Pre.new([Text.new('foobar')])
30
+ assert_text ' foobar',SuperPre.new('foobar')
31
+ assert_text " foobar\n bar",Pre.new([Text.new("foobar\nbar")])
32
+ end
33
+
34
+ def test_quote
35
+ assert_text ' foobar',Quote.new([Text.new('foobar')])
36
+ end
37
+
38
+ def test_table
39
+ assert_text "|foo|bar|\n|baz|xyzzy|",
40
+ Table.new([th('foo'),th('bar')],
41
+ [td('baz') ,td('xyzzy')])
42
+ end
43
+
44
+ def test_list
45
+ assert_text "- aaa\n- bbb",Ul.new(Li.new([Text.new('aaa')]),Li.new([Text.new('bbb')]))
46
+ assert_text "1. aaa\n2. bbb",Ol.new(Li.new([Text.new('aaa')]),Li.new([Text.new('bbb')]))
47
+ assert_text "1.- aaa\n2. bbb",Ol.new(Ul.new(Li.new([Text.new('aaa')])),
48
+ Li.new([Text.new('bbb')]))
49
+ assert_text "-- aaa\n- bbb",Ul.new(Ul.new(Li.new([Text.new('aaa')])),
50
+ Li.new([Text.new('bbb')]))
51
+ end
52
+
53
+ def th str
54
+ Th.new [Text.new(str)]
55
+ end
56
+
57
+ def td str
58
+ Td.new [Text.new(str)]
59
+ end
60
+ end
metadata CHANGED
@@ -3,7 +3,7 @@ rubygems_version: 0.8.11
3
3
  specification_version: 1
4
4
  name: hparser
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.1.1
6
+ version: 0.2.0
7
7
  date: 2006-08-05 00:00:00 +09:00
8
8
  summary: Hatena Format Parser
9
9
  require_paths:
@@ -29,6 +29,7 @@ authors:
29
29
  - MIZUNO Hiroki
30
30
  files:
31
31
  - test/test_block.rb
32
+ - test/test_hatena.rb
32
33
  - test/test_head.rb
33
34
  - test/test_html.rb
34
35
  - test/test_id.rb
@@ -37,10 +38,13 @@ files:
37
38
  - test/test_p.rb
38
39
  - test/test_pair.rb
39
40
  - test/test_table.rb
41
+ - test/test_text.rb
40
42
  - test/test_url.rb
41
43
  - lib/hparser.rb
44
+ - lib/hparser/hatena.rb
42
45
  - lib/hparser/html.rb
43
46
  - lib/hparser/parser.rb
47
+ - lib/hparser/text.rb
44
48
  - lib/hparser/block/all.rb
45
49
  - lib/hparser/block/collectable.rb
46
50
  - lib/hparser/block/head.rb
@@ -62,6 +66,7 @@ files:
62
66
  - README
63
67
  test_files:
64
68
  - test/test_block.rb
69
+ - test/test_hatena.rb
65
70
  - test/test_head.rb
66
71
  - test/test_html.rb
67
72
  - test/test_id.rb
@@ -70,6 +75,7 @@ test_files:
70
75
  - test/test_p.rb
71
76
  - test/test_pair.rb
72
77
  - test/test_table.rb
78
+ - test/test_text.rb
73
79
  - test/test_url.rb
74
80
  rdoc_options:
75
81
  - --main