Class: AuthorEngine::CodeEditor::Highlighting
- Inherits:
-
Object
- Object
- AuthorEngine::CodeEditor::Highlighting
- Includes:
- Part::Colors
- Defined in:
- lib/author_engine/code_editor/highlighting.rb
Constant Summary
Constants included from Part::Colors
Instance Method Summary collapse
- #highlight(string:, text:) ⇒ Object
-
#initialize ⇒ Highlighting
constructor
A new instance of Highlighting.
- #style(text:, token:) ⇒ Object
Methods included from Part::Colors
#black, #blue, #brown, #dark_blue, #dark_gray, #dark_green, #dark_purple, #green, #indigo, #light_gray, #orange, #peach, #pink, #red, #rgb, #white, #xml_color, #yellow
Constructor Details
#initialize ⇒ Highlighting
Returns a new instance of Highlighting.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/author_engine/code_editor/highlighting.rb', line 6 def initialize @highlight_colors = { instance_variable: xml_color(green), keyword: xml_color(red), method: xml_color(yellow), ident: xml_color(yellow), comment: xml_color(dark_gray), constant: xml_color(orange), delimiter: xml_color(blue), content: xml_color(blue), integer: xml_color(blue), float: xml_color(blue), symbol: xml_color(blue), } @last_text = "" end |
Instance Method Details
#highlight(string:, text:) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/author_engine/code_editor/highlighting.rb', line 25 def highlight(string:, text:) return unless @last_text != string @last_text = string buffer = "" tokens = CodeRay.scan(string, :ruby).tokens tokens.each_with_index do |token, index| buffer = "#{buffer}#{style(text: token, token: tokens[index+1])}" if token.is_a?(String) end text. = buffer end |
#style(text:, token:) ⇒ Object
39 40 41 42 43 44 45 46 |
# File 'lib/author_engine/code_editor/highlighting.rb', line 39 def style(text:, token:) color = @highlight_colors.dig(token) if color return "<c=#{color}>#{text}</c>" else return text end end |