ClothRed 0.2.0 → 0.3.0

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.
data/lib/clothred.rb CHANGED
@@ -7,9 +7,6 @@ TODO: enhance docs, as more methods come availlable
7
7
  Author:: Phillip "CynicalRyan" Gawlowski (mailto:cmdjackryan@gmail.com)
8
8
  Copyright:: Copyright (c) 2007 Phillip Gawlowski
9
9
  License:: BSD
10
-
11
-
12
-
13
10
  =end
14
11
 
15
12
 
@@ -26,31 +23,45 @@ class ClothRed < String
26
23
  ]
27
24
 
28
25
  HEADINGS = [
29
- ["<h1>","h1. "], ["</h1>", ""], ["<h2>","h2. "],["</h2>", ""],
30
- ["<h3>","h3. "], ["</h3>", ""], ["<h4>","h4. "],["</h4>", ""],
31
- ["<h5>","h5. "], ["</h5>", ""], ["<h6>","h6. "],["</h6>", ""]
26
+ ["<h1>","h1. "], ["</h1>", ""], ["<h2>","h2. "], ["</h2>", ""],
27
+ ["<h3>","h3. "], ["</h3>", ""], ["<h4>","h4. "], ["</h4>", ""],
28
+ ["<h5>","h5. "], ["</h5>", ""], ["<h6>","h6. "], ["</h6>", ""],
29
+ ["<h7>","h7. "], ["</h7>", ""]
32
30
  ]
33
31
 
34
32
  STRUCTURES = [
35
33
  ["<p>", ""],["</p>","\n\n"], ["<blockquote>", "bq. "], ["</blockquote>",""]
36
34
  ]
37
35
 
36
+ ENTITIES = [
37
+ ["&#8220;", '"'], ["&#8221;", '"'], ["&#8212;", "--"], ["&#8212;", "--"],
38
+ ["&#8211;","-"], ["&#8230;", "..."], ["&#215;", " x "], ["&#8482;","(tm)"],
39
+ ["&#174;","(R)"], ["&#169;","(C)"], ["&#8217;", "'"]
40
+ ]
41
+
42
+ TABLES = [
43
+ ["<table>",""], ["</table>",""], ["<tr>",""], ["</tr>","|\n"], ["<td>","|"],
44
+ ["</td>",""], ["<th>", "|_."], ["</th>", ""]
45
+ ]
46
+
38
47
  def initialize (html)
39
48
  super(html)
40
- @workingcopy = html.dup
49
+ @workingcopy = html
41
50
  end
42
51
  #++
43
52
  #Call all necessary methods to convert a string of HTML into Textile markup.
44
53
 
45
54
  def to_textile
46
-
47
- headings(@workingcopy)
48
- structure(@workingcopy)
49
- text_formatting(@workingcopy)
55
+
50
56
 
51
- @workingcopy
57
+ headings(@workingcopy)
58
+ structure(@workingcopy)
59
+ text_formatting(@workingcopy)
60
+ entities(@workingcopy)
61
+ tables(@workingcopy)
62
+ @workingcopy
52
63
 
53
- end
64
+ end
54
65
 
55
66
  #--
56
67
  #The conversion methods themselves are private.
@@ -72,6 +83,14 @@ end
72
83
  end
73
84
 
74
85
 
86
+ def entities(text)
87
+ ENTITIES.each do |htmlentity, textileentity|
88
+ text.gsub!(htmlentity, textileentity)
89
+ end
90
+ text
91
+ end
92
+
93
+
75
94
  def structure(text)
76
95
  STRUCTURES.each do |htmltag, textiletag|
77
96
  text.gsub!(htmltag, textiletag)
@@ -80,7 +99,9 @@ end
80
99
  end
81
100
 
82
101
  def tables(text)
83
- #TODO: Translate Tables
102
+ TABLES.each do |htmltag, textiletag|
103
+ text.gsub!(htmltag, textiletag)
104
+ end
84
105
  text
85
106
  end
86
107
 
@@ -0,0 +1,31 @@
1
+ # test_entities.rb
2
+ # 12. April 2007
3
+ #
4
+
5
+ $:.unshift File.join(File.dirname(__FILE__),'..','lib')
6
+
7
+ begin
8
+ require "rubygems"
9
+ require "clothred"
10
+ rescue LoadError
11
+ require "clothred"
12
+ end
13
+
14
+ require 'test/unit'
15
+
16
+ class TestClothRedEntities < Test::Unit::TestCase
17
+
18
+ ENTITIES_TEST = [
19
+ ["&#8220;", '"'], ["&#8221;", '"'], ["&#8212;", "--"], ["&#8212;", "--"],
20
+ ["&#8211;","-"], ["&#8230;", "..."], ["&#215;", " x "], ["&#8482;","(tm)"],
21
+ ["&#174;","(R)"], ["&#169;","(C)"], ["&#8217;", "'"]
22
+ ]
23
+
24
+ def test_entities
25
+ ENTITIES_TEST.each do |html, textile|
26
+ test_html = ClothRed.new(html)
27
+ result = test_html.to_textile
28
+ assert_equal(textile,result)
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,32 @@
1
+ # test_tables.rb
2
+ # 12. April 2007
3
+ #
4
+
5
+ $:.unshift File.join(File.dirname(__FILE__),'..','lib')
6
+
7
+ begin
8
+ require "rubygems"
9
+ require "clothred"
10
+ rescue LoadError
11
+ require "clothred"
12
+ end
13
+
14
+ require 'test/unit'
15
+
16
+ class TestClothRedTables < Test::Unit::TestCase
17
+
18
+ TABLES_TEST = [
19
+ ["<table><tr><td>name</td><td>age</td><td>sex</td></tr><tr><td>joan</td><td>24</td><td>f</td></tr></table>",
20
+ "|name|age|sex|\n|joan|24|f|\n"],
21
+ ["<table><tr><th>name</th><th>age</th><th>sex</th></tr><tr><td>joan</td><td>24</td><td>f</td></tr></table>",
22
+ "|_.name|_.age|_.sex|\n|joan|24|f|\n"]
23
+ ]
24
+
25
+ def test_entities
26
+ TABLES_TEST.each do |html, textile|
27
+ test_html = ClothRed.new(html)
28
+ result = test_html.to_textile
29
+ assert_equal(textile,result)
30
+ end
31
+ end
32
+ end
metadata CHANGED
@@ -3,7 +3,7 @@ rubygems_version: 0.9.1
3
3
  specification_version: 1
4
4
  name: ClothRed
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.2.0
6
+ version: 0.3.0
7
7
  date: 2007-04-12 00:00:00 +02:00
8
8
  summary: "RedCloth in reverse: Converting HTML into Textile markup"
9
9
  require_paths:
@@ -31,9 +31,11 @@ authors:
31
31
  files:
32
32
  - lib/clothred.rb
33
33
  - lib/README.rdoc
34
+ - test/test_entities.rb
34
35
  - test/test_formatting.rb
35
36
  - test/test_headings.rb
36
37
  - test/test_structure.rb
38
+ - test/test_tables.rb
37
39
  - doc/html
38
40
  - doc/html/classes
39
41
  - doc/html/created.rid