wikicloth 0.6.2 → 0.6.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -75,7 +75,7 @@ module WikiCloth
75
75
  ret = "<h#{self.depth}>" + (options[:noedit] == true ? "" :
76
76
  "<span class=\"editsection\">&#91;<a href=\"" + options[:link_handler].section_link(self.id) +
77
77
  "\" title=\"Edit section: #{self.title}\">edit</a>&#93;</span> ") +
78
- "<span id=\"#{self.id}\" class=\"mw-headline\"><a name=\"#{self.id}\">#{self.title}</a></span></h#{self.depth}>"
78
+ "<span id=\"#{self.id}\" class=\"mw-headline\"><a name=\"#{self.id}\">#{self.title}</a></span></h#{self.depth}>\n"
79
79
  end
80
80
  #ret += @template ? self.gsub(/\{\{\{\s*([A-Za-z0-9]+)\s*\}\}\}/,'\1') : self
81
81
  ret += self
@@ -13,13 +13,12 @@ class WikiBuffer::Link < WikiBuffer
13
13
 
14
14
  def to_s
15
15
  link_handler = @options[:link_handler]
16
- unless self.internal_link
17
- url = "#{params[0]}".strip
18
- url = 'http://' + url if url !~ /^\s*(([a-z]+):\/\/|[\?\/])/
19
-
20
- return link_handler.external_link(url, "#{params[1]}".strip)
16
+ unless self.internal_link || params[0].strip !~ /^\s*(([a-z]+):\/\/|[\?\/])/
17
+ return link_handler.external_link("#{params[0]}".strip, "#{params[1]}".strip)
21
18
  else
22
19
  case
20
+ when !self.internal_link
21
+ return "[#{params[0]}]"
23
22
  when params[0] =~ /^:(.*)/
24
23
  return link_handler.link_for(params[0],params[1])
25
24
  when params[0] =~ /^\s*([a-zA-Z0-9-]+)\s*:(.*)$/
@@ -1,4 +1,5 @@
1
1
  require 'expression_parser'
2
+ require 'uri'
2
3
 
3
4
  module WikiCloth
4
5
 
@@ -22,6 +23,10 @@ class WikiBuffer::Var < WikiBuffer
22
23
  @tag_size = val
23
24
  end
24
25
 
26
+ def skip_links?
27
+ true
28
+ end
29
+
25
30
  def skip_html?
26
31
  false
27
32
  end
@@ -46,7 +51,7 @@ class WikiBuffer::Var < WikiBuffer
46
51
  elsif self.is_param?
47
52
  ret = nil
48
53
  @options[:buffer].buffers.reverse.each do |b|
49
- ret = b.get_param(params[0],params[1]) if b.instance_of?(WikiBuffer::HTMLElement) && b.element_name == "template"
54
+ ret = b.get_param(params[0].downcase,params[1]) if b.instance_of?(WikiBuffer::HTMLElement) && b.element_name == "template"
50
55
  end
51
56
  ret.to_s
52
57
  else
@@ -62,7 +67,7 @@ class WikiBuffer::Var < WikiBuffer
62
67
  count = 0
