This repository has been archived by the owner on Sep 8, 2023. It is now read-only.
forked from rouge-ruby/rouge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bibtex.rb
115 lines (98 loc) · 2.71 KB
/
bibtex.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# -*- coding: utf-8 -*- #
# frozen_string_literal: true
# Regular expressions based on https://github.com/SaswatPadhi/prismjs-bibtex
# and https://github.com/alecthomas/chroma/blob/master/lexers/b/bibtex.go
module Rouge
module Lexers
class BibTeX < RegexLexer
title 'BibTeX'
desc "BibTeX"
tag 'bibtex'
aliases 'bib'
filenames '*.bib'
valid_punctuation = Regexp.quote("@!$&.\\:;<>?[]^`|~*/+-")
valid_name = /[a-z_#{valid_punctuation}][\w#{valid_punctuation}]*/io
state :root do
mixin :whitespace
rule %r/@(#{valid_name})/o do |m|
match = m[1].downcase
if match == "comment"
token Comment
elsif match == "preamble"
token Name::Class
push :closing_brace
push :value
push :opening_brace
elsif match == "string"
token Name::Class
push :closing_brace
push :field
push :opening_brace
else
token Name::Class
push :closing_brace
push :command_body
push :opening_brace
end
end
rule %r/.+/, Comment
end
state :opening_brace do
mixin :whitespace
rule %r/[{(]/, Punctuation, :pop!
end
state :closing_brace do
mixin :whitespace
rule %r/[})]/, Punctuation, :pop!
end
state :command_body do
mixin :whitespace
rule %r/[^\s\,\}]+/ do
token Name::Label
pop!
push :fields
end
end
state :fields do
mixin :whitespace
rule %r/,/, Punctuation, :field
rule(//) { pop! }
end
state :field do
mixin :whitespace
rule valid_name do
token Name::Attribute
push :value
push :equal_sign
end
rule(//) { pop! }
end
state :equal_sign do
mixin :whitespace
rule %r/=/, Punctuation, :pop!
end
state :value do
mixin :whitespace
rule valid_name, Name::Variable
rule %r/"/, Literal::String, :quoted_string
rule %r/\{/, Literal::String, :braced_string
rule %r/\d+/, Literal::Number
rule %r/#/, Punctuation
rule(//) { pop! }
end
state :quoted_string do
rule %r/\{/, Literal::String, :braced_string
rule %r/"/, Literal::String, :pop!
rule %r/[^\{\"]+/, Literal::String
end
state :braced_string do
rule %r/\{/, Literal::String, :braced_string
rule %r/\}/, Literal::String, :pop!
rule %r/[^\{\}]+/, Literal::String
end
state :whitespace do
rule %r/\s+/, Text
end
end
end
end