godfat-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 ADDED
@@ -0,0 +1,11 @@
1
+ = pagify changes history
2
+
3
+ == friendly_format 0.5.1 / 2008-12-11
4
+
5
+ * first release to rubyforge
6
+
7
+ == friendly_format 0.5 / 2008-11-05
8
+
9
+ * 1 major enhancement
10
+ * Birthday!
11
+ * extracted from Ludy::XhtmlFormatter!
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 &lt; 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,3 @@
1
+ module FriendlyFormat
2
+ VERSION = '0.5.1'
3
+ 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 &gt; 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('&', '&amp;')
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('<', '&lt;')
169
+ end
170
+
171
+ end
@@ -0,0 +1,111 @@
1
+ http://phpbb.godfat.org/viewtopic.php?t=873
2
+ <pre> 作者 godfat (godfat 真常) 站內 ProjectX
3
+ 標題 Re: [PSC] variants timer
4
+ 時間 2008/03/04 Tue 00:34:54
5
+ http://phpbb.godfat.org/viewtopic.php?t=873
6
+ <a href="http://phpbb.godfat.org/viewtopic.php?t=873">
7
+ 測試</a>
8
+ <a href="http://phpbb.godfat.org/viewtopic.php?t=873">
9
+ <img src="http://phpbb.godfat.org/templates/subSilver/images/logo_phpBB.gif"/></a>
10
+ ※ 引述《godfat (godfat 真常)》之銘言:
11
+ > 3. tuple + traits, 也就是一堆 template 技巧了
12
+ > class Dispatcher{
13
+ > template <class T>
14
+ > void sub(shared_ptr<T> p){
15
+ > get_tuple_data_field_by_type<T>(data).push_back(p);
16
+ > }
17
+ > tuple<A, B, C> data;
18
+ > };
19
+
20
+ 以上 A, B, C 是指 timer_subscriber_list<A>, etc.
21
+
22
+ 這個東西會需要從 T => tuple index 的 mapping
23
+ 現在這部份做完了...........(template 沒忘太多啊哈哈)
24
+
25
+ #include <tr1/tuple>
26
+ #include <tr1/type_traits>
27
+ #include <iostream>
28
+
29
+ using namespace std::tr1;
30
+
31
+ // 拿來判斷兩個整數是否相同,這是為了檢查有沒有爆表
32
+ // 像是 tuple_element<3, tuple<int,int,int> >::type
33
+ // 這樣就是爆表,因為這 tuple 沒這麼大
34
+ template <bool> struct is_equal_type { typedef true_type type; };
35
+ template <> struct is_equal_type<false>{ typedef false_type type; };
36
+
37
+ // 定義相等的話,is_equal<1,1>::value 會是 1
38
+ // is_equal<1,2>::value 會是 0
39
+ // ::type 會是 true_type/false_type (from tr1)
40
+ template <int Lhs, int Rhs> struct is_equal{
41
+ enum{ value = Lhs == Rhs };
42
+ typedef typename is_equal_type<value>::type type;
43
+ };
44
+
45
+ template <class T, class Tuple, int Index> struct type_index_imp;
46
+
47
+ // 如果跑到這裡,表示爆表了,在我的 g++4.3 上會顯示:
48
+ // tuple.cpp:20: error: ‘type_not_found’ is not a member of "T"
49
+ template <class T, class Tuple, int Index, class Bool>
50
+ struct type_index_check{
51
+ enum{ value = T::type_not_found };
52
+ };
53
+
54
+ // 沒有爆表就繼續找吧
55
+ template <class T, class Tuple, int Index>
56
+ struct type_index_check<T, Tuple, Index, false_type>{
57
+ enum{ value = type_index_imp<T, Tuple, Index+1>::value };
58
+ };
59
+
60
+ // 找到了!
61
+ template <class T, class Tuple, int Index, class Bool>
62
+ struct type_index_if{
63
+ enum{ value = Index }; // found, stop recursion
64
+ };
65
+
66
+ // 不直接呼叫 imp, 呼叫 check 檢查是否爆表
67
+ template <class T, class Tuple, int Index>
68
+ struct type_index_if<T, Tuple, Index, false_type>{
69
+ enum{ value = type_index_check<T, Tuple, Index,
70
+ typename is_equal<Index+1, tuple_size<Tuple>::value>::type>::value };
71
+ };
72
+
73
+ // 檢查兩個型別是否相同
74
+ template <class T, class Tuple, int Index>
75
+ struct type_index_imp{
76
+ enum{ value = type_index_if<T, Tuple, Index,
77
+ typename is_same<T, typename tuple_element<Index, Tuple>::type>::type>::value };
78
+ };
79
+
80
+ // simple wrapper
81
+ template <class T, class Tuple>
82
+ struct type_index{
83
+ enum{ value = type_index_imp<T, Tuple, 0>::value };
84
+ };
85
+
86
+ int main(){
87
+ typedef tuple<int, char, void> test_t;
88
+ std::cout << type_index<int, test_t>::value << std::endl;
89
+ std::cout << type_index<char, test_t>::value << std::endl;
90
+ std::cout << type_index<void, test_t>::value << std::endl;
91
+ std::cout << tuple_size<test_t>::value << std::endl;
92
+ // std::cout << type_index<int*, test_t>::value << std::endl;
93
+ }
94
+
95
+ 輸出:
96
+ 0
97
+ 1
98
+ 2
99
+ 3
100
+
101
+ 最後一行註解拿掉,則顯示:
102
+ tuple.cpp:20: error: "type_not_found" is not a member of "T"
103
+
104
+ 這樣應該是蠻完整的了。有了這個之後.... 後面還有得做 XD
105
+ 至少還需要一個 tuple for_each, 這也會用 template 遞迴展開
106
+
107
+ --
108
+ ※ Orign: 群星的眷屬 chance.twbbs.org
109
+ ◆ Author: godfat From 220-135-28-18.HINET-IP.hinet.net</pre>
110
+
111
+ end of article...
@@ -0,0 +1,108 @@
1
+ <a href="http://phpbb.godfat.org/viewtopic.php?t=873" title="http://phpbb.godfat.org/viewtopic.php?t=873">http://phpbb.godfat.org/viewtopic.php?t=873</a><br /><pre> 作者 godfat (godfat 真常) 站內 ProjectX
2
+ 標題 Re: [PSC] variants timer
3
+ 時間 2008/03/04 Tue 00:34:54
4
+ <a href="http://phpbb.godfat.org/viewtopic.php?t=873" title="http://phpbb.godfat.org/viewtopic.php?t=873">http://phpbb.godfat.org/viewtopic.php?t=873</a>
5
+ &lt;a href="<a href="http://phpbb.godfat.org/viewtopic.php?t=873" title="http://phpbb.godfat.org/viewtopic.php?t=873">http://phpbb.godfat.org/viewtopic.php?t=873</a>">
6
+ 測試&lt;/a>
7
+ &lt;a href="<a href="http://phpbb.godfat.org/viewtopic.php?t=873" title="http://phpbb.godfat.org/viewtopic.php?t=873">http://phpbb.godfat.org/viewtopic.php?t=873</a>">
8
+ &lt;img src="<a href="http://phpbb.godfat.org/templates/subSilver/images/logo_phpBB.gif" title="http://phpbb.godfat.org/templates/subSilver/images/logo_phpBB.gif">http://phpbb.godfat.org/templates/subSilver/images/logo_phpBB.gif</a>"/>&lt;/a>
9
+ ※ 引述《godfat (godfat 真常)》之銘言:
10
+ > 3. tuple + traits, 也就是一堆 template 技巧了
11
+ > class Dispatcher{
12
+ > template &lt;class T>
13
+ > void sub(shared_ptr&lt;T> p){
14
+ > get_tuple_data_field_by_type&lt;T>(data).push_back(p);
15
+ > }
16
+ > tuple&lt;A, B, C> data;
17
+ > };
18
+
19
+ 以上 A, B, C 是指 timer_subscriber_list&lt;A>, etc.
20
+
21
+ 這個東西會需要從 T => tuple index 的 mapping
22
+ 現在這部份做完了...........(template 沒忘太多啊哈哈)
23
+
24
+ #include &lt;tr1/tuple>
25
+ #include &lt;tr1/type_traits>
26
+ #include &lt;iostream>
27
+
28
+ using namespace std::tr1;
29
+
30
+ // 拿來判斷兩個整數是否相同,這是為了檢查有沒有爆表
31
+ // 像是 tuple_element&lt;3, tuple&lt;int,int,int> >::type
32
+ // 這樣就是爆表,因為這 tuple 沒這麼大
33
+ template &lt;bool> struct is_equal_type { typedef true_type type; };
34
+ template &lt;> struct is_equal_type&lt;false>{ typedef false_type type; };
35
+
36
+ // 定義相等的話,is_equal&lt;1,1>::value 會是 1
37
+ // is_equal&lt;1,2>::value 會是 0
38
+ // ::type 會是 true_type/false_type (from tr1)
39
+ template &lt;int Lhs, int Rhs> struct is_equal{
40
+ enum{ value = Lhs == Rhs };
41
+ typedef typename is_equal_type&lt;value>::type type;
42
+ };
43
+
44
+ template &lt;class T, class Tuple, int Index> struct type_index_imp;
45
+
46
+ // 如果跑到這裡,表示爆表了,在我的 g++4.3 上會顯示:
47
+ // tuple.cpp:20: error: ‘type_not_found’ is not a member of "T"
48
+ template &lt;class T, class Tuple, int Index, class Bool>
49
+ struct type_index_check{
50
+ enum{ value = T::type_not_found };
51
+ };
52
+
53
+ // 沒有爆表就繼續找吧
54
+ template &lt;class T, class Tuple, int Index>
55
+ struct type_index_check&lt;T, Tuple, Index, false_type>{
56
+ enum{ value = type_index_imp&lt;T, Tuple, Index+1>::value };
57
+ };
58
+
59
+ // 找到了!
60
+ template &lt;class T, class Tuple, int Index, class Bool>
61
+ struct type_index_if{
62
+ enum{ value = Index }; // found, stop recursion
63
+ };
64
+
65
+ // 不直接呼叫 imp, 呼叫 check 檢查是否爆表
66
+ template &lt;class T, class Tuple, int Index>
67
+ struct type_index_if&lt;T, Tuple, Index, false_type>{
68
+ enum{ value = type_index_check&lt;T, Tuple, Index,
69
+ typename is_equal&lt;Index+1, tuple_size&lt;Tuple>::value>::type>::value };
70
+ };
71
+
72
+ // 檢查兩個型別是否相同
73
+ template &lt;class T, class Tuple, int Index>
74
+ struct type_index_imp{
75
+ enum{ value = type_index_if&lt;T, Tuple, Index,
76
+ typename is_same&lt;T, typename tuple_element&lt;Index, Tuple>::type>::type>::value };
77
+ };
78
+
79
+ // simple wrapper
80
+ template &lt;class T, class Tuple>
81
+ struct type_index{
82
+ enum{ value = type_index_imp&lt;T, Tuple, 0>::value };
83
+ };
84
+
85
+ int main(){
86
+ typedef tuple&lt;int, char, void> test_t;
87
+ std::cout &lt;&lt; type_index&lt;int, test_t>::value &lt;&lt; std::endl;
88
+ std::cout &lt;&lt; type_index&lt;char, test_t>::value &lt;&lt; std::endl;
89
+ std::cout &lt;&lt; type_index&lt;void, test_t>::value &lt;&lt; std::endl;
90
+ std::cout &lt;&lt; tuple_size&lt;test_t>::value &lt;&lt; std::endl;
91
+ // std::cout &lt;&lt; type_index&lt;int*, test_t>::value &lt;&lt; std::endl;
92
+ }
93
+
94
+ 輸出:
95
+ 0
96
+ 1
97
+ 2
98
+ 3
99
+
100
+ 最後一行註解拿掉,則顯示:
101
+ tuple.cpp:20: error: "type_not_found" is not a member of "T"
102
+
103
+ 這樣應該是蠻完整的了。有了這個之後.... 後面還有得做 XD
104
+ 至少還需要一個 tuple for_each, 這也會用 template 遞迴展開
105
+
106
+ --
107
+ ※ Orign: 群星的眷屬 chance.twbbs.org
108
+ ◆ Author: godfat From 220-135-28-18.HINET-IP.hinet.net</pre><br /><br />end of article...<br />
@@ -0,0 +1,168 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'minitest/unit'
5
+ MiniTest::Unit.autorun
6
+
7
+ require 'friendly_format'
8
+
9
+ # 2008-05-09 godfat
10
+ class TestFriendlyFormat < MiniTest::Unit::TestCase
11
+ include FriendlyFormat
12
+
13
+ def test_article
14
+ str =
15
+ ' http://friends.roodo.com/forum/viewTopic/10170
16
+ 用 Haskell 寫成的名軟體?
17
+
18
+ 主題發起人 : godfat 真常
19
+
20
+ 2008年04月02日 01:24
21
+ 第一個當然是首推唐鳳寫的 Pugs!
22
+ 是基於 Perl 6 一直實作困難,所以才決定寫 Pugs 當中繼者。
23
+
24
+ Pugs 無論對 Haskell 社群和 Perl 6 社群都有很大的影響。
25
+ 唐大師:
26
+ http://zh.wikipedia.org/wiki/唐鳳
27
+
28
+ 另一個就是 Darcs 了,非常強大的 revision control system,
29
+ 蠻多人在用的。理所當然,比 svn 先進多了。
30
+ svn 其實也差不多是要慢慢式微了...
31
+ 取而代之的大概會是 SVK, git, 和 Darcs 吧
32
+ 目前看到是這三個最多,都很先進。不過我對 git 沒好感。
33
+
34
+ 另一方面還有非常多,Haskell 正在成長中啊 ~~~
35
+ 很多知名 library 都有 haskell binding 了,wxWidgets,
36
+ OpenGL, 還有一些 web cgi 之類的東西也有。
37
+
38
+ 現在切入正是時機啊... XD'
39
+
40
+ assert_equal \
41
+ ' <a href="http://friends.roodo.com/forum/viewTopic/10170" title="http://friends.roodo.com/forum/viewTopic/10170">http://friends.roodo.com/forum/viewTopic/10170</a>
42
+ 用 Haskell 寫成的名軟體?
43
+
44
+ 主題發起人 : godfat 真常
45
+
46
+ 2008年04月02日 01:24
47
+ 第一個當然是首推唐鳳寫的 Pugs!
48
+ 是基於 Perl 6 一直實作困難,所以才決定寫 Pugs 當中繼者。
49
+
50
+ Pugs 無論對 Haskell 社群和 Perl 6 社群都有很大的影響。
51
+ 唐大師:
52
+ <a href="http://zh.wikipedia.org/wiki/" title="http://zh.wikipedia.org/wiki/">http://zh.wikipedia.org/wiki/</a>唐鳳
53
+
54
+ 另一個就是 Darcs 了,非常強大的 revision control system,
55
+ 蠻多人在用的。理所當然,比 svn 先進多了。
56
+ svn 其實也差不多是要慢慢式微了...
57
+ 取而代之的大概會是 SVK, git, 和 Darcs 吧
58
+ 目前看到是這三個最多,都很先進。不過我對 git 沒好感。
59
+
60
+ 另一方面還有非常多,Haskell 正在成長中啊 ~~~
61
+ 很多知名 library 都有 haskell binding 了,wxWidgets,
62
+ OpenGL, 還有一些 web cgi 之類的東西也有。
63
+
64
+ 現在切入正是時機啊... XD', s = format_autolink(str)
65
+ assert_equal s, format_autolink_regexp(str)
66
+ end
67
+ def test_persent
68
+ str =
69
+ 'XDDDD
70
+ http://www.amazon.co.jp/%E3%80%8C%E7%84%94~%E3%83%9B%E3%83%A0%E3%83%A9%E3%80%8D~Ar-tonelico2-hymmnos-concert-Side-%E7%B4%85~/dp/B000VKZL30/ref=pd_sbs_sw_img_2 orz'
71
+
72
+ assert_equal \
73
+ 'XDDDD
74
+ <a href="http://www.amazon.co.jp/%E3%80%8C%E7%84%94~%E3%83%9B%E3%83%A0%E3%83%A9%E3%80%8D~Ar-tonelico2-hymmnos-concert-Side-%E7%B4%85~/dp/B000VKZL30/ref=pd_sbs_sw_img_2" title="http://www.amazon.co.jp/%E3%80%8C%E7%84%94~%E3%83%9B%E3%83%A0%E3%83%A9%E3%80%8D~Ar-tonelico2-hymmnos-concert-Side-%E7%B4%85~/dp/B000VKZL30/ref=pd_sbs_sw_img_2">http://www.amazon.co.jp/%E3%80%8C%E7%84%94~%E3%83%9B%E3%83%A0%E3%83%A9%E...</a> orz', s = format_autolink(str)
75
+ assert_equal s, format_autolink_regexp(str)
76
+ end
77
+ def test_img_src
78
+ str =
79
+ 'Thirst for Knowledge
80
+ <img src="http://friends.roodo.com/images/diary_photos_large/15386/MjMyNjYtdGhpcnN0X2Zvcl9rbm93bGVkZ2U=.jpg" />
81
+
82
+ 2007年12月14日
83
+ '
84
+ assert_equal str, s = format_autolink(str)
85
+ assert_equal s, format_autolink_regexp(str)
86
+ end
87
+ def test_wikipedia_persent
88
+ str = 'http://en.wikipedia.org/wiki/Haskell_%28programming_language%29'
89
+ assert_equal \
90
+ '<a href="http://en.wikipedia.org/wiki/Haskell_%28programming_language%29" title="http://en.wikipedia.org/wiki/Haskell_%28programming_language%29">http://en.wikipedia.org/wiki/Haskell_%28programming_language%29</a>', s = format_autolink(str)
91
+ assert_equal s, format_autolink_regexp(str)
92
+ end
93
+ def test_wikipedia_parentheses
94
+ str = 'http://en.wikipedia.org/wiki/Haskell_(programming_language)'
95
+ assert_equal \
96
+ '<a href="http://en.wikipedia.org/wiki/Haskell_" title="http://en.wikipedia.org/wiki/Haskell_" class="XD">http://en.wikipedia.org/wiki/Haskell_</a>(programming_language)', s = format_autolink(str, :class => 'XD')
97
+ assert_equal s, format_autolink_regexp(str, :class => 'XD')
98
+ end
99
+ def test_fixing_html
100
+ str = 'test<p>if missing end of p'
101
+ assert_equal 'test<p>if missing end of p</p>', format_autolink(str)
102
+ str = 'test<p>if missing<a> end of p'
103
+ assert_equal 'test<p>if missing<a> end of p</a></p>', format_autolink(str)
104
+ end
105
+ def test_www_url
106
+ str = 'go to www.google.com to see if you can see'
107
+ assert_equal 'go to <a href="http://www.google.com" title="http://www.google.com">www.google.com</a> to see if you can see',
108
+ s = format_autolink(str)
109
+ assert_equal s, format_autolink_regexp(str)
110
+ end
111
+ def test_escape_html_and_correct_html
112
+ str = 'test<p>if missing end of p'
113
+ assert_equal 'test<p>if missing end of p</p>', format_article(str, :p)
114
+ assert_equal 'test&lt;p>if missing end of p&lt;/p>', format_article(str, :a)
115
+ str = '<pre>asdasd<a>orz'
116
+ assert_equal '<pre>asdasd&lt;a>orz</pre>', format_article(str, :a, :pre)
117
+ assert_equal '&lt;pre>asdasd<a>orz</a>&lt;/pre>', format_article(str, :a)
118
+ assert_equal '<pre>asdasd&lt;a>orz</pre>', format_article(str, :pre)
119
+ str = 'orz<img>asd'
120
+ assert_equal 'orz<img />asd', format_article(str, :img)
121
+ assert_equal 'orz&lt;img>asd', format_article(str)
122
+ end
123
+ def test_trim_url
124
+ str = 'test with http://890123456789012345678901234567890123456789012345678901234567890123456789.com'
125
+
126
+ assert_equal 'test with <a href="http://890123456789012345678901234567890123456789012345678901234567890123456789.com" title="http://890123456789012345678901234567890123456789012345678901234567890123456789.com">http://89012345678901234567890123456789012345678901234567890123456789012...</a>', s = format_article(str)
127
+ assert_equal s, format_autolink_regexp(str)
128
+ end
129
+ def test_escape_html
130
+ str = 'a lambda expression is &lambda; x. x+1'
131
+ assert_equal str, format_article(str)
132
+ str = 'as you can see, use &lt;img src="asd"/&gt; to use'
133
+ assert_equal str, format_article(str)
134
+ end
135
+ def test_html_with_pre_and_newline2br
136
+ result = File.read('test/sample/complex_article_result.txt')
137
+ input = File.read('test/sample/complex_article.txt')
138
+
139
+ assert_equal result, format_article(input, :pre)
140
+ assert_equal result, format_article(input, SetCommon.new)
141
+ end
142
+ def test_simple_link
143
+ s = '今天是我一歲生日 <a href="http://godfat.org/" title="http://godfat.org/">http://godfat.org/</a> 真的嗎?'
144
+ assert_equal s, format_article(s, :a)
145
+ assert_equal s, format_autolink_regexp(s)
146
+ end
147
+ def test_extra_xd_at_tail
148
+ input = '<img style="float: right;" src="http://flolac.iis.sinica.edu.tw/lambdawan/sites/default/files/ruby.png.thumb.jpg"/>
149
+ <a href="http://www.ruby-forum.com/topic/169911">JRuby 1.1.5 Released</a>
150
+ <a href="http://jruby.codehaus.org/">JRuby</a> 是用 Java 寫成的 Ruby interpreter/compiler.
151
+ 原本 JRuby 只是普通的 open source project, 後來因為 <a href="http://www.sun.com/">Sun Microsystem</a>,
152
+ 也就是 Java 的開發公司,看好 JRuby, 於是僱用 JRuby team,
153
+ full time 開發 JRuby. 後來 JRuby 在各方面都快速大幅成長,
154
+ 尤其效能有了不可思議的大幅提昇,可能是 Sun 有一些撇步沒有公開吧。
155
+
156
+ 效能大幅提昇之後,JRuby 開發沒有停緩,接下來是非常大量的相容性提昇。
157
+ 也從原本僅支援 interpret mode 到後來也支援 just in time 與 ahead of time 的
158
+ compilation mode. 非常驚人的開發速度。
159
+ <zzz><xd>
160
+ 此外,其中一位開發者,<a href="http://blog.headius.com/">Charles Nutter</a> 也經常參與 <a href="http://www.ruby-forum.com/forum/14">ruby-core</a> 的討論,
161
+ 對於 Ruby 的開發頗有貢獻。'
162
+
163
+ expected = '<img src="http://flolac.iis.sinica.edu.tw/lambdawan/sites/default/files/ruby.png.thumb.jpg" style="float: right;" /><br /><a href="http://www.ruby-forum.com/topic/169911">JRuby 1.1.5 Released</a><br /><a href="http://jruby.codehaus.org/">JRuby</a> 是用 Java 寫成的 Ruby interpreter/compiler.<br />原本 JRuby 只是普通的 open source project, 後來因為 <a href="http://www.sun.com/">Sun Microsystem</a>,<br />也就是 Java 的開發公司,看好 JRuby, 於是僱用 JRuby team,<br />full time 開發 JRuby. 後來 JRuby 在各方面都快速大幅成長,<br />尤其效能有了不可思議的大幅提昇,可能是 Sun 有一些撇步沒有公開吧。<br /><br />效能大幅提昇之後,JRuby 開發沒有停緩,接下來是非常大量的相容性提昇。<br />也從原本僅支援 interpret mode 到後來也支援 just in time 與 ahead of time 的<br />compilation mode. 非常驚人的開發速度。<br /><zzz>&lt;xd><br />此外,其中一位開發者,<a href="http://blog.headius.com/">Charles Nutter</a> 也經常參與 <a href="http://www.ruby-forum.com/forum/14">ruby-core</a> 的討論,<br />對於 Ruby 的開發頗有貢獻。&lt;/xd></zzz>'
164
+
165
+ assert_equal expected, format_article(input, SetCommon.new, :zzz)
166
+
167
+ end
168
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: godfat-friendly_format
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.1
5
+ platform: ruby
6
+ authors:
7
+ - "Lin Jen-Shin (a.k.a. godfat \xE7\x9C\x9F\xE5\xB8\xB8)"
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-12-11 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: hpricot
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 0.6.0
23
+ version:
24
+ - !ruby/object:Gem::Dependency
25
+ name: bones
26
+ version_requirement:
27
+ version_requirements: !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - ">="
30
+ - !ruby/object:Gem::Version
31
+ version: 2.1.0
32
+ version:
33
+ - !ruby/object:Gem::Dependency
34
+ name: minitest
35
+ version_requirement:
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 1.3.0
41
+ version:
42
+ description: ""
43
+ email: godfat (XD) godfat.org
44
+ executables: []
45
+
46
+ extensions: []
47
+
48
+ extra_rdoc_files:
49
+ - CHANGES
50
+ - LICENSE
51
+ - NOTICE
52
+ - README
53
+ - TODO
54
+ - friendly_format.gemspec
55
+ files:
56
+ - CHANGES
57
+ - LICENSE
58
+ - NOTICE
59
+ - README
60
+ - Rakefile
61
+ - TODO
62
+ - friendly_format.gemspec
63
+ - lib/friendly_format.rb
64
+ - lib/friendly_format/set_common.rb
65
+ - lib/friendly_format/set_strict.rb
66
+ - lib/friendly_format/version.rb
67
+ - tasks/ann.rake
68
+ - tasks/bones.rake
69
+ - tasks/gem.rake
70
+ - tasks/git.rake
71
+ - tasks/manifest.rake
72
+ - tasks/notes.rake
73
+ - tasks/post_load.rake
74
+ - tasks/rdoc.rake
75
+ - tasks/rubyforge.rake
76
+ - tasks/setup.rb
77
+ - tasks/spec.rake
78
+ - tasks/svn.rake
79
+ - tasks/test.rake
80
+ - test/sample/complex_article.txt
81
+ - test/sample/complex_article_result.txt
82
+ - test/test_friendly_format.rb
83
+ has_rdoc: true
84
+ homepage: http://github.com/godfat/friendly_format
85
+ post_install_message:
86
+ rdoc_options:
87
+ - --diagram
88
+ - --charset=utf-8
89
+ - --inline-source
90
+ - --line-numbers
91
+ - --promiscuous
92
+ - --main
93
+ - README
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: "0"
101
+ version:
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: "0"
107
+ version:
108
+ requirements: []
109
+
110
+ rubyforge_project: ludy
111
+ rubygems_version: 1.2.0
112
+ signing_key:
113
+ specification_version: 2
114
+ summary: ""
115
+ test_files:
116
+ - test/test_friendly_format.rb