Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for syntax highlighting with new RubyParser AST
- Loading branch information
Showing
6 changed files
with
99 additions
and
40 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
49 changes: 49 additions & 0 deletions
49
lib/yard/generators/helpers/html_syntax_highlight_helper.rb
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
module YARD | ||
module Generators | ||
module Helpers | ||
module HtmlSyntaxHighlightHelper | ||
def html_syntax_highlight(source) | ||
tokenlist = Parser::Ruby::RubyParser.parse(source, "(syntax_highlight)").tokens | ||
output = "" | ||
tokenlist.each do |s| | ||
output << "<span class='tstring'>" if [:tstring_beg, :regexp_beg].include?(s[0]) | ||
case s.first | ||
when :nl, :ignored_nl, :sp | ||
output << h(s.last) | ||
when :ident | ||
output << "<span class='id #{h(s.last)}'>#{h(s.last)}</span>" | ||
else | ||
output << "<span class='#{s.first}'>#{h(s.last)}</span>" | ||
end | ||
output << "</span>" if [:tstring_end, :regexp_end].include?(s[0]) | ||
end | ||
output | ||
rescue Parser::Ruby::ParserSyntaxError | ||
source | ||
end | ||
|
||
def html_syntax_highlight_legacy(source) | ||
tokenlist = Parser::Ruby::Legacy::TokenList.new(source) | ||
tokenlist.map do |s| | ||
prettyclass = s.class.class_name.sub(/^Tk/, '').downcase | ||
prettysuper = s.class.superclass.class_name.sub(/^Tk/, '').downcase | ||
|
||
case s | ||
when Parser::Ruby::Legacy::RubyToken::TkWhitespace, Parser::Ruby::Legacy::RubyToken::TkUnknownChar | ||
h s.text | ||
when Parser::Ruby::Legacy::RubyToken::TkId | ||
prettyval = h(s.text) | ||
"<span class='#{prettyval} #{prettyclass} #{prettysuper}'>#{prettyval}</span>" | ||
else | ||
"<span class='#{prettyclass} #{prettysuper}'>#{h s.text}</span>" | ||
end | ||
end.join | ||
end | ||
|
||
if RUBY18 | ||
alias html_syntax_highlight html_syntax_highlight_legacy | ||
end | ||
end | ||
end | ||
end | ||
end |
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