-
Notifications
You must be signed in to change notification settings - Fork 409
/
highlighter.rb
48 lines (40 loc) · 1012 Bytes
/
highlighter.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# frozen_string_literal: true
require 'rouge'
# While Rouge is downlevel (Rouge PR#1715 unreleased)
module Rouge
module Lexers
class Swift
prepend :root do
rule(/\b(?:async|await|isolated)\b/, Keyword)
rule(/\b(?:actor|nonisolated)\b/, Keyword::Declaration)
end
end
end
end
module Jazzy
# This module helps highlight code
module Highlighter
SWIFT = 'swift'
OBJC = 'objective_c'
class Formatter < Rouge::Formatters::HTML
def initialize(language)
@language = language
super()
end
def stream(tokens, &block)
yield "<pre class=\"highlight #{@language}\"><code>"
super
yield "</code></pre>\n"
end
end
def self.highlight_swift(source)
highlight(source, SWIFT)
end
def self.highlight_objc(source)
highlight(source, OBJC)
end
def self.highlight(source, language)
source && Rouge.highlight(source, language, Formatter.new(language))
end
end
end