Browse files

Added content processor kramdown

  • Loading branch information...
1 parent e61b0f0 commit a0c1a770a5f1eec597abcfe2f84b3b5b655d7acb @gettalong committed Nov 23, 2009
View
36 doc/contentprocessor/kramdown.page
@@ -0,0 +1,36 @@
+---
+title: Webgen::ContentProcessor::Kramdown
+---
+## Description
+
+This processor converts the content, which is assumed to be in kramdown format, to HTML by using the
+[kramdown][1] library. The kramdown format is basically Markdown with some extensions like automatic
+header ID generation, using markup inside HTML elements and so on. There are a [quick reference][2]
+and a detailed [syntax documentation][3] available on the kramdown homepage.
+
+Example
+-------
+
+Here is a short sample of a text in kramdown markup:
+
+ # This a h1 header
+ {:#myid}
+
+ You can just write *your* paragraphs here and
+ [link][1] them below. This is **nice** format!
+
+ > Citations are easy too.
+ > Really. And you can assign them attributes.
+ {:.information}
+
+ * Lists
+ * aren't
+ * difficult
+ * either.
+
+ [1]: http://someurl.com
+
+
+[1]: http://kramdown.rubyforge.org/
+[2]: http://kramdown.rubyforge.org/syntax.html
+[3]: http://kramdown.rubyforge.org/quickref.html
View
35 doc/reference_configuration.page
@@ -401,6 +401,36 @@ configuration options) and/or how to use it in a webgen tag (for tag configurati
contentprocessor.erubis.options: {:trim: true}
+* ### contentprocessor.kramdown.handle_links
+
+ This configuration option can be used to specify whether all links generated via kramdown syntax
+ should be automatically run through the [relocatable tag]({relocatable: tag/relocatable.html}).
+ This saves some typing and makes the document more robust since all links are automatically
+ checked and warnings displayed if the link target is not found.
+
+ * Syntax: `BOOLEAN` where `BOOLEAN` is either `true` or `false`.
+
+ <%= show_default['contentprocessor.kramdown.handle_links] %>
+
+ * Example for setting the option in the configuration file:
+
+ contentprocessor.kramdown.handle_links: false
+
+
+* ### contentprocessor.kramdown.options
+
+ This configuration option can be used to specify processing and converting options for
+ kramdown.
+
+ * Syntax: `\{KEY: VALUE, ...}` where `KEY` and `VALUE` are key-value pairs of kramdown options
+
+ <%= show_default['contentprocessor.kramdown.options] %>
+
+ * Example for setting the option in the configuration file:
+
+ contentprocessor.kramdown.options: {:auto_ids: false}
+
+
* ### contentprocessor.redcloth.hard\_breaks
Specifies whether hard breaks (i.e. single newlines) in paragraphs are converted to HTML break
@@ -1205,8 +1235,3 @@ shows how to set the option in the configuration file.
# All things regarding logging
config.logger.mask(nil, :doc => 'Only show logging events which match the regexp mask')
-
-
-
-
-
View
1 lib/webgen/contentprocessor.rb
@@ -77,6 +77,7 @@ module ContentProcessor
autoload :Head, 'webgen/contentprocessor/head'
autoload :Tidy, 'webgen/contentprocessor/tidy'
autoload :Xmllint, 'webgen/contentprocessor/xmllint'
+ autoload :Kramdown, 'webgen/contentprocessor/kramdown'
# Return the list of all available content processors.
def self.list
View
26 lib/webgen/contentprocessor/kramdown.rb
@@ -0,0 +1,26 @@
+# -*- encoding: utf-8 -*-
+
+module Webgen::ContentProcessor
+
+ # Processes content in kramdown format (based on Markdown) using the +kramdown+ library.
+ class Kramdown
+
+ include Webgen::Loggable
+
+ # Convert the content in +context+ to HTML.
+ def call(context)
+ require 'kramdown'
+ require 'webgen/contentprocessor/kramdown/html'
+ doc = ::Kramdown::Document.new(context.content, context.website.config['contentprocessor.kramdown.options'])
+ context.content = KramdownHtmlConverter.convert(doc, context)
+ doc.warnings.each do |warn|
+ log(:warn) { "Warning while parsing <#{context.ref_node.alcn}> with kramdown: #{warn}" }
+ end
+ context
+ rescue LoadError
+ raise Webgen::LoadError.new('kramdown', self.class.name, context.dest_node.alcn, 'kramdown')
+ end
+
+ end
+
+end
View
32 lib/webgen/contentprocessor/kramdown/html.rb
@@ -0,0 +1,32 @@
+# -*- encoding: utf-8 -*-
+
+require 'kramdown'
+
+module Webgen::ContentProcessor
+
+ class KramdownHtmlConverter < ::Kramdown::Converter::Html
+
+ def initialize(doc, context) #:nodoc:
+ super(doc)
+ @context = context
+ @do_convert = context.website.config['contentprocessor.kramdown.handle_links']
+ end
+
+ # Convert the Kramdown document +doc+ to HTML using the webgen +context+ object.
+ def self.convert(doc, context)
+ new(doc, context).convert(doc.tree)
+ end
+
+ def convert_a(el, inner, indent)
+ el.options[:attr]['href'] = @context.tag('relocatable', {'path' => el.options[:attr]['href']}) if @do_convert
+ "<a#{options_for_element(el)}>#{inner}</a>"
+ end
+
+ def convert_img(el, inner, indent)
+ el.options[:attr]['src'] = @context.tag('relocatable', {'path' => el.options[:attr]['src']}) if @do_convert
+ "<img#{options_for_element(el)} />"
+ end
+
+ end
+
+end
View
7 lib/webgen/default_config.rb
@@ -141,7 +141,8 @@
'fragments' => 'Webgen::ContentProcessor::Fragments',
'head' => 'Webgen::ContentProcessor::Head',
'tidy' => 'Webgen::ContentProcessor::Tidy',
- 'xmllint' => 'Webgen::ContentProcessor::Xmllint'
+ 'xmllint' => 'Webgen::ContentProcessor::Xmllint',
+ 'kramdown' => 'Webgen::ContentProcessor::Kramdown'
}, :doc => 'Content processor name to class map')
Webgen::WebsiteAccess.website.blackboard.add_service(:content_processor_names, Webgen::ContentProcessor.method(:list))
@@ -174,6 +175,10 @@
config.contentprocessor.xmllint.options("--catalogs --noout --valid", :doc => 'Options passed to the xmllint command')
+config.contentprocessor.kramdown.options({:auto_ids => true}, :doc => 'The options hash for the kramdown processor')
+config.contentprocessor.kramdown.handle_links(true, :doc => 'Whether all links in a kramdown document should be handled by webgen')
+
+
config.tag.relocatable.path(nil, :doc => 'The path which should be made relocatable', :mandatory => 'default')
config.tag.menu.start_level(1, :doc => 'The level at which the menu starts.')
View
50 test/test_contentprocessor_kramdown.rb
@@ -0,0 +1,50 @@
+# -*- encoding: utf-8 -*-
+
+require 'test/unit'
+require 'helper'
+require 'webgen/node'
+require 'webgen/tree'
+require 'webgen/contentprocessor'
+
+class TestContentProcessorKramdown < Test::Unit::TestCase
+
+ include Test::WebsiteHelper
+
+ def test_call
+ obj = Webgen::ContentProcessor::Kramdown.new
+ node = Webgen::Node.new(Webgen::Node.new(Webgen::Tree.new.dummy_root, '/', '/'), 'test', 'test')
+ hello = Webgen::Node.new(node.tree.root, 'hello.en.html', 'hello.html')
+
+ # test normal invocation
+ context = Webgen::Context.new(:content => '# header', :chain => [node])
+ assert_equal("<h1 id=\"header\">header</h1>\n", obj.call(context).content)
+
+ # test automatic handling of links
+ @website.config['contentprocessor.kramdown.handle_links'] = true
+ context.content = 'Link [test](hello.html)'
+ assert_equal("<p>Link <a href=\"hello.en.html\">test</a></p>\n", obj.call(context).content)
+ context.content = 'Link ![test](hello.html)'
+ assert_equal("<p>Link <img alt=\"test\" src=\"hello.en.html\" /></p>\n", obj.call(context).content)
+
+ @website.config['contentprocessor.kramdown.handle_links'] = false
+ context.content = 'Link [test](hello.html)'
+ assert_equal("<p>Link <a href=\"hello.html\">test</a></p>\n", obj.call(context).content)
+ context.content = 'Link ![test](hello.html)'
+ assert_equal("<p>Link <img alt=\"test\" src=\"hello.html\" /></p>\n", obj.call(context).content)
+
+ # test warning messages
+ output = StringIO.new('')
+ @website.logger = ::Logger.new(output)
+ @website.logger.level = Logger::WARN
+
+ context.content = '{::unknownextension::}'
+ obj.call(context)
+ assert_equal("", context.content)
+ output.rewind; assert_match(/No extension named 'unknownextension' found/, output.read)
+
+ # test raised error when library not found
+ def obj.require(lib); raise LoadError; end
+ assert_raise(Webgen::LoadError) { obj.call(context) }
+ end
+
+end
View
5 website/src/news/release_0_5_11.page
@@ -16,6 +16,10 @@ modified_at: 2009-08-?? ??:??:??
--- name:changes pipeline:tags,maruku
+* Major changes:
+
+ - Added content processor [kramdown][2] (faster Markdown parser)
+
* Minor changes:
- Added a CSS class to the HTML tag of the currently selected language of [Tag::Langbar][1]
@@ -34,3 +38,4 @@ modified_at: 2009-08-?? ??:??:??
`#render_block` was called via the context object
[1]: {relocatable: ../documentation/tag/langbar.html}
+[2]: {relocatable: ../documentation/contentprocessor/kramdown.html}

0 comments on commit a0c1a77

Please sign in to comment.