Project

General

Profile

Patch #1651 » scm_patch_pygmentize3.diff

cleaner version (against stable 0.8.2, r2614) - Holger Just, 2009-03-23 00:45

View differences:

app/helpers/application_helper.rb (working copy)
20 20
require 'forwardable'
21 21
require 'cgi'
22 22

  
23
# This is needed for Pygments Integration
24
require 'tempfile'
25

  
26
# Ruby pre 1.9 does not provide the full functionality needed
27
# Tempfiles need to have a fixed suffix to be correctly recognized
28
# by pygments.
29
# See ApplicationHelper.syntax_hightlight()
30
ruby_version = RUBY_VERSION.split('.')
31
if Integer(ruby_version[0]) <= 1 and Integer(ruby_version[1]) <= 8
32
  class Tempfile
33
    # This is the imlementation comming from Ruby 1.9.0.0
34
    def make_tmpname(basename, n)
35
      case basename
36
      when Array
37
        prefix, suffix = *basename
38
      else
39
        prefix, suffix = basename, ''
40
      end                                                   
41

  
42
      t = Time.now.strftime("%Y%m%d")
43
      path = "#{prefix}#{t}-#{$$}-#{rand(0x100000000).to_s(36)}-#{n}#{suffix}"
44
    end
45
    private :make_tmpname
46
  end
47
end
48

  
23 49
module ApplicationHelper
24 50
  include Redmine::WikiFormatting::Macros::Definitions
25 51
  include GravatarHelper::PublicMethods
......
189 215
  end
190 216

  
191 217
  def syntax_highlight(name, content)
192
    type = CodeRay::FileType[name]
193
    type ? CodeRay.scan(content, type).html : h(content)
218
    tmp_file = Tempfile.new(['redmine_pygments', File.extname(name)])
219
    tmp_file_path = tmp_file.path()
220

  
221
    tmp_file.write(content)
222
    tmp_file.flush()
223
    code = IO.popen("pygmentize -f html -O encoding=utf8 '#{tmp_file_path}' 2>&1").read()
224
    if code.empty?
225
      code = IO.popen("pygmentize -l text -f html -O encoding=utf8 '#{tmp_file_path}' 2>&1").read()
226
    end
227
    tmp_file.close!()
228
    if code.empty?
229
      h(content)
230
    else
231
      "%s" % code[28...-13]
232
    end
194 233
  end
195 234

  
196 235
  def to_path_param(path)
public/stylesheets/scm.css (working copy)
182 182
.CodeRay .ty { color:#339; font-weight:bold }
183 183
.CodeRay .v  { color:#036 }
184 184
.CodeRay .xt { color:#444 }
185

  
186
# Pygments Rules
187
.c { color: #808080 } /* Comment */
188
.err { color: #F00000; background-color: #F0A0A0 } /* Error */
189
.k { color: #008000; font-weight: bold } /* Keyword */
190
.o { color: #303030 } /* Operator */
191
.cm { color: #808080 } /* Comment.Multiline */
192
.cp { color: #507090 } /* Comment.Preproc */
193
.c1 { color: #808080 } /* Comment.Single */
194
.cs { color: #cc0000; font-weight: bold } /* Comment.Special */
195
.gd { color: #A00000 } /* Generic.Deleted */
196
.ge { font-style: italic } /* Generic.Emph */
197
.gr { color: #FF0000 } /* Generic.Error */
198
.gh { color: #000080; font-weight: bold } /* Generic.Heading */
199
.gi { color: #00A000 } /* Generic.Inserted */
200
.go { color: #808080 } /* Generic.Output */
201
.gp { color: #c65d09; font-weight: bold } /* Generic.Prompt */
202
.gs { font-weight: bold } /* Generic.Strong */
203
.gu { color: #800080; font-weight: bold } /* Generic.Subheading */
204
.gt { color: #0040D0 } /* Generic.Traceback */
205
.kc { color: #008000; font-weight: bold } /* Keyword.Constant */
206
.kd { color: #008000; font-weight: bold } /* Keyword.Declaration */
207
.kp { color: #003080; font-weight: bold } /* Keyword.Pseudo */
208
.kr { color: #008000; font-weight: bold } /* Keyword.Reserved */
209
.kt { color: #303090; font-weight: bold } /* Keyword.Type */
210
.m { color: #6000E0; font-weight: bold } /* Literal.Number */
211
.s { background-color: #fff0f0 } /* Literal.String */
212
.na { color: #0000C0 } /* Name.Attribute */
213
.nb { color: #007020 } /* Name.Builtin */
214
.nc { color: #B00060; font-weight: bold } /* Name.Class */
215
.no { color: #003060; font-weight: bold } /* Name.Constant */
216
.nd { color: #505050; font-weight: bold } /* Name.Decorator */
217
.ni { color: #800000; font-weight: bold } /* Name.Entity */
218
.ne { color: #F00000; font-weight: bold } /* Name.Exception */
219
.nf { color: #0060B0; font-weight: bold } /* Name.Function */
220
.nl { color: #907000; font-weight: bold } /* Name.Label */
221
.nn { color: #0e84b5; font-weight: bold } /* Name.Namespace */
222
.nt { color: #007000 } /* Name.Tag */
223
.nv { color: #906030 } /* Name.Variable */
224
.ow { color: #000000; font-weight: bold } /* Operator.Word */
225
.w { color: #bbbbbb } /* Text.Whitespace */
226
.mf { color: #6000E0; font-weight: bold } /* Literal.Number.Float */
227
.mh { color: #005080; font-weight: bold } /* Literal.Number.Hex */
228
.mi { color: #0000D0; font-weight: bold } /* Literal.Number.Integer */
229
.mo { color: #4000E0; font-weight: bold } /* Literal.Number.Oct */
230
.sb { background-color: #fff0f0 } /* Literal.String.Backtick */
231
.sc { color: #0040D0 } /* Literal.String.Char */
232
.sd { color: #D04020 } /* Literal.String.Doc */
233
.s2 { background-color: #fff0f0 } /* Literal.String.Double */
234
.se { color: #606060; font-weight: bold; background-color: #fff0f0 } /* Literal.String.Escape */
235
.sh { background-color: #fff0f0 } /* Literal.String.Heredoc */
236
.si { background-color: #e0e0e0 } /* Literal.String.Interpol */
237
.sx { color: #D02000; background-color: #fff0f0 } /* Literal.String.Other */
238
.sr { color: #000000; background-color: #fff0ff } /* Literal.String.Regex */
239
.s1 { background-color: #fff0f0 } /* Literal.String.Single */
240
.ss { color: #A06000 } /* Literal.String.Symbol */
241
.bp { color: #007020 } /* Name.Builtin.Pseudo */
242
.vc { color: #306090 } /* Name.Variable.Class */
243
.vg { color: #d07000; font-weight: bold } /* Name.Variable.Global */
244
.vi { color: #3030B0 } /* Name.Variable.Instance */
245
.il { color: #0000D0; font-weight: bold } /* Literal.Number.Integer.Long */
(4-4/6)