facwparser 0.0.2 → 0.0.3

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.
@@ -19,12 +19,24 @@ module Facwparser
19
19
  def render_html(options)
20
20
  raise "TODO: render_html is not implemented: " + self.class.to_s + "\n"
21
21
  end
22
+ def render_text(options)
23
+ CGI.unescapeHTML(render_html(options).strip.gsub(/<\/?[^>]*>/, ""))
24
+ end
22
25
  private
23
26
  def render_html_by_name_and_value(name, value, element_join_char = '')
24
- ["<#{CGI.escapeHTML name}>", CGI.escapeHTML(value), "</#{CGI.escapeHTML name}>"].join(element_join_char)
27
+ if name.is_a? Array
28
+ ["<#{CGI.escapeHTML name[0]} " + name[1].map{|k,v| CGI.escapeHTML(k) + '="' + CGI.escapeHTML(v) + '"' + ">"}.join(' '), CGI.escapeHTML(value), "</#{CGI.escapeHTML name[0]}>"].join(element_join_char)
29
+ else
30
+ ["<#{CGI.escapeHTML name}>", CGI.escapeHTML(value), "</#{CGI.escapeHTML name}>"].join(element_join_char)
31
+ end
25
32
  end
26
- def render_html_by_name_and_childlen(name, children, options, childlen_join_char = '', element_join_char = '')
27
- ["<#{CGI.escapeHTML name}>", children.map {|c| c.render_html(options) }.join(childlen_join_char), "</#{CGI.escapeHTML name}>"].join(element_join_char)
33
+ def render_html_by_name_and_children(name, children, options, children_join_char = '', element_join_char = '')
34
+ if name.is_a? Array
35
+ ["<#{CGI.escapeHTML name[0]} " + name[1].map{|k,v| CGI.escapeHTML(k) + '="' + CGI.escapeHTML(v) + '"' + ">"}.join(' '),
36
+ children.map {|c| c.render_html(options) }.join(children_join_char), "</#{CGI.escapeHTML name[0]}>"].join(element_join_char)
37
+ else
38
+ ["<#{CGI.escapeHTML name}>", children.map {|c| c.render_html(options) }.join(children_join_char), "</#{CGI.escapeHTML name}>"].join(element_join_char)
39
+ end
28
40
  end
29
41
  end
30
42
 
@@ -39,7 +51,7 @@ module Facwparser
39
51
  "</blockquote>\n"
40
52
  else
41
53
  @children ||= Parser.parse_value(@source, options)
42
- render_html_by_name_and_childlen('p', @children, options) + "\n"
54
+ render_html_by_name_and_children('p', @children, options) + "\n"
43
55
  end
44
56
  end
45
57
  end
@@ -50,6 +62,7 @@ module Facwparser
50
62
  end
51
63
  class Heading < ElementBase
52
64
  attr_reader :level, :value
65
+ attr_accessor :id
53
66
  def initialize(source, level, value)
54
67
  super(source)
55
68
  @level = level
@@ -57,7 +70,12 @@ module Facwparser
57
70
  end
58
71
  def render_html(options)
59
72
  @children = Parser.parse_value value, options
60
- render_html_by_name_and_childlen("h#{level}", @children, options) + "\n"
73
+
74
+ if @id
75
+ render_html_by_name_and_children(["h#{level}", {'id' => @id}], @children, options) + "\n"
76
+ else
77
+ render_html_by_name_and_children("h#{level}", @children, options) + "\n"
78
+ end
61
79
  end
62
80
  end
63
81
  class List < ElementBase
@@ -72,7 +90,7 @@ module Facwparser
72
90
  self
73
91
  end
74
92
  def render_html(options)
75
- (render_html_by_name_and_childlen(@type == '#' ? 'ol' : 'ul', @children, options, "\n", "\n") + "\n").gsub("\n\n", "\n")
93
+ (render_html_by_name_and_children(@type == '#' ? 'ol' : 'ul', @children, options, "\n", "\n") + "\n").gsub("\n\n", "\n")
76
94
  end
77
95
  end
78
96
  class ListItem < ElementBase
@@ -85,7 +103,7 @@ module Facwparser
85
103
  end
86
104
  def render_html(options)
87
105
  @children ||= Parser.parse_value value, options
88
- render_html_by_name_and_childlen('li', @children, options)
106
+ render_html_by_name_and_children('li', @children, options)
89
107
  end
90
108
  end
91
109
  class Table < ElementBase
@@ -99,9 +117,9 @@ module Facwparser
99
117
  end
100
118
  def render_html(options)
101
119
  "<table>\n" +
