gitlab-rouge 1.9.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +23 -0
- data/LICENSE +186 -0
- data/bin/rougify +16 -0
- data/gitlab-rouge.gemspec +17 -0
- data/lib/rouge.rb +57 -0
- data/lib/rouge/cli.rb +363 -0
- data/lib/rouge/demos/apache +21 -0
- data/lib/rouge/demos/applescript +2 -0
- data/lib/rouge/demos/c +8 -0
- data/lib/rouge/demos/clojure +5 -0
- data/lib/rouge/demos/coffeescript +5 -0
- data/lib/rouge/demos/common_lisp +1 -0
- data/lib/rouge/demos/conf +4 -0
- data/lib/rouge/demos/cpp +8 -0
- data/lib/rouge/demos/csharp +5 -0
- data/lib/rouge/demos/css +4 -0
- data/lib/rouge/demos/dart +6 -0
- data/lib/rouge/demos/diff +7 -0
- data/lib/rouge/demos/elixir +1 -0
- data/lib/rouge/demos/erb +1 -0
- data/lib/rouge/demos/erlang +7 -0
- data/lib/rouge/demos/factor +5 -0
- data/lib/rouge/demos/gherkin +17 -0
- data/lib/rouge/demos/glsl +14 -0
- data/lib/rouge/demos/go +7 -0
- data/lib/rouge/demos/groovy +9 -0
- data/lib/rouge/demos/haml +5 -0
- data/lib/rouge/demos/handlebars +7 -0
- data/lib/rouge/demos/haskell +6 -0
- data/lib/rouge/demos/html +8 -0
- data/lib/rouge/demos/http +14 -0
- data/lib/rouge/demos/ini +4 -0
- data/lib/rouge/demos/io +11 -0
- data/lib/rouge/demos/java +5 -0
- data/lib/rouge/demos/javascript +1 -0
- data/lib/rouge/demos/json +1 -0
- data/lib/rouge/demos/json-doc +1 -0
- data/lib/rouge/demos/liquid +11 -0
- data/lib/rouge/demos/literate_coffeescript +3 -0
- data/lib/rouge/demos/literate_haskell +7 -0
- data/lib/rouge/demos/llvm +20 -0
- data/lib/rouge/demos/lua +12 -0
- data/lib/rouge/demos/make +6 -0
- data/lib/rouge/demos/markdown +4 -0
- data/lib/rouge/demos/matlab +6 -0
- data/lib/rouge/demos/moonscript +16 -0
- data/lib/rouge/demos/nginx +5 -0
- data/lib/rouge/demos/nim +27 -0
- data/lib/rouge/demos/objective_c +14 -0
- data/lib/rouge/demos/ocaml +12 -0
- data/lib/rouge/demos/perl +5 -0
- data/lib/rouge/demos/php +3 -0
- data/lib/rouge/demos/plaintext +1 -0
- data/lib/rouge/demos/powershell +49 -0
- data/lib/rouge/demos/prolog +9 -0
- data/lib/rouge/demos/properties +7 -0
- data/lib/rouge/demos/puppet +6 -0
- data/lib/rouge/demos/python +6 -0
- data/lib/rouge/demos/qml +9 -0
- data/lib/rouge/demos/r +8 -0
- data/lib/rouge/demos/racket +24 -0
- data/lib/rouge/demos/ruby +9 -0
- data/lib/rouge/demos/rust +12 -0
- data/lib/rouge/demos/sass +3 -0
- data/lib/rouge/demos/scala +3 -0
- data/lib/rouge/demos/scheme +4 -0
- data/lib/rouge/demos/scss +5 -0
- data/lib/rouge/demos/sed +4 -0
- data/lib/rouge/demos/shell +2 -0
- data/lib/rouge/demos/slim +17 -0
- data/lib/rouge/demos/smalltalk +6 -0
- data/lib/rouge/demos/sml +4 -0
- data/lib/rouge/demos/sql +1 -0
- data/lib/rouge/demos/swift +5 -0
- data/lib/rouge/demos/tcl +1 -0
- data/lib/rouge/demos/tex +1 -0
- data/lib/rouge/demos/toml +9 -0
- data/lib/rouge/demos/tulip +14 -0
- data/lib/rouge/demos/vb +4 -0
- data/lib/rouge/demos/viml +5 -0
- data/lib/rouge/demos/xml +2 -0
- data/lib/rouge/demos/yaml +4 -0
- data/lib/rouge/formatter.rb +50 -0
- data/lib/rouge/formatters/html.rb +117 -0
- data/lib/rouge/formatters/null.rb +19 -0
- data/lib/rouge/formatters/terminal256.rb +176 -0
- data/lib/rouge/lexer.rb +443 -0
- data/lib/rouge/lexers/apache.rb +68 -0
- data/lib/rouge/lexers/apache/keywords.yml +453 -0
- data/lib/rouge/lexers/apple_script.rb +367 -0
- data/lib/rouge/lexers/c.rb +212 -0
- data/lib/rouge/lexers/clojure.rb +112 -0
- data/lib/rouge/lexers/coffeescript.rb +174 -0
- data/lib/rouge/lexers/common_lisp.rb +345 -0
- data/lib/rouge/lexers/conf.rb +24 -0
- data/lib/rouge/lexers/cpp.rb +66 -0
- data/lib/rouge/lexers/csharp.rb +88 -0
- data/lib/rouge/lexers/css.rb +271 -0
- data/lib/rouge/lexers/dart.rb +104 -0
- data/lib/rouge/lexers/diff.rb +31 -0
- data/lib/rouge/lexers/elixir.rb +108 -0
- data/lib/rouge/lexers/erb.rb +56 -0
- data/lib/rouge/lexers/erlang.rb +118 -0
- data/lib/rouge/lexers/factor.rb +302 -0
- data/lib/rouge/lexers/gherkin.rb +137 -0
- data/lib/rouge/lexers/gherkin/keywords.rb +14 -0
- data/lib/rouge/lexers/glsl.rb +135 -0
- data/lib/rouge/lexers/go.rb +178 -0
- data/lib/rouge/lexers/groovy.rb +104 -0
- data/lib/rouge/lexers/haml.rb +228 -0
- data/lib/rouge/lexers/handlebars.rb +79 -0
- data/lib/rouge/lexers/haskell.rb +183 -0
- data/lib/rouge/lexers/html.rb +94 -0
- data/lib/rouge/lexers/http.rb +80 -0
- data/lib/rouge/lexers/ini.rb +57 -0
- data/lib/rouge/lexers/io.rb +68 -0
- data/lib/rouge/lexers/java.rb +76 -0
- data/lib/rouge/lexers/javascript.rb +297 -0
- data/lib/rouge/lexers/liquid.rb +287 -0
- data/lib/rouge/lexers/literate_coffeescript.rb +33 -0
- data/lib/rouge/lexers/literate_haskell.rb +36 -0
- data/lib/rouge/lexers/llvm.rb +84 -0
- data/lib/rouge/lexers/lua.rb +122 -0
- data/lib/rouge/lexers/lua/builtins.rb +22 -0
- data/lib/rouge/lexers/make.rb +116 -0
- data/lib/rouge/lexers/markdown.rb +154 -0
- data/lib/rouge/lexers/matlab.rb +74 -0
- data/lib/rouge/lexers/matlab/builtins.rb +11 -0
- data/lib/rouge/lexers/moonscript.rb +110 -0
- data/lib/rouge/lexers/nginx.rb +71 -0
- data/lib/rouge/lexers/nim.rb +152 -0
- data/lib/rouge/lexers/objective_c.rb +197 -0
- data/lib/rouge/lexers/ocaml.rb +111 -0
- data/lib/rouge/lexers/perl.rb +197 -0
- data/lib/rouge/lexers/php.rb +173 -0
- data/lib/rouge/lexers/php/builtins.rb +204 -0
- data/lib/rouge/lexers/plain_text.rb +25 -0
- data/lib/rouge/lexers/powershell.rb +96 -0
- data/lib/rouge/lexers/prolog.rb +64 -0
- data/lib/rouge/lexers/properties.rb +55 -0
- data/lib/rouge/lexers/puppet.rb +128 -0
- data/lib/rouge/lexers/python.rb +228 -0
- data/lib/rouge/lexers/qml.rb +72 -0
- data/lib/rouge/lexers/r.rb +56 -0
- data/lib/rouge/lexers/racket.rb +542 -0
- data/lib/rouge/lexers/ruby.rb +415 -0
- data/lib/rouge/lexers/rust.rb +191 -0
- data/lib/rouge/lexers/sass.rb +74 -0
- data/lib/rouge/lexers/sass/common.rb +180 -0
- data/lib/rouge/lexers/scala.rb +142 -0
- data/lib/rouge/lexers/scheme.rb +112 -0
- data/lib/rouge/lexers/scss.rb +34 -0
- data/lib/rouge/lexers/sed.rb +170 -0
- data/lib/rouge/lexers/shell.rb +152 -0
- data/lib/rouge/lexers/slim.rb +228 -0
- data/lib/rouge/lexers/smalltalk.rb +116 -0
- data/lib/rouge/lexers/sml.rb +347 -0
- data/lib/rouge/lexers/sql.rb +140 -0
- data/lib/rouge/lexers/swift.rb +144 -0
- data/lib/rouge/lexers/tcl.rb +192 -0
- data/lib/rouge/lexers/tex.rb +72 -0
- data/lib/rouge/lexers/toml.rb +71 -0
- data/lib/rouge/lexers/tulip.rb +75 -0
- data/lib/rouge/lexers/vb.rb +164 -0
- data/lib/rouge/lexers/viml.rb +101 -0
- data/lib/rouge/lexers/viml/keywords.rb +12 -0
- data/lib/rouge/lexers/xml.rb +59 -0
- data/lib/rouge/lexers/yaml.rb +364 -0
- data/lib/rouge/plugins/redcarpet.rb +30 -0
- data/lib/rouge/regex_lexer.rb +439 -0
- data/lib/rouge/template_lexer.rb +22 -0
- data/lib/rouge/text_analyzer.rb +48 -0
- data/lib/rouge/theme.rb +195 -0
- data/lib/rouge/themes/base16.rb +130 -0
- data/lib/rouge/themes/colorful.rb +67 -0
- data/lib/rouge/themes/github.rb +71 -0
- data/lib/rouge/themes/molokai.rb +82 -0
- data/lib/rouge/themes/monokai.rb +92 -0
- data/lib/rouge/themes/monokai_sublime.rb +90 -0
- data/lib/rouge/themes/thankful_eyes.rb +71 -0
- data/lib/rouge/token.rb +182 -0
- data/lib/rouge/util.rb +101 -0
- data/lib/rouge/version.rb +7 -0
- metadata +231 -0
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- coding: utf-8 -*- #
|
2
|
+
|
3
|
+
module Rouge
|
4
|
+
module Lexers
|
5
|
+
class PlainText < Lexer
|
6
|
+
title "Plain Text"
|
7
|
+
desc "A boring lexer that doesn't highlight anything"
|
8
|
+
|
9
|
+
tag 'plaintext'
|
10
|
+
aliases 'text'
|
11
|
+
filenames '*.txt'
|
12
|
+
mimetypes 'text/plain'
|
13
|
+
|
14
|
+
default_options :token => 'Text'
|
15
|
+
|
16
|
+
def token
|
17
|
+
@token ||= Token[option :token]
|
18
|
+
end
|
19
|
+
|
20
|
+
def stream_tokens(string, &b)
|
21
|
+
yield self.token, string
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,96 @@
|
|
1
|
+
# -*- coding: utf-8 -*- #
|
2
|
+
|
3
|
+
module Rouge
|
4
|
+
module Lexers
|
5
|
+
load_const :Shell, 'shell.rb'
|
6
|
+
|
7
|
+
class Powershell < Shell
|
8
|
+
title 'powershell'
|
9
|
+
desc 'powershell'
|
10
|
+
tag 'powershell'
|
11
|
+
aliases 'posh'
|
12
|
+
filenames '*.ps1', '*.psm1', '*.psd1'
|
13
|
+
mimetypes 'text/plain'
|
14
|
+
|
15
|
+
ATTRIBUTES = %w(
|
16
|
+
CmdletBinding ConfirmImpact DefaultParameterSetName HelpURI SupportsPaging
|
17
|
+
SupportsShouldProcess PositionalBinding
|
18
|
+
).join('|')
|
19
|
+
|
20
|
+
KEYWORDS = %w(
|
21
|
+
Begin Exit Process Break Filter Return Catch Finally Sequence Class For
|
22
|
+
Switch Continue ForEach Throw Data From Trap Define Function Try Do If
|
23
|
+
Until DynamicParam In Using Else InlineScript Var ElseIf Parallel While
|
24
|
+
End Param Workflow
|
25
|
+
).join('|')
|
26
|
+
|
27
|
+
KEYWORDS_TYPE = %w(
|
28
|
+
bool byte char decimal double float int long object sbyte
|
29
|
+
short string uint ulong ushort
|
30
|
+
).join('|')
|
31
|
+
|
32
|
+
OPERATORS = %w(
|
33
|
+
-split -isplit -csplit -join -is -isnot -as -eq -ieq -ceq -ne -ine
|
34
|
+
-cne -gt -igt -cgt -ge -ige -cge -lt -ilt -clt -le -ile -cle -like
|
35
|
+
-ilike -clike -notlike -inotlike -cnotlike -match -imatch -cmatch
|
36
|
+
-notmatch -inotmatch -cnotmatch -contains -icontains -ccontains
|
37
|
+
-notcontains -inotcontains -cnotcontains -replace -ireplace
|
38
|
+
-creplace -band -bor -bxor -and -or -xor \. & = \+= -= \*= \/= %=
|
39
|
+
).join('|')
|
40
|
+
|
41
|
+
BUILTINS = %w(
|
42
|
+
Add-Content Add-History Add-Member Add-PSSnapin Clear-Content
|
43
|
+
Clear-Item Clear-Item Property Clear-Variable Compare-Object
|
44
|
+
ConvertFrom-SecureString Convert-Path ConvertTo-Html
|
45
|
+
ConvertTo-SecureString Copy-Item Copy-ItemProperty Export-Alias
|
46
|
+
Export-Clixml Export-Console Export-Csv ForEach-Object
|
47
|
+
Format-Custom Format-List Format-Table Format-Wide
|
48
|
+
Get-Acl Get-Alias Get-AuthenticodeSignature Get-ChildItem
|
49
|
+
Get-Command Get-Content Get-Credential Get-Culture Get-Date
|
50
|
+
Get-EventLog Get-ExecutionPolicy Get-Help Get-History
|
51
|
+
Get-Host Get-Item Get-ItemProperty Get-Location Get-Member
|
52
|
+
Get-PfxCertificate Get-Process Get-PSDrive Get-PSProvider
|
53
|
+
Get-PSSnapin Get-Service Get-TraceSource Get-UICulture
|
54
|
+
Get-Unique Get-Variable Get-WmiObject Group-Object
|
55
|
+
Import-Alias Import-Clixml Import-Csv Invoke-Expression
|
56
|
+
Invoke-History Invoke-Item Join-Path Measure-Command
|
57
|
+
Measure-Object Move-Item Move-ItemProperty New-Alias
|
58
|
+
New-Item New-ItemProperty New-Object New-PSDrive New-Service
|
59
|
+
New-TimeSpan New-Variable Out-Default Out-File Out-Host Out-Null
|
60
|
+
Out-Printer Out-String Pop-Location Push-Location Read-Host
|
61
|
+
Remove-Item Remove-ItemProperty Remove-PSDrive Remove-PSSnapin
|
62
|
+
Remove-Variable Rename-Item Rename-ItemProperty Resolve-Path
|
63
|
+
Restart-Service Resume-Service Select-Object Select-String
|
64
|
+
Set-Acl Set-Alias Set-AuthenticodeSignature Set-Content Set-Date
|
65
|
+
Set-ExecutionPolicy Set-Item Set-ItemProperty Set-Location
|
66
|
+
Set-PSDebug Set-Service Set-TraceSource Set-Variable Sort-Object
|
67
|
+
Split-Path Start-Service Start-Sleep Start-Transcript Stop-Process
|
68
|
+
Stop-Service Stop-Transcript Suspend-Service Tee-Object Test-Path
|
69
|
+
Trace-Command Update-FormatData Update-TypeData Where-Object
|
70
|
+
Write-Debug Write-Error Write-Host Write-Output Write-Progress
|
71
|
+
Write-Verbose Write-Warning ac asnp cat cd chdir clc clear clhy
|
72
|
+
cli clp cls clv cnsn compare copy cp cpi cpp curl cvpa dbp del
|
73
|
+
diff dir dnsn ebp echo epal epcsv epsn erase etsn exsn fc fl
|
74
|
+
foreach ft fw gal gbp gc gci gcm gcs gdr ghy gi gjb gl gm gmo
|
75
|
+
gp gps group gsn gsnp gsv gu gv gwmi h history icm iex ihy ii
|
76
|
+
ipal ipcsv ipmo ipsn irm ise iwmi iwr kill lp ls man md measure
|
77
|
+
mi mount move mp mv nal ndr ni nmo npssc nsn nv ogv oh popd ps
|
78
|
+
pushd pwd r rbp rcjb rcsn rd rdr ren ri rjb rm rmdir rmo rni rnp
|
79
|
+
rp rsn rsnp rujb rv rvpa rwmi sajb sal saps sasv sbp sc select
|
80
|
+
set shcm si sl sleep sls sort sp spjb spps spsv start sujb sv
|
81
|
+
swmi tee trcm type wget where wjb write \% \?
|
82
|
+
).join('|')
|
83
|
+
|
84
|
+
prepend :basic do
|
85
|
+
rule %r(<#[\s,\S]*?#>)m, Comment::Multiline
|
86
|
+
rule /#.*$/, Comment::Single
|
87
|
+
rule /\b(#{OPERATORS})\s*\b/i, Operator
|
88
|
+
rule /\b(#{ATTRIBUTES})\s*\b/i, Name::Attribute
|
89
|
+
rule /\b(#{KEYWORDS})\s*\b/i, Keyword
|
90
|
+
rule /\b(#{KEYWORDS_TYPE})\s*\b/i, Keyword::Type
|
91
|
+
rule /\bcase\b/, Keyword, :case
|
92
|
+
rule /\b(#{BUILTINS})\s*\b(?!\.)/i, Name::Builtin
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
# -*- coding: utf-8 -*- #
|
2
|
+
|
3
|
+
module Rouge
|
4
|
+
module Lexers
|
5
|
+
class Prolog < RegexLexer
|
6
|
+
title "Prolog"
|
7
|
+
desc "The Prolog programming language (http://en.wikipedia.org/wiki/Prolog)"
|
8
|
+
tag 'prolog'
|
9
|
+
aliases 'prolog'
|
10
|
+
filenames '*.pro', '*.P', '*.prolog', '*.pl'
|
11
|
+
mimetypes 'text/x-prolog'
|
12
|
+
|
13
|
+
def self.analyze_text(text)
|
14
|
+
return 0.1 if text =~ /\A\w+(\(\w+\,\s*\w+\))*\./
|
15
|
+
return 0.1 if text.include? ':-'
|
16
|
+
end
|
17
|
+
|
18
|
+
state :basic do
|
19
|
+
rule /\s+/, Text
|
20
|
+
rule /^#.*/, Comment::Single
|
21
|
+
rule /\/\*/, Comment::Multiline, :nested_comment
|
22
|
+
|
23
|
+
rule /[\[\](){}|.,;!]/, Punctuation
|
24
|
+
rule /:-|-->/, Punctuation
|
25
|
+
|
26
|
+
rule /"[^"]*"/, Str::Double
|
27
|
+
|
28
|
+
rule /\d+\.\d+/, Num::Float
|
29
|
+
rule /\d+/, Num
|
30
|
+
end
|
31
|
+
|
32
|
+
state :atoms do
|
33
|
+
rule /[[:lower:]]([_[:word:][:digit:]])*/, Str::Symbol
|
34
|
+
rule /'[^']*'/, Str::Symbol
|
35
|
+
end
|
36
|
+
|
37
|
+
state :operators do
|
38
|
+
rule /(<|>|=<|>=|==|=:=|=|\/|\/\/|\*|\+|-)(?=\s|[a-zA-Z0-9\[])/,
|
39
|
+
Operator
|
40
|
+
rule /is/, Operator
|
41
|
+
rule /(mod|div|not)/, Operator
|
42
|
+
rule /[#&*+-.\/:<=>?@^~]+/, Operator
|
43
|
+
end
|
44
|
+
|
45
|
+
state :variables do
|
46
|
+
rule /[A-Z]+\w*/, Name::Variable
|
47
|
+
rule /_[[:word:]]*/, Name::Variable
|
48
|
+
end
|
49
|
+
|
50
|
+
state :root do
|
51
|
+
mixin :basic
|
52
|
+
mixin :atoms
|
53
|
+
mixin :variables
|
54
|
+
mixin :operators
|
55
|
+
end
|
56
|
+
|
57
|
+
state :nested_comment do
|
58
|
+
rule /\/\*/, Comment::Multiline, :push
|
59
|
+
rule /\s*\*[^*\/]+/, Comment::Multiline
|
60
|
+
rule /\*\//, Comment::Multiline, :pop!
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
# -*- coding: utf-8 -*- #
|
2
|
+
|
3
|
+
module Rouge
|
4
|
+
module Lexers
|
5
|
+
class Properties < RegexLexer
|
6
|
+
title ".properties"
|
7
|
+
desc '.properties config files for Java'
|
8
|
+
tag 'properties'
|
9
|
+
|
10
|
+
filenames '*.properties'
|
11
|
+
mimetypes 'text/x-java-properties'
|
12
|
+
|
13
|
+
def self.analyze_text(text)
|
14
|
+
return 0.1 if text =~ /\A\[[\w.]+\]\s*\w+=\w+/
|
15
|
+
end
|
16
|
+
|
17
|
+
identifier = /[\w.]+/
|
18
|
+
|
19
|
+
state :basic do
|
20
|
+
rule /[!#].*?\n/, Comment
|
21
|
+
rule /\s+/, Text
|
22
|
+
rule /\\\n/, Str::Escape
|
23
|
+
end
|
24
|
+
|
25
|
+
state :root do
|
26
|
+
mixin :basic
|
27
|
+
|
28
|
+
rule /(#{identifier})(\s*)([=:])/ do
|
29
|
+
groups Name::Property, Text, Punctuation
|
30
|
+
push :value
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
state :value do
|
35
|
+
rule /\n/, Text, :pop!
|
36
|
+
mixin :basic
|
37
|
+
rule /"/, Str, :dq
|
38
|
+
rule /'.*?'/, Str
|
39
|
+
mixin :esc_str
|
40
|
+
rule /[^\\\n]+/, Str
|
41
|
+
end
|
42
|
+
|
43
|
+
state :dq do
|
44
|
+
rule /"/, Str, :pop!
|
45
|
+
mixin :esc_str
|
46
|
+
rule /[^\\"]+/m, Str
|
47
|
+
end
|
48
|
+
|
49
|
+
state :esc_str do
|
50
|
+
rule /\\u[0-9]{4}/, Str::Escape
|
51
|
+
rule /\\./m, Str::Escape
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,128 @@
|
|
1
|
+
# -*- coding: utf-8 -*- #
|
2
|
+
|
3
|
+
module Rouge
|
4
|
+
module Lexers
|
5
|
+
class Puppet < RegexLexer
|
6
|
+
title "Puppet"
|
7
|
+
desc 'The Puppet configuration management language (puppetlabs.org)'
|
8
|
+
tag 'puppet'
|
9
|
+
aliases 'pp'
|
10
|
+
filenames '*.pp'
|
11
|
+
|
12
|
+
def self.analyze_text(text)
|
13
|
+
return 1 if text.shebang? 'puppet-apply'
|
14
|
+
return 1 if text.shebang? 'puppet'
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.keywords
|
18
|
+
@keywords ||= Set.new %w(
|
19
|
+
and case class default define else elsif if in import inherits
|
20
|
+
node unless
|
21
|
+
)
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.constants
|
25
|
+
@constants ||= Set.new %w(
|
26
|
+
false true undef
|
27
|
+
)
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.metaparameters
|
31
|
+
@metaparameters ||= Set.new %w(
|
32
|
+
before require notify subscribe
|
33
|
+
)
|
34
|
+
end
|
35
|
+
|
36
|
+
id = /[a-z]\w*/
|
37
|
+
cap_id = /[A-Z]\w*/
|
38
|
+
qualname = /(::)?(#{id}::)*\w+/
|
39
|
+
|
40
|
+
state :whitespace do
|
41
|
+
rule /\s+/m, Text
|
42
|
+
rule /#.*?\n/, Comment
|
43
|
+
end
|
44
|
+
|
45
|
+
state :root do
|
46
|
+
mixin :whitespace
|
47
|
+
|
48
|
+
rule /[$]#{qualname}/, Name::Variable
|
49
|
+
rule /(#{id})(?=\s*[=+]>)/m do |m|
|
50
|
+
if self.class.metaparameters.include? m[0]
|
51
|
+
token Keyword::Pseudo
|
52
|
+
else
|
53
|
+
token Name::Property
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
rule /(#{qualname})(?=\s*[(])/m, Name::Function
|
58
|
+
rule cap_id, Name::Class
|
59
|
+
|
60
|
+
rule /[+=|~-]>|<[|~-]/, Punctuation
|
61
|
+
rule /[:}();\[\]]/, Punctuation
|
62
|
+
|
63
|
+
# HACK for case statements and selectors
|
64
|
+
rule /{/, Punctuation, :regex_allowed
|
65
|
+
rule /,/, Punctuation, :regex_allowed
|
66
|
+
|
67
|
+
rule /(in|and|or)\b/, Operator::Word
|
68
|
+
rule /[=!<>]=/, Operator
|
69
|
+
rule /[=!]~/, Operator, :regex_allowed
|
70
|
+
rule %r([<>!+*/-]), Operator
|
71
|
+
|
72
|
+
rule /(class|include)(\s*)(#{qualname})/ do
|
73
|
+
groups Keyword, Text, Name::Class
|
74
|
+
end
|
75
|
+
|
76
|
+
rule /node\b/, Keyword, :regex_allowed
|
77
|
+
|
78
|
+
rule /'(\\[\\']|[^'])*'/m, Str::Single
|
79
|
+
rule /"/, Str::Double, :dquotes
|
80
|
+
|
81
|
+
rule /\d+([.]\d+)?(e[+-]\d+)?/, Num
|
82
|
+
|
83
|
+
# a valid regex. TODO: regexes are only allowed
|
84
|
+
# in certain places in puppet.
|
85
|
+
rule qualname do |m|
|
86
|
+
if self.class.keywords.include? m[0]
|
87
|
+
token Keyword
|
88
|
+
elsif self.class.constants.include? m[0]
|
89
|
+
token Keyword::Constant
|
90
|
+
else
|
91
|
+
token Name
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
state :regex_allowed do
|
97
|
+
mixin :whitespace
|
98
|
+
rule %r(/), Str::Regex, :regex
|
99
|
+
|
100
|
+
rule(//) { pop! }
|
101
|
+
end
|
102
|
+
|
103
|
+
state :regex do
|
104
|
+
rule %r(/), Str::Regex, :pop!
|
105
|
+
rule /\\./, Str::Escape
|
106
|
+
rule /[(){}]/, Str::Interpol
|
107
|
+
rule /\[/, Str::Interpol, :regex_class
|
108
|
+
rule /./, Str::Regex
|
109
|
+
end
|
110
|
+
|
111
|
+
state :regex_class do
|
112
|
+
rule /\]/, Str::Interpol, :pop!
|
113
|
+
rule /(?<!\[)-(?=\])/, Str::Regex
|
114
|
+
rule /-/, Str::Interpol
|
115
|
+
rule /\\./, Str::Escape
|
116
|
+
rule /[^\\\]-]+/, Str::Regex
|
117
|
+
end
|
118
|
+
|
119
|
+
state :dquotes do
|
120
|
+
rule /"/, Str::Double, :pop!
|
121
|
+
rule /[^$\\"]+/m, Str::Double
|
122
|
+
rule /\\./m, Str::Escape
|
123
|
+
rule /[$]#{qualname}/, Name::Variable
|
124
|
+
rule /[$][{]#{qualname}[}]/, Name::Variable
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
@@ -0,0 +1,228 @@
|
|
1
|
+
# -*- coding: utf-8 -*- #
|
2
|
+
|
3
|
+
module Rouge
|
4
|
+
module Lexers
|
5
|
+
class Python < RegexLexer
|
6
|
+
title "Python"
|
7
|
+
desc "The Python programming language (python.org)"
|
8
|
+
tag 'python'
|
9
|
+
aliases 'py'
|
10
|
+
filenames '*.py', '*.pyw', '*.sc', 'SConstruct', 'SConscript', '*.tac'
|
11
|
+
mimetypes 'text/x-python', 'application/x-python'
|
12
|
+
|
13
|
+
def self.analyze_text(text)
|
14
|
+
return 1 if text.shebang?(/pythonw?(3|2(\.\d)?)?/)
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.keywords
|
18
|
+
@keywords ||= %w(
|
19
|
+
assert break continue del elif else except exec
|
20
|
+
finally for global if lambda pass print raise
|
21
|
+
return try while yield as with
|
22
|
+
)
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.builtins
|
26
|
+
@builtins ||= %w(
|
27
|
+
__import__ abs all any apply basestring bin bool buffer
|
28
|
+
bytearray bytes callable chr classmethod cmp coerce compile
|
29
|
+
complex delattr dict dir divmod enumerate eval execfile exit
|
30
|
+
file filter float frozenset getattr globals hasattr hash hex id
|
31
|
+
input int intern isinstance issubclass iter len list locals
|
32
|
+
long map max min next object oct open ord pow property range
|
33
|
+
raw_input reduce reload repr reversed round set setattr slice
|
34
|
+
sorted staticmethod str sum super tuple type unichr unicode
|
35
|
+
vars xrange zip
|
36
|
+
)
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.builtins_pseudo
|
40
|
+
@builtins_pseudo ||= %w(self None Ellipsis NotImplemented False True)
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.exceptions
|
44
|
+
@exceptions ||= %w(
|
45
|
+
ArithmeticError AssertionError AttributeError
|
46
|
+
BaseException DeprecationWarning EOFError EnvironmentError
|
47
|
+
Exception FloatingPointError FutureWarning GeneratorExit IOError
|
48
|
+
ImportError ImportWarning IndentationError IndexError KeyError
|
49
|
+
KeyboardInterrupt LookupError MemoryError NameError
|
50
|
+
NotImplemented NotImplementedError OSError OverflowError
|
51
|
+
OverflowWarning PendingDeprecationWarning ReferenceError
|
52
|
+
RuntimeError RuntimeWarning StandardError StopIteration
|
53
|
+
SyntaxError SyntaxWarning SystemError SystemExit TabError
|
54
|
+
TypeError UnboundLocalError UnicodeDecodeError
|
55
|
+
UnicodeEncodeError UnicodeError UnicodeTranslateError
|
56
|
+
UnicodeWarning UserWarning ValueError VMSError Warning
|
57
|
+
WindowsError ZeroDivisionError
|
58
|
+
)
|
59
|
+
end
|
60
|
+
|
61
|
+
identifier = /[a-z_][a-z0-9_]*/i
|
62
|
+
dotted_identifier = /[a-z_.][a-z0-9_.]*/i
|
63
|
+
state :root do
|
64
|
+
rule /\n+/m, Text
|
65
|
+
rule /^(:)(\s*)([ru]{,2}""".*?""")/mi do
|
66
|
+
groups Punctuation, Text, Str::Doc
|
67
|
+
end
|
68
|
+
|
69
|
+
rule /[^\S\n]+/, Text
|
70
|
+
rule /#.*$/, Comment
|
71
|
+
rule /[\[\]{}:(),;]/, Punctuation
|
72
|
+
rule /\\\n/, Text
|
73
|
+
rule /\\/, Text
|
74
|
+
|
75
|
+
rule /(in|is|and|or|not)\b/, Operator::Word
|
76
|
+
rule /!=|==|<<|>>|[-~+\/*%=<>&^|.]/, Operator
|
77
|
+
|
78
|
+
rule /(def)((?:\s|\\\s)+)/ do
|
79
|
+
groups Keyword, Text
|
80
|
+
push :funcname
|
81
|
+
end
|
82
|
+
|
83
|
+
rule /(class)((?:\s|\\\s)+)/ do
|
84
|
+
groups Keyword, Text
|
85
|
+
push :classname
|
86
|
+
end
|
87
|
+
|
88
|
+
rule /(from)((?:\s|\\\s)+)/ do
|
89
|
+
groups Keyword::Namespace, Text
|
90
|
+
push :fromimport
|
91
|
+
end
|
92
|
+
|
93
|
+
rule /(import)((?:\s|\\\s)+)/ do
|
94
|
+
groups Keyword::Namespace, Text
|
95
|
+
push :import
|
96
|
+
end
|
97
|
+
|
98
|
+
# TODO: not in python 3
|
99
|
+
rule /`.*?`/, Str::Backtick
|
100
|
+
rule /(?:r|ur|ru)"""/i, Str, :tdqs
|
101
|
+
rule /(?:r|ur|ru)'''/i, Str, :tsqs
|
102
|
+
rule /(?:r|ur|ru)"/i, Str, :dqs
|
103
|
+
rule /(?:r|ur|ru)'/i, Str, :sqs
|
104
|
+
rule /u?"""/i, Str, :escape_tdqs
|
105
|
+
rule /u?'''/i, Str, :escape_tsqs
|
106
|
+
rule /u?"/i, Str, :escape_dqs
|
107
|
+
rule /u?'/i, Str, :escape_sqs
|
108
|
+
|
109
|
+
rule /@#{dotted_identifier}/i, Name::Decorator
|
110
|
+
|
111
|
+
# using negative lookbehind so we don't match property names
|
112
|
+
rule /(?<!\.)#{identifier}/ do |m|
|
113
|
+
if self.class.keywords.include? m[0]
|
114
|
+
token Keyword
|
115
|
+
elsif self.class.exceptions.include? m[0]
|
116
|
+
token Name::Builtin
|
117
|
+
elsif self.class.builtins.include? m[0]
|
118
|
+
token Name::Builtin
|
119
|
+
elsif self.class.builtins_pseudo.include? m[0]
|
120
|
+
token Name::Builtin::Pseudo
|
121
|
+
else
|
122
|
+
token Name
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
rule identifier, Name
|
127
|
+
|
128
|
+
rule /(\d+\.\d*|\d*\.\d+)(e[+-]?[0-9]+)?/i, Num::Float
|
129
|
+
rule /\d+e[+-]?[0-9]+/i, Num::Float
|
130
|
+
rule /0[0-7]+/, Num::Oct
|
131
|
+
rule /0x[a-f0-9]+/i, Num::Hex
|
132
|
+
rule /\d+L/, Num::Integer::Long
|
133
|
+
rule /\d+/, Num::Integer
|
134
|
+
end
|
135
|
+
|
136
|
+
state :funcname do
|
137
|
+
rule identifier, Name::Function, :pop!
|
138
|
+
end
|
139
|
+
|
140
|
+
state :classname do
|
141
|
+
rule identifier, Name::Class, :pop!
|
142
|
+
end
|
143
|
+
|
144
|
+
state :import do
|
145
|
+
# non-line-terminating whitespace
|
146
|
+
rule /(?:[ \t]|\\\n)+/, Text
|
147
|
+
|
148
|
+
rule /as\b/, Keyword::Namespace
|
149
|
+
rule /,/, Operator
|
150
|
+
rule dotted_identifier, Name::Namespace
|
151
|
+
rule(//) { pop! } # anything else -> go back
|
152
|
+
end
|
153
|
+
|
154
|
+
state :fromimport do
|
155
|
+
# non-line-terminating whitespace
|
156
|
+
rule /(?:[ \t]|\\\n)+/, Text
|
157
|
+
|
158
|
+
rule /import\b/, Keyword::Namespace, :pop!
|
159
|
+
rule dotted_identifier, Name::Namespace
|
160
|
+
end
|
161
|
+
|
162
|
+
state :strings do
|
163
|
+
rule /%(\([a-z0-9_]+\))?[-#0 +]*([0-9]+|[*])?(\.([0-9]+|[*]))?/i, Str::Interpol
|
164
|
+
end
|
165
|
+
|
166
|
+
state :strings_double do
|
167
|
+
rule /[^\\"%\n]+/, Str
|
168
|
+
mixin :strings
|
169
|
+
end
|
170
|
+
|
171
|
+
state :strings_single do
|
172
|
+
rule /[^\\'%\n]+/, Str
|
173
|
+
mixin :strings
|
174
|
+
end
|
175
|
+
|
176
|
+
state :nl do
|
177
|
+
rule /\n/, Str
|
178
|
+
end
|
179
|
+
|
180
|
+
state :escape do
|
181
|
+
rule %r(\\
|
182
|
+
( [\\abfnrtv"']
|
183
|
+
| \n
|
184
|
+
| N{.*?}
|
185
|
+
| u[a-fA-F0-9]{4}
|
186
|
+
| U[a-fA-F0-9]{8}
|
187
|
+
| x[a-fA-F0-9]{2}
|
188
|
+
| [0-7]{1,3}
|
189
|
+
)
|
190
|
+
)x, Str::Escape
|
191
|
+
end
|
192
|
+
|
193
|
+
state :dqs do
|
194
|
+
rule /"/, Str, :pop!
|
195
|
+
rule /\\\\|\\"|\\\n/, Str::Escape
|
196
|
+
mixin :strings_double
|
197
|
+
end
|
198
|
+
|
199
|
+
state :sqs do
|
200
|
+
rule /'/, Str, :pop!
|
201
|
+
rule /\\\\|\\'|\\\n/, Str::Escape
|
202
|
+
mixin :strings_single
|
203
|
+
end
|
204
|
+
|
205
|
+
state :tdqs do
|
206
|
+
rule /"""/, Str, :pop!
|
207
|
+
rule /"/, Str
|
208
|
+
mixin :strings_double
|
209
|
+
mixin :nl
|
210
|
+
end
|
211
|
+
|
212
|
+
state :tsqs do
|
213
|
+
rule /'''/, Str, :pop!
|
214
|
+
rule /'/, Str
|
215
|
+
mixin :strings_single
|
216
|
+
mixin :nl
|
217
|
+
end
|
218
|
+
|
219
|
+
%w(tdqs tsqs dqs sqs).each do |qtype|
|
220
|
+
state :"escape_#{qtype}" do
|
221
|
+
mixin :escape
|
222
|
+
mixin :"#{qtype}"
|
223
|
+
end
|
224
|
+
end
|
225
|
+
|
226
|
+
end
|
227
|
+
end
|
228
|
+
end
|