facwparser 0.0.8 → 0.0.9
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/facwparser/parser.rb +10 -10
- data/lib/facwparser/version.rb +1 -1
- data/tests/units/parser/test_unescape_text.rb +27 -0
- metadata +4 -2
data/lib/facwparser/parser.rb
CHANGED
@@ -6,6 +6,9 @@ require File.dirname(__FILE__) + '/element'
|
|
6
6
|
|
7
7
|
module Facwparser
|
8
8
|
module Parser
|
9
|
+
|
10
|
+
SPECIAL_CHARACTERS_REGEX = %w( [ ] \\ * + _ ? { } ! ^ ~ - ).map{|e| Regexp.escape(e)}.join
|
11
|
+
|
9
12
|
def self.parse(content, options = {})
|
10
13
|
elements = parse_block(content, options)
|
11
14
|
process_elements(elements, options)
|
@@ -19,9 +22,9 @@ module Facwparser
|
|
19
22
|
end
|
20
23
|
|
21
24
|
def self.add_headings_to_toc(elements, options)
|
22
|
-
tocs = elements.select{ |e| e.
|
25
|
+
tocs = elements.select{ |e| e.is_a?(Element::TocMacro)}
|
23
26
|
if !tocs.empty?
|
24
|
-
headings = elements.select{ |e| e.
|
27
|
+
headings = elements.select{ |e| e.is_a?(Element::Heading) && e.level == 1}
|
25
28
|
id = 0
|
26
29
|
headings.each { |h|
|
27
30
|
h.id = 'heading_' + id.to_s
|
@@ -37,7 +40,7 @@ module Facwparser
|
|
37
40
|
list_stack = []
|
38
41
|
elements.each { |e|
|
39
42
|
case
|
40
|
-
when e.
|
43
|
+
when e.is_a?(Element::ListItem)
|
41
44
|
while list_stack.size > e.level
|
42
45
|
list_stack.pop
|
43
46
|
end
|
@@ -131,16 +134,13 @@ module Facwparser
|
|
131
134
|
end
|
132
135
|
|
133
136
|
def self.unescape_text(text)
|
134
|
-
text.gsub(/\\([
|
137
|
+
text.gsub(/\\([#{SPECIAL_CHARACTERS_REGEX}])/o) {
|
135
138
|
$1
|
136
139
|
}
|
137
140
|
end
|
138
141
|
|
139
142
|
def self.parse_value(value, options)
|
140
143
|
|
141
|
-
#jira
|
142
|
-
#img
|
143
|
-
|
144
144
|
children = []
|
145
145
|
|
146
146
|
value.split("\n").each { |l|
|
@@ -175,11 +175,11 @@ module Facwparser
|
|
175
175
|
children << Element::ColorMacroStart.new(s[0], unescape_text(s[1]))
|
176
176
|
when s.scan(/\{color\}/)
|
177
177
|
children << Element::ColorMacroEnd.new(s[0])
|
178
|
-
when s.scan(/[^\[\a^\\*+_?{}!-]+/)
|
179
|
-
children << Element::Text.new(s[0], unescape_text(s[0]))
|
180
178
|
when s.scan(/\\\\/)
|
181
179
|
children << Element::Br.new(s[0])
|
182
|
-
when s.scan(/\\[
|
180
|
+
when s.scan(/\\([#{SPECIAL_CHARACTERS_REGEX}])/o)
|
181
|
+
children << Element::Text.new(s[0], s[1])
|
182
|
+
when s.scan(/[^#{SPECIAL_CHARACTERS_REGEX}]+/o)
|
183
183
|
children << Element::Text.new(s[0], unescape_text(s[0]))
|
184
184
|
else
|
185
185
|
children << Element::Text.new(s.rest, unescape_text(s.rest))
|
data/lib/facwparser/version.rb
CHANGED
@@ -0,0 +1,27 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require 'test/unit'
|
3
|
+
require File.dirname(__FILE__) + '/../../../lib/facwparser/parser'
|
4
|
+
|
5
|
+
|
6
|
+
class TestUnescapeText < Test::Unit::TestCase
|
7
|
+
|
8
|
+
def test_unescape_text
|
9
|
+
|
10
|
+
%w( [ ] \\ * + _ ? { } ! ^ ~ - ).each { |e|
|
11
|
+
assert_equal(e, Facwparser::Parser.unescape_text('\\' + e))
|
12
|
+
}
|
13
|
+
|
14
|
+
assert_equal('hogehoge', Facwparser::Parser.unescape_text('hogehoge'))
|
15
|
+
assert_equal('hoge[]', Facwparser::Parser.unescape_text('hoge\[\]'))
|
16
|
+
assert_equal('hoge\\', Facwparser::Parser.unescape_text('hoge\\\\'))
|
17
|
+
assert_equal('hoge*', Facwparser::Parser.unescape_text('hoge\*'))
|
18
|
+
assert_equal('hoge+_?{}', Facwparser::Parser.unescape_text('hoge\+\_\?\{\}'))
|
19
|
+
assert_equal('hoge!^~-', Facwparser::Parser.unescape_text('hoge\!\^\~\-'))
|
20
|
+
|
21
|
+
assert_equal('hoge[]', Facwparser::Parser.unescape_text('hoge[]'))
|
22
|
+
assert_equal('hoge\\', Facwparser::Parser.unescape_text('hoge\\'))
|
23
|
+
assert_equal('hoge*', Facwparser::Parser.unescape_text('hoge*'))
|
24
|
+
assert_equal('hoge+_?{}', Facwparser::Parser.unescape_text('hoge+_?{}'))
|
25
|
+
assert_equal('hoge!^~-', Facwparser::Parser.unescape_text('hoge!^~-'))
|
26
|
+
end
|
27
|
+
end
|
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.
|
4
|
+
version: 0.0.9
|
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-
|
12
|
+
date: 2013-03-06 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: Fuxxing Atlassian Confluence Wiki Parser
|
15
15
|
email:
|
@@ -63,6 +63,7 @@ files:
|
|
63
63
|
- tests/units/parser/test_add_table_elements.rb
|
64
64
|
- tests/units/parser/test_parse_block.rb
|
65
65
|
- tests/units/parser/test_parse_value.rb
|
66
|
+
- tests/units/parser/test_unescape_text.rb
|
66
67
|
homepage: https://github.com/haruyama/facwparser
|
67
68
|
licenses: []
|
68
69
|
post_install_message:
|
@@ -88,3 +89,4 @@ signing_key:
|
|
88
89
|
specification_version: 3
|
89
90
|
summary: Parser of Atlassian Confluence Wiki Markup.
|
90
91
|
test_files: []
|
92
|
+
has_rdoc:
|