Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Fix #450: syntax_highlight_each_line #73

Open
wants to merge 3 commits into from

2 participants

Ruby-chan Kornelius Kalnbach
Ruby-chan

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 3 unique commits by 1 author.

Jun 05, 2011
Kornelius Kalnbach Fix #450: syntax_highlight_each_line 8d988e6
Aug 21, 2011
Kornelius Kalnbach Merge branch 'master' into fix-450-highlight-multi-line-tokens
Conflicts:
	app/views/common/_file.rhtml
	app/views/repositories/annotate.rhtml
1bd7475
Oct 29, 2011
Kornelius Kalnbach Merge branch 'master' of git://github.com/chiliproject/chiliproject i…
…nto fix-450-highlight-multi-line-tokens
51e4253
This page is out of date. Refresh to see the latest.
16  app/helpers/application_helper.rb
@@ -327,6 +327,22 @@ def time_tag(time)
327 327
   def syntax_highlight(name, content)
328 328
     Redmine::SyntaxHighlighting.highlight_by_filename(content, name)
329 329
   end
  330
+  
  331
+  # Call syntax_highlight and make sure that tags do not spread across
  332
+  # multiple lines.
  333
+  def syntax_highlight_each_line(name, content)
  334
+    highlighted = syntax_highlight(name, content)
  335
+    
  336
+    nesting = []
  337
+    highlighted.scan(/^(.*)\n?/) do |line,|
  338
+      open = nesting.join
  339
+      line.scan(%r!<(/)?span[^>]*>?!) do |is_close_tag,|
  340
+        is_close_tag ? nesting.pop : nesting.push($&)
  341
+      end
  342
+      close = '</span>' * nesting.size
  343
+      yield "#{open}#{line}#{close}\n"
  344
+    end
  345
+  end
330 346
 
331 347
   def to_path_param(path)
332 348
     path.to_s.split(%r{[/\\]}).select {|p| !p.blank?}
2  app/views/common/_file.rhtml
@@ -2,7 +2,7 @@
2 2
 <table class="filecontent syntaxhl">
3 3
 <tbody>
4 4
 <% line_num = 1 %>
5  
-<% syntax_highlight(filename, to_utf8_for_attachments(content)).each_line do |line| %>
  5
+<% syntax_highlight_each_line(filename, to_utf8_for_attachments(content)) do |line| %>
6 6
 <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>
7 7
 <% line_num += 1 %>
8 8
 <% end %>
2  app/views/repositories/annotate.rhtml
@@ -14,7 +14,7 @@
14 14
 <table class="filecontent annotate syntaxhl">
15 15
   <tbody>
16 16
     <% line_num = 1 %>
17  
-    <% syntax_highlight(@path, to_utf8_for_repositories(@annotate.content)).each_line do |line| %>
  17
+    <% syntax_highlight_each_line(@path, to_utf8_for_repositories(@annotate.content)) do |line| %>
18 18
     <% revision = @annotate.revisions[line_num-1] %>
19 19
     <tr class="bloc-<%= revision.nil? ? 0 : colors[revision.identifier || revision.revision] %>">
20 20
       <th class="line-num" id="L<%= line_num %>"><a href="#L<%= line_num %>"><%= line_num %></a></th>
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.