63
68
  tag_attr = self.params[1..-1].collect { |p|
64
69
  if p.instance_of?(Hash)
65
- "#{p[:name]}=\"#{p[:value]}\""
70
+ "#{p[:name].downcase}=\"#{p[:value]}\""
66
71
  else
67
72
  count += 1
68
73
  "#{count}=\"#{p}\""
@@ -98,6 +103,17 @@ class WikiBuffer::Var < WikiBuffer
98
103
  rescue RuntimeError
99
104
  "Expression error: #{$!}"
100
105
  end
106
+ when "#ifexpr"
107
+ val = false
108
+ begin
109
+ val = ExpressionParser::Parser.new.parse(params.first)
110
+ rescue RuntimeError
111
+ end
112
+ if val
113
+ params[1]
114
+ else
115
+ params[2]
116
+ end
101
117
  when "#ifeq"
102
118
  if params[0] =~ /^[0-9A-Fa-f]+$/ && params[1] =~ /^[0-9A-Fa-f]+$/
103
119
  params[0].to_i == params[1].to_i ? params[2] : params[3]
@@ -122,6 +138,8 @@ class WikiBuffer::Var < WikiBuffer
122
138
  when "#capture"
123
139
  @options[:params][params.first] = params[1]
124
140
  ""
141
+ when "urlencode"
142
+ URI.escape(params.first, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
125
143
  when "lc"
126
144
  params.first.downcase
127
145
  when "uc"
@@ -132,6 +150,17 @@ class WikiBuffer::Var < WikiBuffer
132
150
  params.first[0,1].downcase + params.first[1,-1]
133
151
  when "plural"
134
152
  params.first.to_i > 1 ? params[1] : params[2]
153
+ when "ns"
154
+ values = { "" => "", "0" => "", "1" => "Talk", "talk" => "Talk", "2" => "User", "user" => "User",
155
+ "3" => "User talk", "user_talk" => "User talk", "4" => "Meta", "project" => "Meta",
156
+ "5" => "Meta talk", "project_talk" => "Meta talk", "6" => "File", "image" => "File",
157
+ "7" => "File talk", "image_talk" => "File talk", "8" => "MediaWiki", "mediawiki" => "MediaWiki",
158
+ "9" => "MediaWiki talk", "mediawiki_talk" => "MediaWiki talk", "10" => "Template",
159
+ "template" => "Template", "11" => "Template talk", "template_talk" => "Template talk",
160
+ "12" => "Help", "help" => "Help", "13" => "Help talk", "help_talk" => "Help talk",
161
+ "14" => "Category", "category" => "Category", "15" => "Category talk", "category_talk" => "Category talk",
162
+ "-2" => "Media", "media" => "Media", "-1" => "Special", "special" => "Special" }
163
+ values[params.first.gsub(/\s+/,'_').downcase]
135
164
  when "debug"
136
165
  ret = nil
137
166
  case params.first
@@ -30,6 +30,10 @@ class WikiBuffer
30
30
  false
31
31
  end
32
32
 
33
+ def skip_links?
34
+ false
35
+ end
36
+
33
37
  def data
34
38
  @data ||= ""
35
39
  end
@@ -58,7 +62,7 @@ class WikiBuffer
58
62
  end
59
63
 
60
64
  def to_s
61
- "<p>" + self.params.join("\n") + "</p>"
65
+ self.params.join("\n")
62
66
  end
63
67
 
64
68
  def check_globals()
@@ -74,7 +78,7 @@ class WikiBuffer
74
78
  @indent = nil
75
79
  return true
76
80
  end
77
- if current_char == " " && @indent.nil? && @buffers[-1].class != WikiBuffer::HTMLElement
81
+ if current_char == " " && @indent.nil? && ![WikiBuffer::HTMLElement,WikiBuffer::Var].include?(@buffers[-1].class)
78
82
  "\n<pre> ".each_char { |c| @buffers[-1].add_char(c) }
79
83
  @indent = @buffers[-1].object_id
80
84
  return true
@@ -103,7 +107,7 @@ class WikiBuffer
103
107
  return true
104
108
 
105
109
  # start link
106
- when current_char == '[' && previous_char != '['
110
+ when current_char == '[' && previous_char != '[' && !@buffers[-1].skip_links?
107
111
  @buffers << WikiBuffer::Link.new("",@options)
108
112
  return true
109
113
 
@@ -217,11 +221,9 @@ class WikiBuffer
217
221
  @list_data = cdata
218
222
  tmp
219
223
  }
220
- self.data += line + "\n"
224
+ self.data += line + ""
221
225
  end
222
226
 
223
- self.data = "</p><p>" if self.data.blank?
224
-
225
227
  self.params << self.data.auto_link
226
228
  self.data = ""
227
229
  else
@@ -81,7 +81,7 @@ class WikiLinkHandler
81
81
  end
82
82
 
83
83
  def url_for(page)
84
- "javascript:void(0)"
84
+ "#{page}"
85
85
  end
86
86
 
87
87
  def link_attributes_for(page)
@@ -99,13 +99,20 @@ class WikiLinkHandler
99
99
  if self.params.has_key?(resource)
100
100
  self.params[resource]
101
101
  else
102
- ret = template(resource)
103
- unless ret.nil?
104
- @included_templates ||= {}
105
- @included_templates[resource] ||= 0
102
+ @template_cache ||= {}
103
+ if @template_cache[resource]
106
104
  @included_templates[resource] += 1
105
+ @template_cache[resource]
106
+ else
107
+ ret = template(resource)
108
+ unless ret.nil?
109
+ @included_templates ||= {}
110
+ @included_templates[resource] ||= 0
111
+ @included_templates[resource] += 1
112
+ end
113
+ @template_cache[resource] = ret
114
+ ret
107
115
  end
108
- ret
109
116
  end
110
117
  end
111
118
 
@@ -146,7 +153,7 @@ class WikiLinkHandler
146
153
  type = nil
147
154
  w = 180
148
155
  h = nil
149
- title = nil
156
+ title = ''
150
157
  ffloat = false
151
158
 
152
159
  options.each do |x|
data/lib/wikicloth.rb CHANGED
@@ -10,7 +10,7 @@ String.send(:include, ExtendedString)
10
10
 
11
11
  module WikiCloth
12
12
 
13
- VERSION = "0.6.2"
13
+ VERSION = "0.6.3"
14
14
 
15
15
  class WikiCloth
16
16
 
@@ -62,7 +62,7 @@ module WikiCloth
62
62
  data << "\n" if data.last(1) != "\n"
63
63
  buffer = WikiBuffer.new("",options)
64
64
  data.each_char { |c| buffer.add_char(c) }
65
- buffer.to_s
65
+ "<p>"+buffer.to_s.gsub(/\n\s*\n/m) { |p| "</p>\n\n<p>" }+"</p>"
66
66
  end
67
67
 
68
68
  def to_html(opt={})
data/run_tests.rb CHANGED
@@ -29,7 +29,7 @@ puts @wiki.to_html
29
29
 
30
30
  @wiki = WikiParser.new({
31
31
  :params => { "PAGENAME" => "Testing123" },
32
- :data => "\n[[Hello World]] From {{ PAGENAME }} on {{ date }}\n"
32
+ :data => "[[Hello World]] From {{ PAGENAME }} on {{ date }}"
33
33
  })
34
34
  puts @wiki.to_html
35
35
 
@@ -27,9 +27,26 @@ class WikiClothTest < ActiveSupport::TestCase
27
27
  test "links and references" do
28
28
  wiki = WikiCloth::Parser.new(:data => File.open(File.join(File.dirname(__FILE__), '../sample_documents/george_washington.wiki'), READ_MODE) { |f| f.read })
29
29
  data = wiki.to_html
30
- assert wiki.external_links.size == 41
30
+ assert wiki.external_links.size == 38
31
31
  assert wiki.references.size == 76
32
- assert wiki.internal_links.size == 560
32
+ assert wiki.internal_links.size == 450
33
+ end
34
+
35
+ test "Embedded images with no explicit title" do
36
+ wiki = WikiParser.new(:data => "[[Image:Rectangular coordinates.svg|left|thumb|250px]]")
37
+ test = true
38
+ begin
39
+ data = wiki.to_html
40
+ rescue
41
+ test = false
42
+ end
43
+ assert test == true
44
+ end
45
+
46
+ test "First item in list not created when list is preceded by a heading" do
47
+ wiki = WikiParser.new(:data => "=Heading=\n* One\n* Two\n* Three")
48
+ data = wiki.to_html
49
+ assert data !~ /\*/
33
50
  end
34
51
 
35
52
  test "behavior switch should not show up in the html output" do
@@ -62,12 +79,6 @@ EOS
62
79
  assert data !~ /\]/
