friendly_format 0.5.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/CHANGES +11 -0
- data/LICENSE +201 -0
- data/NOTICE +10 -0
- data/README +45 -0
- data/Rakefile +58 -0
- data/TODO +6 -0
- data/friendly_format.gemspec +42 -0
- data/lib/friendly_format/set_common.rb +16 -0
- data/lib/friendly_format/set_strict.rb +12 -0
- data/lib/friendly_format/version.rb +3 -0
- data/lib/friendly_format.rb +171 -0
- data/tasks/ann.rake +80 -0
- data/tasks/bones.rake +20 -0
- data/tasks/gem.rake +192 -0
- data/tasks/git.rake +40 -0
- data/tasks/manifest.rake +48 -0
- data/tasks/notes.rake +27 -0
- data/tasks/post_load.rake +39 -0
- data/tasks/rdoc.rake +50 -0
- data/tasks/rubyforge.rake +55 -0
- data/tasks/setup.rb +279 -0
- data/tasks/spec.rake +54 -0
- data/tasks/svn.rake +47 -0
- data/tasks/test.rake +40 -0
- data/test/sample/complex_article.txt +111 -0
- data/test/sample/complex_article_result.txt +108 -0
- data/test/test_friendly_format.rb +168 -0
- metadata +119 -0
data/CHANGES
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,201 @@
|
|
1
|
+
Apache License
|
2
|
+
Version 2.0, January 2004
|
3
|
+
http://www.apache.org/licenses/
|
4
|
+
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
6
|
+
|
7
|
+
1. Definitions.
|
8
|
+
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
11
|
+
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
13
|
+
the copyright owner that is granting the License.
|
14
|
+
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
16
|
+
other entities that control, are controlled by, or are under common
|
17
|
+
control with that entity. For the purposes of this definition,
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
19
|
+
direction or management of such entity, whether by contract or
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
22
|
+
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
24
|
+
exercising permissions granted by this License.
|
25
|
+
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
27
|
+
including but not limited to software source code, documentation
|
28
|
+
source, and configuration files.
|
29
|
+
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
31
|
+
transformation or translation of a Source form, including but
|
32
|
+
not limited to compiled object code, generated documentation,
|
33
|
+
and conversions to other media types.
|
34
|
+
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
36
|
+
Object form, made available under the License, as indicated by a
|
37
|
+
copyright notice that is included in or attached to the work
|
38
|
+
(an example is provided in the Appendix below).
|
39
|
+
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
46
|
+
the Work and Derivative Works thereof.
|
47
|
+
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
49
|
+
the original version of the Work and any modifications or additions
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
61
|
+
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
64
|
+
subsequently incorporated within the Work.
|
65
|
+
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
72
|
+
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
78
|
+
where such license applies only to those patent claims licensable
|
79
|
+
by such Contributor that are necessarily infringed by their
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
82
|
+
institute patent litigation against any entity (including a
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
85
|
+
or contributory patent infringement, then any patent licenses
|
86
|
+
granted to You under this License for that Work shall terminate
|
87
|
+
as of the date such litigation is filed.
|
88
|
+
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
91
|
+
modifications, and in Source or Object form, provided that You
|
92
|
+
meet the following conditions:
|
93
|
+
|
94
|
+
(a) You must give any other recipients of the Work or
|
95
|
+
Derivative Works a copy of this License; and
|
96
|
+
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
98
|
+
stating that You changed the files; and
|
99
|
+
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
102
|
+
attribution notices from the Source form of the Work,
|
103
|
+
excluding those notices that do not pertain to any part of
|
104
|
+
the Derivative Works; and
|
105
|
+
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
108
|
+
include a readable copy of the attribution notices contained
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
111
|
+
of the following places: within a NOTICE text file distributed
|
112
|
+
as part of the Derivative Works; within the Source form or
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
114
|
+
within a display generated by the Derivative Works, if and
|
115
|
+
wherever such third-party notices normally appear. The contents
|
116
|
+
of the NOTICE file are for informational purposes only and
|
117
|
+
do not modify the License. You may add Your own attribution
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
120
|
+
that such additional attribution notices cannot be construed
|
121
|
+
as modifying the License.
|
122
|
+
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
124
|
+
may provide additional or different license terms and conditions
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
128
|
+
the conditions stated in this License.
|
129
|
+
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
133
|
+
this License, without any additional terms or conditions.
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
135
|
+
the terms of any separate license agreement you may have executed
|
136
|
+
with Licensor regarding such Contributions.
|
137
|
+
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
140
|
+
except as required for reasonable and customary use in describing the
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
142
|
+
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
152
|
+
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
158
|
+
incidental, or consequential damages of any character arising as a
|
159
|
+
result of this License or out of the use or inability to use the
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
162
|
+
other commercial damages or losses), even if such Contributor
|
163
|
+
has been advised of the possibility of such damages.
|
164
|
+
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
168
|
+
or other liability obligations and/or rights consistent with this
|
169
|
+
License. However, in accepting such obligations, You may act only
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
174
|
+
of your accepting any such warranty or additional liability.
|
175
|
+
|
176
|
+
END OF TERMS AND CONDITIONS
|
177
|
+
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
179
|
+
|
180
|
+
To apply the Apache License to your work, attach the following
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
182
|
+
replaced with your own identifying information. (Don't include
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
184
|
+
comment syntax for the file format. We also recommend that a
|
185
|
+
file or class name and description of purpose be included on the
|
186
|
+
same "printed page" as the copyright notice for easier
|
187
|
+
identification within third-party archives.
|
188
|
+
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
190
|
+
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
192
|
+
you may not use this file except in compliance with the License.
|
193
|
+
You may obtain a copy of the License at
|
194
|
+
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
196
|
+
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
200
|
+
See the License for the specific language governing permissions and
|
201
|
+
limitations under the License.
|
data/NOTICE
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
= notice for other stuffs
|
2
|
+
|
3
|
+
* this project skeleton is built through Mr Bones 2.1.0:
|
4
|
+
gem install bones
|
5
|
+
http://codeforpeople.rubyforge.org/bones/
|
6
|
+
|
7
|
+
* test cases were written in mini-test.
|
8
|
+
|
9
|
+
* there's a regular expression was extracted from drupal,
|
10
|
+
and translated from php.
|
data/README
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
= friendly_format 0.5
|
2
|
+
by Lin Jen-Shin (a.k.a. godfat-真常[http://godfat.org])
|
3
|
+
godfat (XD) godfat.org
|
4
|
+
|
5
|
+
== LINKS:
|
6
|
+
|
7
|
+
* github-project[http://github.com/godfat/friendly_format]
|
8
|
+
* rubyforge-project[http://rubyforge.org/projects/ludy]
|
9
|
+
|
10
|
+
== DESCRIPTION:
|
11
|
+
|
12
|
+
make user input be valid xhtml and format it with gsub("\n", "<br/>") etc.
|
13
|
+
you can partially allow some tags and don't escape them.
|
14
|
+
|
15
|
+
== SYNOPSIS:
|
16
|
+
|
17
|
+
FriendlyFormat.format_article(text, :a, :img)
|
18
|
+
FriendlyFormat.format_article(text, FriendlyFormat::SetStrict.new)
|
19
|
+
FriendlyFormat.format_article(text, FriendlyFormat::SetCommon.new)
|
20
|
+
|
21
|
+
== REQUIREMENTS:
|
22
|
+
|
23
|
+
* hpricot 0.6 and later
|
24
|
+
|
25
|
+
== INSTALL:
|
26
|
+
|
27
|
+
* sudo gem install friendly_format
|
28
|
+
|
29
|
+
== LICENSE:
|
30
|
+
|
31
|
+
Apache License 2.0
|
32
|
+
|
33
|
+
Copyright (c) 2008, Lin Jen-Shin (a.k.a. godfat 真常)
|
34
|
+
|
35
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
36
|
+
you may not use this file except in compliance with the License.
|
37
|
+
You may obtain a copy of the License at
|
38
|
+
|
39
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
40
|
+
|
41
|
+
Unless required by applicable law or agreed to in writing, software
|
42
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
43
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
44
|
+
See the License for the specific language governing permissions and
|
45
|
+
limitations under the License.
|
data/Rakefile
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
begin
|
4
|
+
require 'bones'
|
5
|
+
Bones.setup
|
6
|
+
rescue LoadError
|
7
|
+
load 'tasks/setup.rb' # this line should already be there
|
8
|
+
end
|
9
|
+
|
10
|
+
PROJ.name = 'friendly_format'
|
11
|
+
# supress warnings, there's too many warnings in dm-core
|
12
|
+
# PROJ.ruby_opts.delete '-w'
|
13
|
+
|
14
|
+
PROJ.gem.dependencies << ['hpricot', '>=0.6.0']
|
15
|
+
PROJ.gem.development_dependencies << ['minitest', '>=1.3.0']
|
16
|
+
# PROJ.gem.executables = ["bin/#{PROJ.name}"]
|
17
|
+
|
18
|
+
task :default do
|
19
|
+
Rake.application.options.show_task_pattern = /./
|
20
|
+
Rake.application.display_tasks_and_comments
|
21
|
+
end
|
22
|
+
|
23
|
+
namespace :gem do
|
24
|
+
desc "create #{PROJ.name}.gemspec"
|
25
|
+
task 'gemspec' do
|
26
|
+
puts "rake gem:debug > #{PROJ.name}.gemspec"
|
27
|
+
File.open("#{PROJ.name}.gemspec", 'w'){|spec| spec << `rake gem:debug`.sub(/.*/, '')}
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
PROJ.authors = 'Lin Jen-Shin (a.k.a. godfat 真常)'
|
32
|
+
PROJ.email = 'godfat (XD) godfat.org'
|
33
|
+
PROJ.url = "http://github.com/godfat/#{PROJ.name}"
|
34
|
+
PROJ.description = PROJ.summary = paragraphs_of('README', 'description').join("\n\n")
|
35
|
+
PROJ.changes = paragraphs_of('CHANGES', 0..1).join("\n\n")
|
36
|
+
PROJ.rubyforge.name = 'ludy'
|
37
|
+
PROJ.version = File.read("lib/#{PROJ.name}/version.rb").gsub(/.*VERSION = '(.*)'.*/m, '\1')
|
38
|
+
|
39
|
+
PROJ.manifest_file = 'Manifest'
|
40
|
+
PROJ.exclude += ['^Manifest$', '^tmp', 'tmp$', '^pkg',
|
41
|
+
'^\.gitignore$', '^ann-', '\.sqlite3$', '\.db$']
|
42
|
+
|
43
|
+
PROJ.rdoc.remote_dir = PROJ.name
|
44
|
+
|
45
|
+
PROJ.readme_file = 'README'
|
46
|
+
PROJ.rdoc.main = 'README'
|
47
|
+
PROJ.rdoc.exclude += ['Rakefile', '^tasks', '^test']
|
48
|
+
PROJ.rdoc.include << '\w+'
|
49
|
+
PROJ.rdoc.opts << '--diagram' if !WIN32 and `which dot` =~ %r/\/dot/
|
50
|
+
PROJ.rdoc.opts += ['--charset=utf-8', '--inline-source',
|
51
|
+
'--line-numbers', '--promiscuous']
|
52
|
+
|
53
|
+
PROJ.spec.opts << '--color'
|
54
|
+
|
55
|
+
PROJ.ann.file = "ann-#{PROJ.name}-#{PROJ.version}"
|
56
|
+
PROJ.ann.paragraphs.concat %w[LINKS SYNOPSIS REQUIREMENTS INSTALL LICENSE]
|
57
|
+
|
58
|
+
CLEAN.include Dir['**/*.rbc']
|
data/TODO
ADDED
@@ -0,0 +1,6 @@
|
|
1
|
+
= friendly_article todo list
|
2
|
+
* make format_article take an optional size argument that indicates
|
3
|
+
how long should the result be. it should take < as 1 character and
|
4
|
+
<a href="/">嗯</a> as 1 character too. never cut out tag and unicode.
|
5
|
+
|
6
|
+
* an executable to format text from stdin to stdout.
|
@@ -0,0 +1,42 @@
|
|
1
|
+
|
2
|
+
# -*- encoding: utf-8 -*-
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = %q{friendly_format}
|
6
|
+
s.version = "0.5.1"
|
7
|
+
|
8
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
9
|
+
s.authors = ["Lin Jen-Shin (a.k.a. godfat \347\234\237\345\270\270)"]
|
10
|
+
s.date = %q{2008-12-11}
|
11
|
+
s.description = %q{}
|
12
|
+
s.email = %q{godfat (XD) godfat.org}
|
13
|
+
s.extra_rdoc_files = ["CHANGES", "LICENSE", "NOTICE", "README", "TODO", "friendly_format.gemspec"]
|
14
|
+
s.files = ["CHANGES", "LICENSE", "NOTICE", "README", "Rakefile", "TODO", "friendly_format.gemspec", "lib/friendly_format.rb", "lib/friendly_format/set_common.rb", "lib/friendly_format/set_strict.rb", "lib/friendly_format/version.rb", "tasks/ann.rake", "tasks/bones.rake", "tasks/gem.rake", "tasks/git.rake", "tasks/manifest.rake", "tasks/notes.rake", "tasks/post_load.rake", "tasks/rdoc.rake", "tasks/rubyforge.rake", "tasks/setup.rb", "tasks/spec.rake", "tasks/svn.rake", "tasks/test.rake", "test/sample/complex_article.txt", "test/sample/complex_article_result.txt", "test/test_friendly_format.rb"]
|
15
|
+
s.has_rdoc = true
|
16
|
+
s.homepage = %q{http://github.com/godfat/friendly_format}
|
17
|
+
s.rdoc_options = ["--diagram", "--charset=utf-8", "--inline-source", "--line-numbers", "--promiscuous", "--main", "README"]
|
18
|
+
s.require_paths = ["lib"]
|
19
|
+
s.rubyforge_project = %q{ludy}
|
20
|
+
s.rubygems_version = %q{1.3.1}
|
21
|
+
s.summary = %q{}
|
22
|
+
s.test_files = ["test/test_friendly_format.rb"]
|
23
|
+
|
24
|
+
if s.respond_to? :specification_version then
|
25
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
26
|
+
s.specification_version = 2
|
27
|
+
|
28
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
29
|
+
s.add_runtime_dependency(%q<hpricot>, [">= 0.6.0"])
|
30
|
+
s.add_development_dependency(%q<bones>, [">= 2.1.0"])
|
31
|
+
s.add_development_dependency(%q<minitest>, [">= 1.3.0"])
|
32
|
+
else
|
33
|
+
s.add_dependency(%q<hpricot>, [">= 0.6.0"])
|
34
|
+
s.add_dependency(%q<bones>, [">= 2.1.0"])
|
35
|
+
s.add_dependency(%q<minitest>, [">= 1.3.0"])
|
36
|
+
end
|
37
|
+
else
|
38
|
+
s.add_dependency(%q<hpricot>, [">= 0.6.0"])
|
39
|
+
s.add_dependency(%q<bones>, [">= 2.1.0"])
|
40
|
+
s.add_dependency(%q<minitest>, [">= 1.3.0"])
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
|
2
|
+
module FriendlyFormat
|
3
|
+
# reference:
|
4
|
+
# http://www.w3.org/TR/1999/REC-html401-19991224/index/elements.html
|
5
|
+
# allow most tags to use, use it in weblog or somewhere only
|
6
|
+
# a few people have permission to post or edit.
|
7
|
+
class SetCommon < Set
|
8
|
+
def initialize
|
9
|
+
super([ :a, :area, :b, :big, :blockquote, :br,
|
10
|
+
:center, :cite, :code, :del, :div, :em,
|
11
|
+
:font, :h1, :h2, :h3, :h4, :h5, :h6, :hr,
|
12
|
+
:i, :img, :li, :map, :object, :ol, :p,
|
13
|
+
:pre, :small, :span, :strong, :u, :ul ])
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
|
2
|
+
module FriendlyFormat
|
3
|
+
# only allow a few tags that won't affect too many layouts.
|
4
|
+
# use this in forum or somewhere many people have permissions
|
5
|
+
# to post or edit articles.
|
6
|
+
class SetStrict < Set
|
7
|
+
def initialize
|
8
|
+
super([ :a, :b, :code, :del, :em, :font,
|
9
|
+
:i, :img, :li, :ol, :strong, :u, :ul ])
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,171 @@
|
|
1
|
+
|
2
|
+
require 'set'
|
3
|
+
require 'friendly_format/set_common'
|
4
|
+
require 'friendly_format/set_strict'
|
5
|
+
|
6
|
+
# 2008-05-09 godfat
|
7
|
+
module FriendlyFormat
|
8
|
+
module_function
|
9
|
+
# format entire article for you, passing allowed tags to it.
|
10
|
+
# you can use Set or Symbol to specify which tags would be allowed.
|
11
|
+
# default was no tags at all, all tags would be escaped.
|
12
|
+
# it uses Hpricot to parse input.
|
13
|
+
def format_article html, *args
|
14
|
+
FriendlyFormat.format_article_entrance(html,
|
15
|
+
args.inject(Set.new){ |allowed_tags, arg|
|
16
|
+
case arg
|
17
|
+
when Symbol; allowed_tags << arg
|
18
|
+
when Set; allowed_tags += arg
|
19
|
+
else; raise(TypeError.new("expected Symbol or Set, got #{arg.class}"))
|
20
|
+
end
|
21
|
+
allowed_tags
|
22
|
+
})
|
23
|
+
end
|
24
|
+
|
25
|
+
# automaticly add "a href" tag on text starts from
|
26
|
+
# http/ftp/mailto/etc protocol. use Hpricot to parse and
|
27
|
+
# regexp translated from drupal to find where's the target.
|
28
|
+
# it uses simplified regexp to do the task. see format_url.
|
29
|
+
def format_autolink html, attrs = {}
|
30
|
+
require 'hpricot'
|
31
|
+
|
32
|
+
doc = Hpricot.parse html
|
33
|
+
doc.each_child{ |c|
|
34
|
+
next unless c.kind_of?(Hpricot::Text)
|
35
|
+
c.content = format_url c.content, attrs
|
36
|
+
}
|
37
|
+
doc.to_html
|
38
|
+
end
|
39
|
+
|
40
|
+
# translated from drupal-6.2/modules/filter/filter.module
|
41
|
+
# same as format_autolink, but doesn't use Hpricot,
|
42
|
+
# use only regexp.
|
43
|
+
def format_autolink_regexp text, attrs = {}
|
44
|
+
attrs = attrs.map{ |k,v| " #{k}=\"#{v}\""}.join
|
45
|
+
# Match absolute URLs.
|
46
|
+
" #{text}".gsub(%r{(<p>|<li>|<br\s*/?>|[ \n\r\t\(])((http://|https://|ftp://|mailto:|smb://|afp://|file://|gopher://|news://|ssl://|sslv2://|sslv3://|tls://|tcp://|udp://)([a-zA-Z0-9@:%_+*~#?&=.,/;-]*[a-zA-Z0-9@:%_+*~#&=/;-]))([.,?!]*?)(?=(</p>|</li>|<br\s*/?>|[ \n\r\t\)])?)}i){ |match|
|
47
|
+
match = [match, $1, $2, $3, $4, $5]
|
48
|
+
match[2] = match[2] # escape something here
|
49
|
+
caption = FriendlyFormat.trim match[2]
|
50
|
+
# match[2] = sanitize match[2]
|
51
|
+
match[1]+'<a href="'+match[2]+'" title="'+match[2]+"\"#{attrs}>"+
|
52
|
+
caption+'</a>'+match[5]
|
53
|
+
|
54
|
+
# Match e-mail addresses.
|
55
|
+
}.gsub(%r{(<p>|<li>|<br\s*/?>|[ \n\r\t\(])([A-Za-z0-9._-]+@[A-Za-z0-9._+-]+\.[A-Za-z]{2,4})([.,?!]*?)(?=(</p>|</li>|<br\s*/?>|[ \n\r\t\)]))}i, '\1<a href="mailto:\2">\2</a>\3').
|
56
|
+
|
57
|
+
# Match www domains/addresses.
|
58
|
+
gsub(%r{(<p>|<li>|[ \n\r\t\(])(www\.[a-zA-Z0-9@:%_+*~#?&=.,/;-]*[a-zA-Z0-9@:%_+~#\&=/;-])([.,?!]*?)(?=(</p>|</li>|<br\s*/?>|[ \n\r\t\)]))}i){ |match|
|
59
|
+
match = [match, $1, $2, $3, $4, $5]
|
60
|
+
match[2] = match[2] # escape something here
|
61
|
+
caption = FriendlyFormat.trim match[2]
|
62
|
+
# match[2] = sanitize match[2]
|
63
|
+
match[1]+'<a href="http://'+match[2]+'" title="http://'+match[2]+"\"#{attrs}>"+
|
64
|
+
caption+'</a>'+match[3]
|
65
|
+
}[1..-1]
|
66
|
+
end
|
67
|
+
|
68
|
+
# same as format_autolink_regexp, but it's simplified and
|
69
|
+
# cannot process text composed with html and plain text.
|
70
|
+
# used in format_autolink.
|
71
|
+
def format_url text, attrs = {}
|
72
|
+
# translated from drupal-6.2/modules/filter/filter.module
|
73
|
+
# Match absolute URLs.
|
74
|
+
text.gsub(
|
75
|
+
%r{((http://|https://|ftp://|mailto:|smb://|afp://|file://|gopher://|news://|ssl://|sslv2://|sslv3://|tls://|tcp://|udp://|www\.)([a-zA-Z0-9@:%_+*~#?&=.,/;-]*[a-zA-Z0-9@:%_+*~#&=/;-]))([.,?!]*?)}i){ |match|
|
76
|
+
url = $1 # is there any other way to get this variable?
|
77
|
+
|
78
|
+
caption = FriendlyFormat.trim url
|
79
|
+
attrs = attrs.map{ |k,v| " #{k}=\"#{v}\""}.join
|
80
|
+
|
81
|
+
# Match www domains/addresses.
|
82
|
+
url = "http://#{url}" unless url =~ %r{^http://}
|
83
|
+
"<a href=\"#{url}\" title=\"#{url}\"#{attrs}>#{caption}</a>"
|
84
|
+
# Match e-mail addresses.
|
85
|
+
}.gsub( %r{([A-Za-z0-9._-]+@[A-Za-z0-9._+-]+\.[A-Za-z]{2,4})([.,?!]*?)}i,
|
86
|
+
'<a href="mailto:\1">\1</a>')
|
87
|
+
end
|
88
|
+
|
89
|
+
# convert newline character(s) to <br />
|
90
|
+
def format_newline text
|
91
|
+
# windows: \r\n
|
92
|
+
# mac os 9: \r
|
93
|
+
text.gsub("\r\n", "\n").tr("\r", "\n").gsub("\n", '<br />')
|
94
|
+
end
|
95
|
+
|
96
|
+
private
|
97
|
+
# extract it to public?
|
98
|
+
def self.trim text, length = 75
|
99
|
+
# Use +3 for '...' string length.
|
100
|
+
if text.size <= 3
|
101
|
+
'...'
|
102
|
+
elsif text.size > length
|
103
|
+
"#{text[0...length-3]}..."
|
104
|
+
else
|
105
|
+
text
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
# perhaps we should escape all inside code instead of pre?
|
110
|
+
def self.escape_all_inside_pre html, allowed_tags
|
111
|
+
return html unless allowed_tags.member? :pre
|
112
|
+
# don't bother nested pre, because we escape all tags in pre
|
113
|
+
html = html + '</pre>' unless html =~ %r{</pre>}i
|
114
|
+
html.gsub(%r{<pre>(.*)</pre>}mi){
|
115
|
+
# stop escaping for '>' because drupal's url filter would make > into url...
|
116
|
+
# is there any other way to get matched group?
|
117
|
+
"<pre>#{FriendlyFormat.escape_lt(FriendlyFormat.escape_amp($1))}</pre>"
|
118
|
+
}
|
119
|
+
end
|
120
|
+
|
121
|
+
# recursion entrance
|
122
|
+
def self.format_article_entrance html, allowed_tags = Set.new
|
123
|
+
require 'hpricot'
|
124
|
+
FriendlyFormat.format_article_elems(Hpricot.parse(
|
125
|
+
FriendlyFormat.escape_all_inside_pre(html, allowed_tags)), allowed_tags)
|
126
|
+
end
|
127
|
+
|
128
|
+
# recursion
|
129
|
+
def self.format_article_elems elems, allowed_tags = Set.new, no_format_newline = false
|
130
|
+
elems.children.map{ |e|
|
131
|
+
if e.kind_of?(Hpricot::Text)
|
132
|
+
if no_format_newline
|
133
|
+
format_url(e.content)
|
134
|
+
else
|
135
|
+
format_newline format_url(e.content)
|
136
|
+
end
|
137
|
+
elsif e.kind_of?(Hpricot::Elem)
|
138
|
+
if allowed_tags.member? e.name.to_sym
|
139
|
+
if e.empty? || e.name == 'a'
|
140
|
+
e.to_html
|
141
|
+
else
|
142
|
+
e.stag.inspect +
|
143
|
+
FriendlyFormat.format_article_elems(e, allowed_tags, e.stag.name == 'pre') +
|
144
|
+
(e.etag || Hpricot::ETag.new(e.stag.name)).inspect
|
145
|
+
end
|
146
|
+
else
|
147
|
+
if e.empty?
|
148
|
+
FriendlyFormat.escape_lt(e.stag.inspect)
|
149
|
+
else
|
150
|
+
FriendlyFormat.escape_lt(e.stag.inspect) +
|
151
|
+
FriendlyFormat.format_article_elems(e, allowed_tags) +
|
152
|
+
FriendlyFormat.escape_lt((e.etag || Hpricot::ETag.new(e.stag.name)).inspect)
|
153
|
+
end
|
154
|
+
end
|
155
|
+
end
|
156
|
+
}.join
|
157
|
+
end
|
158
|
+
|
159
|
+
def self.escape_amp text
|
160
|
+
text.gsub('&', '&')
|
161
|
+
end
|
162
|
+
|
163
|
+
# i cannot find a way to escape both lt and gt,
|
164
|
+
# so it's a trick that just escape lt and no browser
|
165
|
+
# would treat complex lt and gt structure to be a tag
|
166
|
+
# wraping content.
|
167
|
+
def self.escape_lt text
|
168
|
+
text.gsub('<', '<')
|
169
|
+
end
|
170
|
+
|
171
|
+
end
|