Skip to content

Commit 35aeb15

Browse files
aviateskr-aviatesk
authored andcommitted
provide more helpful API for highlight themes
1 parent 46a0f8b commit 35aeb15

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ version = "0.10.3"
66
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
77
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
88
Highlights = "eafb193a-b7ab-5a9e-9068-77385905fa72"
9+
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
910
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
1011
Markdown = "d6f4376e-aef5-505a-96c1-9c027394607a"
1112
Mustache = "ffc61752-8dc7-55ee-8c37-f3e9cdd09e70"

src/Weave.jl

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module Weave
22

33
using Highlights, Mustache, Requires, Pkg, REPL
4+
using InteractiveUtils: subtypes
45

56

67
# directories
@@ -54,6 +55,16 @@ List supported output formats with its description.
5455
"""
5556
list_out_formats() = [k => v.description for (k,v) in FORMATS]
5657

58+
"""
59+
list_highlight_themes()
60+
61+
List all the available syntax highlight themes, which can be passed to [`weave`](@ref)'s
62+
`highlight_theme` keyword argument.
63+
64+
See also: [`weave`](@ref), [Highlights.jl's showcase page](https://juliadocs.github.io/Highlights.jl/latest/demo/themes/)
65+
"""
66+
list_highlight_themes() = subtypes(Highlights.AbstractTheme)
67+
5768
"""
5869
tangle(source::AbstractString; kwargs...)
5970
@@ -114,7 +125,10 @@ Weave an input document to output file.
114125
* `:refresh` runs all code chunks and save new cache
115126
- `template::Union{Nothing,AbstractString,Mustache.MustacheTokens} = nothing`: Template (file path) or `Mustache.MustacheTokens`s for `md2html` or `md2tex` formats
116127
- `css::Union{Nothing,AbstractString} = nothing`: Path of a CSS file used for md2html format
117-
- `highlight_theme::Union{Nothing,Type{<:Highlights.AbstractTheme}} = nothing`: Theme used for syntax highlighting (defaults to `Highlights.Themes.DefaultTheme`)
128+
- `highlight_theme::Union{Nothing,AbstractString,Symbol,Type{<:Highlights.AbstractTheme}} = nothing`: Theme used for syntax highlighting.
129+
* If given `nothing` (default), Weave will use `Highlights.Themes.DefaultTheme`
130+
* If given an instance of `AbstractString` or `Symbol`, Weave will try to search a theme based on string matching, e.g. `highlight_theme = "github"` will use `Highlights.Themes.GitHubTheme`
131+
* If given an instance of `Highlights.AbstractTheme`, it will be directly used
118132
- `pandoc_options::Vector{<:AbstractString} = $(DEFAULT_PANDOC_OPTIONS)`: `String`s of options to pass to pandoc for `pandoc2html` and `pandoc2pdf` formats, e.g. `["--toc", "-N"]`
119133
- `latex_cmd::Vector{<:AbstractString} = $(DEFAULT_LATEX_CMD)`: The command used to make PDF file from .tex
120134
- `keep_unicode::Bool = false`: If `true`, do not convert unicode characters to their respective latex representation. This is especially useful if a font and tex-engine with support for unicode characters are used
@@ -135,7 +149,7 @@ function weave(
135149
cache::Symbol = :off,
136150
template::Union{Nothing,AbstractString,Mustache.MustacheTokens} = nothing,
137151
css::Union{Nothing,AbstractString} = nothing, # TODO: rename to `stylesheet`
138-
highlight_theme::Union{Nothing,Type{<:Highlights.AbstractTheme}} = nothing,
152+
highlight_theme::Union{Nothing,AbstractString,Symbol,Type{<:Highlights.AbstractTheme}} = nothing,
139153
pandoc_options::Vector{<:AbstractString} = DEFAULT_PANDOC_OPTIONS,
140154
latex_cmd::Vector{<:AbstractString} = DEFAULT_LATEX_CMD,
141155
keep_unicode::Bool = false,
@@ -326,6 +340,7 @@ include_weave(source, informat = nothing) = include_weave(Main, source, informat
326340

327341
export weave,
328342
list_out_formats,
343+
list_highlight_themes,
329344
tangle,
330345
convert_doc,
331346
notebook,

src/rendering/common.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,15 @@ get_highlight_stylesheet(mime, highlight_theme::Type{<:Highlights.AbstractTheme}
157157

158158
get_highlight_theme(::Nothing) = Highlights.Themes.DefaultTheme
159159
get_highlight_theme(highlight_theme::Type{<:Highlights.AbstractTheme}) = highlight_theme
160+
function get_highlight_theme(s)
161+
themes = list_highlight_themes()
162+
s = string(s)
163+
i = findfirst(themes) do theme
164+
occursin(Regex(s, "i"), string(theme))
165+
end
166+
isnothing(i) && error("no highlight theme found for $s")
167+
return themes[i]
168+
end
160169

161170
highlight_code(mime, code, highlight_theme) =
162171
highlight(mime, strip(code), Highlights.Lexers.JuliaLexer, highlight_theme)

0 commit comments

Comments
 (0)