-
Notifications
You must be signed in to change notification settings - Fork 9
/
arend-highlighter.rb
94 lines (75 loc) · 2.12 KB
/
arend-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
49
50
51
52
53
54
55
56
57
58
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
require 'rly'
class ArendLexer < Rly::Lex
start_char = '~!@#\$%\^&\*\-\+=<>\?/\|\[\]:a-zA-Z_|\u005D\u2200-\u22FF'
id = "[#{start_char}][#{start_char}0-9']*"
token(:COMMENT, /\-\- .*|\{-([^-]|-+[^}])*-\}/) do |tok|
tok.value = "<span class=\'c\'>#{tok.value}</span>"
tok
end
token(:OPERATOR, /(=>|@|==|=|\+|\*|\$)'*|->|:|,|\||`#{id}`?/) do |tok|
tok.value = "<span class=\'o\'>#{tok.value}</span>"
tok
end
token(:UNKNOWN, /_/) do |tok|
tok.value = "<span class=\'u\'>#{tok.value}</span>"
tok
end
token(:GOAL, /\{\?(#{id})?\}/) do |tok|
tok.value = "<span class=\'g\'>#{tok.value}</span>"
tok
end
token(:ID, /#{id}/)
token(:PROP, /\\Prop/) do |tok|
tok.value = "<span class=\'kt\'>#{tok.value}</span>"
tok
end
token(:SET, /\\Set[0-9]*/) do |tok|
tok.value = "<span class=\'kt\'>#{tok.value}</span>"
tok
end
token(:TYPE, /\\([0-9]+-)?Type[0-9]*/) do |tok|
tok.value = "<span class=\'kt\'>#{tok.value}</span>"
tok
end
token(:KW, /\\[^\s]+/) do |tok|
tok.value = "<span class=\'k\'>#{tok.value}</span>"
tok
end
token(:NUMBER, /-?[0-9]+/) do |tok|
tok.value = "<span class=\'n\'>#{tok.value}</span>"
tok
end
token(:ANY, /[ \t\r\n\(\)\.]+/)
token(:BRACE, /[\{\}]/)
on_error do |tok|
puts "Illegal character: #{tok.value} at #{tok.lexer.pos}"
tok.lexer.pos += 1
nil
end
end
class ArendHighlighter < Liquid::Block
def render(context)
lexer = ArendLexer.new(super(context).strip)
s = StringIO.new
s << "<div class=\'highlighter-rouge\'><div class=\'highlight\'><pre class=\'highlight\'><code>"
while tok = lexer.next do
s << tok.value
end
s << "</code></pre></div></div>"
s.string
end
end
class ArendInlineHighlighter < Liquid::Block
def render(context)
lexer = ArendLexer.new(super(context).strip)
s = StringIO.new
s << "<span class=\"inl-highlight\">"
while tok = lexer.next do
s << tok.value
end
s << "</span>"
s.string
end
end
Liquid::Template.register_tag("arend", ArendHighlighter)
Liquid::Template.register_tag("ard", ArendInlineHighlighter)