Skip to content
Permalink
Browse files

Use CodeRay for syntax highlighting

  • Loading branch information...
mvidner committed Nov 11, 2016
1 parent 68bacb7 commit c466707cade7c3c66177102a0e3f07ad41dff493
Showing with 9 additions and 15 deletions.
  1. +1 −0 README.md
  2. +1 −0 code-explorer.gemspec
  3. +7 −15 lib/code_explorer/numbered_lines.rb
@@ -24,6 +24,7 @@ Identifies fully qualified class names and makes an inheritance graph
- [Graphviz](http://www.graphviz.org/) (graph visualizer)
- [Sinatra](http://www.sinatrarb.com/) (a small web framework)
- [Cheetah](https://github.com/openSUSE/cheetah) (runs commands)
- [CodeRay](http://coderay.rubychan.de/) (syntax highlighting)

## License

@@ -36,4 +36,5 @@ Gem::Specification.new do |s|
s.add_dependency "parser", "~> 2.0"
s.add_dependency "sinatra", "~> 1"
s.add_dependency "cheetah", "~> 0" # for calling dot (graphviz.rpm)
s.add_dependency "coderay", "~> 1" # syntax highlighting
end
@@ -1,18 +1,10 @@
require "coderay"

# relies on escape_html

# convert plain text to HTML where lines are hyperlinkable (emulate RFC 5147)
# Convert plain text to HTML where lines are hyperlinkable.
# Emulate RFC 5147 fragment identifier: #line=42
def numbered_lines(text)
lines = text.lines
count_width = lines.count.to_s.size
lines.each_with_index.map do |line, i|
i += 1 # lines are counted from 1

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("")
# but CodeRay wants to remove the equal sign;
tag = "lI" + "-Ne" # avoid the literal tag if we process our own source
html = CodeRay.scan(text, :ruby).page(line_number_anchors: tag)
html.gsub(tag, "line=")
end

0 comments on commit c466707

Please sign in to comment.
You can’t perform that action at this time.