Sha256: a8c7e2a1caa303f1459fe597f7ea7f6dedefc0754582fb3bc13a05426c7a99d1

Contents?: true

Size: 1.77 KB

Versions: 2

Compression:

Stored size: 1.77 KB

Contents

# -*- encoding: utf-8 -*-

require 'webgen/content_processor'
webgen_require 'kramdown'

module Webgen
  class ContentProcessor

    # Processes content in kramdown format (based on Markdown) using the +kramdown+ library.
    module Kramdown

      class CustomHtmlConverter < ::Kramdown::Converter::Html #:nodoc:
        public_class_method(:new)

        def initialize(root, options, context)
          super(root, options)
          @context = context
          @do_convert = context.website.config['content_processor.kramdown.handle_links']
          @ignore_fragments = context.website.config['content_processor.kramdown.ignore_unknown_fragments']
        end

        def convert_a(el, indent)
          el.attr['href'] = @context.tag('relocatable', {'path' => el.attr['href'],
                                           'ignore_unknown_fragment' => @ignore_fragments}) if @do_convert
          super
        end

        def convert_img(el, indent)
          el.attr['src'] = @context.tag('relocatable', {'path' => el.attr['src'],
                                          'ignore_unknown_fragment' => @ignore_fragments}) if @do_convert
          super
        end

      end

      # Convert the content in +context+ to HTML.
      def self.call(context)
        options = context.website.config['content_processor.kramdown.options'].dup
        options[:link_defs] = context.website.ext.link_definitions.merge(options[:link_defs] || {})
        doc = ::Kramdown::Document.new(context.content, options)
        context.content = CustomHtmlConverter.new(doc.root, doc.options, context).convert(doc.root)
        doc.warnings.each do |warn|
          context.website.logger.warn { "kramdown warning while parsing <#{context.ref_node}>: #{warn}" }
        end
        context
      end

    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
webgen-1.0.0 lib/webgen/content_processor/kramdown.rb
webgen-1.0.0.beta3 lib/webgen/content_processor/kramdown.rb