Sha256: 33554bea20e727cb8ae4864ccba7aa339cfdec18c99e18154e2be17862045d12

Contents?: true

Size: 1.28 KB

Versions: 3

Compression:

Stored size: 1.28 KB

Contents

# -*- encoding : utf-8 -*-

require 'redcarpet'
require 'coderay'
require 'tilt'
require 'tilt/erb'
require 'tilt/haml'
require 'tilt/redcarpet'

module Gitdocs
  module RenderingHelper
    # @param [String, nil] pathname
    #
    # @return [nil] if the pathname is nil
    # @return [String]
    def file_content_render(pathname)
      return unless pathname

      tilt = Tilt.new(
        pathname,
        1, # line number
        fenced_code_blocks: true,
        renderer:           CodeRayify.new(filter_html: true, hard_wrap: true)
      )
      %(<div class="tilt">#{tilt.render}</div>)
    rescue LoadError, RuntimeError # No tilt support
      if path.text?
        code_ray = CodeRay.scan_file(pathname)
        %(<pre class="CodeRay">#{code_ray.encode(:html)}</pre>)
      else
        %(<embed class="inline-file" src="#{request.path_info}?mode=raw"></embed>)
      end
    end

    class CodeRayify < ::Redcarpet::Render::Safe
      # Override the safe #block_code with CodeRay, if a language is present.
      #
      # @param [String] code
      # @param [String] langauge
      #
      # @return [String]
      def block_code(code, language)
        if language
          CodeRay.scan(code, language).div
        else
          super(code, language)
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
gitdocs-0.6.2 lib/gitdocs/rendering_helper.rb
gitdocs-0.6.1 lib/gitdocs/rendering_helper.rb
gitdocs-0.6.0 lib/gitdocs/rendering_helper.rb