Skip to content

Commit

Permalink
Use CodeRay for syntax highlighting
Browse files Browse the repository at this point in the history
  • Loading branch information
mvidner committed Aug 17, 2017
1 parent 68bacb7 commit c466707
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 15 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Identifies fully qualified class names and makes an inheritance graph
- [Graphviz](http://www.graphviz.org/) (graph visualizer) - [Graphviz](http://www.graphviz.org/) (graph visualizer)
- [Sinatra](http://www.sinatrarb.com/) (a small web framework) - [Sinatra](http://www.sinatrarb.com/) (a small web framework)
- [Cheetah](https://github.com/openSUSE/cheetah) (runs commands) - [Cheetah](https://github.com/openSUSE/cheetah) (runs commands)
- [CodeRay](http://coderay.rubychan.de/) (syntax highlighting)


## License ## License


Expand Down
1 change: 1 addition & 0 deletions code-explorer.gemspec
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ Gem::Specification.new do |s|
s.add_dependency "parser", "~> 2.0" s.add_dependency "parser", "~> 2.0"
s.add_dependency "sinatra", "~> 1" s.add_dependency "sinatra", "~> 1"
s.add_dependency "cheetah", "~> 0" # for calling dot (graphviz.rpm) s.add_dependency "cheetah", "~> 0" # for calling dot (graphviz.rpm)
s.add_dependency "coderay", "~> 1" # syntax highlighting
end end
22 changes: 7 additions & 15 deletions lib/code_explorer/numbered_lines.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,18 +1,10 @@
require "coderay"


# relies on escape_html # Convert plain text to HTML where lines are hyperlinkable.

# Emulate RFC 5147 fragment identifier: #line=42
# convert plain text to HTML where lines are hyperlinkable (emulate RFC 5147)
def numbered_lines(text) def numbered_lines(text)
lines = text.lines # but CodeRay wants to remove the equal sign;
count_width = lines.count.to_s.size tag = "lI" + "-Ne" # avoid the literal tag if we process our own source
lines.each_with_index.map do |line, i| html = CodeRay.scan(text, :ruby).page(line_number_anchors: tag)
i += 1 # lines are counted from 1 html.gsub(tag, "line=")

show_line_num = i.to_s.rjust(count_width).gsub(" ", " ")
escaped_line = escape_html(line.chomp).gsub(" ", " ")
id = "line=#{i}" # RFC 5147 fragment identifier

"<tt><a id='#{id}' href='##{id}'>#{show_line_num}</a></tt> " \
"<code>#{escaped_line}</code><br>\n"
end.join("")
end end

0 comments on commit c466707

Please sign in to comment.