102
- render_html_by_name_and_childlen('thead', @children.take(1), options, "\n", "\n") + "\n" +
103
- render_html_by_name_and_childlen('tbody', @children.drop(1), options, "\n", "\n") + "\n" +
104
- "</table>\n"
120
+ render_html_by_name_and_children('thead', @children.take(1), options, "\n", "\n") + "\n" +
121
+ render_html_by_name_and_children('tbody', @children.drop(1), options, "\n", "\n") + "\n" +
122
+ "</table>\n"
105
123
  end
106
124
  end
107
125
  class TableHeaders < ElementBase
@@ -112,8 +130,8 @@ module Facwparser
112
130
  end
113
131
  def render_html(options)
114
132
  "<tr>" +
115
- @elements.map { |e| render_html_by_name_and_childlen('th', Parser.parse_value(e, options), options) }.join() +
116
- "</tr>"
133
+ @elements.map { |e| render_html_by_name_and_children('th', Parser.parse_value(e, options), options) }.join() +
134
+ "</tr>"
117
135
  end
118
136
  end
119
137
  class TableData < ElementBase
@@ -148,31 +166,27 @@ module Facwparser
148
166
  end
149
167
  @elements << element if !element.empty?
150
168
  end
151
- def render_html(options)
152
- "<tr>" +
153
- @elements.map { |e| render_html_by_name_and_childlen('td', Parser.parse_value(e, options), options) }.join() +
154
- "</tr>"
155
- end
169
+
170
+ def render_html(options)
171
+ "<tr>" +
172
+ @elements.map { |e| render_html_by_name_and_children('td', Parser.parse_value(e, options), options) }.join() +
173
+ "</tr>"
174
+ end
156
175
  end
157
176
 
158
177
  class MacroBase < ElementBase
159
178
  end
160
179
  class TocMacro < MacroBase
180
+ attr_accessor :headings
161
181
  def initialize(source, options = nil)
162
182
  super(source)
163
183
  @options = options
164
184
  end
165
185
  def render_html(options)
166
- "TODO: table of contents Macro\n"
167
- end
168
- end
169
- class PagetreeMacro < MacroBase
170
- def initialize(source, options = nil)
171
- super(source)
172
- @options = options
173
- end
174
- def render_html(options)
175
- "TODO: pagetree macro\n"
186
+ "<ul>\n" +
187
+ @headings.map{|h| '<li><a href="#%s">%s</a></li>' %
188
+ [CGI.escapeHTML(h.id), CGI.escapeHTML(h.render_text(options))]}.join("\n") +
189
+ "\n</ul>\n"
176
190
  end
177
191
  end
178
192
  class NoformatMacro < MacroBase
@@ -226,26 +240,29 @@ module Facwparser
226
240
  elsif @text =~ /\A(?:https?|ftp|file):.+\z/
227
241
  return '<a href="' + CGI.escapeHTML(@text) +'">' + CGI.escapeHTML(@text) + '</a>'
228
242
  else
229
- return '[' + CGI.escapeHTML(@text) + ']'
243
+ url_prefix = (options && options['url_prefix']) || '/'
244
+ return '<a href="' + CGI.escapeHTML(url_prefix + @text) +'">' + CGI.escapeHTML(@text) + '</a>'
230
245
  end
231
246
  end
232
247
  end
233
248
 
234
- class Bold < InlineElementBase
249
+ class Strong < InlineElementBase
235
250
  def render_html(options)
236
- render_html_by_name_and_value('b', @text)
251
+ render_html_by_name_and_value('strong', @text)
237
252
  end
238
253
  end
239
254
 
240
- class Italic < InlineElementBase
255
+ class Emphasis < InlineElementBase
241
256
  def render_html(options)
242
- render_html_by_name_and_value('i', @text)
257
+ render_html_by_name_and_value('em', @text)
243
258
  end
244
259
  end
245
260
 
246
261
  class Strike < InlineElementBase
247
262
  def render_html(options)
248
- render_html_by_name_and_value('s', @text)
263
+ '<span style="text-decoration: line-through;">' +
264
+ CGI.escapeHTML(@text) +
265
+ '</span>'
249
266
  end
250
267
  end
251
268
 
@@ -273,9 +290,9 @@ module Facwparser
273
290
  end
274
291
  end
275
292
 
276
- class TT < InlineElementBase
293
+ class Monospace < InlineElementBase
277
294
  def render_html(options)
278
- render_html_by_name_and_value('tt', @text)
295
+ render_html_by_name_and_value('code', @text)
279
296
  end
280
297
  end
281
298
 
@@ -304,7 +321,7 @@ module Facwparser
304
321
  @options = options
