# encoding: utf-8 #-- # Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies) # Copyright (C) 2008 Johan Sørensen # Copyright (C) 2008 Tor Arne Vestbø # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . #++ module BlobsHelper include RepositoriesHelper include TreesHelper HIGHLIGHTER_TO_EXT = { "list" => /\.(lisp|cl|l|mud|el|clj)$/, "hs" => /\.hs$/, "css" => /\.css$/, "lua" => /\.lua$/, "ml" => /\.(ml|mli)$/, "proto" => /\.proto$/, "sql" => /\.(sql|ddl|dml)$/, "vb" => /\.vb$/, "wiki" => /\.(mediawiki|wikipedia|wiki)$/, } ASCII_MIME_TYPES_EXCEPTIONS = [ /^text/ ] def textual?(blob) !binary?(blob) end def binary?(blob) blob.binary? end def image?(blob) blob.mime_type =~ /^image/ end def highlightable?(blob) if File.extname(blob.name) == "" return false end if %w[.txt .textile .md .rdoc .markdown].include?(File.extname(blob.name)) return false end true end def language_of_file(filename) if lang_tuple = HIGHLIGHTER_TO_EXT.find{|lang, matcher| filename =~ matcher } return lang_tuple.first end end def render_highlighted(text, filename, code_theme_class = nil) out = [] lang_class = "lang" + File.extname(filename).sub('.', '-') out << %Q{} text.to_s.split("\n").each_with_index do |line, count| lineno = count + 1 out << %Q{} out << %Q{} code_classes = "code" code_classes << " #{code_theme_class}" if code_theme_class ext = File.extname(filename).sub(/^\./, '') out << %Q{} out << "" end out << "
#{lineno}
#{h(line)}
" out.join("\n") end def too_big_to_render?(size) size > 350.kilobytes end end