-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
9 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |