Sha256: 9c4951f0acb57ff3ab0daea12e1b708ccea2c84d246aa7b6870b469c64edc27c

Contents?: true

Size: 866 Bytes

Versions: 2

Compression:

Stored size: 866 Bytes

Contents

require 'kramdown'
require 'htmlentities'
require 'mastalk/extensions'

module Mastalk
  # Document class to preprocess
  # mastalk specific syntax
  class Document
    include Mastalk::Extensions

    attr_reader :source

    def initialize(source)
      @source = source.dup
    end

    def to_html(options = {})
      auto_ids = options[:auto_id].nil? ? true : options[:auto_ids]
      kramdown = Kramdown::Document.new(
        preprocess(source),
        auto_ids: auto_ids
      )
      html, _ = Kramdown::Converter::Html.convert(kramdown.root, kramdown.options)
      ::HTMLEntities.new.decode(html)
    end

    private

    def preprocess(source)
      extensions.each do |regex, block|
        while source.match(regex)
          source.sub!(regex, block.call(Regexp.last_match.captures.first).strip)
        end
      end
      source
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mastalk-0.9.1 lib/mastalk/document.rb
mastalk-0.9.0 lib/mastalk/document.rb