texta 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Manifest +4 -0
- data/README.rdoc +3 -0
- data/Rakefile +18 -0
- data/lib/texta.rb +176 -0
- data/texta.gemspec +39 -0
- metadata +94 -0
data/Manifest
ADDED
data/README.rdoc
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'echoe'
|
4
|
+
|
5
|
+
Echoe.new('texta', '0.1.0') do |p|
|
6
|
+
p.description = "texta: heuristic text formatting"
|
7
|
+
p.url = "http://github.com/pboy/texta"
|
8
|
+
p.author = "pboy"
|
9
|
+
p.email = "eno-pboy@open-lab.org"
|
10
|
+
p.ignore_pattern = ["tmp/*", "script/*"]
|
11
|
+
p.runtime_dependencies = %w(nokogiri htmlentities rdiscount)
|
12
|
+
end
|
13
|
+
|
14
|
+
Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
|
15
|
+
|
16
|
+
task :default do
|
17
|
+
require "test/test.rb"
|
18
|
+
end
|
data/lib/texta.rb
ADDED
@@ -0,0 +1,176 @@
|
|
1
|
+
require "rubygems"
|
2
|
+
require "nokogiri"
|
3
|
+
require "htmlentities"
|
4
|
+
require "rdiscount"
|
5
|
+
|
6
|
+
module Texta
|
7
|
+
def self.init
|
8
|
+
String.send :include, self
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.is_html?(text)
|
12
|
+
text =~ /<(ul|p|br|li|b|i)\b(.*?)>/
|
13
|
+
end
|
14
|
+
|
15
|
+
def texta(mode = :text)
|
16
|
+
Texta.send mode, self
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.html(str)
|
20
|
+
str = ::HTMLEntities.new.encode text(str), :named
|
21
|
+
RDiscount.new(str).to_html
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.text(str)
|
25
|
+
return str unless is_html?(str)
|
26
|
+
T.new(str)
|
27
|
+
end
|
28
|
+
|
29
|
+
class T < String
|
30
|
+
def initialize(html)
|
31
|
+
body = Nokogiri.HTML(html).css("body").first
|
32
|
+
super children(body).join("\n")
|
33
|
+
end
|
34
|
+
|
35
|
+
def children(node)
|
36
|
+
nodes = node.children.map do |n|
|
37
|
+
process(n)
|
38
|
+
end.compact
|
39
|
+
end
|
40
|
+
|
41
|
+
def process(node)
|
42
|
+
case node.name
|
43
|
+
when *%w(i b li)
|
44
|
+
children(node).join(" ")
|
45
|
+
when /h([1-2])/
|
46
|
+
char = $1 == "1" ? "=" : "-"
|
47
|
+
s = children(node).join(" ")
|
48
|
+
"#{s}\n" + char * [ [s.length, 6].min, 32 ].max + "\n\n"
|
49
|
+
when /h([1-6])/
|
50
|
+
"#" * $1.to_i + " " + children(node).join(" ") + "\n\n"
|
51
|
+
when "text"
|
52
|
+
s = node.to_s.gsub(/(^\s+)|(\s+$)/, "")
|
53
|
+
s == "" ? nil : s
|
54
|
+
when "p"
|
55
|
+
children(node).join(" ") + "\n"
|
56
|
+
when "ul"
|
57
|
+
children(node).map { |s| "- #{s}\n" }.join + "\n"
|
58
|
+
else
|
59
|
+
invalid_argument!("node #{node.name}")
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
class String
|
66
|
+
include Texta
|
67
|
+
end
|
68
|
+
|
69
|
+
module Texta::Etest
|
70
|
+
def assert_equal_xml(x1, x2)
|
71
|
+
return if x1.gsub(/\s/, "") == x2.gsub(/\s/, "")
|
72
|
+
assert_equal(x1 ,x2)
|
73
|
+
end
|
74
|
+
|
75
|
+
def assert_testa(input, text, html)
|
76
|
+
assert_equal text || input, input.texta(:text)
|
77
|
+
assert_equal_xml html, input.texta(:html)
|
78
|
+
end
|
79
|
+
|
80
|
+
# Tests are "<input>, "<text>", "<output>""
|
81
|
+
def test_plain_text
|
82
|
+
input = "XYZ"
|
83
|
+
text = "XYZ"
|
84
|
+
html = "<p>XYZ</p>"
|
85
|
+
|
86
|
+
assert_testa input, text, html
|
87
|
+
end
|
88
|
+
|
89
|
+
def test_entities
|
90
|
+
input = "ÄÖÜäöü"
|
91
|
+
html = "html"
|
92
|
+
# input = "ÄÖÜäöü" #{ }"ÄÖÜäöü"
|
93
|
+
html = "<p>ÄÖÜäöü</p>"
|
94
|
+
|
95
|
+
assert_testa input, nil, html #{}"ÄÖÜäöü", html
|
96
|
+
end
|
97
|
+
|
98
|
+
def _test_list
|
99
|
+
input = <<TXT
|
100
|
+
Information
|
101
|
+
|
102
|
+
- Full-HD
|
103
|
+
- Auflösung: 1920 x 1080
|
104
|
+
- Format: 16:9
|
105
|
+
|
106
|
+
Die Ware wird originalverpackt und mit dem vom Hersteller beschriebenen Zubehör an Sie ausgeliefert.
|
107
|
+
TXT
|
108
|
+
|
109
|
+
text = <<TEXT
|
110
|
+
Information
|
111
|
+
|
112
|
+
- Full-HD
|
113
|
+
- Auflösung: 1920 x 1080
|
114
|
+
- Format: 16:9
|
115
|
+
|
116
|
+
Die Ware wird originalverpackt und mit dem vom Hersteller beschriebenen Zubehör an Sie ausgeliefert.
|
117
|
+
TEXT
|
118
|
+
|
119
|
+
html = <<HTML
|
120
|
+
<p>Information</p>
|
121
|
+
<ul>
|
122
|
+
<li>Full-HD</li>
|
123
|
+
<li>Auflösung: 1920 x 1080</li>
|
124
|
+
<li>Format: 16:9</li>
|
125
|
+
</ul>
|
126
|
+
<p>
|
127
|
+
Die Ware wird originalverpackt und mit dem vom Hersteller beschriebenen Zubehör an Sie ausgeliefert.
|
128
|
+
</p>
|
129
|
+
HTML
|
130
|
+
|
131
|
+
assert_testa input, text, html
|
132
|
+
end
|
133
|
+
|
134
|
+
def test_string_module
|
135
|
+
assert_equal "a chd d\n", "a <b>chd</b> d".texta(:text)
|
136
|
+
end
|
137
|
+
|
138
|
+
def test_invalid_option
|
139
|
+
assert_raise(NoMethodError) {
|
140
|
+
"ss".texta(:x)
|
141
|
+
}
|
142
|
+
end
|
143
|
+
|
144
|
+
def test_t1
|
145
|
+
input = <<HTML
|
146
|
+
<p>
|
147
|
+
Read this book!
|
148
|
+
</p>
|
149
|
+
<ul>
|
150
|
+
<li>All you ever wanted to know about html!</li>
|
151
|
+
<li>and even more</li>
|
152
|
+
<li>at your fingertips</li>
|
153
|
+
</ul>
|
154
|
+
HTML
|
155
|
+
|
156
|
+
text = <<TEXT
|
157
|
+
Read this book!
|
158
|
+
|
159
|
+
- All you ever wanted to know about html!
|
160
|
+
- and even more
|
161
|
+
- at your fingertips
|
162
|
+
|
163
|
+
TEXT
|
164
|
+
|
165
|
+
html = <<HTML
|
166
|
+
<p>Read this book!</p>
|
167
|
+
<ul>
|
168
|
+
<li>All you ever wanted to know about html!</li>
|
169
|
+
<li>and even more</li>
|
170
|
+
<li>at your fingertips</li>
|
171
|
+
</ul>
|
172
|
+
HTML
|
173
|
+
|
174
|
+
assert_testa input, text, html
|
175
|
+
end
|
176
|
+
end
|
data/texta.gemspec
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{texta}
|
5
|
+
s.version = "0.1.0"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["pboy"]
|
9
|
+
s.date = %q{2009-12-17}
|
10
|
+
s.description = %q{texta: heuristic text formatting}
|
11
|
+
s.email = %q{eno-pboy@open-lab.org}
|
12
|
+
s.extra_rdoc_files = ["README.rdoc", "lib/texta.rb"]
|
13
|
+
s.files = ["README.rdoc", "Rakefile", "lib/texta.rb", "Manifest", "texta.gemspec"]
|
14
|
+
s.homepage = %q{http://github.com/pboy/texta}
|
15
|
+
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Texta", "--main", "README.mdown"]
|
16
|
+
s.require_paths = ["lib"]
|
17
|
+
s.rubyforge_project = %q{texta}
|
18
|
+
s.rubygems_version = %q{1.3.5}
|
19
|
+
s.summary = %q{texta: heuristic text formatting}
|
20
|
+
|
21
|
+
if s.respond_to? :specification_version then
|
22
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
23
|
+
s.specification_version = 3
|
24
|
+
|
25
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
26
|
+
s.add_runtime_dependency(%q<nokogiri>, [">= 0"])
|
27
|
+
s.add_runtime_dependency(%q<htmlentities>, [">= 0"])
|
28
|
+
s.add_runtime_dependency(%q<rdiscount>, [">= 0"])
|
29
|
+
else
|
30
|
+
s.add_dependency(%q<nokogiri>, [">= 0"])
|
31
|
+
s.add_dependency(%q<htmlentities>, [">= 0"])
|
32
|
+
s.add_dependency(%q<rdiscount>, [">= 0"])
|
33
|
+
end
|
34
|
+
else
|
35
|
+
s.add_dependency(%q<nokogiri>, [">= 0"])
|
36
|
+
s.add_dependency(%q<htmlentities>, [">= 0"])
|
37
|
+
s.add_dependency(%q<rdiscount>, [">= 0"])
|
38
|
+
end
|
39
|
+
end
|
metadata
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: texta
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- pboy
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-12-17 00:00:00 +01:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: nokogiri
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: htmlentities
|
27
|
+
type: :runtime
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: "0"
|
34
|
+
version:
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: rdiscount
|
37
|
+
type: :runtime
|
38
|
+
version_requirement:
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: "0"
|
44
|
+
version:
|
45
|
+
description: "texta: heuristic text formatting"
|
46
|
+
email: eno-pboy@open-lab.org
|
47
|
+
executables: []
|
48
|
+
|
49
|
+
extensions: []
|
50
|
+
|
51
|
+
extra_rdoc_files:
|
52
|
+
- README.rdoc
|
53
|
+
- lib/texta.rb
|
54
|
+
files:
|
55
|
+
- README.rdoc
|
56
|
+
- Rakefile
|
57
|
+
- lib/texta.rb
|
58
|
+
- Manifest
|
59
|
+
- texta.gemspec
|
60
|
+
has_rdoc: true
|
61
|
+
homepage: http://github.com/pboy/texta
|
62
|
+
licenses: []
|
63
|
+
|
64
|
+
post_install_message:
|
65
|
+
rdoc_options:
|
66
|
+
- --line-numbers
|
67
|
+
- --inline-source
|
68
|
+
- --title
|
69
|
+
- Texta
|
70
|
+
- --main
|
71
|
+
- README.mdown
|
72
|
+
require_paths:
|
73
|
+
- lib
|
74
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: "0"
|
79
|
+
version:
|
80
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: "1.2"
|
85
|
+
version:
|
86
|
+
requirements: []
|
87
|
+
|
88
|
+
rubyforge_project: texta
|
89
|
+
rubygems_version: 1.3.5
|
90
|
+
signing_key:
|
91
|
+
specification_version: 3
|
92
|
+
summary: "texta: heuristic text formatting"
|
93
|
+
test_files: []
|
94
|
+
|