Sha256: 137975bb328174ec62cc298e696717da726b275e9353d96c6d8fe7db74f6035d

Contents?: true

Size: 874 Bytes

Versions: 4

Compression:

Stored size: 874 Bytes

Contents

# encoding: UTF-8

require 'kramdown'

module Spontaneous::Field
  class Markdown < Base
    has_editor

    def outputs
      [:html]
    end

    def generate_html(input)
      input.to_html
    end

    def preprocess(input)
      # convert lines ending with newlines into a <br/>
      # as official Markdown syntax isn't suitable for
      # casual users
      # code taken from:
      # http://github.github.com/github-flavored-markdown/
      output = input.gsub(/^[\w\<][^\n]*\n+/) do |x|
        x =~ /\n{2}/ ? x : (x.strip!; x << "  \n")
      end

      # prevent foo_bar_baz from ending up with an italic word in the middle
      output.gsub!(/(^(?! {4}|\t)\w+_\w+_\w[\w_]*)/) do |x|
        x.gsub('_', '\_') if x.split('').sort.to_s[0..1] == '__'
      end
      Kramdown::Document.new(output)
    end

    self.register(:markdown, :text, :richtext)
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
spontaneous-0.2.0.beta4 lib/spontaneous/field/markdown.rb
spontaneous-0.2.0.beta3 lib/spontaneous/field/markdown.rb
spontaneous-0.2.0.beta2 lib/spontaneous/field/markdown.rb
spontaneous-0.2.0.beta1 lib/spontaneous/field/markdown.rb