63
80
  end
64
81
 
65
- test "external links without a http:// prefix" do
66
- wiki = WikiCloth::Parser.new(:data => "[www.google.com]")
67
- data = wiki.to_html
68
- assert data =~ /http/
69
- end
70
-
71
82
  test "auto pre at end of document" do
72
83
  wiki = WikiParser.new(:data => "test\n\n hello\n world\nend")
73
84
  data = wiki.to_html
@@ -162,10 +173,10 @@ EOS
162
173
  test "disable edit stuff" do
163
174
  wiki = WikiParser.new(:data => "= Hallo =")
164
175
  data = wiki.to_html
165
- assert_equal data, "<p>\n<h1><span class=\"editsection\">&#91;<a href=\"?section=Hallo\" title=\"Edit section: Hallo\">edit</a>&#93;</span> <span class=\"mw-headline\" id=\"Hallo\"><a name=\"Hallo\">Hallo</a></span></h1>\n</p>"
176
+ assert_equal data, "<p>\n<h1><span class=\"editsection\">&#91;<a href=\"?section=Hallo\" title=\"Edit section: Hallo\">edit</a>&#93;</span> <span class=\"mw-headline\" id=\"Hallo\"><a name=\"Hallo\">Hallo</a></span></h1></p>"
166
177
 
167
178
  data = wiki.to_html(:noedit => true)
168
- assert_equal data, "<p>\n<h1><span class=\"mw-headline\" id=\"Hallo\"><a name=\"Hallo\">Hallo</a></span></h1>\n</p>"
179
+ assert_equal data, "<p>\n<h1><span class=\"mw-headline\" id=\"Hallo\"><a name=\"Hallo\">Hallo</a></span></h1></p>"
169
180
 
170
181
  end
171
182
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wikicloth
3
3
  version: !ruby/object:Gem::Version
4
- hash: 3
4
+ hash: 1
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 6
9
- - 2
10
- version: 0.6.2
9
+ - 3
10
+ version: 0.6.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - David Ricciardi
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-01-06 00:00:00 +00:00
18
+ date: 2011-01-31 00:00:00 +00:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency