Remove CodeRay highlight and replace with pipeline/rouge

pull/6423/head
Oliver Günther 7 years ago
parent 01cb0ea656
commit 907dafd366
No known key found for this signature in database
GPG Key ID: A3A8BDAD7C0C552C
  1. 8
      Gemfile
  2. 9
      Gemfile.lock
  3. 2
      app/assets/stylesheets/content/_index.sass
  4. 9
      app/assets/stylesheets/content/_wiki.sass
  5. 8
      app/assets/stylesheets/content/editor/_ckeditor.sass
  6. 4
      app/assets/stylesheets/content/editor/_index.sass
  7. 22
      app/assets/stylesheets/content/editor/_markdown.sass
  8. 206
      app/assets/stylesheets/content/editor/_rouge.scss
  9. 2
      app/assets/stylesheets/openproject/_legacy.sass
  10. 2
      app/assets/stylesheets/openproject/_scm.sass
  11. 175
      app/assets/stylesheets/openproject/coderay.scss
  12. 2
      app/helpers/application_helper.rb
  13. 2
      app/views/common/_file.html.erb
  14. 2
      app/views/repositories/annotate.html.erb
  15. 2
      app/views/wiki/_content.html.erb
  16. 2
      frontend/src/app/components/work-packages/wp-single-view/wp-single-view.html
  17. 1
      frontend/src/app/components/wp-edit/wp-edit-field/wp-edit-field.component.ts
  18. 3
      frontend/src/app/modules/fields/display/field-types/wp-display-formattable-field.module.ts
  19. 52
      lib/open_project/syntax_highlighting.rb
  20. 39
      lib/open_project/text_formatting/filters/markdown_filter.rb
  21. 53
      lib/open_project/text_formatting/filters/syntax_highlight_filter.rb
  22. 3
      lib/open_project/text_formatting/formatters/markdown/formatter.rb
  23. 2
      lib/open_project/text_formatting/formatters/textile/redcloth_wrapper.rb
  24. 3
      spec_legacy/unit/helpers/application_helper_spec.rb

@ -36,7 +36,6 @@ gem 'activerecord-session_store', '~> 1.1.0'
gem 'rails', '~> 5.1.5'
gem 'responders', '~> 2.4'
gem 'coderay', '~> 1.1.2'
gem 'rubytree', git: 'https://github.com/dr0verride/RubyTree.git', ref: '06f53ee'
gem 'rdoc', '>= 2.4.2'
@ -70,9 +69,14 @@ gem 'stringex', '~> 2.7.1'
# CommonMark markdown parser with GFM extension
gem 'commonmarker', '~> 0.17.9'
# HTML pipeline for transformations on text formatter output
# such as sanitization or additional features
gem 'html-pipeline', '~> 2.7.1'
gem 'html-pipeline', '~> 2.8.0'
# Requires escape-utils for faster escaping
gem 'escape_utils', '~> 1.0'
# Syntax highlighting used in html-pipeline with rouge
gem 'rouge', '~> 3.1.1'
# HTML sanitization used for html-pipeline
gem 'sanitize', '~> 4.6.0'
# HTML autolinking for mails and urls (replaces autolink)

@ -252,6 +252,7 @@ GEM
nokogiri (>= 1.4.3)
erbse (0.0.2)
erubi (1.7.1)
escape_utils (1.2.1)
eventmachine (1.2.5)
excon (0.52.0)
execjs (2.7.0)
@ -303,7 +304,7 @@ GEM
hashie (3.5.6)
health_check (2.6.0)
rails (>= 4.0)
html-pipeline (2.7.1)
html-pipeline (2.8.3)
activesupport (>= 2)
nokogiri (>= 1.4)
htmldiff (0.0.1)
@ -481,6 +482,7 @@ GEM
rinku (2.0.4)
roar (1.1.0)
representable (~> 3.0.0)
rouge (3.1.1)
rspec (3.7.0)
rspec-core (~> 3.7.0)
rspec-expectations (~> 3.7.0)
@ -635,7 +637,6 @@ DEPENDENCIES
cells-erb (~> 0.0.8)
cells-rails (~> 0.0.6)
chromedriver-helper (~> 1.2.0)
coderay (~> 1.1.2)
color-tools (~> 1.3.0)
commonmarker (~> 0.17.9)
cucumber (~> 3.0.0)
@ -646,6 +647,7 @@ DEPENDENCIES
date_validator (~> 0.9.0)
delayed_job_active_record (~> 4.1.1)
equivalent-xml (~> 0.6)
escape_utils (~> 1.0)
factory_bot (~> 4.8)
factory_bot_rails (~> 4.8)
faker
@ -655,7 +657,7 @@ DEPENDENCIES
gon (~> 6.2.0)
grape (~> 1.0)
health_check
html-pipeline (~> 2.7.1)
html-pipeline (~> 2.8.0)
htmldiff
i18n-js (~> 3.0.0)
json_spec (~> 1.1.4)
@ -699,6 +701,7 @@ DEPENDENCIES
retriable (~> 3.1.1)
rinku (~> 2.0.4)
roar (~> 1.1.0)
rouge (~> 3.1.1)
rspec (~> 3.7.0)
rspec-activemodel-mocks (~> 1.0.3)!
rspec-example_disabler!

