Sha256: f7c9994cf0ea128f931bcd43c8b27405c166576a358bd5ff1cc2cee1598270f0

Contents?: true

Size: 1.64 KB

Versions: 3

Compression:

Stored size: 1.64 KB

Contents

if RUBY_VERSION >= '1.9'
  require 'rdoc/markup/to_html'
else
  require 'rdoc/generators/html_generator'
  require 'ostruct'
end

require 'RedCloth'
require 'rdiscount'

module Docify
  module Markup
    extend self
  
    # Auto-detect format from filename and render content
    def render(filename, content)
      name = File.basename(filename.to_s.strip)
      raise ArgumentError, 'Filename required!' if name.empty?
      format = detect_format(name)
      format == :text ? content : self.send(format, content)
    end
    
    if RUBY_VERSION >= '1.9'
      # Render content for RDoc on Ruby 1.9
      def rdoc(content)
        markup = RDoc::Markup::ToHtml.new
        markup.convert(content)
      end
    else
      # Render content for RDoc
      def rdoc(content)
        simple_markup = SM::SimpleMarkup.new
        generator = Generators::HyperlinkHtml.new('', OpenStruct.new)
        simple_markup.add_special(/((link:|https?:|mailto:|ftp:|www\.)\S+\w)/, :HYPERLINK)
        simple_markup.add_special(/(((\{.*?\})|\b\S+?)\[\S+?\.\S+?\])/, :TIDYLINK)
        simple_markup.convert(content, generator)     
      end
    end
    
    # Render content for Markdown
    def markdown(content)
      Markdown.new(content).to_html
    end
    
    # Render content for Textile
    def textile(content)
      RedCloth.new(content).to_html
    end
  
    protected
    
    # Detect markup format from filename
    def detect_format(filename)
      case(filename)
        when /\.rdoc/i
          :rdoc
        when /\.(md|mkdn?|mdown|markdown)/i
          :markdown
        when /\.textile/i
          :textile
        else
          :text
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
docify-1.0.4 lib/docify/markup.rb
docify-1.0.3 lib/docify/markup.rb
docify-1.0.2 lib/docify/markup.rb