wikitext 1.5.3 → 1.6

Sign up to get free protection for your applications and to get access to all the features.
data/ext/parser.c CHANGED
@@ -237,7 +237,7 @@ VALUE _Wikitext_hyperlink(parser_t *parser, VALUE link_prefix, VALUE link_target
237
237
  rb_str_append(string, link_prefix);
238
238
  rb_str_append(string, link_target);
239
239
 
240
- /* special handling for mailto URIs */
240
+ // special handling for mailto URIs
241
241
  const char *mailto = "mailto:";
242
242
  if (NIL_P(link_prefix) &&
243
243
  RSTRING_LEN(link_target) >= (long)sizeof(mailto) &&
@@ -258,7 +258,7 @@ VALUE _Wikitext_hyperlink(parser_t *parser, VALUE link_prefix, VALUE link_target
258
258
  void _Wikitext_append_img(parser_t *parser, char *token_ptr, int token_len)
259
259
  {
260
260
  rb_str_cat(parser->output, img_start, sizeof(img_start) - 1); // <img src="
261
- if (!NIL_P(parser->img_prefix))
261
+ if (!NIL_P(parser->img_prefix) && *token_ptr != '/') // len always > 0
262
262
  rb_str_append(parser->output, parser->img_prefix);
263
263
  rb_str_cat(parser->output, token_ptr, token_len);
264
264
  rb_str_cat(parser->output, img_alt, sizeof(img_alt) - 1); // " alt="
@@ -271,6 +271,8 @@ void _Wikitext_append_img(parser_t *parser, char *token_ptr, int token_len)
271
271
  // each time we enter one of those spans must ++ the indentation level
272
272
  void _Wikitext_indent(parser_t *parser)
273
273
  {
274
+ if (parser->base_indent == -1) // indentation disabled
275
+ return;
274
276
  int space_count = parser->current_indent + parser->base_indent;
275
277
  if (space_count > 0)
276
278
  {
@@ -290,6 +292,8 @@ void _Wikitext_indent(parser_t *parser)
290
292
 
291
293
  void _Wikitext_dedent(parser_t *parser, VALUE emit)
292
294
  {
295
+ if (parser->base_indent == -1) // indentation disabled
296
+ return;
293
297
  parser->current_indent -= 2;
294
298
  if (emit != Qtrue)
295
299
  return;
@@ -990,9 +994,15 @@ VALUE Wikitext_parser_parse(int argc, VALUE *argv, VALUE self)
990
994
  // :indent => 0 (or more)
991
995
  if (rb_funcall(options, rb_intern("has_key?"), 1, ID2SYM(rb_intern("indent"))) == Qtrue)
992
996
  {
993
- base_indent = NUM2INT(rb_hash_aref(options, ID2SYM(rb_intern("indent"))));
994
- if (base_indent < 0)
995
- base_indent = 0;
997
+ VALUE indent = rb_hash_aref(options, ID2SYM(rb_intern("indent")));
998
+ if (indent == Qfalse)
999
+ base_indent = -1; // indentation disabled
1000
+ else
1001
+ {
1002
+ base_indent = NUM2INT(indent);
1003
+ if (base_indent < 0)
1004
+ base_indent = 0;
1005
+ }
996
1006
  }
997
1007
 
998
1008
  // :base_heading_level => 0/1/2/3/4/5/6
@@ -2416,14 +2426,14 @@ VALUE Wikitext_parser_parse(int argc, VALUE *argv, VALUE self)
2416
2426
  {
2417
2427
  if (type == PATH || type == PRINTABLE || type == ALNUM || type == SPECIAL_URI_CHARS)
2418
2428
  rb_str_cat(parser->link_target, token->start, TOKEN_LEN(token));
2419
- else if (type == IMG_END)
2429
+ else if (type == IMG_END && RSTRING_LEN(parser->link_target) > 0)
2420
2430
  {
2421
2431
  // success
2422
2432
  _Wikitext_append_img(parser, RSTRING_PTR(parser->link_target), RSTRING_LEN(parser->link_target));
2423
2433
  token = NULL;
2424
2434
  break;
2425
2435
  }
2426
- else // unexpected token (syntax error)
2436
+ else // unexpected token or zero-length target (syntax error)
2427
2437
  {
2428
2438
  // rollback
2429
2439
  rb_str_cat(parser->output, literal_img_start, sizeof(literal_img_start) - 1);
@@ -21,7 +21,8 @@
21
21
  # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
22
22
  # POSSIBILITY OF SUCH DAMAGE.
23
23
 
24
- require 'wikitext'
24
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'ext',
25
+ 'wikitext'))
25
26
 
26
27
  class NilClass
27
28
  def to_wikitext
@@ -22,7 +22,8 @@
22
22
  # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
23
23
  # POSSIBILITY OF SUCH DAMAGE.
24
24
 
25
- require 'wikitext'
25
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'ext',
26
+ 'wikitext'))
26
27
 
27
28
  module Wikitext
28
29
  class Parser
@@ -21,7 +21,7 @@
21
21
  # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
22
22
  # POSSIBILITY OF SUCH DAMAGE.
23
23
 
24
- require 'wikitext/string'
24
+ require File.expand_path(File.join(File.dirname(__FILE__), 'string'))
25
25
 
26
26
  module Wikitext
27
27
  class TemplateHandler
@@ -21,11 +21,13 @@
21
21
  # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
22
22
  # POSSIBILITY OF SUCH DAMAGE.
23
23
 
24
- require 'wikitext/parser'
24
+ require File.expand_path(File.join(File.dirname(__FILE__), 'parser'))
25
25
 
26
26
  class String
27
27
  def to_wikitext options = {}
28
- Wikitext::Parser.shared_parser.parse wikitext_preprocess, options
28
+ default_options = { :indent => false }
29
+ Wikitext::Parser.shared_parser.parse wikitext_preprocess,
30
+ default_options.merge(options)
29
31
  end
30
32
  alias :w :to_wikitext
31
33
 
@@ -22,5 +22,5 @@
22
22
  # POSSIBILITY OF SUCH DAMAGE.
23
23
 
24
24
  module Wikitext
25
- VERSION = '1.5.3'
25
+ VERSION = '1.6'
26
26
  end # module Wikitext
data/rails/init.rb CHANGED
@@ -24,7 +24,8 @@
24
24
  # Avoid Rails bug #2266 by not requiring during "rake gems:build"
25
25
  # See: https://rails.lighthouseapp.com/projects/8994/tickets/2266
26
26
  unless $gems_build_rake_task
27
- require 'wikitext/nil_class'
28
- require 'wikitext/string'
29
- require 'wikitext/rails'
27
+ libdir = File.join(File.dirname(__FILE__), '..', 'lib', 'wikitext')
28
+ require File.expand_path(File.join(libdir, 'nil_class'))
29
+ require File.expand_path(File.join(libdir, 'string'))
30
+ require File.expand_path(File.join(libdir, 'rails'))
30
31
  end
data/spec/img_spec.rb CHANGED
@@ -43,6 +43,15 @@ describe Wikitext::Parser, 'embedding img tags' do
43
43
  @parser.parse('{{foo/bar.png}}').should == %Q{<p><img src="/images/foo/bar.png" alt="foo/bar.png" /></p>\n}
44
44
  end
45
45
 
46
+ it 'should pass through empty image tags unchanged' do
47
+ @parser.parse('{{}}').should == %Q{<p>{{}}</p>\n}
48
+ end
49
+
50
+ it 'should not append prefix if img src starts with a slash' do
51
+ @parser.parse('{{/foo.png}}').should ==
52
+ %Q{<p><img src="/foo.png" alt="/foo.png" /></p>\n}
53
+ end
54
+
46
55
  it 'should work in BLOCKQUOTE blocks' do
47
56
  expected = dedent <<-END
48
57
  <blockquote>
@@ -31,6 +31,10 @@ def indent spaces, string
31
31
  string.gsub /^/, ' ' * spaces
32
32
  end
33
33
 
34
+ def nodent string
35
+ string.gsub /^ */, ''
36
+ end
37
+
34
38
  describe Wikitext::Parser, 'indentation' do
35
39
  before do
36
40
  @parser = Wikitext::Parser.new
@@ -43,34 +47,38 @@ describe Wikitext::Parser, 'indentation' do
43
47
  end
44
48
 
45
49
  it 'should default to no additional indentation' do
46
- @parser.parse('* foo').should == @default_output
50
+ @parser.parse(@input).should == @default_output
47
51
  end
48
52
 
49
53
  it 'should add additional indentation as indicated by the "indent" option' do
50
- @parser.parse('* foo', :indent => 1).should == indent(1, @default_output)
51
- @parser.parse('* foo', :indent => 2).should == indent(2, @default_output)
52
- @parser.parse('* foo', :indent => 3).should == indent(3, @default_output)
53
- @parser.parse('* foo', :indent => 4).should == indent(4, @default_output)
54
- @parser.parse('* foo', :indent => 5).should == indent(5, @default_output)
55
- @parser.parse('* foo', :indent => 6).should == indent(6, @default_output)
54
+ @parser.parse(@input, :indent => 1).should == indent(1, @default_output)
55
+ @parser.parse(@input, :indent => 2).should == indent(2, @default_output)
56
+ @parser.parse(@input, :indent => 3).should == indent(3, @default_output)
57
+ @parser.parse(@input, :indent => 4).should == indent(4, @default_output)
58
+ @parser.parse(@input, :indent => 5).should == indent(5, @default_output)
59
+ @parser.parse(@input, :indent => 6).should == indent(6, @default_output)
60
+ end
61
+
62
+ it 'should perform no indentation when "indent" is "false"' do
63
+ @parser.parse(@input, :indent => false).should == nodent(@default_output)
56
64
  end
57
65
 
58
66
  it 'should complain if the "indent" option is nil' do
59
- lambda { @parser.parse('* foo', :indent => nil) }.should raise_error(TypeError)
67
+ lambda { @parser.parse(@input, :indent => nil) }.should raise_error(TypeError)
60
68
  end
61
69
 
62
70
  it 'should complain if the "indent" options is not an integer' do
63
- lambda { @parser.parse('* foo', :indent => 'bar') }.should raise_error(TypeError)
64
- lambda { @parser.parse('* foo', :indent => /baz/) }.should raise_error(TypeError)
71
+ lambda { @parser.parse(@input, :indent => 'bar') }.should raise_error(TypeError)
72
+ lambda { @parser.parse(@input, :indent => /baz/) }.should raise_error(TypeError)
65
73
  end
66
74
 
67
75
  it 'should treat a negative "indent" as though it were zero' do
68
- @parser.parse('* foo', :indent => -4).should == @default_output
76
+ @parser.parse(@input, :indent => -4).should == @default_output
69
77
  end
70
78
 
71
79
  it 'should coerce a float "indent" into an integer' do
72
- @parser.parse('* foo', :indent => 0.0).should == @default_output
73
- @parser.parse('* foo', :indent => 2.0).should == <<-END
80
+ @parser.parse(@input, :indent => 0.0).should == @default_output
81
+ @parser.parse(@input, :indent => 2.0).should == <<-END
74
82
  <ul>
75
83
  <li>foo</li>
76
84
  </ul>
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: 1.5.3
4
+ version: "1.6"
5
5
  platform: ruby
6
6
  authors:
7
7
  - Wincent Colaiuta
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-04-28 00:00:00 +02:00
12
+ date: 2009-05-07 00:00:00 +02:00
13
13
  default_executable:
14
14
  dependencies: []
15
15