-
Notifications
You must be signed in to change notification settings - Fork 62
/
MarkdownHighlighter.jl
68 lines (63 loc) · 2.31 KB
/
MarkdownHighlighter.jl
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
using Crayons
import Markdown
import .OhMyREPL.Passes.SyntaxHighlighter.SYNTAX_HIGHLIGHTER_SETTINGS
import .OhMyREPL.HIGHLIGHT_MARKDOWN
function Markdown.term(io::IO, md::Markdown.Code, columns)
code = md.code
# Want to remove potential.
lang = md.language == "" ? "" : first(split(md.language))
outputs = String[]
sourcecodes = String[]
do_syntax = false
if occursin(r"jldoctest;?", lang) || lang == "julia-repl"
do_syntax = true
code_blocks = split("\n" * code, "\njulia> ")
for codeblock in code_blocks[2:end] #
expr, pos = Meta.parse(codeblock, 1, raise = false);
sourcecode, output = begin
if pos > length(codeblock)
codeblock, ""
else
ind = nextind(codeblock, 0, pos)
pind = prevind(codeblock, ind)
codeblock[1:pind], codeblock[ind:end]
end
end
push!(sourcecodes, string(sourcecode))
push!(outputs, string(output))
end
elseif lang == "julia" || lang == ""
do_syntax = true
push!(sourcecodes, code)
push!(outputs, "")
end
if do_syntax && HIGHLIGHT_MARKDOWN[]
for (sourcecode, output) in zip(sourcecodes, outputs)
tokens = collect(tokenize(sourcecode))
crayons = fill(Crayon(), length(tokens))
SYNTAX_HIGHLIGHTER_SETTINGS(crayons, tokens, 0, sourcecode)
buff = IOBuffer()
if lang == "jldoctest" || lang == "julia-repl"
print(buff, Crayon(foreground = :red, bold = true), "julia> ", Crayon(reset = true))
end
for (token, crayon) in zip(tokens, crayons)
print(buff, crayon)
print(buff, untokenize(token, sourcecode))
print(buff, Crayon(reset = true))
end
print(buff, output)
str = String(take!(buff))
for line in Markdown.lines(str)
print(io, " "^Markdown.margin)
println(io, line)
end
end
else
Base.with_output_color(:cyan, io) do io
for line in Markdown.lines(md.code)
print(io, " "^Markdown.margin)
println(io, line)
end
end
end
end