wikicloth 0.8.2 → 0.8.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -58,43 +58,45 @@ module WikiCloth
58
58
  end
59
59
 
60
60
  def render(opt={})
61
- self.options = { :noedit => false, :locale => I18n.default_locale, :fast => true, :output => :html, :link_handler => self.link_handler,
61
+ self.options = { :noedit => false, :fast => true, :output => :html, :link_handler => self.link_handler,
62
62
  :params => self.params, :sections => self.sections }.merge(self.options).merge(opt)
63
63
  self.options[:link_handler].params = options[:params]
64
64
 
65
- I18n.locale = self.options[:locale]
66
-
67
- data = self.sections.collect { |s| s.render(self.options) }.join
68
- data.gsub!(/<!--(.|\s)*?-->/,"")
69
- data << "\n" if data.last(1) != "\n"
70
- data << "garbage"
71
-
72
- buffer = WikiBuffer.new("",options)
73
-
74
- begin
75
- if self.options[:fast]
76
- until data.empty?
77
- case data
78
- when /\A\w+/
79
- data = $'
80
- @current_row += $&.length
81
- buffer.add_word($&)
82
- when /\A[^\w]+(\w|)/m
83
- data = $'
84
- $&.each_char { |c| add_current_char(buffer,c) }
65
+ locale = self.options[:locale] || I18n.locale
66
+
67
+ I18n.with_locale(locale) do
68
+ data = self.sections.collect { |s| s.render(self.options) }.join
69
+ data.gsub!(/<!--(.|\s)*?-->/,"")
70
+ data << "\n" if data.last(1) != "\n"
71
+ data << "garbage"
72
+
73
+ buffer = WikiBuffer.new("",options)
74
+
75
+ begin
76
+ if self.options[:fast]
77
+ until data.empty?
78
+ case data
79
+ when /\A\w+/
80
+ data = $'
81
+ @current_row += $&.length
82
+ buffer.add_word($&)
83
+ when /\A[^\w]+(\w|)/m
84
+ data = $'
85
+ $&.each_char { |c| add_current_char(buffer,c) }
86
+ end
85
87
  end
88
+ else
89
+ data.each_char { |c| add_current_char(buffer,c) }
86
90
  end
87
- else
88
- data.each_char { |c| add_current_char(buffer,c) }
91
+ rescue => err
92
+ debug_tree = buffer.buffers.collect { |b| b.debug }.join("-->")
93
+ puts I18n.t("unknown error on line", :line => @current_line, :row => @current_row, :tree => debug_tree)
94
+ raise err
89
95
  end
90
- rescue => err
91
- debug_tree = buffer.buffers.collect { |b| b.debug }.join("-->")
92
- puts I18n.t("unknown error on line", :line => @current_line, :row => @current_row, :tree => debug_tree)
93
- raise err
94
- end
95
96
 
96
- buffer.eof()
97
- buffer.send("to_#{self.options[:output]}")
97
+ buffer.eof()
98
+ buffer.send("to_#{self.options[:output]}")
99
+ end
98
100
  end
99
101
 
100
102
  def sections
@@ -2,6 +2,8 @@ begin
2
2
  require 'rinku'
3
3
  rescue LoadError
4
4
  require 'twitter-text'
5
+ require 'nokogiri'
6
+ require 'htmlentities'
5
7
  end
6
8
 
7
9
  READ_MODE = "r:UTF-8"
@@ -34,7 +36,13 @@ module ExtendedString
34
36
  end
35
37
  else
36
38
  def auto_link
37
- Twitter::Autolink.auto_link(to_s)
39
+ doc = Nokogiri::HTML::DocumentFragment.parse(to_s)
40
+ doc.xpath(".//text()").each do |node|
41
+ autolink = Twitter::Autolink.auto_link_urls(node.to_s, :suppress_no_follow => true, :target_blank => false)
42
+ autolink = HTMLEntities.new.decode(autolink) if node.parent.name == "pre"
43
+ node.replace autolink
44
+ end
45
+ doc.to_s
38
46
  end
39
47
  end
40
48
 
@@ -1,3 +1,3 @@
1
1
  module WikiCloth
2
- VERSION = "0.8.2" unless defined?(::WikiCloth::VERSION)
2
+ VERSION = "0.8.3" unless defined?(::WikiCloth::VERSION)
3
3
  end
@@ -218,6 +218,8 @@ class WikiBuffer
218
218
  (@paragraph_open ? "</p>" : "") + gen_heading($1.length,$2)
219
219
  }
