Class: Scriptorium::SyntaxHighlighter

Inherits:
Object
  • Object
show all
Defined in:
lib/scriptorium/syntax_highlighter.rb

Constant Summary collapse

DEFAULT_THEME =

Default theme - can be customized

{
  'keyword' => 'color: #07a; font-weight: bold;',
  'string' => 'color: #690;',
  'comment' => 'color: #708090; font-style: italic;',
  'function' => 'color: #dd4a68;',
  'number' => 'color: #905;',
  'symbol' => 'color: #905;',
  'operator' => 'color: #9a6e3a;',
  'class-name' => 'color: #dd4a68;',
  'regex' => 'color: #e90;',
  'variable' => 'color: #e90;',
  'interpolation' => 'color: #690;',  # same as string
  'error' => 'color: #d73a49; font-weight: bold;',
  'whitespace' => 'color: transparent;',
  'other' => 'color: #24292e;'
}

Instance Method Summary collapse

Constructor Details

#initialize(theme = nil) ⇒ SyntaxHighlighter

Returns a new instance of SyntaxHighlighter.



23
24
25
26
# File 'lib/scriptorium/syntax_highlighter.rb', line 23

def initialize(theme = nil)
  @theme = theme || DEFAULT_THEME
  @formatter = Rouge::Formatters::HTML.new
end

Instance Method Details

#generate_cssObject

Generate CSS for the theme



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/scriptorium/syntax_highlighter.rb', line 59

def generate_css
  css = []
  css << "/* Syntax highlighting styles */"
  css << "pre {"
  css << "  background: #f5f5f5;"
  css << "  padding: 15px;"
  css << "  border-radius: 5px;"
  css << "  overflow-x: auto;"
  css << "}"
  css << ""
  css << "pre code {"
  css << "  font-family: 'Consolas', 'Monaco', 'Andale Mono', monospace;"
  css << "  font-size: 14px;"
  css << "}"
  css << ""
  css << "/* Override Bootstrap height constraints for syntax highlighting */"
  css << "pre code[class*=\"language-\"] {"
  css << "  height: auto !important;"
  css << "  max-height: none !important;"
  css << "  min-height: auto !important;"
  css << "  overflow: visible !important;"
  css << "}"
  css << ""
  css << "pre:has(code[class*=\"language-\"]) {"
  css << "  height: auto !important;"
  css << "  max-height: none !important;"
  css << "  min-height: auto !important;"
  css << "  overflow: visible !important;"
  css << "}"
  
  @theme.each do |token, style|
    css << ".token.#{token} { #{style} }"
  end
  
  css.join("\n")
end

#highlight(code, language = nil) ⇒ Object

Highlight code with language detection



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/scriptorium/syntax_highlighter.rb', line 29

def highlight(code, language = nil)
  return escape_html(code) if language.nil? || language.empty?
  
  language = language.downcase.strip
  
  # Map common language names to Rouge lexers
  lexer_name = map_language(language)
  return escape_html(code) unless lexer_name
  
  begin
    lexer = Rouge::Lexer.find(lexer_name)
    puts "DEBUG: Found lexer: #{lexer.class}"
    highlighted = @formatter.format(lexer.lex(code))
    puts "DEBUG: Rouge output length: #{highlighted.length}"
    puts "DEBUG: Raw Rouge output (first 500 chars): #{highlighted[0..500]}"
    puts "DEBUG: HTML contains <div>: #{highlighted.include?('<div')}"
    puts "DEBUG: HTML contains <br>: #{highlighted.include?('<br')}"
    puts "DEBUG: HTML contains \\n: #{highlighted.include?("\n")}"
    
    # Massage the output to use our CSS classes
    massage_output(highlighted, language)
  rescue => e
    # Fallback to escaped HTML if highlighting fails
    puts "DEBUG: Rouge highlighting failed for #{language}: #{e.message}"
    puts "DEBUG: Backtrace: #{e.backtrace.first(3).join(', ')}"
    escape_html(code)
  end
end