Sha256: 20d26f2ba5d1ca70706f629cb984b8566e3fc3989139648b4d2d3fb125f4c7d8

Contents?: true

Size: 1.89 KB

Versions: 1

Compression:

Stored size: 1.89 KB

Contents

module Slodown
  class Formatter
    def initialize(source)
      @current = @source = source.to_s
    end

    # Runs the entire pipeline.
    #
    def complete
      markdown.autolink.sanitize
    end

    # Convert the current document state from Markdown into HTML.
    #
    def markdown
      @current = Kramdown::Document.new(@current).to_slodown_html
      self
    end

    # Auto-link URLs through Rinku.
    #
    def autolink
      @current = Rinku.auto_link(@current)
      self
    end

    # Sanitize HTML tags.
    #
    def sanitize(mode = :normal)
      @current = case mode
      when :normal
        Sanitize.clean(@current,
          elements: %w(
            p a span sub sup strong em div hr abbr
            ul ol li
            blockquote pre code
            h1 h2 h3 h4 h5 h6
            img object param del
          ),
          attributes: {
            :all     => ['class', 'style', 'title'],
            'a'      => ['href', 'rel', 'name'],
            'li'     => ['id'],
            'sup'    => ['id'],
            'img'    => ['src', 'title', 'alt', 'width', 'height'],
            'object' => ['width', 'height'],
            'param'  => ['name', 'value'],
            'embed'  => ['allowscriptaccess', 'width', 'height', 'src'],
            'iframe' => ['width', 'height', 'src']
          },
          protocols: {
            'a' => { 'href' => ['ftp', 'http', 'https', 'mailto', '#fn', '#fnref', :relative] },
            'img' => {'src'  => ['http', 'https', :relative]},
            'iframe' => {'src'  => ['http', 'https']},
            'embed' => {'src'  => ['http', 'https']},
            'object' => {'src'  => ['http', 'https']},
            'li' => {'id' => ['fn']},
            'sup' => {'id' => ['fnref']}
          },
          transformers: EmbedTransformer)
      else
        Sanitize.clean(@current)
      end

      self
    end

    def to_s
      @current
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
slodown-0.1.2 lib/slodown/formatter.rb