]> git.wincent.com - wikitext.git/commitdiff
Backend support for HTML5/XML output styles
authorWincent Colaiuta <win@wincent.com>
Sun, 13 Jun 2010 09:29:15 +0000 (11:29 +0200)
committerWincent Colaiuta <win@wincent.com>
Sun, 13 Jun 2010 09:29:15 +0000 (11:29 +0200)
HTML5 has two output syntaxes, HTML and XML. Under HTML5 the HTML output
syntax is likely to be predominant, given that the XML syntax can only
be used if it is served with an appropriate MIME type (ie.
application/xml), and the vast majority of deployed web servers are
configured to use a text/html MIME type.

So, this dictates that for our 2.0 release we are going to default to
emitting HTML syntax, but allow the user to override the default and
emit XML syntax if required.

In practice, the only place in the code base where this necessitates a
change is the part where we emit "img" tags. In the XML syntax, these
tags should be self-closing; ie:

  <img src="foo.png" alt="Foo!" />

Whereas in the HTML syntax they should not (even if all user agents
accept them anyway); ie:

  <img src="foo.png" alt="Foo!">

Signed-off-by: Wincent Colaiuta <win@wincent.com>
ext/parser.c
spec/img_spec.rb
spec/integration_spec.rb
spec/parser_spec.rb

index 0363a85e1f45301a8528880ab388805bfe7c7122..b6fd5a5b6badd8f58a752ddfea16039551e739f9 100644 (file)
@@ -129,7 +129,8 @@ const char escaped_blockquote[]         = "&gt; ";
 const char ext_link_end[]               = "]";
 const char literal_img_start[]          = "{{";
 const char img_start[]                  = "<img src=\"";
-const char img_end[]                    = "\" />";
+const char img_end_xml[]                = "\" />";
+const char img_end_html[]               = "\">";
 const char img_alt[]                    = "\" alt=\"";
 const char pre_class_start[]            = "<pre class=\"";
 const char pre_class_end[]              = "-syntax\">";
