texta 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +1 -1
- data/lib/texta.rb +76 -4
- data/texta.gemspec +2 -2
- metadata +2 -2
data/Rakefile
CHANGED
data/lib/texta.rb
CHANGED
@@ -4,6 +4,8 @@ require "htmlentities"
|
|
4
4
|
require "rdiscount"
|
5
5
|
|
6
6
|
module Texta
|
7
|
+
class Error < ArgumentError; end
|
8
|
+
|
7
9
|
def self.init
|
8
10
|
String.send :include, self
|
9
11
|
end
|
@@ -50,23 +52,50 @@ module Texta
|
|
50
52
|
|
51
53
|
def process(node)
|
52
54
|
case node.name
|
53
|
-
|
55
|
+
# bold
|
56
|
+
when "b", "strong"
|
57
|
+
"**" + children(node).join(" ") + "**"
|
58
|
+
# italic
|
59
|
+
when "i", "em"
|
60
|
+
"_" + children(node).join(" ") + "_"
|
61
|
+
# plain
|
62
|
+
when "u"
|
54
63
|
children(node).join(" ")
|
64
|
+
when "p"
|
65
|
+
children(node).join(" ") + "\n"
|
66
|
+
when "br"
|
67
|
+
"\n"
|
68
|
+
|
69
|
+
# headlines
|
55
70
|
when /h([1-2])/
|
56
71
|
char = $1 == "1" ? "=" : "-"
|
57
72
|
s = children(node).join(" ")
|
58
73
|
"#{s}\n" + char * [ [s.length, 6].min, 32 ].max + "\n\n"
|
59
74
|
when /h([1-6])/
|
60
75
|
"#" * $1.to_i + " " + children(node).join(" ") + "\n\n"
|
76
|
+
|
77
|
+
# text nodes
|
61
78
|
when "text"
|
62
79
|
s = node.to_s.gsub(/(^\s+)|(\s+$)/, "")
|
63
80
|
s == "" ? nil : s
|
64
|
-
|
81
|
+
|
82
|
+
# tables
|
83
|
+
|
84
|
+
when "table"
|
85
|
+
children(node).join("")
|
86
|
+
when "tr"
|
65
87
|
children(node).join(" ") + "\n"
|
88
|
+
when "th"
|
89
|
+
"**" + children(node).join(" ") + "**"
|
90
|
+
when "td"
|
91
|
+
children(node).join(" ")
|
92
|
+
# lists
|
66
93
|
when "ul"
|
67
94
|
children(node).map { |s| "- #{s}\n" }.join + "\n"
|
95
|
+
when "li"
|
96
|
+
children(node).join(" ")
|
68
97
|
else
|
69
|
-
|
98
|
+
raise Error, "node <#{node.name}>"
|
70
99
|
end
|
71
100
|
end
|
72
101
|
end
|
@@ -147,7 +176,7 @@ HTML
|
|
147
176
|
end
|
148
177
|
|
149
178
|
def test_string_module
|
150
|
-
assert_equal "a chd d\n", "a <b>chd</b> d".texta(:text)
|
179
|
+
assert_equal "a **chd** d\n", "a <b>chd</b> d".texta(:text)
|
151
180
|
end
|
152
181
|
|
153
182
|
def test_invalid_option
|
@@ -192,4 +221,47 @@ HTML
|
|
192
221
|
def test_nil
|
193
222
|
assert_testa nil, nil, nil
|
194
223
|
end
|
224
|
+
|
225
|
+
def test_table
|
226
|
+
input = <<-HTML
|
227
|
+
<table><tr>
|
228
|
+
<th>Farben</th>
|
229
|
+
<td>halbtransparent, halbtransparent blau (Polycarbonat)</td>
|
230
|
+
</tr>
|
231
|
+
<tr>
|
232
|
+
<th>Material</th>
|
233
|
+
<td>Körper und Flügel aus Polycarbonat</td>
|
234
|
+
</tr>
|
235
|
+
<tr>
|
236
|
+
<th>Abmessungen</th>
|
237
|
+
<td>
|
238
|
+
<u>Höhe</u>: 44cm<br>
|
239
|
+
<u>Durchmesser</u>: 122cm<br>
|
240
|
+
</td>
|
241
|
+
</tr>
|
242
|
+
<tr><th>Sonstiges</th><td>Steuerung über Fernbedienung</td></tr>
|
243
|
+
</table>
|
244
|
+
HTML
|
245
|
+
|
246
|
+
text = <<-TEXT
|
247
|
+
**Farben** halbtransparent, halbtransparent blau (Polycarbonat)
|
248
|
+
**Material** Körper und Flügel aus Polycarbonat
|
249
|
+
**Abmessungen** Höhe : 44cm
|
250
|
+
Durchmesser : 122cm
|
251
|
+
|
252
|
+
**Sonstiges** Steuerung über Fernbedienung
|
253
|
+
TEXT
|
254
|
+
|
255
|
+
html = <<-HTML
|
256
|
+
<p><strong>Farben</strong> halbtransparent, halbtransparent blau (Polycarbonat)
|
257
|
+
<strong>Material</strong> Körper und Flügel aus Polycarbonat
|
258
|
+
<strong>Abmessungen</strong> Höhe : 44cm
|
259
|
+
Durchmesser : 122cm</p>
|
260
|
+
|
261
|
+
<p> <strong>Sonstiges</strong> Steuerung über Fernbedienung</p>
|
262
|
+
HTML
|
263
|
+
|
264
|
+
assert_testa input, text, html
|
265
|
+
end
|
266
|
+
|
195
267
|
end
|
data/texta.gemspec
CHANGED
@@ -2,11 +2,11 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = %q{texta}
|
5
|
-
s.version = "0.1.
|
5
|
+
s.version = "0.1.3"
|
6
6
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
8
|
s.authors = ["pboy"]
|
9
|
-
s.date = %q{2009-12-
|
9
|
+
s.date = %q{2009-12-26}
|
10
10
|
s.description = %q{texta: heuristic text formatting}
|
11
11
|
s.email = %q{eno-pboy@open-lab.org}
|
12
12
|
s.extra_rdoc_files = ["README.rdoc", "lib/texta.rb"]
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: texta
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- pboy
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-12-
|
12
|
+
date: 2009-12-26 00:00:00 +01:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|