220
220
 
221
+ self.data = self.data.auto_link
222
+
221
223
  # Paragraphs
222
224
  if is_heading
223
225
  @paragraph_open = false
@@ -232,7 +234,7 @@ class WikiBuffer
232
234
  end
233
235
  end
234
236
 
235
- self.params << self.data.auto_link
237
+ self.params << self.data
236
238
  self.data = ""
237
239
  else
238
240
  self.data << current_char
@@ -1,8 +1,5 @@
1
-
2
1
  require 'rubygems'
3
- gem "test-unit", ">= 2.0.5"
4
- require 'active_support'
5
- require 'active_support/test_case'
6
2
  require 'test/unit'
3
+ require 'active_support'
7
4
  I18n.load_path << File.join(File.dirname(__FILE__),'locales.yml')
8
5
  require File.join(File.dirname(__FILE__),'../init')
@@ -37,7 +37,7 @@ class WikiParser < WikiCloth::Parser
37
37
  end
38
38
  end
39
39
 
40
- class WikiClothTest < ActiveSupport::TestCase
40
+ class WikiClothTest < Test::Unit::TestCase
41
41
 
42
42
  test "a url should be automaticly linked" do
43
43
  wiki = WikiCloth::Parser.new(:data => "\n\n\nhttp://www.google.com/")
@@ -51,10 +51,16 @@ class WikiClothTest < ActiveSupport::TestCase
51
51
  assert data.include?('<a href="http://www.google.com/">')
52
52
  wiki = WikiCloth::Parser.new(:data => "[https://github.com/repo/README.md README]")
53
53
  data = wiki.to_html
54
- assert data.include?('https://github.com/repo/README.md')
54
+ assert_equal data, "\n<p><a href=\"https://github.com/repo/README.md\" target=\"_blank\">README</a></p>"
55
55
  assert data =~ /\>\s*README\s*\</
56
56
  end
57
57
 
58
+ test "autolinking keeps html entities intact" do
59
+ wiki = WikiCloth::Parser.new(:data => "<div>&amp; &gt;</div><pre>&amp; &lt;</pre> https://github.com/repo/README.md &gt; &amp;")
60
+ data = wiki.to_html
61
+ assert_equal "\n<p><div>&amp; &gt;</div><pre>&amp; &lt;</pre> <a href=\"https://github.com/repo/README.md\">https://github.com/repo/README.md</a> &gt; &amp;</p>", data
62
+ end
63
+
58
64
  test "image url override" do
59
65
  wiki = WikiCloth::Parser.new(:data => "[[Image:test.jpg]]")
60
66
  data = wiki.to_html
@@ -279,7 +285,7 @@ class WikiClothTest < ActiveSupport::TestCase
279
285
  test "template vars should not be parsed inside a pre tag" do
280
286
  wiki = WikiCloth::Parser.new(:data => "<pre>{{{1}}}</pre>")
281
287
  data = wiki.to_html
282
- assert data =~ /&#123;&#123;&#123;1&#125;&#125;&#125;/
288
+ assert data =~ /\{\{\{1\}\}\}/
283
289
  end
284
290
 
285
291
  test "[[ links ]] should not work inside pre tags" do
@@ -391,7 +397,7 @@ EOS
391
397
  end
392
398
 
393
399
  test "sanitize html" do
394
- wiki = WikiParser.new(:data => "<script type=\"text/javascript\" src=\"bla.js\"></script>\n<a href=\"test.html\" onmouseover=\"alert('hello world');\">test</a>\n")
400
+ wiki = WikiParser.new(:data => "<pre>&lt;test</pre><script type=\"text/javascript\" src=\"bla.js\"></script>\n<a href=\"test.html\" onmouseover=\"alert('hello world');\">test</a>\n")
395
401
  data = wiki.to_html
396
402
  assert !(data =~ /<script/)
397
403
  assert !(data =~ /onmouseover/)
@@ -440,7 +446,7 @@ EOS
440
446
  test "empty item in toc" do
441
447
  wiki = WikiCloth::WikiCloth.new({:data => "__TOC__\n=A="})
442
448
  data = wiki.render
443
- assert data.include?("<table id=\"toc\" class=\"toc\" summary=\"Contents\"><tr><td><div id=\"toctitle\"><h2>Table of Contents</h2></div><ul></li><li><a href=\"#A\">A</a></li></ul></td></tr></table>")
449
+ assert data.include?("<table id=\"toc\" class=\"toc\" summary=\"Contents\"><tr><td>\n<div id=\"toctitle\"><h2>Table of Contents</h2></div>\n<ul><li><a href=\"#A\">A</a></li></ul>\n</td></tr></table>")
444
450
  end
