]> git.wincent.com - wikitext.git/commitdiff
Add :pre_code option
authorWincent Colaiuta <win@wincent.com>
Mon, 14 Dec 2015 17:48:43 +0000 (09:48 -0800)
committerWincent Colaiuta <win@wincent.com>
Mon, 14 Dec 2015 17:48:43 +0000 (09:48 -0800)
Closes: https://github.com/wincent/wikitext/issues/2
doc/rdoc.rb
ext/wikitext/parser.c
spec/pre_spec.rb

index 51cc77a6c136f25d2f71807220794a28abb7444a..7db674293081143aa98dd10c6ea6c6dc376c2638 100644 (file)
@@ -119,6 +119,23 @@ module Wikitext
   #     !!!html
   #     http://apple.com/
   #
   #     !!!html
   #     http://apple.com/
   #
+  # == +pre_code+ (boolean)
+  #
+  # When true, "pre" blocks are formatted using "code" elements. For example:
+  #
+  #     !!!wikitext
+  #     <pre>foo</pre>
+  #
+  # Produces:
+  #
+  #     !!!html
+  #     <pre><code>foo</code></pre>
+  #
+  # When +false+ (the default), it produces:
+  #
+  #     !!!html
+  #     <pre>foo</pre>
+  #
   # == +space_to_underscore+ (boolean)
   #
   # Whether spaces in link targets should be encoded normally or transformed
   # == +space_to_underscore+ (boolean)
   #
   # Whether spaces in link targets should be encoded normally or transformed
index ab327357a41e5b7a994a0940866b27e5cb133f00..3a9ca65ce11ac4754121ccf58618ec7d3226706d 100644 (file)
@@ -61,6 +61,7 @@ typedef struct
     bool    pending_crlf;
     bool    autolink;
     bool    space_to_underscore;
     bool    pending_crlf;
     bool    autolink;
     bool    space_to_underscore;
+    bool    pre_code;
 } parser_t;
 
 const char null_str[]                   = { 0 };
 } parser_t;
 
 const char null_str[]                   = { 0 };
@@ -165,6 +166,7 @@ parser_t *parser_new(void)
     parser->pending_crlf            = false;
     parser->autolink                = true;
     parser->space_to_underscore     = true;
     parser->pending_crlf            = false;
     parser->autolink                = true;
     parser->space_to_underscore     = true;
+    parser->pre_code                = false;
     return parser;
 }
 
     return parser;
 }
 
@@ -525,6 +527,8 @@ void wiki_append_pre_start(parser_t *parser, token_t *token)
     }
     else
         str_append(parser->output, pre_start, sizeof(pre_start) - 1);
     }
     else
         str_append(parser->output, pre_start, sizeof(pre_start) - 1);
+    if (parser->pre_code)
+        str_append(parser->output, code_start, sizeof(code_start) - 1);
     ary_push(parser->scope, PRE_START);
     ary_push(parser->line, PRE_START);
 }
     ary_push(parser->scope, PRE_START);
     ary_push(parser->line, PRE_START);
 }
@@ -565,6 +569,8 @@ void wiki_pop_from_stack(parser_t *parser, str_t *target)
     {
         case PRE:
         case PRE_START:
     {
         case PRE:
         case PRE_START:
+            if (parser->pre_code)
+                str_append(target, code_end, sizeof(code_end) - 1);
             str_append(target, pre_end, sizeof(pre_end) - 1);
             str_append_str(target, parser->line_ending);
             wiki_dedent(parser, false);
             str_append(target, pre_end, sizeof(pre_end) - 1);
             str_append_str(target, parser->line_ending);
             wiki_dedent(parser, false);
@@ -1013,6 +1019,7 @@ VALUE Wikitext_parser_initialize(int argc, VALUE *argv, VALUE self)
     VALUE img_prefix                    = rb_str_new2("/images/");
     VALUE output_style                  = ID2SYM(rb_intern("html"));
     VALUE space_to_underscore           = Qtrue;
     VALUE img_prefix                    = rb_str_new2("/images/");
     VALUE output_style                  = ID2SYM(rb_intern("html"));
     VALUE space_to_underscore           = Qtrue;
+    VALUE pre_code                      = Qfalse;
     VALUE minimum_fulltext_token_length = INT2NUM(3);
     VALUE base_heading_level            = INT2NUM(0);
 
     VALUE minimum_fulltext_token_length = INT2NUM(3);
     VALUE base_heading_level            = INT2NUM(0);
 
@@ -1031,6 +1038,7 @@ VALUE Wikitext_parser_initialize(int argc, VALUE *argv, VALUE self)
         img_prefix                      = OVERRIDE_IF_SET(img_prefix);
         output_style                    = OVERRIDE_IF_SET(output_style);
         space_to_underscore             = OVERRIDE_IF_SET(space_to_underscore);
         img_prefix                      = OVERRIDE_IF_SET(img_prefix);
         output_style                    = OVERRIDE_IF_SET(output_style);
         space_to_underscore             = OVERRIDE_IF_SET(space_to_underscore);
+        pre_code                        = OVERRIDE_IF_SET(pre_code);
         minimum_fulltext_token_length   = OVERRIDE_IF_SET(minimum_fulltext_token_length);
         base_heading_level              = OVERRIDE_IF_SET(base_heading_level);
     }
         minimum_fulltext_token_length   = OVERRIDE_IF_SET(minimum_fulltext_token_length);
         base_heading_level              = OVERRIDE_IF_SET(base_heading_level);
     }
