Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Fix #450: syntax_highlight_each_line #73

Closed
wants to merge 3 commits into from

3 participants

Ruby-chan Holger Just Kornelius Kalnbach
Ruby-chan

A fix for highlighting in repo file actions "view" and "annotate": Multi-line tokens are highlighted correctly.

korny added some commits
Holger Just meineerde closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jun 5, 2011
  1. Kornelius Kalnbach
Commits on Aug 21, 2011
  1. Kornelius Kalnbach

    Merge branch 'master' into fix-450-highlight-multi-line-tokens

    korny authored
    Conflicts:
    	app/views/common/_file.rhtml
    	app/views/repositories/annotate.rhtml
Commits on Oct 29, 2011
  1. Kornelius Kalnbach

    Merge branch 'master' of git://github.com/chiliproject/chiliproject i…

    korny authored
    …nto fix-450-highlight-multi-line-tokens
This page is out of date. Refresh to see the latest.
16 app/helpers/application_helper.rb
View
@@ -327,6 +327,22 @@ def time_tag(time)
def syntax_highlight(name, content)
Redmine::SyntaxHighlighting.highlight_by_filename(content, name)
end
+
+ # Call syntax_highlight and make sure that tags do not spread across
+ # multiple lines.
+ def syntax_highlight_each_line(name, content)
+ highlighted = syntax_highlight(name, content)
+
+ nesting = []
+ highlighted.scan(/^(.*)\n?/) do |line,|
+ open = nesting.join
+ line.scan(%r!<(/)?span[^>]*>?!) do |is_close_tag,|
+ is_close_tag ? nesting.pop : nesting.push($&)
+ end
+ close = '</span>' * nesting.size
+ yield "#{open}#{line}#{close}\n"
+ end
+ end
def to_path_param(path)
path.to_s.split(%r{[/\\]}).select {|p| !p.blank?}
2  app/views/common/_file.rhtml
View
@@ -2,7 +2,7 @@
<table class="filecontent syntaxhl">
<tbody>
<% line_num = 1 %>
-<% syntax_highlight(filename, to_utf8_for_attachments(content)).each_line do |line| %>
+<% syntax_highlight_each_line(filename, to_utf8_for_attachments(content)) do |line| %>
<tr><th class="line-num" id="L<%= line_num %>"><a href="#L<%= line_num %>"><%= line_num %></a></th><td class="line-code"><pre><%= line %></pre></td></tr>
<% line_num += 1 %>
<% end %>
2  app/views/repositories/annotate.rhtml
View
@@ -14,7 +14,7 @@
<table class="filecontent annotate syntaxhl">
<tbody>
<% line_num = 1 %>
- <% syntax_highlight(@path, to_utf8_for_repositories(@annotate.content)).each_line do |line| %>
+ <% syntax_highlight_each_line(@path, to_utf8_for_repositories(@annotate.content)) do |line| %>
<% revision = @annotate.revisions[line_num-1] %>
<tr class="bloc-<%= revision.nil? ? 0 : colors[revision.identifier || revision.revision] %>">
<th class="line-num" id="L<%= line_num %>"><a href="#L<%= line_num %>"><%= line_num %></a></th>
Something went wrong with that request. Please try again.