red_cloth_formatters_plain 0.1

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/MIT-LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2009 Joseph HALTER
2
+
3
+ Permission is hereby granted, free of charge, to any person
4
+ obtaining a copy of this software and associated documentation
5
+ files (the "Software"), to deal in the Software without
6
+ restriction, including without limitation the rights to use,
7
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the
9
+ Software is furnished to do so, subject to the following
10
+ conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
data/README.textile ADDED
@@ -0,0 +1,31 @@
1
+ h2. RedCloth::Formatters::Plain
2
+
3
+ Similar to HTML and Latex formatters included in RedCloth, this one allows you to convert Textile to plain text discarding the Textile syntax.
4
+
5
+ h3. Dependencies
6
+
7
+ This plugin does of course require the RedCloth gem.
8
+
9
+ h3. Example
10
+
11
+ <code>
12
+ RedCloth.new("p. this is *simple* _test_").to_plain
13
+ </code>
14
+
15
+ will return:
16
+
17
+ <code>
18
+ "this is simple test"
19
+ </code>
20
+
21
+ h3. Example displaying URLs
22
+
23
+ <code>
24
+ RedCloth.new(%Q{"Please contact support":http://example.com/support}).to_plain
25
+ </code>
26
+
27
+ will return:
28
+
29
+ <code>
30
+ "Please contact support <http://example.com/support>"
31
+ </code>
data/Rakefile ADDED
@@ -0,0 +1,38 @@
1
+ # load rspec as GEM or plugin
2
+ rspec_gem_dir = nil
3
+ Dir["#{File.dirname(__FILE__)}/../../gems/*"].each do |subdir|
4
+ rspec_gem_dir = subdir if subdir.gsub("#{File.dirname(__FILE__)}/../../gems/","") =~ /^(\w+-)?rspec-(\d+)/ && File.exist?("#{subdir}/lib/spec/rake/spectask.rb")
5
+ end
6
+ rspec_plugin_dir = File.expand_path(File.dirname(__FILE__) + '/../rspec')
7
+ if rspec_gem_dir && (test ?d, rspec_plugin_dir)
8
+ raise "\n#{'*'*50}\nYou have rspec installed in both vendor/gems and vendor/plugins\nPlease pick one and dispose of the other.\n#{'*'*50}\n\n"
9
+ end
10
+ if rspec_gem_dir
11
+ $:.unshift("#{rspec_gem_dir}/lib")
12
+ elsif File.exist?(rspec_plugin_dir)
13
+ $:.unshift("#{rspec_plugin_dir}/lib")
14
+ end
15
+
16
+ # load rspec tasks
17
+ require "rake"
18
+ require "spec/rake/spectask"
19
+
20
+ desc "Default: run specs."
21
+ task :default => :spec
22
+
23
+ desc "Run the specs"
24
+ Spec::Rake::SpecTask.new(:spec) do |t|
25
+ t.spec_opts = ["--colour --format progress --loadby mtime --reverse"]
26
+ t.spec_files = FileList["spec/**/*_spec.rb"]
27
+ end
28
+
29
+ $LOAD_PATH.unshift File.expand_path("../lib/", __FILE__)
30
+ require "red_cloth_formatters_plain"
31
+
32
+ task :build do
33
+ system "gem build redcloth-formatters-plain.gemspec"
34
+ end
35
+
36
+ task :release => :build do
37
+ system "gem push red_cloth_formatters_plain-#{RedCloth::Formatters::Plain::VERSION}.gem"
38
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require "red_cloth_formatters_plain"
@@ -0,0 +1,238 @@
1
+ # encoding: UTF-8
2
+ require "cgi"
3
+ require "RedCloth"
4
+
5
+ module RedCloth
6
+ module Formatters
7
+ module Plain
8
+ VERSION = "0.1"
9
+
10
+ class Sanitizer
11
+ def self.strip_tags(text)
12
+ # use Rails Sanitizer if available
13
+ begin
14
+ text = ActionController::Base.helpers.strip_tags(text)
15
+ rescue
16
+ # otherwise, use custom method inspired from:
17
+ # RedCloth::Formatters::HTML.clean_html
18
+ # not very secure, but it's ok as output is still
19
+ # meant to be escaped if it needs to be shown
20
+ text.gsub!( /<!\[CDATA\[/, '' )
21
+ text.gsub!( /<(\/*)([A-Za-z]\w*)([^>]*?)(\s?\/?)>/ ){|m| block_given? ? yield(m) : ""}
22
+ end
23
+ CGI.unescapeHTML(text)
24
+ end
25
+ end
26
+
27
+ include RedCloth::Formatters::Base
28
+
29
+ [:h1, :h2, :h3, :h4, :h5, :h6, :p, :pre, :div].each do |m|
30
+ define_method(m) do |opts|
31
+ "#{opts[:text]}\n"
32
+ end
33
+ end
34
+
35
+ [:strong, :code, :em, :i, :b, :ins, :sup, :sub, :span, :cite].each do |m|
36
+ define_method(m) do |opts|
37
+ opts[:block] = true
38
+ "#{opts[:text]}"
39
+ end
40
+ end
41
+
42
+ def hr(opts)
43
+ "\n"
44
+ end
45
+
46
+ def acronym(opts)
47
+ opts[:block] = true
48
+ opts[:title] ? "#{opts[:text]}(#{opts[:title]})" : "#{opts[:text]}"
49
+ end
50
+
51
+ def caps(opts)
52
+ "#{opts[:text]}"
53
+ end
54
+
55
+ # don't render deleted text
56
+ def del(opts)
57
+ ""
58
+ end
59
+
60
+ # simple list with dashes
61
+ [:ol, :ul].each do |m|
62
+ define_method("#{m}_open") do |opts|
63
+ opts[:block] = true
64
+ opts[:nest] > 1 ? "\n" : ""
65
+ end
66
+ define_method("#{m}_close") do |opts|
67
+ ""
68
+ end
69
+ end
70
+ def li_open(opts)
71
+ @li_need_closing = true
72
+ "#{" " * (opts[:nest]-1)}- #{opts[:text]}"
73
+ end
74
+ def li_close(opts=nil)
75
+ # avoid multiple line breaks when closing multiple list items
76
+ output = @li_need_closing ? "\n" : ""
77
+ @li_need_closing = false
78
+ output
79
+ end
80
+ def dl_open(opts)
81
+ opts[:block] = true
82
+ ""
83
+ end
84
+ def dl_close(opts=nil)
85
+ ""
86
+ end
87
+ def dt(opts)
88
+ "#{opts[:text]}:\n"
89
+ end
90
+ def dd(opts)
91
+ " #{opts[:text]}\n"
92
+ end
93
+
94
+ # don't render tables
95
+ [:td, :tr_open, :tr_close, :table_open, :table_close].each do |m|
96
+ define_method(m) do |opts|
97
+ ""
98
+ end
99
+ end
100
+
101
+ # just add newlines before blockquotes
102
+ [:bc_open, :bq_open].each do |m|
103
+ define_method(m) do |opts|
104
+ opts[:block] = true
105
+ ""
106
+ end
107
+ end
108
+ def bc_close(opts)
109
+ "\n"
110
+ end
111
+ def bq_close(opts)
112
+ ""
113
+ end
114
+
115
+ # render link name followed by <url>
116
+ # uses !LINK_OPEN_TAG! and !LINK_CLOSE_TAG! as a way to identify
117
+ # the < > otherwise they will be stripped when all html tags are stripped
118
+ #
119
+ def link(opts)
120
+ "#{opts[:name]} !LINK_OPEN_TAG!#{opts[:href]}!LINK_CLOSE_TAG!"
121
+ end
122
+
123
+ # render image alternative text or title if not available
124
+ def image(opts)
125
+ "#{opts[:alt] || opts[:title]}"
126
+ end
127
+
128
+ # don't render footnotes
129
+ [:footno, :fn].each do |m|
130
+ define_method(m) do |opts|
131
+ ""
132
+ end
133
+ end
134
+
135
+ def snip(opts)
136
+ opts[:text] + "\n"
137
+ end
138
+
139
+ # render unescaped quotes and special chars
140
+ def quote1(opts)
141
+ "'#{opts[:text]}'"
142
+ end
143
+ def quote2(opts)
144
+ "\"#{opts[:text]}\""
145
+ end
146
+ def multi_paragraph_quote(opts)
147
+ "\"#{opts[:text]}"
148
+ end
149
+ def ellipsis(opts)
150
+ "#{opts[:text]}..."
151
+ end
152
+ {
153
+ :emdash => "-",
154
+ :endash => " - ",
155
+ :arrow => "->",
156
+ :trademark => "™",
157
+ :registered => "®",
158
+ :copyright => "©",
159
+ :amp => "&",
160
+ :gt => ">",
161
+ :lt => "<",
162
+ :br => "\n",
163
+ :quot => "\"",
164
+ :squot => "'",
165
+ :apos => "'",
166
+ }.each do |method, output|
167
+ define_method(method) do |opts|
168
+ output
169
+ end
170
+ end
171
+ def dim(opts)
172
+ opts[:text]
173
+ end
174
+ def entity(opts)
175
+ unescape("&#{opts[:text]};")
176
+ end
177
+
178
+ # strip HTML tags
179
+ def html(opts)
180
+ strip_tags(opts[:text]) + "\n"
181
+ end
182
+ def html_block(opts)
183
+ strip_tags(opts[:text])
184
+ end
185
+ def notextile(opts)
186
+ if filter_html
187
+ "#{opts[:text]}"
188
+ else
189
+ strip_tags(opts[:text])
190
+ end
191
+ end
192
+ def inline_html(opts)
193
+ if filter_html
194
+ "#{opts[:text]}"
195
+ else
196
+ strip_tags(opts[:text])
197
+ end
198
+ end
199
+
200
+ # unchanged
201
+ def ignored_line(opts)
202
+ opts[:text] + "\n"
203
+ end
204
+
205
+ private
206
+
207
+ def strip_tags(text)
208
+ Sanitizer.strip_tags(text)
209
+ end
210
+
211
+ def unescape(str)
212
+ CGI.unescapeHTML(str.to_s)
213
+ end
214
+
215
+ # no escaping
216
+ [:escape, :escape_pre, :escape_attribute].each do |m|
217
+ define_method(m) do |text|
218
+ text
219
+ end
220
+ end
221
+ def after_transform(text)
222
+ text.chomp!
223
+ end
224
+ def before_transform(text)
225
+ end
226
+ end
227
+ end
228
+ class TextileDoc
229
+ def to_plain(*rules)
230
+ apply_rules(rules)
231
+ output = to(Formatters::Plain)
232
+ output = Formatters::Plain::Sanitizer.strip_tags(output)
233
+ # replace special link hooks with < and >
234
+ # See #RedCloth::Formatters::Plain#link above
235
+ output.gsub("!LINK_OPEN_TAG!", "<").gsub("!LINK_CLOSE_TAG!", ">")
236
+ end
237
+ end
238
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: red_cloth_formatters_plain
3
+ version: !ruby/object:Gem::Version
4
+ hash: 9
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ version: "0.1"
10
+ platform: ruby
11
+ authors:
12
+ - Joseph Halter
13
+ - Jeff Zellman
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-10-07 00:00:00 -05:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rspec
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :development
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: RedCloth
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ type: :runtime
48
+ version_requirements: *id002
49
+ description: Allows Redcloth to output plain text
50
+ email:
51
+ - jzellman@gmail.com
52
+ executables: []
53
+
54
+ extensions: []
55
+
56
+ extra_rdoc_files: []
57
+
58
+ files:
59
+ - lib/red_cloth_formatters_plain.rb
60
+ - MIT-LICENSE
61
+ - README.textile
62
+ - Rakefile
63
+ - init.rb
64
+ has_rdoc: true
65
+ homepage:
66
+ licenses: []
67
+
68
+ post_install_message:
69
+ rdoc_options: []
70
+
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ hash: 3
79
+ segments:
80
+ - 0
81
+ version: "0"
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ hash: 23
88
+ segments:
89
+ - 1
90
+ - 3
91
+ - 6
92
+ version: 1.3.6
93
+ requirements: []
94
+
95
+ rubyforge_project:
96
+ rubygems_version: 1.3.7
97
+ signing_key:
98
+ specification_version: 3
99
+ summary: Redcloth Plain Text Formatter
100
+ test_files: []
101
+