305
322
  end
306
323
  def render_html(options)
307
- return '<font color="' + CGI.escapeHTML(@options) +'">'
324
+ return '<span style="color: ' + CGI.escapeHTML(@options) +'">'
308
325
  end
309
326
  end
310
327
 
@@ -313,7 +330,7 @@ module Facwparser
313
330
  super(source)
314
331
  end
315
332
  def render_html(options)
316
- return '</font>'
333
+ return '</span>'
317
334
  end
318
335
  end
319
336
 
@@ -14,7 +14,23 @@ module Facwparser
14
14
  def self.process_elements(elements, options)
15
15
  # TODO toc
16
16
  processed = add_list_elements(elements, options)
17
- add_table_elements(processed, options)
17
+ processed = add_table_elements(processed, options)
18
+ processed = add_toc(processed, options)
19
+ processed
20
+ end
21
+
22
+ def self.add_toc(elements, options)
23
+ tocs = elements.select{ |e| e.class == Element::TocMacro}
24
+ if !tocs.empty?
25
+ headings = elements.select{ |e| e.class == Element::Heading && e.level == 1}
26
+ id = 0
27
+ headings.each { |h|
28
+ h.id = 'heading_' + id.to_s
29
+ id += 1
30
+ }
31
+ tocs.each {|t| t.headings = headings }
32
+ end
33
+ elements
18
34
  end
19
35
 
20
36
  def self.add_list_elements(elements, options)
@@ -90,9 +106,6 @@ module Facwparser
90
106
  when s.scan(/\{toc(:.*)?\} *\n/)
91
107
  p = nil
92
108
  elements << Element::TocMacro.new(s[0], s[1] ? s[1][1,] : nil)
93
- when s.scan(/\{pagetree(:.*)?\} *\n/)
94
- p = nil
95
- elements << Element::PagetreeMacro.new(s[0], s[1] ? s[1][1,] : nil)
96
109
  when s.scan(/\{noformat\} *\n(?m)(.+?\n)\{noformat\} *\n/)
97
110
  p = nil
98
111
  elements << Element::NoformatMacro.new(s[0], s[1])
@@ -138,9 +151,9 @@ module Facwparser
138
151
  when s.scan(/\[(.+?)(?<!\\)\]/)
139
152
  children << Element::A.new(s[0], unescape_text(s[1]))
140
153
  when s.scan(/\*(.+?)(?<!\\)\*/)
141
- children << Element::Bold.new(s[0], unescape_text(s[1]))
154
+ children << Element::Strong.new(s[0], unescape_text(s[1]))
142
155
  when s.scan(/\_(.+?)(?<!\\)\_/)
143
- children << Element::Italic.new(s[0], unescape_text(s[1]))
156
+ children << Element::Emphasis.new(s[0], unescape_text(s[1]))
144
157
  when s.scan(/\-(.+?)(?<!\\)\-/)
145
158
  children << Element::Strike.new(s[0], unescape_text(s[1]))
146
159
  when s.scan(/\+(.+?)(?<!\\)\+/)
@@ -152,9 +165,11 @@ module Facwparser
152
165
  when s.scan(/\?\?(.+?)(?<!\\)\?\?/)
153
166
  children << Element::Q.new(s[0], unescape_text(s[1]))
154
167
  when s.scan(/\{\{(.+?)(?<!\\)\}\}/)
155
- children << Element::TT.new(s[0], unescape_text(s[1]))
168
+ children << Element::Monospace.new(s[0], unescape_text(s[1]))
156
169
  when s.scan(/\!(https?:(?:.+?))(?<!\\)\!/)
157
170
  children << Element::Image.new(s[0], unescape_text(s[1]))
171
+ when s.scan(/\!(\/(?:.+?))(?<!\\)\!/)
172
+ children << Element::Image.new(s[0], unescape_text(s[1]))
158
173
  when s.scan(/\{jira:(.+?)(?<!\\)\}/)
159
174
  children << Element::JiraMacro.new(s[0], unescape_text(s[1]))
160
175
  when s.scan(/\{color:(.+?)(?<!\\)\}/)
@@ -1,3 +1,3 @@
1
1
  module Facwparser
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/lib/facwparser.rb CHANGED
@@ -3,7 +3,7 @@ require 'facwparser/parser'
3
3
  require 'facwparser/render'
4
4
 
5
5
  module Facwparser
6
- def self.to_html(source, options = nil)
6
+ def self.to_html(source, options = {})
7
7
  Render.render_html(Parser.parse(source, options), options) + "\n"
8
8
  end
9
9
  end
