wparser 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Manifest.txt CHANGED
@@ -6,16 +6,18 @@ Rakefile
6
6
  bin/wparser
7
7
  lib/wparser.rb
8
8
  lib/wparser/block/block.rb
9
- lib/wparser/block/blocks.rb
9
+ lib/wparser/block/blockquote.rb
10
10
  lib/wparser/block/list_nonumber.rb
11
11
  lib/wparser/block/list_number.rb
12
- lib/wparser/break.rb
12
+ lib/wparser/block/pre.rb
13
+ lib/wparser/util/break.rb
13
14
  lib/wparser/cli.rb
14
15
  lib/wparser/inline/deleted.rb
15
16
  lib/wparser/inline/heading.rb
16
17
  lib/wparser/inline/http.rb
17
18
  lib/wparser/inline/inline.rb
18
19
  lib/wparser/inline/italic.rb
20
+ lib/wparser/inline/paragraph.rb
19
21
  lib/wparser/inline/strong.rb
20
22
  lib/wparser/inline/underLine.rb
21
23
  lib/wparser/parser.rb
data/README.rdoc CHANGED
@@ -1,9 +1,11 @@
1
1
  = wparser
2
2
 
3
- * http://github.com/#{github_username}/#{project_name}
3
+ * http://github.com/iori/wparser
4
4
 
5
5
  == DESCRIPTION:
6
6
 
7
+ http://wiki.github.com/iori/wparser/
8
+
7
9
  This package is Wiki Format Parser.
8
10
  Wiki => html
9
11
 
@@ -12,9 +14,11 @@ Wiki => html
12
14
 
13
15
  == SYNOPSIS:
14
16
 
17
+ http://wiki.github.com/iori/wparser/tutorial
18
+
15
19
  require 'wparser'
16
20
 
17
- parser = WParser.parser.new
21
+ parser = WParser.new
18
22
  parser.parse(text)
19
23
 
20
24
  == REQUIREMENTS:
@@ -23,7 +27,9 @@ parser.parse(text)
23
27
 
24
28
  == INSTALL:
25
29
 
26
- gem install wparser
30
+ http://wiki.github.com/iori/wparser/setup
31
+
32
+ $sudo gem install wparser
27
33
 
28
34
  == LICENSE:
29
35
 
data/lib/wparser.rb CHANGED
@@ -3,10 +3,10 @@ $:.unshift(File.dirname(__FILE__)) unless
3
3
 
4
4
  #require File.join(directory, 'wparser', 'Parser')
5
5
  require 'wparser/parser'
6
- module Wparser
7
- VERSION = '0.0.2'
6
+ module WParser
7
+ VERSION = '0.1.0'
8
8
 
9
- def Wparser.new
9
+ def WParser.new
10
10
  Parser.new
11
11
  end
12
12
  end
@@ -1,17 +1,37 @@
1
- require 'wparser/block/blocks'
1
+ require 'wparser/block/pre'
2
+ require 'wparser/block/blockquote'
2
3
  require 'wparser/block/list_number'
3
4
  require 'wparser/block/list_nonumber'
4
5
 
5
6
  module Block
6
7
  include Blocks
7
- include List_number
8
- include List_nonumber
9
8
 
10
9
  def parse text
11
- parse_text = Blocks.parse(text)
12
- parse_text = List_number.parse(parse_text)
13
- parse_text = List_nonumber.parse(parse_text)
10
+ @protected_preTag = nil
11
+ parse_text = text.collect{|line| parse_line line}
14
12
  end
15
13
 
16
- module_function :parse
14
+ private
15
+ def parse_line line
16
+ parse_line = line
17
+ parse_line = Pre.parse parse_line
18
+
19
+ if parse_line =~ /^<pre.*>/
20
+ @protected_preTag = true
21
+ elsif parse_line =~ /<\/pre>/
22
+ @protected_preTag = nil
23
+ end
24
+
25
+ #puts @protected_preTag
26
+
27
+ if @protected_preTag == nil
28
+ parse_line = Blockquote.parse parse_line
29
+ parse_line = List_number.parse parse_line
30
+ parse_line = List_nonumber.parse parse_line
31
+ else
32
+ parse_line
33
+ end
34
+ end
35
+
36
+ module_function :parse, :parse_line
17
37
  end
