@@ -16,23 +16,92 @@ function pygmentize()
16
16
return _pygmentize[]
17
17
end
18
18
19
+ const _pygmentize_version = Ref {Union{VersionNumber, Nothing}} ()
20
+ function pygmentize_version ()
21
+ isassigned (_pygmentize_version) && return _pygmentize_version[]
22
+
23
+ pygmentize_cmd = pygmentize ()
24
+ if isnothing (pygmentize_cmd)
25
+ return _pygmentize_version[] = nothing
26
+ end
27
+
28
+ cmd = ` $pygmentize_cmd -V`
29
+ @static if Sys. iswindows ()
30
+ # Avoid encoding issues with pipes on Windows by using cmd.exe to capture stdout for us
31
+ cmd = ` cmd.exe /C $cmd `
32
+ cmd = addenv (cmd, " PYTHONUTF8" => 1 )
33
+ end
34
+ version_str = readchomp (cmd)
35
+
36
+ pos = findfirst (" Pygments version " , version_str)
37
+ if ! isnothing (pos)
38
+ version_start = last (pos) + 1
39
+ version_end = findnext (' ,' , version_str, version_start) - 1
40
+ version = tryparse (VersionNumber, version_str[version_start: version_end])
41
+ else
42
+ version = nothing
43
+ end
44
+
45
+ if isnothing (version)
46
+ @warn " Could not parse Pygments version:\n $version_str "
47
+ end
48
+
49
+ return _pygmentize_version[] = version
50
+ end
51
+
52
+ function pygmentize_support (lexer)
53
+ highlighter_ver = pygmentize_version ()
54
+ if isnothing (highlighter_ver)
55
+ @warn " Syntax highlighting of $lexer code relies on Pygments.\n \
56
+ Use `pip install pygments` to install the lastest version" maxlog = 1
57
+ return false
58
+ elseif lexer == " ptx"
59
+ if highlighter_ver < v " 2.16"
60
+ @warn " Pygments supports PTX highlighting starting from version 2.16\n \
61
+ Detected version $highlighter_ver \n \
62
+ Please update with `pip install pygments -U`" maxlog = 1
63
+ return false
64
+ end
65
+ return true
66
+ elseif lexer == " gcn"
67
+ if highlighter_ver < v " 2.8"
68
+ @warn " Pygments supports GCN highlighting starting from version 2.8\n \
69
+ Detected version $highlighter_ver \n \
70
+ Please update with `pip install pygments -U`" maxlog = 1
71
+ return false
72
+ end
73
+ return true
74
+ else
75
+ return false
76
+ end
77
+ end
78
+
19
79
function highlight (io:: IO , code, lexer)
20
- highlighter = pygmentize ()
21
80
have_color = get (io, :color , false )
22
81
if ! have_color
23
82
print (io, code)
24
83
elseif lexer == " llvm"
25
84
InteractiveUtils. print_llvm (io, code)
26
- elseif highlighter != = nothing
27
- custom_lexer = joinpath (dirname (@__DIR__ ), " res" , " pygments" , " $lexer .py" )
28
- if isfile (custom_lexer)
29
- lexer = ` $custom_lexer -x`
85
+ elseif pygmentize_support (lexer)
86
+ lexer = lexer == " gcn" ? " amdgpu" : lexer
87
+ pygments_args = String[pygmentize (), " -f" , " terminal" , " -P" , " bg=dark" , " -l" , lexer]
88
+ @static if Sys. iswindows ()
89
+ # Avoid encoding issues with pipes on Windows by using a temporary file
90
+ mktemp () do tmp_path, tmp_io
91
+ println (tmp_io, code)
92
+ close (tmp_io)
93
+ push! (pygments_args, " -o" , tmp_path, tmp_path)
94
+ cmd = Cmd (pygments_args)
95
+ wait (open (cmd)) # stdout and stderr go to devnull
96
+ print (io, read (tmp_path, String))
97
+ end
98
+ else
99
+ cmd = Cmd (pygments_args)
100
+ pipe = open (cmd, " r+" )
101
+ print (pipe, code)
102
+ close (pipe. in)
103
+ print (io, read (pipe, String))
30
104
end
31
-
32
- pipe = open (` $highlighter -f terminal -P bg=dark -l $lexer ` , " r+" )
33
- print (pipe, code)
34
- close (pipe. in)
35
- print (io, read (pipe, String))
36
105
else
37
106
print (io, code)
38
107
end
0 commit comments