@ -68,4 +68,4 @@
@import content/hidden
@import content/menus/_project_autocompletion
@import content/editor/ckeditor
@import content/editor/index

@ -64,15 +64,6 @@ div.wiki
.external.icon-context:before
padding: 4px 4px 0 0
pre
margin: 1em 1em 1em 1.6em
padding: 2px 2px 2px 0
background-color: #fafafa
border: 1px solid #dadada
width: auto
overflow-x: auto
overflow-y: hidden
ul.toc
margin-bottom: 12px
margin-right: 12px

@ -1,4 +1,3 @@
@import './macros'
// Wrapper for inline text editor
.op-ckeditor-element
@ -19,3 +18,10 @@
// Min height for the editable section
.ck-editor__editable
min-height: 20vh
// Correct code style
.ck-content pre
margin: 10px 0
code
display: block

@ -0,0 +1,4 @@
@import './markdown'
@import './ckeditor'
@import './macros'
@import './rouge'

@ -0,0 +1,22 @@
// Specific styles for displaying rendered markdown on all pages
// This is still selected by div.wiki since this is still used throughout the app.
div.wiki
// Style pre tags alone
pre
margin: 1em 1em 1em 1.6em
padding: 2px 2px 2px 0
background-color: #fafafa
border: 1px solid #dadada
width: auto
overflow-x: auto
overflow-y: hidden
// Avoid doubling borders in pre > code
pre > code
display: block
border: none
background: transparent
color: initial

@ -0,0 +1,206 @@
.highlight table td { padding: 5px; }
.highlight table pre { margin: 0; }
.highlight .cm {
color: #999988;
font-style: italic;
}
.highlight .cp {
color: #999999;
font-weight: bold;
}
.highlight .c1 {
color: #999988;
font-style: italic;
}
.highlight .cs {
color: #999999;
font-weight: bold;
font-style: italic;
}
.highlight .c, .highlight .cd {
color: #999988;
font-style: italic;
}
.highlight .err {
color: #a61717;
background-color: #e3d2d2;
}
.highlight .gd {
color: #000000;
background-color: #ffdddd;
}
.highlight .ge {
color: #000000;
font-style: italic;
}
.highlight .gr {
color: #aa0000;
}
.highlight .gh {
color: #999999;
}
.highlight .gi {
color: #000000;
background-color: #ddffdd;
}
.highlight .go {
color: #888888;
}
.highlight .gp {
color: #555555;
}
.highlight .gs {
font-weight: bold;
}
.highlight .gu {
color: #aaaaaa;
}
.highlight .gt {
color: #aa0000;
}
.highlight .kc {
color: #000000;
font-weight: bold;
}
.highlight .kd {
color: #000000;
font-weight: bold;
}
.highlight .kn {
color: #000000;
font-weight: bold;
}
.highlight .kp {
color: #000000;
font-weight: bold;
}
.highlight .kr {
color: #000000;
font-weight: bold;
}
.highlight .kt {
color: #445588;
font-weight: bold;
}
.highlight .k, .highlight .kv {
color: #000000;
font-weight: bold;
}
.highlight .mf {
color: #009999;
}
.highlight .mh {
color: #009999;
}
.highlight .il {
color: #009999;
}
.highlight .mi {
color: #009999;
}
.highlight .mo {
color: #009999;
}
.highlight .m, .highlight .mb, .highlight .mx {
color: #009999;
}
.highlight .sb {
color: #d14;
}
.highlight .sc {
color: #d14;
}
.highlight .sd {
color: #d14;
}
.highlight .s2 {
color: #d14;
}
.highlight .se {
color: #d14;
}
.highlight .sh {
color: #d14;
}
.highlight .si {
color: #d14;
}
.highlight .sx {
color: #d14;
}
.highlight .sr {
color: #009926;
}
.highlight .s1 {
color: #d14;
}
.highlight .ss {
color: #990073;
}
.highlight .s {
color: #d14;
}
.highlight .na {
color: #008080;
}
.highlight .bp {
color: #999999;
}
.highlight .nb {
color: #0086B3;
}
.highlight .nc {
color: #445588;
font-weight: bold;
}
.highlight .no {
color: #008080;
}
.highlight .nd {
color: #3c5d5d;
font-weight: bold;
}
.highlight .ni {
color: #800080;
}
.highlight .ne {
color: #990000;
font-weight: bold;
}
.highlight .nf {
color: #990000;
font-weight: bold;
}
.highlight .nl {
color: #990000;
font-weight: bold;
}
.highlight .nn {
color: #555555;
}
.highlight .nt {
color: #000080;
}
.highlight .vc {
color: #008080;
}
.highlight .vg {
color: #008080;
}
.highlight .vi {
color: #008080;
}
.highlight .nv {
color: #008080;
}
.highlight .ow {
color: #000000;
font-weight: bold;
}
.highlight .o {
color: #000000;
font-weight: bold;
}
.highlight .w {
color: #bbbbbb;
}

