Module: MiniSyntax::Highlighter::JavaScript

Defined in:
lib/minisyntax/highlighter/javascript.rb

Class Method Summary collapse

Class Method Details

.highlight(code) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/minisyntax/highlighter/javascript.rb', line 4

def self.highlight(code)
  keywords = %w(break continue do for import new this void case default else function in return typeof while comment delete export if label switch var with)
  keywords += %w(catch enum throw class extends try const finally debugger super)
  code.gsub! /\b(#{keywords.join('|')})\b/, "<em>\\1</em>"
  code.gsub! /\b([A-Z_][a-zA-Z0-9_]+)\b/, "<b>\\1</b>"
  code.gsub! /("(.*?)"|'.*?')/, "<q>\\1</q>"
  code.gsub! %r((//.*?)$) do |comment|
    if comment.gsub(%r(<q>(.*?)</q>), "\\1") =~ %r(</q>)
      comment
    else
      comment.gsub! %r(</?(b|i|em|var|code)>), ""
      %Q(<i>#{comment}</i>)
    end
  end
  code
end