@@ -0,0 +1,15 @@
1
+ require 'wparser/parse'
2
+
3
+ module Blocks
4
+ class Blockquote < Parse
5
+ BLOCK_TABLE = {
6
+ ">>" => '<blockquote>',
7
+ "<<" => '</blockquote>',
8
+ }
9
+
10
+ def self.parse line
11
+ @result = line.gsub(/(^>>)|(^<<)/){|s| BLOCK_TABLE[s]}
12
+ super
13
+ end
14
+ end
15
+ end
@@ -1,22 +1,19 @@
1
- module List_nonumber
2
- def parse text
3
- start_ul_rex = %r!
4
- ^\s*$\n # 空行 => \n<ul>\n
5
- ^\*\s(\w*)\n # * list_start => <li>list_start</li>\n
6
- !x
7
- parse_text = text.gsub(start_ul_rex){|s| "\n<ul>\n<li>#$1</li>\n"}
1
+ require 'wparser/parser'
8
2
 
9
- end_ul_rex = %r!
10
- ^\*\s(\w*)\n # * list_end => <li>list_end</li>\n
11
- ^\s*$\n # 空行 => </ul>\n\n
12
- !x
13
- parse_text = parse_text.gsub(end_ul_rex){|s| "<li>#$1</li>\n</ul>\n\n"}
14
-
15
- # * list => <li>list</li>
16
- parse_text = parse_text.gsub(/^\*\s(\w*)\n/){|s| "<li>#$1</li>\n"}
17
- return text if parse_text == nil
18
- parse_text
19
- end
20
-
21
- module_function :parse
3
+ module Blocks
4
+ class List_nonumber < Parse
5
+ def self.parse line
6
+ rex = /^\*\s*(\w*)/
7
+ @result = if line =~ rex and @list_flag == nil
8
+ @list_flag = true
9
+ line.sub(rex, "<ul>\n<li>#$1</li>")
10
+ elsif line =~ rex
11
+ line.sub(rex, "<li>#$1</li>")
12
+ elsif line !~ rex and @list_flag == true
13
+ @list_flag = nil
14
+ "</ul>#{line}\n"
15
+ end
16
+ super
17
+ end
18
+ end
22
19
  end
@@ -1,22 +1,20 @@
1
- module List_number
2
- def parse text
3
- start_ul_rex = %r!
4
- ^\s*$\n # 空行 => \n<ul>\n
5
- ^\#\s(\w*)\n # # list_start => <li>list_start</li>\n
6
- !x
7
- parse_text = text.gsub(start_ul_rex){|s| "\n<ol>\n<li>#$1</li>\n"}
1
+ require 'wparser/parse'
8
2
 
9
- end_ul_rex = %r!
10
- ^\#\s(\w*)\n # # list_end => <li>list_end</li>\n
11
- ^\s*$\n # 空行 => </ol>\n\n
12
- !x
13
- parse_text = parse_text.gsub(end_ul_rex){|s| "<li>#$1</li>\n</ol>\n\n"}
3
+ module Blocks
4
+ class List_number < Parse
5
+ def self.parse line
6
+ rex = /^\#\s*(\w*)/
7
+ @result = if line =~ rex and @list_flag == nil
8
+ @list_flag = true
9
+ line.sub(rex, "<ol>\n<li>#$1</li>")
10
+ elsif line =~ rex
11
+ line.sub(rex, "<li>#$1</li>")
12
+ elsif line !~ rex and @list_flag == true
13
+ @list_flag = nil
14
+ "</ol>#{line}\n"
15
+ end
16
+ super
14
17
 