@ -45,7 +45,7 @@ $version-summary-width: 380px
&:hover
opacity: 1
.highlight
span.highlight
background-color: #FCFD8D
&.token-1
background-color: #faa

@ -26,8 +26,6 @@
* See doc/COPYRIGHT.rdoc for more details. ++*/
@import 'openproject/coderay'
div.changeset-changes ul
list-style-type: none
margin: 0

@ -1,175 +0,0 @@
/*-- copyright
OpenProject is a project management system.
Copyright (C) 2012-2015 the OpenProject Foundation (OPF)
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License version 3.
OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
Copyright (C) 2006-2013 Jean-Philippe Lang
Copyright (C) 2010-2013 the ChiliProject Team
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
See doc/COPYRIGHT.rdoc for more details.
++*/
.CodeRay {
background-color: hsl(0,0%,95%);
// border: 1px solid silver;
color: black;
}
.CodeRay pre {
margin: 0px;
}
// Inline-block style for span.CodeRay
code.CodeRay {
display: inline-block;
}
// Block style for code in pre
pre code.CodeRay {
display: block;
// Pre has its own border, so hide coderay's here.
border: none;
}
.CodeRay span {
display: inline-block;
}
span.CodeRay { white-space: pre; border: 0px; padding: 2px; }
table.CodeRay { border-collapse: collapse; width: 100%; padding: 2px; }
table.CodeRay td { padding: 2px 4px; vertical-align: top; }
.CodeRay .line-numbers {
background-color: hsl(180,65%,90%);
color: gray;
text-align: right;
-webkit-user-select: none;
-moz-user-select: none;
user-select: none;
}
.CodeRay .line-numbers a {
background-color: hsl(180,65%,90%) !important;
color: gray !important;
text-decoration: none !important;
}
.CodeRay .line-numbers pre {
word-break: normal;
}
.CodeRay .line-numbers a:target { color: blue !important; }
.CodeRay .line-numbers .highlighted { color: red !important; }
.CodeRay .line-numbers .highlighted a { color: red !important; }
.CodeRay span.line-numbers { padding: 0px 4px; }
.CodeRay .line { display: block; float: left; width: 100%; }
.CodeRay .code { width: 100%; }
.CodeRay .debug { color: white !important; background: blue !important; }
.CodeRay .annotation { color:#007 }
.CodeRay .attribute-name { color:#b48 }
.CodeRay .attribute-value { color:#700 }
.CodeRay .binary { color:#549 }
.CodeRay .binary .char { color:#325 }
.CodeRay .binary .delimiter { color:#325 }
.CodeRay .char { color:#D20 }
.CodeRay .char .content { color:#D20 }
.CodeRay .char .delimiter { color:#710 }
.CodeRay .class { color:#B06; font-weight:bold }
.CodeRay .class-variable { color:#369 }
.CodeRay .color { color:#0A0 }
.CodeRay .comment { color:#777 }
.CodeRay .comment .char { color:#444 }
.CodeRay .comment .delimiter { color:#444 }
.CodeRay .constant { color:#036; font-weight:bold }
.CodeRay .decorator { color:#B0B }
.CodeRay .definition { color:#099; font-weight:bold }
.CodeRay .delimiter { color:black }
.CodeRay .directive { color:#088; font-weight:bold }
.CodeRay .docstring { color:#D42; }
.CodeRay .doctype { color:#34b }
.CodeRay .done { text-decoration: line-through; color: gray }
.CodeRay .entity { color:#800; font-weight:bold }
.CodeRay .error { color:#F00; background-color:#FAA }
.CodeRay .escape { color:#666 }
.CodeRay .exception { color:#C00; font-weight:bold }
.CodeRay .float { color:#60E }
.CodeRay .function { color:#06B; font-weight:bold }
.CodeRay .function .delimiter { color:#024; font-weight:bold }
.CodeRay .global-variable { color:#d70 }
.CodeRay .hex { color:#02b }
.CodeRay .id { color:#33D; font-weight:bold }
.CodeRay .include { color:#B44; font-weight:bold }
.CodeRay .inline { background-color: hsla(0,0%,0%,0.07); color: black }
.CodeRay .inline-delimiter { font-weight: bold; color: #666 }
.CodeRay .instance-variable { color:#33B }
.CodeRay .integer { color:#00D }
.CodeRay .imaginary { color:#f00 }
.CodeRay .important { color:#D00 }
.CodeRay .key { color: #606 }
.CodeRay .key .char { color: #60f }
.CodeRay .key .delimiter { color: #404 }
.CodeRay .keyword { color:#080; font-weight:bold }
.CodeRay .label { color:#970; font-weight:bold }
.CodeRay .local-variable { color:#950 }
.CodeRay .map .content { color:#808 }
.CodeRay .map .delimiter { color:#40A}
.CodeRay .map { background-color:hsla(200,100%,50%,0.06); }
.CodeRay .namespace { color:#707; font-weight:bold }
.CodeRay .octal { color:#40E }
.CodeRay .operator { }
.CodeRay .predefined { color:#369; font-weight:bold }
.CodeRay .predefined-constant { color:#069 }
.CodeRay .predefined-type { color:#0a8; font-weight:bold }
.CodeRay .preprocessor { color:#579 }
.CodeRay .pseudo-class { color:#00C; font-weight:bold }
.CodeRay .regexp { background-color:hsla(300,100%,50%,0.06); }
.CodeRay .regexp .content { color:#808 }
.CodeRay .regexp .delimiter { color:#404 }
.CodeRay .regexp .modifier { color:#C2C }
.CodeRay .reserved { color:#080; font-weight:bold }
.CodeRay .shell { background-color:hsla(120,100%,50%,0.06); }
.CodeRay .shell .content { color:#2B2 }
.CodeRay .shell .delimiter { color:#161 }
.CodeRay .string { background-color:hsla(0,100%,50%,0.05); }
.CodeRay .string .char { color: #b0b }
.CodeRay .string .content { color: #D20 }
.CodeRay .string .delimiter { color: #710 }
.CodeRay .string .modifier { color: #E40 }
.CodeRay .symbol { color:#A60 }
.CodeRay .symbol .content { color:#A60 }
.CodeRay .symbol .delimiter { color:#740 }
.CodeRay .tag { color:#070; font-weight:bold }
.CodeRay .type { color:#339; font-weight:bold }
.CodeRay .value { color: #088 }
.CodeRay .variable { color:#037 }
.CodeRay .insert { background: hsla(120,100%,50%,0.12) }
.CodeRay .delete { background: hsla(0,100%,50%,0.12) }
.CodeRay .change { color: #bbf; background: #007 }
.CodeRay .head { color: #f8f; background: #505 }
.CodeRay .head .filename { color: white; }
.CodeRay .delete .eyecatcher { background-color: hsla(0,100%,50%,0.2); border: 1px solid hsla(0,100%,45%,0.5); margin: -1px; border-bottom: none; border-top-left-radius: 5px; border-top-right-radius: 5px; }
.CodeRay .insert .eyecatcher { background-color: hsla(120,100%,50%,0.2); border: 1px solid hsla(120,100%,25%,0.5); margin: -1px; border-top: none; border-bottom-left-radius: 5px; border-bottom-right-radius: 5px; }
.CodeRay .insert .insert { color: #0c0; background:transparent; font-weight:bold }
.CodeRay .delete .delete { color: #c00; background:transparent; font-weight:bold }
.CodeRay .change .change { color: #88f }
.CodeRay .head .head { color: #f4f }

@ -300,7 +300,7 @@ module ApplicationHelper
end
def syntax_highlight(name, content)
highlighted = Redmine::SyntaxHighlighting.highlight_by_filename(content, name)
highlighted = OpenProject::SyntaxHighlighting.highlight_by_filename(content, name)
highlighted.each_line do |line|
yield highlighted.html_safe? ? line.html_safe : line
end

@ -27,7 +27,7 @@ See docs/COPYRIGHT.rdoc for more details.
++#%>
<div class="autoscroll">
<table class="filecontent CodeRay">
<table class="filecontent highlight">
<tbody>
<% line_num = 1 %>
<% syntax_highlight(filename, to_utf8_for_attachments(content)) do |line| %>

@ -41,7 +41,7 @@ See docs/COPYRIGHT.rdoc for more details.
<% else %>
<% colors = Hash.new {|k,v| k[v] = (k.size % 12) } %>
<div class="autoscroll">
<table class="filecontent annotate CodeRay">
<table class="filecontent annotate highlight">
<tbody>
<% line_num = 1 %>
<% syntax_highlight(@path, to_utf8_for_repositories(@annotate.content)) do |line| %>

@ -27,6 +27,6 @@ See docs/COPYRIGHT.rdoc for more details.
++#%>
<div class="wiki wiki-content">
<div class="wiki wiki-content highlight">
<%= format_text content, :text, attachments: content.page.attachments %>
</div>

@ -79,7 +79,7 @@
[textContent]="text.fields.description"></h3>
</div>
</div>
<div class="single-attribute wiki work-packages--details--description">
<div class="single-attribute work-packages--details--description">
<wp-edit-field [fieldName]="'description'"
[workPackageId]="workPackage.id"
[wrapperClasses]="'-no-label'"

@ -134,6 +134,7 @@ export class WorkPackageEditFieldComponent implements OnInit {
}
public activate(noWarnings:boolean = false):Promise<WorkPackageEditFieldHandler> {
this.active = true;
return this.activateOnForm(this.wpEditFieldGroup.form, noWarnings);
}

@ -32,8 +32,7 @@ import {ExpressionService} from "../../../../../../common/expression.service";
export class FormattableDisplayField extends DisplayField {
public render(element:HTMLElement, displayText:string):void {
jQuery(element).addClass('-multiline');
jQuery(element).addClass('read-value--html');
element.classList.add('read-value--html', 'wiki', 'highlight', '-multiline');
let span = document.createElement('span');
span.innerHTML = displayText;

@ -27,47 +27,33 @@
# See docs/COPYRIGHT.rdoc for more details.
#++
module Redmine
module OpenProject
module SyntaxHighlighting
class << self
attr_reader :highlighter
delegate :highlight_by_filename, :highlight_by_language, to: :highlighter
# Highlights +text+ as the content of +filename+
# Should not return line numbers nor outer pre tag
# use CodeRay to scan normal text, since it's smart enough to find
# the correct source encoding before passing it to ERB::Util.html_escape
def highlight_by_filename(text, filename)
language = guess_lexer(text, filename)
def highlighter=(name)
if name.is_a?(Module)
@highlighter = name
else
@highlighter = const_get(name)
end
highlight_by_language(text, language)
end
end
module CodeRay
require 'coderay'
require 'coderay/helpers/file_type'
# Highlights +text+ using +language+ syntax
def highlight_by_language(text, language, formatter = Rouge::Formatters::HTML.new)
Rouge.highlight(text, language, formatter).html_safe
end
class << self
# Highlights +text+ as the content of +filename+
# Should not return line numbers nor outer pre tag
# use CodeRay to scan normal text, since it's smart enough to find
# the correct source encoding before passing it to ERB::Util.html_escape
def highlight_by_filename(text, filename)
language = ::CodeRay::FileType[filename]
if language
::CodeRay.scan(text, language).html.html_safe
else
ERB::Util.h(::CodeRay.scan(text, :text).text)
end
end
##
# Guesses the appropriate lexer for the given text using rouge's guesser
# Can be used to extract information using the lexer's name, tag, desc methods
def guess_lexer(text, filename = nil)
guessers = [Rouge::Guessers::Source.new(text)]
guessers << Rouge::Guessers::Filename.new(filename) if filename.present?
# Highlights +text+ using +language+ syntax
# Should not return outer pre tag
def highlight_by_language(text, language)
::CodeRay.scan(text, language).html(line_numbers: :inline, wrap: :span)
end
Rouge::Lexer::guess guessers: guessers
end
end
end
SyntaxHighlighting.highlighter = 'CodeRay'
end

@ -33,22 +33,41 @@ module OpenProject::TextFormatting
class MarkdownFilter < HTML::Pipeline::MarkdownFilter
# Convert Markdown to HTML using CommonMarker
def call
parse_options = %i[LIBERAL_HTML_TAG]
render_options = %i[GITHUB_PRE_LANG]
render_options << :HARDBREAKS if context[:gfm] != false
render_html parse
end
extensions = context.fetch :commonmarker_extensions,
%i[table strikethrough tagfilter]
private
##
# Get initial CommonMarker AST for further processing
#
def parse
parse_options = %i[LIBERAL_HTML_TAG]
# We need liberal html tags thus parsing and rendering are several steps
# Check: We may be able to reuse the ast instead of rendering to html and then parsing with nokogiri again.
ast = CommonMarker.render_doc(text, parse_options, extensions)
html = ast.to_html(render_options, extensions)
html.rstrip!
CommonMarker.render_doc(
text,
parse_options,
commonmark_extensions
)
end
##
# Render the transformed AST
def render_html(ast)
render_options = %i[GITHUB_PRE_LANG]
render_options << :HARDBREAKS if context[:gfm] != false
ast
.to_html(render_options, commonmark_extensions)
.tap(&:rstrip!)
end
html
##
# Extensions to the default CommonMarker operation
def commonmark_extensions
context.fetch :commonmarker_extensions, %i[table strikethrough tagfilter]
end
end
end

@ -0,0 +1,53 @@
#-- encoding: UTF-8
#-- copyright
# OpenProject is a project management system.
# Copyright (C) 2012-2017 the OpenProject Foundation (OPF)
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 3.
#
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
# Copyright (C) 2006-2017 Jean-Philippe Lang
# Copyright (C) 2010-2013 the ChiliProject Team
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# See doc/COPYRIGHT.rdoc for more details.
#++
module OpenProject::TextFormatting
module Filters
class SyntaxHighlightFilter < HTML::Pipeline::SyntaxHighlightFilter
def initialize(*args)
super(*args)
@formatter = highlighter_class
end
##
# Get highlighter class for the current context
def highlighter_class
# Get syntax highlighting options. If we're in a CSS-constrained environment (i.e., mail),
# inline syntax highlighting.
if context[:inline_css]
Rouge::Formatters::HTMLInline.new Rouge::Themes::Github
else
Rouge::Formatters::HTML.new
end
end
end
end
end

@ -56,7 +56,8 @@ module OpenProject::TextFormatting::Formatters
HTML::Pipeline::TableOfContentsFilter,
:macro,
:pattern_matcher,
:autolink
:autolink,
:syntax_highlight
]
end
end

@ -67,7 +67,7 @@ module OpenProject::TextFormatting::Formatters
content = @pre_list[$1.to_i]
if content.match(/<code\s+class="(\w+)">\s?(.+)/m)
content = "<code class=\"#{$1} CodeRay\">" +
Redmine::SyntaxHighlighting.highlight_by_language($2, $1)
OpenProject::SyntaxHighlighting.highlight_by_language($2, $1)
end
content
end

@ -333,8 +333,7 @@ EXPECTED
RAW
expected = <<-EXPECTED
<pre><code class="ruby CodeRay"><span class=\"CodeRay\"><span class="line-numbers"><a href=\"#n1\" name=\"n1\">1</a></span><span class="comment"># Some ruby code here</span></span>
</code></pre>
<pre><code class=\"ruby CodeRay\"><span class=\"c1\"># Some ruby code here</span></code></pre>
EXPECTED
assert_equal expected.gsub(%r{[\r\n\t]}, ''), helper.format_text(raw).gsub(%r{[\r\n\t]}, '')

Loading…
Cancel
Save