Skip to content
GitLab
项目 群组 主题 代码片段
  • /
  • 帮助
    • 帮助
    • 支持
    • 社区论坛
    • 提交反馈
  • 登录
  • L large
  • 项目信息
    • 项目信息
    • 动态
    • 标记
    • 成员
  • 仓库
    • 仓库
    • 文件
    • 提交
    • 分支
    • 标签
    • 贡献者统计
    • 分支图
    • 比较修订版本
  • 议题 21
    • 议题 21
    • 列表
    • 看板
    • 服务台
    • 里程碑
  • 合并请求 12
    • 合并请求 12
  • CI/CD
    • CI/CD
    • 流水线
    • 作业
    • 产物
    • 计划
  • 部署
    • 部署
    • 环境
    • 发布
  • 软件包与镜像库
    • 软件包与镜像库
    • 软件包库
    • 容器镜像库
    • Terraform 模块
    • 模型实验
  • 监控
    • 监控
    • 事件
  • 分析
    • 分析
    • 价值流
    • CI/CD
    • 仓库
  • Wiki
    • Wiki
  • 代码片段
    • 代码片段
  • 动态
  • 分支图
  • 创建新议题
  • 作业
  • 提交
  • 议题看板
收起侧边栏
  • 王权利
  • large
  • 议题
  • #6994

Syntax highlighting diff in repository_push_email.html.haml

Created by: grgcombs

Trying to determine the best way of enabling 'diff' syntax highlighting for the repository_push_email.html.haml commit diffs. I initially tried to use the highlight.js javascript library but I think many email clients don't/won't process javascript embedded in the html body of an email message. The ones I tried completely ignored the javascript.

So, my next thought was to try using the Rouge gem, since it doesn't require Python processes like other gems based on Pygments. Is there any interest in including support for colorized diffs?

Assuming you've run gem install rouge before this...

app/views/layouts/notify.html.haml

diff --git a/app/views/layouts/notify.html.haml b/app/views/layouts/notify.html.
index 991d4f0..cba9595 100644
--- a/app/views/layouts/notify.html.haml
+++ b/app/views/layouts/notify.html.haml
@@ -3,15 +3,15 @@
     %meta{content: "text/html; charset=utf-8", "http-equiv" => "Content-Type"}
       %title
         GitLab
-  :css
-    p.details {
-      font-style:italic;
-      color:#777
-    }
-    .footer p {
-      font-size:small;
-      color:#777
-    }
+  :ruby
+    require 'rouge'
+    stock_style = 'p.details { font-style:italic; color:#777 }'
+    stock_style2 = '.footer p { font-size:small; color:#777 }'
+    haml_io.puts '<style>'
+    haml_io.puts stock_style
+    haml_io.puts stock_style2
+    haml_io.puts Rouge::Themes::Github.render(:scope => '.highlight')
+    haml_io.puts '</style>'
   %body
     %div.content

app/views/notify/repository_push_email.html.haml

diff --git a/app/views/notify/repository_push_email.html.haml b/app/views/notify
index 85a01a5..b4119af 100644
--- a/app/views/notify/repository_push_email.html.haml
+++ b/app/views/notify/repository_push_email.html.haml
@@ -17,10 +17,14 @@
         #{diff.old_path} &rarr; #{diff.new_path}
       - else
         = diff.new_path || diff.old_path
-    %hr
-    %pre
-      = diff.diff
-    %br
+  %hr
+  :ruby
+    require 'rouge'
+    lexer = Rouge::Lexers::Diff.new
+    formatter = Rouge::Formatters::HTML.new(:css_class => 'highlight')
+    lexer = Rouge::Lexers::Diff.new
+    haml_io.puts formatter.format(lexer.lex(diff.diff))
+  %br

 - if @compare.timeout
   %h5 To prevent performance issues changes are hidden
指派人
分配到
工时统计