15
- # * list => <li>list</li>
16
- parse_text = parse_text.gsub(/^\#\s(\w*)\n/){|s| "<li>#$1</li>\n"}
17
- return text if parse_text == nil
18
- parse_text
19
- end
20
-
21
- module_function :parse
18
+ end
19
+ end
22
20
  end
@@ -0,0 +1,23 @@
1
+ require 'wparser/parse'
2
+
3
+ module Blocks
4
+ class Pre < Parse
5
+ BLOCK_TABLE = {
6
+ ">||" => '<pre>',
7
+ "||<" => '</pre>',
8
+ ">|code|" => '<pre class="prettyprint">',
9
+ "|code|<" => '</pre>'
10
+ }
11
+
12
+ def self.parse line
13
+ block_rex = %r!
14
+ (^>\|\|)|(^\|\|<) # pre
15
+ |
16
+ (^\>\|code\|)|(^\|code\|\<) # google-code-prettify
17
+ !x
18
+
19
+ @result = line.gsub(block_rex){|s| BLOCK_TABLE[s]}
20
+ super
21
+ end
22
+ end
23
+ end
@@ -1,9 +1,13 @@
1
- module Deleted
2
- def parse line
3
- parse_line = line.sub(/-\b(\w*)\b-/){|s| "<S>#$1</S>"}
4
- return line if parse_line == nil
5
- parse_line
6
- end
1
+ require 'wparser/parse'
7
2
 
8
- module_function :parse
3
+ module Inlines
4
+ class Deleted < Parse
5
+ def self.parse line
6
+ @result = line.gsub!(/(?:(?!\\-)(^|.)-)(.+?)(?:(?!\\-)(.)-)/){|s|
7
+ "#$1<S>#$2#$3</S>"
8
+ }
9
+ @result = line.gsub!(/\\-/, '-')
10
+ super
11
+ end
12
+ end
9
13
  end
@@ -1,9 +1,10 @@
1
- module Heading
2
- def parse line
3
- parse_line = line.sub(/^h([1-6])\.\s(.*)\n/){|s| "<h#$1>#$2</h#$1>\n" }
4
- return line if parse_line == nil
5
- parse_line
6
- end
1
+ require 'wparser/parse'
7
2
 
8
- module_function :parse
3
+ module Inlines
4
+ class Heading < Parse
5
+ def self.parse line
6
+ @result = line.sub(/^h([1-6])\.\s(.*)\n/){|s| "<h#$1>#$2</h#$1>\n"}
7
+ super
8
+ end
9
+ end
9
10
  end
@@ -1,19 +1,21 @@
1
- module Http
2
- # (?<![.,?\!]) 1.8.7では後読みが出来ない、どーするか。
3
- def parse text
4
- url_rex = %r!
5
- \b
6
- (
7
- http:// [-a-z0-9]+(\.[-a-z0-9]+)*\.(com|edu|info|net) \b
1
+ require 'wparser/parse'
2
+
3
+ module Inlines
4
+ class Http < Parse
5
+ # (?<![.,?\!]) 1.8.7では後読みが出来ない、どーするか。
6
+ def self.parse text
7
+ url_rex = %r!
8
+ \b
8
9
  (
9
- / [-a-z0-9_:\@&?=+,.\!/~*'%\$]*[^.,?\!]
10
- )?
11
- )
12
- !x
13
- parse_text = text.gsub(url_rex){|s| "<a href=\"#$1\">#$1</a>"}
14
- return text if parse_text == nil
15
- parse_text
16
- end
10
+ http:// [-a-z0-9]+(\.[-a-z0-9]+)*\.(com|edu|info|net) \b
11
+ (
12
+ / [-a-z0-9_:\@&?=+,.\!/~*'%\$]*[^.,?\!]
13
+ )?
14
+ )
15
+ !x
17
16
 
18
- module_function :parse
17
+ @result = text.gsub(url_rex){|s| "<a href=\"#$1\">#$1</a>"}
18
+ super
19
+ end
20
+ end
19
21
  end
@@ -4,31 +4,39 @@ require 'wparser/inline/italic'
4
4
  require 'wparser/inline/underLine'
5
5
  require 'wparser/inline/deleted'
6
6
  require 'wparser/inline/http'
7
+ require 'wparser/inline/paragraph'
7
8
 
8
9
  module Inline
9
- include Heading
10
- include Strong
11
- include Italic
12
- include UnderLine
13
- include Deleted
14
- include Http
10
+ include Inlines
15
11
 
16
12
  def parse text
17
- parse_text = ""
13
+ @protected_preTag = nil
14
+ parse_lines = text.collect {|line| parse_line line}
18
15
 
19
- text.each do |line|
20
- parse_line = Heading.parse(line)
21
- parse_line = Strong.parse(parse_line)
22
- parse_line = Italic.parse(parse_line)
23
- parse_line = UnderLine.parse(parse_line)
24
- parse_line = Deleted.parse(parse_line)
16
+ Http.parse parse_lines.to_s
17
+ end
25
18
 
26
- parse_text << parse_line
19
+ private
20
+ # 1行ずつ処理したいメソッド郡
21
+ def parse_line line
22
+ if line =~ /^<pre.*>/
23
+ @protected_preTag = true
24
+ elsif line =~ /<\/pre>/
25
+ @protected_preTag = nil
27
26
  end
28
27
 
29
- parse_text = Http.parse(parse_text)
30
- parse_text
28
+ parse_line = line
29
+ if @protected_preTag == nil
30
+ parse_line = Heading.parse parse_line
31
+ parse_line = Strong.parse parse_line
32
+ parse_line = Italic.parse parse_line
33
+ parse_line = UnderLine.parse parse_line
34
+ parse_line = Deleted.parse parse_line
35
+ parse_line = Paragraph.parse parse_line
36
+ else
37
+ parse_line
38
+ end
31
39
  end
32
40
 
33
- module_function :parse
41
+ module_function :parse, :parse_line
34
42
  end
@@ -1,9 +1,13 @@
1
- module Italic
2
- def parse line
3
- parse_line = line.sub(/\b_(\w*)_\b/){|s| "<I>#$1</I>"}
4
- return line if parse_line == nil
5
- parse_line
6
- end
1
+ require 'wparser/parse'
7
2
 
8
- module_function :parse
3
+ module Inlines
4
+ class Italic < Parse
5
+ def self.parse line
6
+ @result = line.gsub!(/(?:(?!\\_)(^|.)_)(.+?)(?:(?!\\_)(.)_)/){|s|
7
+ "#$1<I>#$2#$3</I>"
8
+ }
9
+ @result = line.gsub!(/\\_/, '_')
10
+ super
11
+ end
12
+ end
9
13
  end
@@ -0,0 +1,27 @@
1
+ require 'wparser/parse'
2
+
3
+ module Inline
4
+ class Paragraph < Parse
5
+ def self.parse line
6
+ rex = %r!
7
+ ^((<(\/?pre.*|\/?h\d|\/?p|\/?ol|\/?ul|\/?li|\/?blockquote)>)
8
+ |
9
+ (\s?\n))
10
+ !x
11
+ @result = if @p_flag == nil
12
+ if line !~ rex and @pre_flag == nil
13
+ @p_flag = true
14
+ "<p>\n" + line.sub(/\n/, "<br />\n")
15
+ end
16
+ else # <p>と</>の間にいます。
17
+ if line =~ rex # 段落の終了点
18
+ @p_flag = nil
19
+ "</p>\n" + line
20
+ else
21
+ line.sub(/\n/, "<br />\n")
22
+ end
23
+ end
24
+ super
25
+ end
26
+ end
27
+ end
@@ -1,9 +1,13 @@
1
- module Strong
2
- def parse line
3
- parse_line = line.sub(/\*\b(\w*)\b\*/){|s| "<strong>#$1</strong>"}
4
- return line if parse_line == nil
5
- parse_line
6
- end
1
+ require 'wparser/parse'
7
2
 
8
- module_function :parse
3
+ module Inlines
4
+ class Strong < Parse
5
+ def self.parse line
6
+ @result = line.gsub!(/(?:(?!\\\*)(^|.)\*)(.+?)(?:(?!\\\*)(.)\*)/){|s|
7
+ "#$1<strong>#$2#$3</strong>"
8
+ }
9
+ @result = line.gsub!(/\\\*/, '*')
10
+ super
11
+ end
12
+ end
9
13
  end
@@ -1,9 +1,13 @@
1
- module UnderLine
2
- def parse line
3
- parse_line = line.sub(/\+\b(\w*)\b\+/){|s| "<U>#$1</U>"}
4
- return line if parse_line == nil
5
- parse_line
6
- end
1
+ require 'wparser/parse'
7
2
 
8
- module_function :parse
3
+ module Inlines
4
+ class UnderLine < Parse
5
+ def self.parse line
6
+ @result = line.gsub!(/(?:(?!\\\+)(^|.)\+)(.+?)(?:(?!\\\+)(.)\+)/){|s|
7
+ "#$1<U>#$2#$3</U>"
8
+ }
9
+ @result = line.gsub!(/\\\+/, '+')
10
+ super
11
+ end
12
+ end
9
13
  end
@@ -1,14 +1,18 @@
1
- require 'wparser/break'
1
+ require 'wparser/util/break'
2
2
  require 'wparser/inline/inline'
3
3
  require 'wparser/block/block'
4
4
 
5
5
  class Parser
6
6
  def parse text
7
- text = Break.parse(text)
8
- text = Inline.parse(text)
9
- text = Block.parse(text)
7
+ parse_all text
8
+ end
9
+
10
+ private
11
+ def parse_all text
12
+ parse_text = text
10
13
 
11
- #html = text.to_html
12
- text
14
+ parse_text = Break.parse parse_text
15
+ parse_text = Block.parse parse_text
16
+ parse_text = Inline.parse parse_text # <p>の為にblockよりも後に!
13
17
  end
14
18
  end
@@ -0,0 +1,8 @@
1
+ require 'wparser/parse'
2
+
3
+ class Break < Parse
4
+ def self.parse text
5
+ @result = text.gsub(/\r\n/, "\n").gsub(/\r/, "\n")
6
+ super
7
+ end
8
+ end
metadata CHANGED
@@ -5,9 +5,9 @@ version: !ruby/object:Gem::Version
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
+ - 1
8
9
  - 0
9
- - 2
10
- version: 0.0.2
10
+ version: 0.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - iori
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-06-13 00:00:00 +09:00
18
+ date: 2010-08-22 00:00:00 +09:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -51,6 +51,8 @@ dependencies:
51
51
  type: :development
52
52
  version_requirements: *id002
53
53
  description: |-
54
+ http://wiki.github.com/iori/wparser/
55
+
54
56
  This package is Wiki Format Parser.
55
57
  Wiki => html
56
58
  email:
@@ -72,16 +74,18 @@ files:
72
74
  - bin/wparser
73
75
  - lib/wparser.rb
74
76
  - lib/wparser/block/block.rb
75
- - lib/wparser/block/blocks.rb
77
+ - lib/wparser/block/blockquote.rb
76
78
  - lib/wparser/block/list_nonumber.rb
77
79
  - lib/wparser/block/list_number.rb
78
- - lib/wparser/break.rb
80
+ - lib/wparser/block/pre.rb
81
+ - lib/wparser/util/break.rb
79
82
  - lib/wparser/cli.rb
80
83
  - lib/wparser/inline/deleted.rb
81
84
  - lib/wparser/inline/heading.rb
82
85
  - lib/wparser/inline/http.rb
83
86
  - lib/wparser/inline/inline.rb
84
87
  - lib/wparser/inline/italic.rb
88
+ - lib/wparser/inline/paragraph.rb
85
89
  - lib/wparser/inline/strong.rb
86
90
  - lib/wparser/inline/underLine.rb
87
91
  - lib/wparser/parser.rb
@@ -95,7 +99,7 @@ files:
95
99
  - spec/wparser_spec.rb
96
100
  - tasks/rspec.rake
97
101
  has_rdoc: true
98
- homepage: http://github.com/#{github_username}/#{project_name}
102
+ homepage: http://github.com/iori/wparser
99
103
  licenses: []
100
104
 
101
105
  post_install_message: PostInstall.txt
@@ -128,6 +132,6 @@ rubyforge_project: wparser
128
132
  rubygems_version: 1.3.7
129
133
  signing_key:
130
134
  specification_version: 3
131
- summary: This package is Wiki Format Parser
135
+ summary: http://wiki.github.com/iori/wparser/ This package is Wiki Format Parser
132
136
  test_files: []
133
137
 
@@ -1,26 +0,0 @@
1
- module Blocks
2
- BLOCK_TABLE = {
3
- ">>" => '<blockquote>',
4
- "<<" => '</blockquote>',
5
- ">||" => '<pre>',
6
- "||<" => '</pre>',
7
- ">|code|" => '<pre class="prettyprint">',
8
- "|code|<" => '</pre>'
9
- }
10
-
11
- def parse text
12
- block_rex = %r!
13
- (^>>)|(^<<) # blockquote
14
- |
15
- (^>\|\|)|(^\|\|<) # pre
16
- |
17
- (^\>\|code\|)|(^\|code\|\<) # google-code-prettify
18
- !x
19
-
20
- parse_text = text.gsub(block_rex){|s| BLOCK_TABLE[s]}
21
- return text if parse_text == nil
22
- parse_text
23
- end
24
-
25
- module_function :parse
26
- end
data/lib/wparser/break.rb DELETED
@@ -1,10 +0,0 @@
1
- module Break
2
- def parse text
3
- parse_text = text.gsub(/\r/, "\n").gsub(/\r\n/, "\n")
4
-
5
- return text if parse_text ==nil
6
- parse_text
7
- end
8
-
9
- module_function :parse
10
- end