445
451
 
446
452
  test "pre at beginning" do
@@ -452,12 +458,12 @@ EOS
452
458
  test "toc declared as list" do
453
459
  wiki = WikiCloth::WikiCloth.new({:data => "__TOC__\n=A=\n==B==\n===C==="})
454
460
  data = wiki.render
455
- assert data.include?("<table id=\"toc\" class=\"toc\" summary=\"Contents\"><tr><td><div id=\"toctitle\"><h2>Table of Contents</h2></div><ul></li><li><a href=\"#A\">A</a><ul><li><a href=\"#B\">B</a><ul><li><a href=\"#C\">C</a></li></ul></ul></ul></td></tr></table>")
461
+ assert data.include?("<table id=\"toc\" class=\"toc\" summary=\"Contents\"><tr><td>\n<div id=\"toctitle\"><h2>Table of Contents</h2></div>\n<ul><li>\n<a href=\"#A\">A</a><ul><li>\n<a href=\"#B\">B</a><ul><li><a href=\"#C\">C</a></li></ul>\n</li></ul>\n</li></ul>\n</td></tr></table>")
456
462
  end
457
463
 
458
464
  test "toc numbered" do
459
465
  wiki = WikiCloth::WikiCloth.new({:data => "=A=\n=B=\n==C==\n==D==\n===E===\n===F===\n====G====\n====H====\n==I==\n=J=\n=K=\n===L===\n===M===\n====N====\n====O===="})
460
466
  data = wiki.render(:noedit => true, :toc_numbered => true)
461
- assert data.include?("<table id=\"toc\" class=\"toc\" summary=\"Contents\"><tr><td><div id=\"toctitle\"><h2>Table of Contents</h2></div><ul></li><li><a href=\"#A\">1 A</a></li><li><a href=\"#B\">2 B</a><ul><li><a href=\"#C\">2.1 C</a></li><li><a href=\"#D\">2.2 D</a><ul><li><a href=\"#E\">2.2.1 E</a></li><li><a href=\"#F\">2.2.2 F</a><ul><li><a href=\"#G\">2.2.2.1 G</a></li><li><a href=\"#H\">2.2.2.2 H</a></li></ul></ul><li><a href=\"#I\">2.3 I</a></li></ul><li><a href=\"#J\">3 J</a></li><li><a href=\"#K\">4 K</a><ul><ul><li><a href=\"#L\">4.1 L</a></li><li><a href=\"#M\">4.2 M</a><ul><li><a href=\"#N\">4.2.1 N</a></li><li><a href=\"#O\">4.2.2 O</a></li></ul></ul></ul></ul></td></tr></table>")
467
+ assert data.include?("<table id=\"toc\" class=\"toc\" summary=\"Contents\"><tr><td>\n<div id=\"toctitle\"><h2>Table of Contents</h2></div>\n<ul>\n<li><a href=\"#A\">1 A</a></li>\n<li>\n<a href=\"#B\">2 B</a><ul>\n<li><a href=\"#C\">2.1 C</a></li>\n<li>\n<a href=\"#D\">2.2 D</a><ul>\n<li><a href=\"#E\">2.2.1 E</a></li>\n<li>\n<a href=\"#F\">2.2.2 F</a><ul>\n<li><a href=\"#G\">2.2.2.1 G</a></li>\n<li><a href=\"#H\">2.2.2.2 H</a></li>\n</ul>\n</li>\n</ul>\n</li>\n<li><a href=\"#I\">2.3 I</a></li>\n</ul>\n</li>\n<li><a href=\"#J\">3 J</a></li>\n<li>\n<a href=\"#K\">4 K</a><ul><ul>\n<li><a href=\"#L\">4.1 L</a></li>\n<li>\n<a href=\"#M\">4.2 M</a><ul>\n<li><a href=\"#N\">4.2.1 N</a></li>\n<li><a href=\"#O\">4.2.2 O</a></li>\n</ul>\n</li>\n</ul></ul>\n</li>\n</ul>\n</td></tr></table>")
462
468
  end
463
469
  end
@@ -23,6 +23,8 @@ spec = Gem::Specification.new do |s|
23
23
  s.add_dependency 'builder'
24
24
  s.add_dependency 'expression_parser'
25
25
  s.add_dependency 'twitter-text'
26
+ s.add_dependency 'nokogiri'
27
+ s.add_dependency 'htmlentities'
26
28
  s.add_development_dependency 'test-unit'
