From 35aeb15b62207b54d86dfa1d897bbb549dd7ccea Mon Sep 17 00:00:00 2001 From: Shuhei Kadowaki Date: Sat, 23 May 2020 20:07:59 +0900 Subject: [PATCH 1/2] provide more helpful API for highlight themes --- Project.toml | 1 + src/Weave.jl | 19 +++++++++++++++++-- src/rendering/common.jl | 9 +++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/Project.toml b/Project.toml index 22676957..34b97749 100644 --- a/Project.toml +++ b/Project.toml @@ -6,6 +6,7 @@ version = "0.10.3" Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f" Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" Highlights = "eafb193a-b7ab-5a9e-9068-77385905fa72" +InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240" JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6" Markdown = "d6f4376e-aef5-505a-96c1-9c027394607a" Mustache = "ffc61752-8dc7-55ee-8c37-f3e9cdd09e70" diff --git a/src/Weave.jl b/src/Weave.jl index 898dabd9..7e1c8b77 100644 --- a/src/Weave.jl +++ b/src/Weave.jl @@ -1,6 +1,7 @@ module Weave using Highlights, Mustache, Requires, Pkg, REPL +using InteractiveUtils: subtypes # directories @@ -54,6 +55,16 @@ List supported output formats with its description. """ list_out_formats() = [k => v.description for (k,v) in FORMATS] +""" + list_highlight_themes() + +List all the available syntax highlight themes, which can be passed to [`weave`](@ref)'s + `highlight_theme` keyword argument. + +See also: [`weave`](@ref), [Highlights.jl's showcase page](https://juliadocs.github.io/Highlights.jl/latest/demo/themes/) +""" +list_highlight_themes() = subtypes(Highlights.AbstractTheme) + """ tangle(source::AbstractString; kwargs...) @@ -114,7 +125,10 @@ Weave an input document to output file. * `:refresh` runs all code chunks and save new cache - `template::Union{Nothing,AbstractString,Mustache.MustacheTokens} = nothing`: Template (file path) or `Mustache.MustacheTokens`s for `md2html` or `md2tex` formats - `css::Union{Nothing,AbstractString} = nothing`: Path of a CSS file used for md2html format -- `highlight_theme::Union{Nothing,Type{<:Highlights.AbstractTheme}} = nothing`: Theme used for syntax highlighting (defaults to `Highlights.Themes.DefaultTheme`) +- `highlight_theme::Union{Nothing,AbstractString,Symbol,Type{<:Highlights.AbstractTheme}} = nothing`: Theme used for syntax highlighting. + * If given `nothing` (default), Weave will use `Highlights.Themes.DefaultTheme` + * 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` + * If given an instance of `Highlights.AbstractTheme`, it will be directly used - `pandoc_options::Vector{<:AbstractString} = $(DEFAULT_PANDOC_OPTIONS)`: `String`s of options to pass to pandoc for `pandoc2html` and `pandoc2pdf` formats, e.g. `["--toc", "-N"]` - `latex_cmd::Vector{<:AbstractString} = $(DEFAULT_LATEX_CMD)`: The command used to make PDF file from .tex - `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( cache::Symbol = :off, template::Union{Nothing,AbstractString,Mustache.MustacheTokens} = nothing, css::Union{Nothing,AbstractString} = nothing, # TODO: rename to `stylesheet` - highlight_theme::Union{Nothing,Type{<:Highlights.AbstractTheme}} = nothing, + highlight_theme::Union{Nothing,AbstractString,Symbol,Type{<:Highlights.AbstractTheme}} = nothing, pandoc_options::Vector{<:AbstractString} = DEFAULT_PANDOC_OPTIONS, latex_cmd::Vector{<:AbstractString} = DEFAULT_LATEX_CMD, keep_unicode::Bool = false, @@ -326,6 +340,7 @@ include_weave(source, informat = nothing) = include_weave(Main, source, informat export weave, list_out_formats, + list_highlight_themes, tangle, convert_doc, notebook, diff --git a/src/rendering/common.jl b/src/rendering/common.jl index 8f39b03d..a549df9d 100644 --- a/src/rendering/common.jl +++ b/src/rendering/common.jl @@ -157,6 +157,15 @@ get_highlight_stylesheet(mime, highlight_theme::Type{<:Highlights.AbstractTheme} get_highlight_theme(::Nothing) = Highlights.Themes.DefaultTheme get_highlight_theme(highlight_theme::Type{<:Highlights.AbstractTheme}) = highlight_theme +function get_highlight_theme(s) + themes = list_highlight_themes() + s = string(s) + i = findfirst(themes) do theme + occursin(Regex(s, "i"), string(theme)) + end + isnothing(i) && error("no highlight theme found for $s") + return themes[i] +end highlight_code(mime, code, highlight_theme) = highlight(mime, strip(code), Highlights.Lexers.JuliaLexer, highlight_theme) From 657ec582df843636638eb686147ed555aad5fb49 Mon Sep 17 00:00:00 2001 From: Shuhei Kadowaki Date: Tue, 2 Jun 2020 19:30:05 +0900 Subject: [PATCH 2/2] add build step to download bootswatch themes, and clean up .gitignore --- .gitignore | 8 ++++---- deps/build.jl | 24 ++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 4 deletions(-) create mode 100644 deps/build.jl diff --git a/.gitignore b/.gitignore index c96e00fb..ff05ccc2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,11 +1,15 @@ Manifest.toml +/doc/build/ +/stylesheets/bootswatch/ + examples/figures/ examples/*.md examples/*.pdf examples/*.html examples/*.rst examples/*.tex + test/**/cache test/**/figures test/documents/output/gadfly_formats_test.txt @@ -19,10 +23,6 @@ test/**/chunk_options.jl test/**/*.ipynb !test/**/*ref.* -doc/build -doc/site - -.idea *.*~ *.aux *.log diff --git a/deps/build.jl b/deps/build.jl new file mode 100644 index 00000000..941a1cf9 --- /dev/null +++ b/deps/build.jl @@ -0,0 +1,24 @@ +# # to update `themes` +# let +# using HTTP, JSON +# r = HTTP.request("GET", "https://bootswatch.com/api/4.json") |> HTTP.payload |> String |> JSON.parse +# lowercase.(get.(values(r["themes"]), "name", "")) |> repr |> clipboard +# end + +bootswatch_version = "4" +themes = ["cerulean", "cosmo", "cyborg", "darkly", "flatly", "journal", "litera", "lumen", "lux", "materia", "minty", "pulse", "sandstone", "simplex", "sketchy", "slate", "solar", "spacelab", "superhero", "united", "yeti"] +targets = ["bootstrap.min.css", "_bootswatch.scss", "_variables.scss"] + +BOOTSWATCH_DIR = normpath(@__DIR__, "..", "stylesheets", "bootswatch") +isdir(BOOTSWATCH_DIR) || mkdir(BOOTSWATCH_DIR) + +function download_theme(theme) + theme_dir = normpath(BOOTSWATCH_DIR, theme) + isdir(theme_dir) || mkdir(theme_dir) + for target in targets + file = normpath(theme_dir, target) + isfile(file) || download("https://bootswatch.com/$(bootswatch_version)/$(theme)/$(target)", file) + end +end + +download_theme.(themes)