Sha256: d692468275b85f4d8fc0cf7ce782d71a93d17b5b7ca8f9c8493634602b4e8f1d

Contents?: true

Size: 1.17 KB

Versions: 2

Compression:

Stored size: 1.17 KB

Contents

module Webgit
  module ApplicationHelper
    EXT_WITH_LANGUAGE = {
      ru: :ruby,
      rb: :ruby,
      rake: :ruby,
      clj: :clojure,
      h: :c,
      py: :python,
      txt: :text,    
      sh: :text,
      js: :java_script,
      htm: :html
    }

    def preview(blob, format)
      f = format_to_sym(format)
      case f
      when :html, :htm then
        "<div>#{blob.content}</div>"
      when :ruby, :java, :js, :python, :scss, :php, :lua, :html, :json, :go, :sql, :yaml, :text, :coffee then
        CodeRay.scan(blob.content.force_encoding('UTF-8'), f).div(:line_numbers => :table, :css => :class)
      when :png, :jpg, :gif then
        "<img src=\"#{preview_path(params[:branch], params[:path])}.#{params[:format]}\" />"
      when '.pdf' then
        "<iframe src=\"#{preview_path(params[:branch], params[:path])}.#{params[:format]}\" width='860' height='800' border='0' style='border:none'></iframe>"
      else
        "Binary File [#{params[:path]}]"
      end    
    end

    private 
      def format_to_sym(format)
        if format
          EXT_WITH_LANGUAGE.fetch(format.to_sym, format.to_sym)
        else
          :text
        end        
      end
    
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
webgit-0.0.2 app/helpers/webgit/application_helper.rb
webgit-0.0.1 app/helpers/webgit/application_helper.rb