@@ -1046,6 +1054,7 @@ VALUE Wikitext_parser_initialize(int argc, VALUE *argv, VALUE self)
     rb_iv_set(self, "@img_prefix",                      img_prefix);
     rb_iv_set(self, "@output_style",                    output_style);
     rb_iv_set(self, "@space_to_underscore",             space_to_underscore);
     rb_iv_set(self, "@img_prefix",                      img_prefix);
     rb_iv_set(self, "@output_style",                    output_style);
     rb_iv_set(self, "@space_to_underscore",             space_to_underscore);
+    rb_iv_set(self, "@pre_code",                        pre_code);
     rb_iv_set(self, "@minimum_fulltext_token_length",   minimum_fulltext_token_length);
     rb_iv_set(self, "@base_heading_level",              base_heading_level);
     return self;
     rb_iv_set(self, "@minimum_fulltext_token_length",   minimum_fulltext_token_length);
     rb_iv_set(self, "@base_heading_level",              base_heading_level);
     return self;
@@ -1150,6 +1159,7 @@ VALUE Wikitext_parser_parse(int argc, VALUE *argv, VALUE self)
     parser->img_prefix              = rb_iv_get(self, "@img_prefix");
     parser->autolink                = rb_iv_get(self, "@autolink") == Qtrue ? true : false;
     parser->space_to_underscore     = rb_iv_get(self, "@space_to_underscore") == Qtrue ? true : false;
     parser->img_prefix              = rb_iv_get(self, "@img_prefix");
     parser->autolink                = rb_iv_get(self, "@autolink") == Qtrue ? true : false;
     parser->space_to_underscore     = rb_iv_get(self, "@space_to_underscore") == Qtrue ? true : false;
+    parser->pre_code                = rb_iv_get(self, "@pre_code") == Qtrue ? true : false;
     parser->line_ending             = str_new_from_string(line_ending);
     parser->base_indent             = base_indent;
     parser->base_heading_level      = base_heading_level;
     parser->line_ending             = str_new_from_string(line_ending);
     parser->base_indent             = base_indent;
     parser->base_heading_level      = base_heading_level;
@@ -1248,6 +1258,8 @@ VALUE Wikitext_parser_parse(int argc, VALUE *argv, VALUE self)
                     wiki_pop_from_stack_up_to(parser, NULL, BLOCKQUOTE, false);
                     wiki_indent(parser);
                     str_append(parser->output, pre_start, sizeof(pre_start) - 1);
                     wiki_pop_from_stack_up_to(parser, NULL, BLOCKQUOTE, false);
                     wiki_indent(parser);
                     str_append(parser->output, pre_start, sizeof(pre_start) - 1);
+                    if (parser->pre_code)
+                        str_append(parser->output, code_start, sizeof(code_start) - 1);
                     ary_push(parser->scope, PRE);
                 }
                 break;
                     ary_push(parser->scope, PRE);
                 }
                 break;
index 39aac5b1524dd610f7e714772caf4b78209fb426..5b047a6b8c12d855114d1203b8ea069c0e476da4 100644 (file)
@@ -54,6 +54,13 @@ describe Wikitext::Parser, 'parsing PRE blocks' do
     @parser.parse(' ').should == "<pre></pre>\n"
   end
 
     @parser.parse(' ').should == "<pre></pre>\n"
   end
 
+  it 'assumes PRE blocks are code when :pre_code is set' do
+    Wikitext::Parser.
+      new(:pre_code => true).
+      parse(' foo').
+      should == "<pre><code>foo</code></pre>\n"
+  end
+
   it 'should sanely handle a leading empty line' do
     @parser.parse(" \n foo").should == "<pre>\nfoo</pre>\n"
   end
   it 'should sanely handle a leading empty line' do
     @parser.parse(" \n foo").should == "<pre>\nfoo</pre>\n"
   end
@@ -148,6 +155,13 @@ describe Wikitext::Parser, 'parsing PRE_START/PRE_END blocks' do
     @parser.parse('<pre>foo</pre>').should == "<pre>foo</pre>\n"
   end
 
     @parser.parse('<pre>foo</pre>').should == "<pre>foo</pre>\n"
   end
 
+  it 'assumes PRE_START/PRE_END blocks are code when :pre_code is set' do
+    Wikitext::Parser.
+      new(:pre_code => true).
+      parse('<pre>foo</pre>').
+      should == "<pre><code>foo</code></pre>\n"
+  end
+
   it 'should pass through PRE unchanged in PRE_START/PRE_END blocks' do
     input = dedent <<-END
       <pre>line 1
   it 'should pass through PRE unchanged in PRE_START/PRE_END blocks' do
     input = dedent <<-END
       <pre>line 1