wikitext 0.3 → 0.4

Sign up to get free protection for your applications and to get access to all the features.
data/spec/img_spec.rb ADDED
@@ -0,0 +1,176 @@
1
+ #!/usr/bin/env ruby
2
+ # Copyright 2008 Wincent Colaiuta
3
+ # This program is free software: you can redistribute it and/or modify
4
+ # it under the terms of the GNU General Public License as published by
5
+ # the Free Software Foundation, either version 3 of the License, or
6
+ # (at your option) any later version.
7
+ #
8
+ # This program is distributed in the hope that it will be useful,
9
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
+ # GNU General Public License for more details.
12
+ #
13
+ # You should have received a copy of the GNU General Public License
14
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
15
+
16
+ require File.join(File.dirname(__FILE__), 'spec_helper.rb')
17
+ require 'wikitext'
18
+
19
+ describe Wikitext::Parser, 'embedding img tags' do
20
+ before do
21
+ @parser = Wikitext::Parser.new
22
+ end
23
+
24
+ it 'should convert valid markup into inline image tags' do
25
+ @parser.parse('{{foo.png}}').should == %Q{<p><img src="/images/foo.png" alt="foo.png" /></p>\n}
26
+ end
27
+
28
+ it 'should appear embedded in an inline flow' do
29
+ @parser.parse('before {{foo.png}} after').should == %Q{<p>before <img src="/images/foo.png" alt="foo.png" /> after</p>\n}
30
+ end
31
+
32
+ it 'should allow images in subdirectories' do
33
+ @parser.parse('{{foo/bar.png}}').should == %Q{<p><img src="/images/foo/bar.png" alt="foo/bar.png" /></p>\n}
34
+ end
35
+
36
+ it 'should work in BLOCKQUOTE blocks' do
37
+ expected = dedent <<-END
38
+ <blockquote>
39
+ <p><img src="/images/foo.png" alt="foo.png" /></p>
40
+ </blockquote>
41
+ END
42
+ @parser.parse('> {{foo.png}}').should == expected
43
+ end
44
+
45
+ it 'should work in unordered lists' do
46
+ input = dedent <<-END
47
+ * {{foo.png}}
48
+ * {{bar.png}}
49
+ * {{baz.png}}
50
+ END
51
+ expected = dedent <<-END
52
+ <ul>
53
+ <li><img src="/images/foo.png" alt="foo.png" /></li>
54
+ <li><img src="/images/bar.png" alt="bar.png" /></li>
55
+ <li><img src="/images/baz.png" alt="baz.png" /></li>
56
+ </ul>
57
+ END
58
+ @parser.parse(input).should == expected
59
+ end
60
+
61
+ it 'should work in ordered lists' do
62
+ input = dedent <<-END
63
+ # {{foo.png}}
64
+ # {{bar.png}}
65
+ # {{baz.png}}
66
+ END
67
+ expected = dedent <<-END
68
+ <ol>
69
+ <li><img src="/images/foo.png" alt="foo.png" /></li>
70
+ <li><img src="/images/bar.png" alt="bar.png" /></li>
71
+ <li><img src="/images/baz.png" alt="baz.png" /></li>
72
+ </ol>
73
+ END
74
+ @parser.parse(input).should == expected
75
+ end
76
+
77
+ it 'should work in <h1> headings' do
78
+ @parser.parse('= {{foo.png}} =').should == %Q{<h1><img src="/images/foo.png" alt="foo.png" /></h1>\n}
79
+ end
80
+
81
+ it 'should work in <h2> headings' do
82
+ @parser.parse('== {{foo.png}} ==').should == %Q{<h2><img src="/images/foo.png" alt="foo.png" /></h2>\n}
83
+ end
84
+
85
+ it 'should work in <h3> headings' do
86
+ @parser.parse('=== {{foo.png}} ===').should == %Q{<h3><img src="/images/foo.png" alt="foo.png" /></h3>\n}
87
+ end
88
+
89
+ it 'should work in <h4> headings' do
90
+ @parser.parse('==== {{foo.png}} ====').should == %Q{<h4><img src="/images/foo.png" alt="foo.png" /></h4>\n}
91
+ end
92
+
93
+ it 'should work in <h5> headings' do
94
+ @parser.parse('===== {{foo.png}} =====').should == %Q{<h5><img src="/images/foo.png" alt="foo.png" /></h5>\n}
95
+ end
96
+
97
+ it 'should work in <h6> headings' do
98
+ @parser.parse('====== {{foo.png}} ======').should == %Q{<h6><img src="/images/foo.png" alt="foo.png" /></h6>\n}
99
+ end
100
+
101
+ it 'should pass single curly braces through unaltered' do
102
+ @parser.parse('{foo.png}').should == %Q{<p>{foo.png}</p>\n}
103
+ end
104
+
105
+ it 'should have no effect inside PRE blocks' do
106
+ @parser.parse(' {{foo.png}}').should == %Q{<pre>{{foo.png}}</pre>\n}
107
+ end
108
+
109
+ it 'should have no effect inside PRE_START blocks' do
110
+ @parser.parse('<pre>{{foo.png}}</pre>').should == %Q{<pre>{{foo.png}}</pre>\n}
111
+ end
112
+
113
+ it 'should have no effect inside NO_WIKI spans' do
114
+ @parser.parse('<nowiki>{{foo.png}}</nowiki>').should == %Q{<p>{{foo.png}}</p>\n}
115
+ end
116
+
117
+ it 'should be passed through in internal link targets' do
118
+ @parser.parse('[[{{foo.png}}]]').should == %Q{<p><a href="/wiki/%7b%7bfoo.png%7d%7d">{{foo.png}}</a></p>\n}
119
+ end
120
+
121
+ it 'should be passed through in internal link text' do
122
+ @parser.parse('[[article|{{foo.png}}]]').should == %Q{<p><a href="/wiki/article">{{foo.png}}</a></p>\n}
123
+ end
124
+
125
+ it 'should not be allowed as an external link target' do
126
+ expected = %Q{<p>[<img src="/images/foo.png" alt="foo.png" /> the link]</p>\n}
127
+ @parser.parse('[{{foo.png}} the link]').should == expected
128
+ end
129
+
130
+ it 'should be passed through in external link text' do
131
+ expected = %Q{<p><a href="http://example.com/" class="external">{{foo.png}}</a></p>\n}
132
+ @parser.parse('[http://example.com/ {{foo.png}}]').should == expected
133
+ end
134
+
135
+ it 'should not allow embedded quotes' do
136
+ @parser.parse('{{"fun".png}}').should == %Q{<p>{{&quot;fun&quot;.png}}</p>\n}
137
+ end
138
+
139
+ it 'should not allow embedded spaces' do
140
+ @parser.parse('{{foo bar.png}}').should == %Q{<p>{{foo bar.png}}</p>\n}
141
+ end
142
+
143
+ it 'should not allow characters beyond printable ASCII' do
144
+ @parser.parse('{{500€.png}}').should == %Q{<p>{{500&#x20ac;.png}}</p>\n}
145
+ end
146
+
147
+ it 'should allow overrides of the image prefix at initialization time' do
148
+ parser = Wikitext::Parser.new(:img_prefix => '/gfx/')
149
+ parser.parse('{{foo.png}}').should == %Q{<p><img src="/gfx/foo.png" alt="foo.png" /></p>\n}
150
+ end
151
+
152
+ it 'should suppress the image prefix if passed an empty string at initialization time' do
153
+ parser = Wikitext::Parser.new(:img_prefix => '')
154
+ parser.parse('{{foo.png}}').should == %Q{<p><img src="foo.png" alt="foo.png" /></p>\n}
155
+ end
156
+
157
+ it 'should suppress image prefix if passed nil at initialization time' do
158
+ parser = Wikitext::Parser.new(:img_prefix => nil)
159
+ parser.parse('{{foo.png}}').should == %Q{<p><img src="foo.png" alt="foo.png" /></p>\n}
160
+ end
161
+
162
+ it 'should allow overrides of the image prefix after initialization' do
163
+ @parser.img_prefix = '/gfx/'
164
+ @parser.parse('{{foo.png}}').should == %Q{<p><img src="/gfx/foo.png" alt="foo.png" /></p>\n}
165
+ end
166
+
167
+ it 'should suppress image if prefix set to an empty string after initialization' do
168
+ @parser.img_prefix = ''
169
+ @parser.parse('{{foo.png}}').should == %Q{<p><img src="foo.png" alt="foo.png" /></p>\n}
170
+ end
171
+
172
+ it 'should suppress image if prefix set to nil after initialization' do
173
+ @parser.img_prefix = nil
174
+ @parser.parse('{{foo.png}}').should == %Q{<p><img src="foo.png" alt="foo.png" /></p>\n}
175
+ end
176
+ end
@@ -90,6 +90,8 @@ describe Wikitext::Parser, 'with large slab of input text' do
90
90
 
91
91
  a normal paragraph again
92
92
 
93
+ {{an_image.png}}
94
+
93
95
  > This is another blockquote which demonstrates that we
94
96
  > can nest other structures inside of it. For example, here
95
97
  > we have a code sample:
@@ -194,6 +196,7 @@ describe Wikitext::Parser, 'with large slab of input text' do
194
196
  which would '''otherwise''' have &lt;tt&gt;special&lt;/tt&gt; meaning
195
197
  although explicit entities &copy; are passed through unchanged</pre>
196
198
  <p>a normal paragraph again</p>
199
+ <p><img src="/images/an_image.png" alt="an_image.png" /></p>
197
200
  <blockquote>
198
201
  <p>This is another blockquote which demonstrates that we can nest other structures inside of it. For example, here we have a code sample:</p>
199
202
  <pre>line 1
@@ -101,9 +101,10 @@ describe Wikitext::Parser, 'tokenizing' do
101
101
 
102
102
  it 'should be able to tokenize strings containing "}"' do
103
103
  # was a bug: we were throwing an exception "failed before finding a token" because our PRINTABLE rule omitted this code point
104
+ # later on the "}" became the RIGHT_CURLY token, so the spec has been modified accordingly
104
105
  lambda { @tokens = @parser.tokenize('}') }.should_not raise_error
105
106
  @tokens.length.should == 2
106
- @tokens[0].token_type.should == :printable
107
+ @tokens[0].token_type.should == :right_curly # was PRINTABLE
107
108
  @tokens[0].string_value.should == '}'
108
109
  @tokens[0].line_start.should == 1
109
110
  @tokens[0].column_start.should == 1
@@ -41,6 +41,10 @@ describe Wikitext::Parser do
41
41
  @parser.internal_link_prefix.should == '/wiki/'
42
42
  end
43
43
 
44
+ it 'should use "/images/" as default img prefix' do
45
+ @parser.img_prefix.should == '/images/'
46
+ end
47
+
44
48
  it 'should turn space-to-underscore off by default' do
45
49
  @parser.space_to_underscore.should == false
46
50
  end
@@ -70,6 +74,12 @@ describe Wikitext::Parser do
70
74
  Wikitext::Parser.new(:internal_link_prefix => '/baz/').internal_link_prefix.should == '/baz/'
71
75
  end
72
76
 
77
+ it 'should allow overriding of the img prefix' do
78
+ Wikitext::Parser.new(:img_prefix => '/bar/').img_prefix.should == '/bar/'
79
+ Wikitext::Parser.new(:img_prefix => '').img_prefix.should == ''
80
+ Wikitext::Parser.new(:img_prefix => nil).img_prefix.should == nil
81
+ end
82
+
73
83
  it 'should allow overriding of space-to-underscore' do
74
84
  Wikitext::Parser.new(:space_to_underscore => true).space_to_underscore.should == true
75
85
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wikitext
3
3
  version: !ruby/object:Gem::Version
4
- version: "0.3"
4
+ version: "0.4"
5
5
  platform: ruby
6
6
  authors:
7
7
  - Wincent Colaiuta
@@ -34,6 +34,7 @@ files:
34
34
  - spec/h4_spec.rb
35
35
  - spec/h5_spec.rb
36
36
  - spec/h6_spec.rb
37
+ - spec/img_spec.rb
37
38
  - spec/indentation_spec.rb
38
39
  - spec/integration_spec.rb
39
40
  - spec/internal_link_spec.rb