Sha256: 4bdcf9e714ac4605caa9589a03874bb7d325b2ada7458f55013bdb60f122936e

Contents?: true

Size: 1.47 KB

Versions: 6

Compression:

Stored size: 1.47 KB

Contents

module RedCloth
  INLINE_FORMATTERS = [:textile, :footnote, :link]

  def self.convert(text)
    new(text).to_html(*INLINE_FORMATTERS)
  end

  module Inline
    FN_RE = /
      (\s+)?    # getting spaces
      %\{       # opening
      (.*?)     # footnote
      \}#       # closing
    /xm

    def footnote(text)
      text.gsub!(FN_RE) do |m|
        %(<span class="footnote">#{$2}</span>)
      end
    end

    LINK_RE = /
      <
      ((?:https?|ftp):\/\/.*?)
      >
    /xm

    def link(text)
      text.gsub!(LINK_RE) do |m|
        %(<a href="#{$1}">#{$1}</a>)
      end
    end
  end

  module Formatters
    module HTML
      def figure(options = {})
        %[<p class="figure"><img src="../images/#{options[:text]}" alt="#{options[:class]}" /><br/><span class="caption">#{options[:class]}</span></p>]
      end

      def note(options = {})
        %[<p class="note">#{options[:text]}</p>]
      end

      def attention(options = {})
        %[<p class="attention">#{options[:text]}</p>]
      end

      def file(options = {})
        base_url = Kitabu.config[:base_url]

        if base_url
          url = File.join(base_url, options[:text])
        else
          url = content
          $stderr << "\nYou're using `file. #{content}` but didn't set base_url in your configuration file.\n"
        end

        %[<p class="file"><span><strong>Download</strong> <a href="#{url}">#{options[:text]}</a></span></p>]
      end
    end
  end
end

RedCloth.send(:include, RedCloth::Inline)

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
kitabu-1.0.1 lib/kitabu/extensions/redcloth.rb
kitabu-1.0.0 lib/kitabu/extensions/redcloth.rb
kitabu-1.0.0.rc4 lib/kitabu/extensions/redcloth.rb
kitabu-1.0.0.rc3 lib/kitabu/extensions/redcloth.rb
kitabu-1.0.0.rc2 lib/kitabu/extensions/redcloth.rb
kitabu-1.0.0.rc1 lib/kitabu/extensions/redcloth.rb