27
29
  s.add_development_dependency 'activesupport'
28
30
  s.add_development_dependency 'i18n'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wikicloth
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.2
4
+ version: 0.8.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-01-27 00:00:00.000000000 Z
12
+ date: 2015-03-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: builder
16
- requirement: &17717700 !ruby/object:Gem::Requirement
16
+ requirement: &21800440 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *17717700
24
+ version_requirements: *21800440
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: expression_parser
27
- requirement: &17717280 !ruby/object:Gem::Requirement
27
+ requirement: &21800020 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *17717280
35
+ version_requirements: *21800020
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: twitter-text
38
- requirement: &17716860 !ruby/object:Gem::Requirement
38
+ requirement: &21799600 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,32 @@ dependencies:
43
43
  version: '0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *17716860
46
+ version_requirements: *21799600
47
+ - !ruby/object:Gem::Dependency
48
+ name: nokogiri
49
+ requirement: &21799180 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :runtime
56
+ prerelease: false
57
+ version_requirements: *21799180
58
+ - !ruby/object:Gem::Dependency
59
+ name: htmlentities
60
+ requirement: &21798760 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :runtime
67
+ prerelease: false
68
+ version_requirements: *21798760
47
69
  - !ruby/object:Gem::Dependency
48
70
  name: test-unit
49
- requirement: &17768360 !ruby/object:Gem::Requirement
71
+ requirement: &21798340 !ruby/object:Gem::Requirement
50
72
  none: false
51
73
  requirements:
52
74
  - - ! '>='
@@ -54,10 +76,10 @@ dependencies:
54
76
  version: '0'
55
77
  type: :development
56
78
  prerelease: false
57
- version_requirements: *17768360
79
+ version_requirements: *21798340
58
80
  - !ruby/object:Gem::Dependency
59
81
  name: activesupport
60
- requirement: &17767940 !ruby/object:Gem::Requirement
82
+ requirement: &21797920 !ruby/object:Gem::Requirement
61
83
  none: false
62
84
  requirements:
63
85
  - - ! '>='
@@ -65,10 +87,10 @@ dependencies:
65
87
  version: '0'
66
88
  type: :development
67
89
  prerelease: false
68
- version_requirements: *17767940
90
+ version_requirements: *21797920
69
91
  - !ruby/object:Gem::Dependency
70
92
  name: i18n
71
- requirement: &17767520 !ruby/object:Gem::Requirement
93
+ requirement: &21835860 !ruby/object:Gem::Requirement
72
94
  none: false
73
95
  requirements:
74
96
  - - ! '>='
@@ -76,10 +98,10 @@ dependencies:
76
98
  version: '0'
77
99
  type: :development
78
100
  prerelease: false
79
- version_requirements: *17767520
101
+ version_requirements: *21835860
80
102
  - !ruby/object:Gem::Dependency
81
103
  name: rake
82
- requirement: &17767100 !ruby/object:Gem::Requirement
104
+ requirement: &21835440 !ruby/object:Gem::Requirement
83
105
  none: false
84
106
  requirements:
85
107
  - - ! '>='
@@ -87,10 +109,10 @@ dependencies:
87
109
  version: '0'
88
110
  type: :development
89
111
  prerelease: false
90
- version_requirements: *17767100
112
+ version_requirements: *21835440
91
113
  - !ruby/object:Gem::Dependency
92
114
  name: rdoc
93
- requirement: &17766680 !ruby/object:Gem::Requirement
115
+ requirement: &21835020 !ruby/object:Gem::Requirement
94
116
  none: false
95
117
  requirements:
96
118
  - - ! '>='
@@ -98,10 +120,10 @@ dependencies:
98
120
  version: '0'
99
121
  type: :development
100
122
  prerelease: false
101
- version_requirements: *17766680
123
+ version_requirements: *21835020
102
124
  - !ruby/object:Gem::Dependency
103
125
  name: simplecov
104
- requirement: &17766260 !ruby/object:Gem::Requirement
126
+ requirement: &21834600 !ruby/object:Gem::Requirement
105
127
  none: false
106
128
  requirements:
107
129
  - - ! '>='
@@ -109,7 +131,7 @@ dependencies:
109
131
  version: '0'
110
132
  type: :development
111
133
  prerelease: false
112
- version_requirements: *17766260
134
+ version_requirements: *21834600
113
135
  description: mediawiki parser
114
136
  email: nricciar@gmail.com
115
137
  executables: []