suung-undress 0.2.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,11 @@
1
+ require "rubygems"
2
+ require "test/unit"
3
+ require "contest"
4
+ Dir[File.expand_path(File.dirname(__FILE__) + "/../lib/**/*.rb")].each do |file|
5
+ require file
6
+ end
7
+
8
+ begin
9
+ require "redgreen"
10
+ rescue LoadError
11
+ end
@@ -0,0 +1,57 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/test_helper")
2
+
3
+ class HpricotExtensionsTest < Test::Unit::TestCase
4
+ GET_ELEM = lambda {|content| Hpricot(content).children[0]}
5
+
6
+ def setup_elements
7
+ @p_without_styles = GET_ELEM.call("<p>some text inside</p>")
8
+ @p_with_styles = GET_ELEM.call("<p style='font-weight:bold; color:#000; font-style: italic'>some text inside</p>")
9
+ @img_without_styles = GET_ELEM.call("<img src='some.jpg' />")
10
+ end
11
+
12
+ context "Hpricot::Styles extensions" do
13
+ setup do
14
+ setup_elements
15
+ end
16
+
17
+ test "delete an specific style from an element" do
18
+ @p_with_styles.del_style("font-weight")
19
+ assert_equal({"color" => "#000", "font-style" => "italic"}, @p_with_styles.styles.to_h)
20
+ end
21
+
22
+ test "get a hash of all styles from an element" do
23
+ assert_kind_of Hpricot::Styles, @p_with_styles.styles
24
+ end
25
+
26
+ test "add new style to an element trough Elem" do
27
+ @p_without_styles.set_style("color", "red")
28
+ assert_equal({"color" => "red"}, @p_without_styles.styles.to_h)
29
+ end
30
+
31
+ test "add new style to an element trough Styles" do
32
+ @p_without_styles.styles["color"] = "red"
33
+ assert_equal({"color" => "red"}, @p_without_styles.styles.to_h)
34
+ end
35
+ end
36
+
37
+ context "Hpricot::Elem extensions" do
38
+ setup do
39
+ setup_elements
40
+ end
41
+
42
+ test "change an element tag name preserving attributes" do
43
+ @p_with_styles.change_tag! "span"
44
+ assert_equal "<span style=\"font-weight:bold; color:#000; font-style: italic\">some text inside</span>", @p_with_styles.to_s
45
+ end
46
+
47
+ test "change an element tag name not preserving attributes" do
48
+ @p_with_styles.change_tag! "span", false
49
+ assert_equal "<span>some text inside</span>", @p_with_styles.to_s
50
+ end
51
+
52
+ test "ignore self closed element tag name" do
53
+ @img_without_styles.change_tag! "hr"
54
+ assert_equal "<img src=\"some.jpg\" />", @img_without_styles.to_s
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,313 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/test_helper")
2
+
3
+ module Undress
4
+ class TextileTest < Test::Unit::TestCase
5
+ def assert_renders_textile(textile, html)
6
+ assert_equal textile, Undress(html).to_textile
7
+ end
8
+
9
+ context "Converting HTML to textile" do
10
+ context "some troubles with empty tags" do
11
+ test "with pre" do
12
+ html = "<pre></pre>"
13
+ textile = "<pre></pre>"
14
+ assert_renders_textile textile, html
15
+ end
16
+
17
+ test "with p" do
18
+ html = "<p></p>"
19
+ textile = ""
20
+ assert_renders_textile textile, html
21
+ end
22
+ end
23
+
24
+ test "converts nested tags" do
25
+ assert_renders_textile "h2. _this is *very* important_\n", "<h2><em>this is <strong>very</strong> important</em></h2>"
26
+ end
27
+
28
+ context "some troubles" do
29
+ test "with sup" do
30
+ html = "<p>e = mc<sup>2</sup></p>"
31
+ textile = "e = mc[^2^]\n"
32
+ assert_renders_textile textile, html
33
+ end
34
+ end
35
+
36
+ context "convert enetities" do
37
+ test "&nbsp;" do
38
+ textile = "some word\n"
39
+ html = "<p>some&nbsp;word</p>"
40
+ assert_renders_textile textile, html
41
+ end
42
+ end
43
+
44
+ context "convert parts of a word" do
45
+ test "some" do
46
+ textile = "s[*o*]me\n"
47
+ html = "<p>s<span style='font-weight:bold;'>o</span>me</p>"
48
+ assert_renders_textile textile, html
49
+ end
50
+
51
+ test "at the end of the word" do
52
+ html = "<p>ds <u>underline</u>sds</p>"
53
+ textile = "ds [+underline+]sds\n"
54
+ assert_renders_textile textile, html
55
+ end
56
+
57
+ test "wihout a letter in headers" do
58
+ html = "<h1>a<em>bc</em></h1>"
59
+ textile = "h1. a[_bc_]\n"
60
+ assert_renders_textile textile, html
61
+ end
62
+
63
+ test "without a letter after the tag" do
64
+ textile = "x[*x xx*]"
65
+ html = "x<strong>x xx</strong>"
66
+ assert_renders_textile textile, html
67
+ end
68
+
69
+ test "italics" do
70
+ textile = "a perfect wo[_r_]ld\n"
71
+ html = "<p>a perfect wo<em>r</em>ld</p>"
72
+ assert_renders_textile textile, html
73
+ end
74
+
75
+ test "bolds" do
76
+ textile = "a perfect wo[*r*]ld\n"
77
+ html = "<p>a perfect wo<strong>r</strong>ld</p>"
78
+ assert_renders_textile textile, html
79
+ end
80
+
81
+ test "underlines" do
82
+ textile = "a perfect wo[+r+]ld\n"
83
+ html = "<p>a perfect wo<ins>r</ins>ld</p>"
84
+ assert_renders_textile textile, html
85
+ end
86
+
87
+ test "line through" do
88
+ textile = "a perfect wo[-r-]ld\n"
89
+ html = "<p>a perfect wo<del>r</del>ld</p>"
90
+ assert_renders_textile textile, html
91
+ end
92
+ end
93
+
94
+ context "inline elements" do
95
+ test "converts <strong> tags" do
96
+ assert_renders_textile "*foo bar*", "<strong>foo bar</strong>"
97
+ end
98
+
99
+ test "converts <em> tags" do
100
+ assert_renders_textile "_foo bar_", "<em>foo bar</em>"
101
+ end
102
+
103
+ test "converts <code> tags" do
104
+ assert_renders_textile "@foo bar@", "<code>foo bar</code>"
105
+ end
106
+
107
+ test "converts <cite> tags" do
108
+ assert_renders_textile "??foo bar??", "<cite>foo bar</cite>"
109
+ end
110
+
111
+ test "converts <sup> tags" do
112
+ assert_renders_textile "foo ^sup^ bar", "foo <sup>sup</sup> bar"
113
+ assert_renders_textile "foo[^sup^]bar", "foo<sup>sup</sup>bar"
114
+ end
115
+
116
+ test "converts <sub> tags" do
117
+ assert_renders_textile "foo ~sub~ bar", "foo <sub>sub</sub> bar"
118
+ assert_renders_textile "foo[~sub~]bar", "foo<sub>sub</sub>bar"
119
+ end
120
+
121
+ test "converts <ins> tags" do
122
+ assert_renders_textile "+foo bar+", "<ins>foo bar</ins>"
123
+ end
124
+
125
+ test "converts <del> tags" do
126
+ assert_renders_textile "-foo bar-", "<del>foo bar</del>"
127
+ end
128
+
129
+ test "converts <acronym> tags" do
130
+ assert_renders_textile "EPA(Environmental Protection Agency)", "<acronym title='Environmental Protection Agency'>EPA</acronym>"
131
+ assert_renders_textile "EPA", "<acronym>EPA</acronym>"
132
+ end
133
+ end
134
+
135
+ context "links" do
136
+ test "converts simple links (without title)" do
137
+ assert_renders_textile "[Foo Bar:/cuack]", "<a href='/cuack'>Foo Bar</a>"
138
+ end
139
+
140
+ test "converts links with titles" do
141
+ assert_renders_textile "[Foo Bar (You should see this):/cuack]", "<a href='/cuack' title='You should see this'>Foo Bar</a>"
142
+ end
143
+ end
144
+
145
+ context "images" do
146
+ test "converts images without alt attributes" do
147
+ assert_renders_textile "!http://example.com/image.png!", "<img src='http://example.com/image.png'/>"
148
+ end
149
+
150
+ test "converts images with alt attributes" do
151
+ assert_renders_textile "!http://example.com/image.png(Awesome Pic)!", "<img src='http://example.com/image.png' alt='Awesome Pic'/>"
152
+ end
153
+ end
154
+
155
+ context "text formatting" do
156
+ test "converts paragraphs" do
157
+ assert_renders_textile "foo\n\nbar\n", "<p>foo</p><p>bar</p>"
158
+ end
159
+
160
+ test "converts <pre> tags which only contain a <code> child" do
161
+ assert_renders_textile "pc. var foo = 1;\n", "<pre><code>var foo = 1;</code></pre>"
162
+ assert_renders_textile "pc. var foo = 1;\n", "<pre> <code>var foo = 1;</code> </pre>"
163
+ end
164
+
165
+ test "leaves <pre> tags which contain mixed content as HTML" do
166
+ assert_renders_textile "<pre> foo bar</pre>", "<pre> foo bar</pre>"
167
+ end
168
+
169
+ test "converts <br> into a new line" do
170
+ assert_renders_textile "Foo\nBar", "Foo<br/>Bar"
171
+ end
172
+
173
+ test "converts blockquotes" do
174
+ assert_renders_textile "bq. foo bar\n", "<blockquote><div>foo bar</div></blockquote>"
175
+ end
176
+
177
+ test "a p inside a blockquote" do
178
+ html = "<blockquote> <p>this text<br />becomes not blockquoted in round trip.</p> </blockquote>"
179
+ greencloth = "bq. this text\nbecomes not blockquoted in round trip.\n"
180
+ assert_renders_textile greencloth, html
181
+ end
182
+
183
+ end
184
+
185
+ context "headers" do
186
+ test "converts <h1> tags" do
187
+ assert_renders_textile "h1. foo bar\n", "<h1>foo bar</h1>"
188
+ end
189
+
190
+ test "converts <h2> tags" do
191
+ assert_renders_textile "h2. foo bar\n", "<h2>foo bar</h2>"
192
+ end
193
+
194
+ test "converts <h3> tags" do
195
+ assert_renders_textile "h3. foo bar\n", "<h3>foo bar</h3>"
196
+ end
197
+
198
+ test "converts <h4> tags" do
199
+ assert_renders_textile "h4. foo bar\n", "<h4>foo bar</h4>"
200
+ end
201
+
202
+ test "converts <h5> tags" do
203
+ assert_renders_textile "h5. foo bar\n", "<h5>foo bar</h5>"
204
+ end
205
+
206
+ test "converts <h6> tags" do
207
+ assert_renders_textile "h6. foo bar\n", "<h6>foo bar</h6>"
208
+ end
209
+ end
210
+
211
+ context "lists" do
212
+ test "converts bullet lists" do
213
+ assert_renders_textile "* foo\n* bar\n", "<ul><li>foo</li><li>bar</li></ul>"
214
+ end
215
+
216
+ test "converts numbered lists" do
217
+ assert_renders_textile "# foo\n# bar\n", "<ol><li>foo</li><li>bar</li></ol>"
218
+ end
219
+
220
+ test "converts nested bullet lists" do
221
+ assert_renders_textile "* foo\n** bar\n* baz\n", "<ul><li>foo<ul><li>bar</li></ul></li><li>baz</li></ul>"
222
+ end
223
+
224
+ test "converts nested numbered lists" do
225
+ assert_renders_textile "# foo\n## bar\n# baz\n", "<ol><li>foo<ol><li>bar</li></ol></li><li>baz</li></ol>"
226
+ end
227
+
228
+ test "converts nested mixed lists" do
229
+ assert_renders_textile "* foo\n## bar\n## baz\n*** quux\n* cuack\n",
230
+ "<ul><li>foo<ol><li>bar</li><li>baz<ul><li>quux</li></ul></li></ol></li><li>cuack</li></ul>"
231
+ end
232
+
233
+ test "converts a definition list" do
234
+ assert_renders_textile "- foo := defining foo =:\n- bar := defining bar =:\n",
235
+ "<dl><dt>foo</dt><dd>defining foo</dd><dt>bar</dt><dd>defining bar</dd></dl>"
236
+ end
237
+ end
238
+
239
+ context "tables" do
240
+ test "converts table with empty cell" do
241
+ html = "<table> <tbody> <tr> <td>&nbsp;a</td> <td> </td> </tr> <tr> <td>&nbsp;b</td> <td>&nbsp;c</td> </tr> </tbody> </table>"
242
+ textile = "|a||\n|b|c|\n"
243
+ assert_renders_textile textile, html
244
+ end
245
+
246
+ test "converts a simple table" do
247
+ assert_renders_textile "|foo|bar|baz|\n|1|2|3|\n",
248
+ "<table><tr><td>foo</td><td>bar</td><td>baz</td></tr><tr><td>1</td><td>2</td><td>3</td></tr></table>"
249
+ end
250
+
251
+ test "converts a table with headers" do
252
+ assert_renders_textile "|_. foo|_. bar|_. baz|\n|1|2|3|\n",
253
+ "<table><tr><th>foo</th><th>bar</th><th>baz</th></tr><tr><td>1</td><td>2</td><td>3</td></tr></table>"
254
+ end
255
+
256
+ test "converts a table with cells that span multiple columns" do
257
+ assert_renders_textile "|foo|bar|baz|\n|\\2. 1|2|\n",
258
+ "<table><tr><td>foo</td><td>bar</td><td>baz</td></tr><tr><td colspan='2'>1</td><td>2</td></tr></table>"
259
+ end
260
+
261
+ test "converts a table with cells that span multiple rows" do
262
+ assert_renders_textile "|/2. foo|bar|baz|\n|1|2|\n",
263
+ "<table><tr><td rowspan='2'>foo</td><td>bar</td><td>baz</td></tr><tr><td>1</td><td>2</td></tr></table>"
264
+ end
265
+ end
266
+
267
+ context "applying post processing rules" do
268
+ test "compresses newlines to a maximum of two consecutive newlines" do
269
+ assert_renders_textile "Foo\n\nBar\n\nBaz\n\n* Quux 1\n* Quux 2\n", "<p>Foo</p><p>Bar</p><p>Baz</p><ul><li>Quux 1</li><li>Quux 2</li></p>"
270
+ end
271
+
272
+ test "strips trailing newlines from the start and end of the output string" do
273
+ assert_renders_textile "Foo\n", "<p>Foo</p>"
274
+ end
275
+
276
+ test "converts all fancy characters introduced by textile back into their 'source code'" do
277
+ assert_renders_textile "What the ... hell?", "What the &#8230; hell?"
278
+ assert_renders_textile "It's mine", "It&#8217;s mine"
279
+ assert_renders_textile "\"Fancy quoting\"", "&#8220;Fancy quoting&#8221;"
280
+ assert_renders_textile "How dashing--right?", "How dashing&#8212;right?"
281
+ assert_renders_textile "How dashing - right?", "How dashing &#8211; right?"
282
+ assert_renders_textile "2 x 2 = 4", "2 &#215; 2 = 4"
283
+ assert_renders_textile "2x2 = 4", "2&#215;2 = 4"
284
+ assert_renders_textile "Registered(r)", "Registered&#174;"
285
+ assert_renders_textile "Copyrighted(c)", "Copyrighted&#169;"
286
+ assert_renders_textile "Trademarked(tm)", "Trademarked&#8482;"
287
+ end
288
+ end
289
+
290
+ context "handling nodes with attributes" do
291
+ test "converts 'lang' to [_]" do
292
+ assert_renders_textile "*[es]hola*", "<strong lang='es'>hola</strong>"
293
+ end
294
+
295
+ test "converts 'class' to (_)" do
296
+ assert_renders_textile "*(foo)hola*", "<strong class='foo'>hola</strong>"
297
+ end
298
+
299
+ test "converts 'id' to (#_)" do
300
+ assert_renders_textile "*(#bar)hola*", "<strong id='bar'>hola</strong>"
301
+ end
302
+
303
+ test "converts both 'class' and 'id' to (_#_)" do
304
+ assert_renders_textile "*(foo#bar)hola*", "<strong id='bar' class='foo'>hola</strong>"
305
+ end
306
+
307
+ test "converts 'style' into {_}" do
308
+ assert_renders_textile "*{color:blue;}hola*", "<strong style='color:blue;'>hola</strong>"
309
+ end
310
+ end
311
+ end
312
+ end
313
+ end
@@ -0,0 +1,45 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "suung-undress"
3
+ s.version = "0.2.5"
4
+ s.date = "2010-05-22"
5
+
6
+ s.description = "Simply translate HTML to Textile, Markdown, or whatever other markup format you need"
7
+ s.summary = "Convert HTML into other markup languages"
8
+ s.homepage = "http://undress.rubyforge.org"
9
+
10
+ s.authors = "Nicolás Sanguinetti"
11
+ s.email = "contacto@nicolassanguinetti.info"
12
+
13
+ s.require_paths = ["lib"]
14
+ s.rubyforge_project = "undress"
15
+ s.has_rdoc = true
16
+ s.rubygems_version = "1.3.1"
17
+
18
+ s.add_dependency "hpricot"
19
+
20
+ if s.respond_to?(:add_development_dependency)
21
+ s.add_development_dependency "sr-mg"
22
+ s.add_development_dependency "contest"
23
+ s.add_development_dependency "redgreen"
24
+ end
25
+
26
+ s.files = %w[
27
+ .gitignore
28
+ LICENSE
29
+ CHANGELOG
30
+ README.rdoc
31
+ Rakefile
32
+ undress.gemspec
33
+ lib/undress.rb
34
+ lib/hpricot_ext.rb
35
+ lib/undress/grammar.rb
36
+ lib/undress/textile.rb
37
+ lib/undress/greencloth.rb
38
+ lib/core_ext/object.rb
39
+ test/test_helper.rb
40
+ test/test_grammar.rb
41
+ test/test_textile.rb
42
+ test/test_greencloth.rb
43
+ test/test_hpricot_ext.rb
44
+ ]
45
+ end
metadata ADDED
@@ -0,0 +1,125 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: suung-undress
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 2
8
+ - 5
9
+ version: 0.2.5
10
+ platform: ruby
11
+ authors:
12
+ - "Nicol\xC3\xA1s Sanguinetti"
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-05-22 00:00:00 +00:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: hpricot
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ version: "0"
30
+ type: :runtime
31
+ version_requirements: *id001
32
+ - !ruby/object:Gem::Dependency
33
+ name: sr-mg
34
+ prerelease: false
35
+ requirement: &id002 !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ segments:
40
+ - 0
41
+ version: "0"
42
+ type: :development
43
+ version_requirements: *id002
44
+ - !ruby/object:Gem::Dependency
45
+ name: contest
46
+ prerelease: false
47
+ requirement: &id003 !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ segments:
52
+ - 0
53
+ version: "0"
54
+ type: :development
55
+ version_requirements: *id003
56
+ - !ruby/object:Gem::Dependency
57
+ name: redgreen
58
+ prerelease: false
59
+ requirement: &id004 !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ segments:
64
+ - 0
65
+ version: "0"
66
+ type: :development
67
+ version_requirements: *id004
68
+ description: Simply translate HTML to Textile, Markdown, or whatever other markup format you need
69
+ email: contacto@nicolassanguinetti.info
70
+ executables: []
71
+
72
+ extensions: []
73
+
74
+ extra_rdoc_files: []
75
+
76
+ files:
77
+ - .gitignore
78
+ - LICENSE
79
+ - CHANGELOG
80
+ - README.rdoc
81
+ - Rakefile
82
+ - undress.gemspec
83
+ - lib/undress.rb
84
+ - lib/hpricot_ext.rb
85
+ - lib/undress/grammar.rb
86
+ - lib/undress/textile.rb
87
+ - lib/undress/greencloth.rb
88
+ - lib/core_ext/object.rb
89
+ - test/test_helper.rb
90
+ - test/test_grammar.rb
91
+ - test/test_textile.rb
92
+ - test/test_greencloth.rb
93
+ - test/test_hpricot_ext.rb
94
+ has_rdoc: true
95
+ homepage: http://undress.rubyforge.org
96
+ licenses: []
97
+
98
+ post_install_message:
99
+ rdoc_options: []
100
+
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ segments:
108
+ - 0
109
+ version: "0"
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ segments:
115
+ - 0
116
+ version: "0"
117
+ requirements: []
118
+
119
+ rubyforge_project: undress
120
+ rubygems_version: 1.3.6
121
+ signing_key:
122
+ specification_version: 3
123
+ summary: Convert HTML into other markup languages
124
+ test_files: []
125
+