File: lib/redmine/syntax_highlighting.rb

Overview
Module Structure
Code

Overview

Module Structure

  module: <Toplevel Module>
  module: Redmine#18
  module: SyntaxHighlighting#19
has properties
module attribute: highlighter [R] #22
module method: highlighter= / 1 #25
  module: CodeRay#34
has properties
module method: highlight_by_filename / 2 #41
module method: highlight_by_language / 2 #48

Code

   1  # Redmine - project management software
   2  # Copyright (C) 2006-2011  Jean-Philippe Lang
   3  #
   4  # This program is free software; you can redistribute it and/or
   5  # modify it under the terms of the GNU General Public License
   6  # as published by the Free Software Foundation; either version 2
   7  # of the License, or (at your option) any later version.
   8  #
   9  # This program is distributed in the hope that it will be useful,
  10  # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  # GNU General Public License for more details.
  13  #
  14  # You should have received a copy of the GNU General Public License
  15  # along with this program; if not, write to the Free Software
  16  # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  17 
  18  module Redmine
  19    module SyntaxHighlighting
  20 
  21      class << self
  22        attr_reader :highlighter
  23        delegate :highlight_by_filename, :highlight_by_language, :to => :highlighter
  24 
  25        def highlighter=(name)
  26          if name.is_a?(Module)
  27            @highlighter = name
  28          else
  29            @highlighter = const_get(name)
  30          end
  31        end
  32      end
  33 
  34      module CodeRay
  35        require 'coderay'
  36        require 'coderay/helpers/file_type'
  37 
  38        class << self
  39          # Highlights +text+ as the content of +filename+
  40          # Should not return line numbers nor outer pre tag
  41          def highlight_by_filename(text, filename)
  42            language = ::CodeRay::FileType[filename]
  43            language ? ::CodeRay.scan(text, language).html(:break_lines => true) : ERB::Util.h(text)
  44          end
  45 
  46          # Highlights +text+ using +language+ syntax
  47          # Should not return outer pre tag
  48          def highlight_by_language(text, language)
  49            ::CodeRay.scan(text, language).html(:line_numbers => :inline, :line_number_anchors => false, :wrap => :span)
  50          end
  51        end
  52      end
  53    end
  54 
  55    SyntaxHighlighting.highlighter = 'CodeRay'
  56  end