@@ -315,13 +316,16 @@ void wiki_append_hyperlink(parser_t *parser, VALUE link_prefix, str_t *link_targ
 
 void wiki_append_img(parser_t *parser, char *token_ptr, int token_len)
 {
-    str_append(parser->output, img_start, sizeof(img_start) - 1);   // <img src="
-    if (!NIL_P(parser->img_prefix) && *token_ptr != '/')            // len always > 0
+    str_append(parser->output, img_start, sizeof(img_start) - 1);           // <img src="
+    if (!NIL_P(parser->img_prefix) && *token_ptr != '/')                    // len always > 0
         str_append_string(parser->output, parser->img_prefix);
     str_append(parser->output, token_ptr, token_len);
-    str_append(parser->output, img_alt, sizeof(img_alt) - 1);       // " alt="
+    str_append(parser->output, img_alt, sizeof(img_alt) - 1);               // " alt="
     str_append(parser->output, token_ptr, token_len);
-    str_append(parser->output, img_end, sizeof(img_end) - 1);       // " />
+    if (parser->output_style == XML_OUTPUT)
+        str_append(parser->output, img_end_xml, sizeof(img_end_xml) - 1);   // " />
+    else
+        str_append(parser->output, img_end_html, sizeof(img_end_html) - 1); // ">
 }
 
 // will emit indentation only if we are about to emit any of:
index c1aa46e187c8aa737005c33028f2827c7bd66444..ba2ed597a7b875c413902822a67910b4b157813f 100644 (file)
@@ -30,15 +30,48 @@ describe Wikitext::Parser, 'embedding img tags' do
   end
 
   it 'should convert valid markup into inline image tags' do
-    @parser.parse('{{foo.png}}').should == %Q{<p><img src="/images/foo.png" alt="foo.png" /></p>\n}
+    expected = %Q{<p><img src="/images/foo.png" alt="foo.png"></p>\n}
+    @parser.parse('{{foo.png}}').should == expected
+  end
+
+  it 'should produce XML output if passed option at parse time' do
+    expected = %Q{<p><img src="/images/foo.png" alt="foo.png" /></p>\n}
+    @parser.parse('{{foo.png}}', :output_style => :xml).should == expected
+  end
+
+  it 'should produce XML output if set via instance variable' do
+    expected = %Q{<p><img src="/images/foo.png" alt="foo.png" /></p>\n}
+    parser = Wikitext::Parser.new
+    parser.output_style = :xml
+    parser.parse('{{foo.png}}').should == expected
+  end
+
+  it 'should produce XML output if option set during initialization' do
+    expected = %Q{<p><img src="/images/foo.png" alt="foo.png" /></p>\n}
+    parser = Wikitext::Parser.new :output_style => :xml
+    parser.parse('{{foo.png}}').should == expected
+  end
+
+  it 'should produce HTML output if passed unrecognized output style' do
+    expected = %Q{<p><img src="/images/foo.png" alt="foo.png"></p>\n}
+
+    # the _only_ recognized override is :xml (case-sensitive)
+    @parser.parse('{{foo.png}}', :output_style => :html).should == expected
+    @parser.parse('{{foo.png}}', :output_style => false).should == expected
+    @parser.parse('{{foo.png}}', :output_style => nil).should == expected
+    @parser.parse('{{foo.png}}', :output_style => 42).should == expected
+    @parser.parse('{{foo.png}}', :output_style => 'XML').should == expected
+    @parser.parse('{{foo.png}}', :output_style => :XML).should == expected
   end
 
   it 'should appear embedded in an inline flow' do
-    @parser.parse('before {{foo.png}} after').should == %Q{<p>before <img src="/images/foo.png" alt="foo.png" /> after</p>\n}
+    expected = %Q{<p>before <img src="/images/foo.png" alt="foo.png"> after</p>\n}
+    @parser.parse('before {{foo.png}} after').should == expected
   end
 
   it 'should allow images in subdirectories' do
-    @parser.parse('{{foo/bar.png}}').should == %Q{<p><img src="/images/foo/bar.png" alt="foo/bar.png" /></p>\n}
+    expected = %Q{<p><img src="/images/foo/bar.png" alt="foo/bar.png"></p>\n}
+    @parser.parse('{{foo/bar.png}}').should == expected
   end
 
   it 'should pass through empty image tags unchanged' do
@@ -46,14 +79,14 @@ describe Wikitext::Parser, 'embedding img tags' do
   end
 
   it 'should not append prefix if img src starts with a slash' do
-    @parser.parse('{{/foo.png}}').should ==
-      %Q{<p><img src="/foo.png" alt="/foo.png" /></p>\n}
+    expected = %Q{<p><img src="/foo.png" alt="/foo.png"></p>\n}
+    @parser.parse('{{/foo.png}}').should == expected
   end
 
   it 'should work in BLOCKQUOTE blocks' do
     expected = dedent <<-END
       <blockquote>
-        <p><img src="/images/foo.png" alt="foo.png" /></p>
+        <p><img src="/images/foo.png" alt="foo.png"></p>
       </blockquote>
     END
     @parser.parse('> {{foo.png}}').should == expected
@@ -67,9 +100,9 @@ describe Wikitext::Parser, 'embedding img tags' do
     END
     expected = dedent <<-END
       <ul>
-        <li><img src="/images/foo.png" alt="foo.png" /></li>
-        <li><img src="/images/bar.png" alt="bar.png" /></li>
-        <li><img src="/images/baz.png" alt="baz.png" /></li>
+        <li><img src="/images/foo.png" alt="foo.png"></li>
+        <li><img src="/images/bar.png" alt="bar.png"></li>
+        <li><img src="/images/baz.png" alt="baz.png"></li>
       </ul>
     END
     @parser.parse(input).should == expected
@@ -83,36 +116,42 @@ describe Wikitext::Parser, 'embedding img tags' do
     END
     expected = dedent <<-END
       <ol>
-        <li><img src="/images/foo.png" alt="foo.png" /></li>
-        <li><img src="/images/bar.png" alt="bar.png" /></li>
-        <li><img src="/images/baz.png" alt="baz.png" /></li>
+        <li><img src="/images/foo.png" alt="foo.png"></li>
+        <li><img src="/images/bar.png" alt="bar.png"></li>
+        <li><img src="/images/baz.png" alt="baz.png"></li>
       </ol>
     END
     @parser.parse(input).should == expected
   end
 
   it 'should work in <h1> headings' do
-    @parser.parse('= {{foo.png}} =').should == %Q{<h1><img src="/images/foo.png" alt="foo.png" /></h1>\n}
+    expected = %Q{<h1><img src="/images/foo.png" alt="foo.png"></h1>\n}
+    @parser.parse('= {{foo.png}} =').should == expected
   end
 
   it 'should work in <h2> headings' do
-    @parser.parse('== {{foo.png}} ==').should == %Q{<h2><img src="/images/foo.png" alt="foo.png" /></h2>\n}
+    expected = %Q{<h2><img src="/images/foo.png" alt="foo.png"></h2>\n}
+    @parser.parse('== {{foo.png}} ==').should == expected
   end
 
   it 'should work in <h3> headings' do
-    @parser.parse('=== {{foo.png}} ===').should == %Q{<h3><img src="/images/foo.png" alt="foo.png" /></h3>\n}
+    expected = %Q{<h3><img src="/images/foo.png" alt="foo.png"></h3>\n}
+    @parser.parse('=== {{foo.png}} ===').should == expected
   end
 
   it 'should work in <h4> headings' do
-    @parser.parse('==== {{foo.png}} ====').should == %Q{<h4><img src="/images/foo.png" alt="foo.png" /></h4>\n}
+    expected = %Q{<h4><img src="/images/foo.png" alt="foo.png"></h4>\n}
+    @parser.parse('==== {{foo.png}} ====').should == expected
   end
 
   it 'should work in <h5> headings' do
-    @parser.parse('===== {{foo.png}} =====').should == %Q{<h5><img src="/images/foo.png" alt="foo.png" /></h5>\n}
+    expected = %Q{<h5><img src="/images/foo.png" alt="foo.png"></h5>\n}
+    @parser.parse('===== {{foo.png}} =====').should == expected
   end
 
   it 'should work in <h6> headings' do
-    @parser.parse('====== {{foo.png}} ======').should == %Q{<h6><img src="/images/foo.png" alt="foo.png" /></h6>\n}
+    expected = %Q{<h6><img src="/images/foo.png" alt="foo.png"></h6>\n}
+    @parser.parse('====== {{foo.png}} ======').should == expected
   end
 
   it 'should pass single curly braces through unaltered' do
@@ -124,23 +163,27 @@ describe Wikitext::Parser, 'embedding img tags' do
   end
 
   it 'should have no effect inside PRE_START blocks' do
-    @parser.parse('<pre>{{foo.png}}</pre>').should == %Q{<pre>{{foo.png}}</pre>\n}
+    expected = %Q{<pre>{{foo.png}}</pre>\n}
+    @parser.parse('<pre>{{foo.png}}</pre>').should == expected
   end
 
   it 'should have no effect inside NO_WIKI spans' do
-    @parser.parse('<nowiki>{{foo.png}}</nowiki>').should == %Q{<p>{{foo.png}}</p>\n}
+    expected = %Q{<p>{{foo.png}}</p>\n}
+    @parser.parse('<nowiki>{{foo.png}}</nowiki>').should == expected
   end
 
   it 'should be passed through in internal link targets' do
-    @parser.parse('[[{{foo.png}}]]').should == %Q{<p><a href="/wiki/%7b%7bfoo.png%7d%7d">{{foo.png}}</a></p>\n}
+    expected = %Q{<p><a href="/wiki/%7b%7bfoo.png%7d%7d">{{foo.png}}</a></p>\n}
+    @parser.parse('[[{{foo.png}}]]').should == expected
   end
 
   it 'should be passed through in internal link text' do
-    @parser.parse('[[article|{{foo.png}}]]').should == %Q{<p><a href="/wiki/article">{{foo.png}}</a></p>\n}
+    expected = %Q{<p><a href="/wiki/article">{{foo.png}}</a></p>\n}
+    @parser.parse('[[article|{{foo.png}}]]').should == expected
   end
 
   it 'should not be allowed as an external link target' do
-    expected = %Q{<p>[<img src="/images/foo.png" alt="foo.png" /> the link]</p>\n}
+    expected = %Q{<p>[<img src="/images/foo.png" alt="foo.png"> the link]</p>\n}
     @parser.parse('[{{foo.png}} the link]').should == expected
   end
 
@@ -150,7 +193,8 @@ describe Wikitext::Parser, 'embedding img tags' do
   end
 
   it 'should not allow embedded quotes' do
-    @parser.parse('{{"fun".png}}').should == %Q{<p>{{&quot;fun&quot;.png}}</p>\n}
+    expected = %Q{<p>{{&quot;fun&quot;.png}}</p>\n}
+    @parser.parse('{{"fun".png}}').should == expected
   end
 
   it 'should not allow embedded spaces' do
@@ -163,31 +207,37 @@ describe Wikitext::Parser, 'embedding img tags' do
 
   it 'should allow overrides of the image prefix at initialization time' do
     parser = Wikitext::Parser.new(:img_prefix => '/gfx/')
-    parser.parse('{{foo.png}}').should == %Q{<p><img src="/gfx/foo.png" alt="foo.png" /></p>\n}
+    expected = %Q{<p><img src="/gfx/foo.png" alt="foo.png"></p>\n}
+    parser.parse('{{foo.png}}').should == expected
   end
 
   it 'should suppress the image prefix if passed an empty string at initialization time' do
     parser = Wikitext::Parser.new(:img_prefix => '')
-    parser.parse('{{foo.png}}').should == %Q{<p><img src="foo.png" alt="foo.png" /></p>\n}
+    expected = %Q{<p><img src="foo.png" alt="foo.png"></p>\n}
+    parser.parse('{{foo.png}}').should == expected
   end
 
   it 'should suppress image prefix if passed nil at initialization time' do
     parser = Wikitext::Parser.new(:img_prefix => nil)
-    parser.parse('{{foo.png}}').should == %Q{<p><img src="foo.png" alt="foo.png" /></p>\n}
+    expected = %Q{<p><img src="foo.png" alt="foo.png"></p>\n}
+    parser.parse('{{foo.png}}').should == expected
   end
 
   it 'should allow overrides of the image prefix after initialization' do
     @parser.img_prefix = '/gfx/'
-    @parser.parse('{{foo.png}}').should == %Q{<p><img src="/gfx/foo.png" alt="foo.png" /></p>\n}
+    expected = %Q{<p><img src="/gfx/foo.png" alt="foo.png"></p>\n}
+    @parser.parse('{{foo.png}}').should == expected
   end
 
   it 'should suppress image if prefix set to an empty string after initialization' do
     @parser.img_prefix = ''
-    @parser.parse('{{foo.png}}').should == %Q{<p><img src="foo.png" alt="foo.png" /></p>\n}
+    expected = %Q{<p><img src="foo.png" alt="foo.png"></p>\n}
+    @parser.parse('{{foo.png}}').should == expected
   end
 
   it 'should suppress image if prefix set to nil after initialization' do
     @parser.img_prefix = nil
-    @parser.parse('{{foo.png}}').should == %Q{<p><img src="foo.png" alt="foo.png" /></p>\n}
+    expected = %Q{<p><img src="foo.png" alt="foo.png"></p>\n}
+    @parser.parse('{{foo.png}}').should == expected
   end
 end
index 1364df34d1b812a98102b474a117f30110d19820..697855a0ce5b909b9114070813bc17355f24a012 100644 (file)
@@ -204,7 +204,7 @@ describe Wikitext::Parser, 'with large slab of input text' do
       which would '''otherwise''' have &lt;tt&gt;special&lt;/tt&gt; meaning
       although explicit entities &copy; are passed through unchanged</pre>
       <p>a normal paragraph again</p>
-      <p><img src="/images/an_image.png" alt="an_image.png" /></p>
+      <p><img src="/images/an_image.png" alt="an_image.png"></p>
       <blockquote>
         <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>
         <pre>line 1
index 6d3bcf06595908981f8f38e96dced1815007c722..ac10436708ae3d2dadfaec4edf5e8f5dbeeef61b 100644 (file)
@@ -35,7 +35,7 @@ describe Wikitext::Parser do
     original_prefix                    = '/images/'
     parser.img_prefix                  = original_prefix
     parser.img_prefix.should           == original_prefix
-    parser.parse('{{foo.png}}').should == %Q{<p><img src="/images/foo.png" alt="foo.png" /></p>\n}
+    parser.parse('{{foo.png}}').should == %Q{<p><img src="/images/foo.png" alt="foo.png"></p>\n}
 
     # second pass
     parser                             = Wikitext::Parser.shared_parser
@@ -43,6 +43,6 @@ describe Wikitext::Parser do
     new_prefix                         = '/totally-different-prefix/'
     parser.img_prefix                  = new_prefix
     parser.img_prefix.should           == new_prefix
-    parser.parse('{{bar.png}}').should == %Q{<p><img src="/totally-different-prefix/bar.png" alt="bar.png" /></p>\n}
+    parser.parse('{{bar.png}}').should == %Q{<p><img src="/totally-different-prefix/bar.png" alt="bar.png"></p>\n}
   end
 end