@@ -0,0 +1,31 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require 'test/unit'
3
+ require File.dirname(__FILE__) + '/../../../lib/facwparser/element'
4
+
5
+
6
+ class TestP < Test::Unit::TestCase
7
+
8
+ def test_a_1
9
+ a = Facwparser::Element::A.new('[hoge|http://www.unixuser.org]', 'hoge|http://www.unixuser.org')
10
+ assert_equal(%Q{<a href="http://www.unixuser.org">hoge</a>},
11
+ a.render_html({}))
12
+ end
13
+
14
+ def test_a_2
15
+ a = Facwparser::Element::A.new('[http://www.unixuser.org]', 'http://www.unixuser.org')
16
+ assert_equal(%Q{<a href="http://www.unixuser.org">http://www.unixuser.org</a>},
17
+ a.render_html({}))
18
+ end
19
+
20
+ def test_a_3
21
+ a = Facwparser::Element::A.new('[hoge]', 'hoge')
22
+ assert_equal(%Q{<a href="/hoge">hoge</a>},
23
+ a.render_html({}))
24
+ end
25
+
26
+ def test_a_4
27
+ a = Facwparser::Element::A.new('[hoge]', 'hoge')
28
+ assert_equal(%Q{<a href="http://www.unixuser.org/hoge">hoge</a>},
29
+ a.render_html({'url_prefix' => 'http://www.unixuser.org/'}))
30
+ end
31
+ end
@@ -15,18 +15,41 @@ class TestHeading < Test::Unit::TestCase
15
15
  heading = Facwparser::Element::Heading.new('h3. hoge>', 3, 'hoge>')
16
16
  assert_equal("<h3>hoge&gt;</h3>\n",
17
17
  heading.render_html({}))
18
+ assert_equal(%Q{hoge>},
19
+ heading.render_text({}))
18
20
  end
19
21
 
20
22
  def test_heading_3
21
23
  heading = Facwparser::Element::Heading.new('h1. hoge[http://www.unixuser.org]', 1, 'hoge[http://www.unixuser.org]')
22
24
  assert_equal(%Q{<h1>hoge<a href="http://www.unixuser.org">http://www.unixuser.org</a></h1>\n},
23
25
  heading.render_html({}))
26
+ assert_equal(%Q{hogehttp://www.unixuser.org},
27
+ heading.render_text({}))
24
28
  end
25
29
 
26
30
  def test_heading_4
27
31
  heading = Facwparser::Element::Heading.new('h1. hoge [http://www.unixuser.org]', 1, 'hoge [http://www.unixuser.org]')
28
32
  assert_equal(%Q{<h1>hoge <a href="http://www.unixuser.org">http://www.unixuser.org</a></h1>\n},
29
33
  heading.render_html({}))
34
+ assert_equal(%Q{hoge http://www.unixuser.org},
35
+ heading.render_text({}))
36
+ end
37
+ def test_heading_5
38
+ heading = Facwparser::Element::Heading.new('h1. hoge[http://www.unixuser.org]', 1, 'hoge[http://www.unixuser.org]')
39
+ heading.id = 'heading_0'
40
+ assert_equal(%Q{<h1 id="heading_0">hoge<a href="http://www.unixuser.org">http://www.unixuser.org</a></h1>\n},
41
+ heading.render_html({}))
42
+ assert_equal(%Q{hogehttp://www.unixuser.org},
43
+ heading.render_text({}))
44
+ end
45
+
46
+ def test_heading_6
47
+ heading = Facwparser::Element::Heading.new('h1. hoge[http://www.unixuser.org]', 1, 'hoge[http://www.unixuser.org]')
48
+ heading.id = 'he<ading_0'
49
+ assert_equal(%Q{<h1 id="he&lt;ading_0">hoge<a href="http://www.unixuser.org">http://www.unixuser.org</a></h1>\n},
50
+ heading.render_html({}))
51
+ assert_equal(%Q{hogehttp://www.unixuser.org},
52
+ heading.render_text({}))
30
53
  end
31
54
  end
32
55
 
@@ -13,7 +13,7 @@ class TestListItem < Test::Unit::TestCase
13
13
 
14
14
  def test_list_item_2
15
15
  li = Facwparser::Element::ListItem.new('## hoge*nyo*', '##', 'hoge*nyo*')
16
- assert_equal(%Q{<li>hoge<b>nyo</b></li>},
16
+ assert_equal(%Q{<li>hoge<strong>nyo</strong></li>},
17
17
  li.render_html({}))
18
18
  end
19
19
  end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require 'test/unit'
3
+ require File.dirname(__FILE__) + '/../../../lib/facwparser/element'
4
+
5
+
6
+ class TestToc < Test::Unit::TestCase
7
+
8
+ def test_toc_1
9
+ toc = Facwparser::Element::TocMacro.new('{toc}', '')
10
+ heading1 = Facwparser::Element::Heading.new('h1. hoge', 1, 'hoge')
11
+ heading1.id = 'heading1'
12
+ heading2 = Facwparser::Element::Heading.new('h1. kuke', 1, 'kuke')
13
+ heading2.id = 'heading2'
14
+
15
+ toc.headings = [heading1, heading2]
16
+
17
+
18
+ assert_equal(%Q{<ul>\n<li><a href="#heading1">hoge</a></li>\n<li><a href="#heading2">kuke</a></li>\n</ul>\n},
19
+ toc.render_html({}))
20
+ end
21
+ end
@@ -138,22 +138,6 @@ EOS
138
138
 
139
139
  end
140
140
 
141
- def test_parse1_pagetree
142
- source =<<EOS
143
- 1
144
- {pagetree:root=@self}
145
- 2
146
- EOS
147
- assert_equal(
148
- [
149
- Facwparser::Element::P.new("1\n"),
150
- Facwparser::Element::PagetreeMacro.new("{pagetree:root=@self}\n", 'root=@self'),
151
- Facwparser::Element::P.new("2\n"),
152
- ],
153
- Facwparser::Parser.parse1(source, {}))
154
-
155
- end
156
-
157
141
  def test_parse1_noformat
158
142
  source =<<EOS
159
143
  1
@@ -31,18 +31,18 @@ class TestParseValue < Test::Unit::TestCase
31
31
  ], Facwparser::Parser.parse_value('[株式会社ミクシィ|https://mixi.co.jp/]', {}))
32
32
  end
33
33
 
34
- def test_parse_value_bold
34
+ def test_parse_value_strong
35
35
  assert_equal([
36
36
  Facwparser::Element::Text.new('1', '1'),
37
- Facwparser::Element::Bold.new('*hoge*', 'hoge'),
37
+ Facwparser::Element::Strong.new('*hoge*', 'hoge'),
38
38
  Facwparser::Element::Text.new('2', '2')
39
39
  ], Facwparser::Parser.parse_value('1*hoge*2', {}))
40
40
  end
41
41
 
42
- def test_parse_value_italic
42
+ def test_parse_value_emphasis
43
43
  assert_equal([
44
44
  Facwparser::Element::Text.new('1', '1'),
45
- Facwparser::Element::Italic.new('_hoge_', 'hoge'),
45
+ Facwparser::Element::Emphasis.new('_hoge_', 'hoge'),
46
46
  Facwparser::Element::Text.new('2', '2')
47
47
  ], Facwparser::Parser.parse_value('1_hoge_2', {}))
48
48
  end
@@ -69,6 +69,11 @@ class TestParseValue < Test::Unit::TestCase
69
69
  Facwparser::Element::Image.new('!http://www.unixuser.org/!', 'http://www.unixuser.org/'),
70
70
  Facwparser::Element::Text.new('2', '2')
71
71
  ], Facwparser::Parser.parse_value('1!http://www.unixuser.org/!2', {}))
72
+ assert_equal([
73
+ Facwparser::Element::Text.new('1', '1'),
74
+ Facwparser::Element::Image.new('!/hoge.png!', '/hoge.png'),
75
+ Facwparser::Element::Text.new('2', '2')
76
+ ], Facwparser::Parser.parse_value('1!/hoge.png!2', {}))
72
77
  end
73
78
 
74
79
  def test_parse_value_jira_macro
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: facwparser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-02-14 00:00:00.000000000 Z
12
+ date: 2013-02-15 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Fuxxing Atlassian Confluence Wiki Parser
15
15
  email:
@@ -31,6 +31,7 @@ files:
31
31
  - lib/facwparser/version.rb
32
32
  - sample/confluence2html.rb
33
33
  - tests/all_tests.rb
34
+ - tests/units/element/test_a.rb
34
35
  - tests/units/element/test_code.rb
35
36
  - tests/units/element/test_heading.rb
36
37
  - tests/units/element/test_horizontal_rule.rb
@@ -42,6 +43,7 @@ files:
42
43
  - tests/units/element/test_table.rb
43
44
  - tests/units/element/test_table_data.rb
44
45
  - tests/units/element/test_table_headers.rb
46
+ - tests/units/element/test_toc.rb
45
47
  - tests/units/parser/test_add_list_elements.rb
46
48
  - tests/units/parser/test_parse1.rb
47
49
  - tests/units/parser/test_parse_value.rb