Skip to content

wip: add more default themes, use bootswatch #338

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -19,10 +23,6 @@ test/**/chunk_options.jl
test/**/*.ipynb
!test/**/*ref.*

doc/build
doc/site

.idea
*.*~
*.aux
*.log
Expand Down
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
24 changes: 24 additions & 0 deletions deps/build.jl
Original file line number Diff line number Diff line change
@@ -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)
19 changes: 17 additions & 2 deletions src/Weave.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Weave

using Highlights, Mustache, Requires, Pkg, REPL
using InteractiveUtils: subtypes


# directories
Expand Down Expand Up @@ -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...)

Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
9 changes: 9 additions & 0 deletions src/rendering/common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down