Some rendering stuff

Not quite working.
This commit is contained in:
Chris de Graaf 2019-08-27 21:59:25 +07:00
parent 0d3b124c07
commit 4d8b5d360d
No known key found for this signature in database
GPG Key ID: 150FFDD9B0073C7B
6 changed files with 73 additions and 35 deletions

View File

@ -9,7 +9,7 @@ using LibGit2: LibGit2
using Pkg: Pkg, TOML, PackageSpec
using REPL.TerminalMenus: MultiSelectMenu, RadioMenu, request
using Mustache: render
using Mustache: entityMap, render
using Parameters: @with_kw
using URIParser: URI
@ -33,10 +33,8 @@ A plugin to be added to a [`Template`](@ref), which adds some functionality or i
"""
abstract type Plugin end
include("licenses.jl")
include("template.jl")
include("generate.jl")
include("plugin.jl")
include("interactive.jl")
end

View File

@ -12,12 +12,28 @@ abstract type BasicPlugin <: Plugin end
default_file(paths::AbstractString...) = joinpath(DEFAULTS_DIR, paths...)
"""
view(::Plugin, ::Template, pkg::AbstractString) -> Dict{String}
view(::Plugin, ::Template, pkg::AbstractString) -> Dict{String, Any}
Return the string replacements to be made for this plugin.
`pkg` is the name of the package being generated.
"""
view(::Plugin, ::Template, ::AbstractString) = Dict{String, Any}()
"""
user_view(::Plugin, ::Template, pkg::AbstractString) -> Dict{String, Any}
The same as [`view`](@ref), but for use only by package *users* for extension.
TODO better explanation
"""
user_view(::Plugin, ::Template, ::AbstractString) = Dict{String, Any}()
"""
tags(::Plugin) -> Tuple{String, String}
Return the tags used for Mustache templating.
"""
tags(::Plugin) = ("{{", "}}")
"""
gitignore(::Plugin) -> Vector{String}
@ -66,29 +82,39 @@ end
Base.string(b::Badge) = "[![$(b.hover)]($(b.image))]($(b.link))"
# Format a plugin's badges as a list of strings, with all substitutions applied.
function badges(p::Plugin, t::Template, pkg_name::AbstractString)
function badges(p::Plugin, t::Template, pkg::AbstractString)
bs = badges(p)
bs isa Vector || (bs = [bs])
bs = map(string, bs)
# TODO render
return map(b -> render_text(string(b), combined_view(p, t, pkg)), bs)
end
"""
gen_plugin(p::Plugin, t::Template, pkg::AbstractString)
gen_plugin(::Plugin, ::Template, pkg::AbstractString)
Generate any files associated with a plugin.
Perform any work associated with a plugin.
`pkg` is the name of the package being generated.
"""
gen_plugin(::Plugin, ::Template, ::AbstractString) = nothing
function gen_plugin(p::BasicPlugin, t::Template, pkg::AbstractString)
function gen_plugin(p::BasicPlugin, t::Template, pkg_dir::AbstractString)
pkg = basename(pkg_dir)
path = joinpath(pkg_dir, destination(p))
text = render_plugin(p, t, pkg)
gen_file(path, text)
end
function render_plugin(p::BasicPlugin, t::Template, pkg::AbstractString)
src = source(p)
src === nothing && return
# TODO template rendering code
text = render(src, view(p, t, pkg); tags=tags(p))
gen_file(joinpath(t.dir, pkg, destination(p)), text)
return render_file(src, combined_view(p, t, pkg), tags(p))
end
function combined_view(p::Plugin, t::Template, pkg::AbstractString)
return merge(view(p, t, pkg), user_view(p, t, pkg))
end
"""
gen_file(file::AbstractString, text::AbstractString)
@ -102,17 +128,22 @@ function gen_file(file::AbstractString, text::AbstractString)
write(file, text)
end
# TODO pls make me better
# Render text from a file.
render_file(file::AbstractString, view, tags) = render_text(read(file, String), view, tags)
render_text(text::AbstractString, view, tags) = render(text, view; tags=tags)
function render_badges(p::BasicPlugin, t::Template, pkg::AbstractString)
end
function render_plugin(p::BasicPlugin, t::Template, pkg::AbstractString)
render_file(source(p), view(p, t, pkg), tags(p))
# Render text, using Mustache's templating system. HTML escaping is disabled.
function render_text(text::AbstractString, view::Dict{<:AbstractString}, tags=nothing)
saved = copy(entityMap)
empty!(entityMap)
return try
if tags === nothing
render(text, view)
else
render(text, view; tags=tags)
end
finally
append!(entityMap, saved)
end
end
include(joinpath("plugins", "essentials.jl"))

View File

@ -28,7 +28,7 @@ badges(::TravisCI) = Badge(
"https://travis-ci.com/{{USER}}/{{PKG}}.jl",
)
function view(p::TravisCI, t::Template, ::AbstractString)
function view(p::TravisCI, t::Template, pkg::AbstractString)
os = String[]
p.linux && push!(os, "linux")
p.osx && push!(os, "osx")
@ -36,13 +36,14 @@ function view(p::TravisCI, t::Template, ::AbstractString)
# TODO: Update the allowed failures as new versions come out.
versions = collect_versions(p.extra_versions, t)
allow_failures = filter(v -> v in versions, ("1.3", "nightly"))
allow_failures = filter(v -> v in versions, ["1.3", "nightly"])
x86 = Dict{String, String}[]
if p.x86
foreach(versions) do v
p.linux && push!(x86, Dict("JULIA" => v, "OS" => "linux", "ARCH" => "x86"))
p.windows && push!(x86, Dict("JULIA" => v, "OS" => "windows", "ARCH" => "x86"))
foreach(versions) do v
p.linux && push!(x86, Dict("JULIA" => v, "OS" => "linux", "ARCH" => "x86"))
p.windows && push!(x86, Dict("JULIA" => v, "OS" => "windows", "ARCH" => "x86"))
end
end
return Dict(

View File

@ -15,6 +15,8 @@ todo
- `assets::Vector{<:AbstractString}=String[]`:
- `makedocs_kwargs::Dict{Symbol}=Dict{Symbol, Any}()`:
- `canonical_url::Union{Function, Nothing}=nothing`:`
- `index_md::AbstractString`
- `make_jl::AbstractString`
!!! note
If deploying documentation with Travis CI, don't forget to complete the required configuration.
@ -24,14 +26,18 @@ struct Documenter{T<:Union{TravisCI, GitLabCI, Nothing}} <: Plugin
assets::Vector{String}
makedocs_kwargs::Dict{Symbol}
canonical_url::Union{Function, Nothing}
make_jl::String
index_md::String
# Can't use @kwdef due to some weird precompilation issues.
function Documenter{T}(
assets::Vector{<:AbstractString}=String[],
makedocs_kwargs::Dict{Symbol}=Dict{Symbol, Any}(),
canonical_url::Union{Function, Nothing}=T === TravisCI ? github_pages_url : nothing,
index_md::AbstractString=default_file("index.md"),
make_jl::AbstractString=default_file("make.jl"),
) where T <: Union{TravisCI, GitLabCI, Nothing}
return new(assets, makedocs_kwargs, canonical_url)
return new(assets, makedocs_kwargs, canonical_url, index_md, make_jl)
end
end

View File

@ -23,7 +23,7 @@ end
source(p::Readme) = p.file
destination(p::Readme) = p.destination
function view(::Readme, t::Template, pkg::AbstractString)
function view(p::Readme, t::Template, pkg::AbstractString)
# Explicitly ordered badges go first.
strings = String[]
done = DataType[]
@ -36,12 +36,14 @@ function view(::Readme, t::Template, pkg::AbstractString)
end
foreach(setdiff(keys(t.plugins), done)) do T
bs = badges(t.plugins[T], t, pkg)
text *= "\n" * join(badges(t.plugins[T], t.user, pkg), "\n")
append!(strings, badges(t.plugins[T], t, pkg))
end
return Dict(
"BADGES" => strings,
"HAS_CITATION" => hasplugin(t, Citation),
"HAS_INLINE_BADGES" => p.inline_badges,
"HAS_INLINE_BADGES" => !isempty(strings) && p.inline_badges,
"PKG" => pkg,
)
end

View File

@ -1,7 +1,7 @@
const DEFAULT_VERSION = VersionNumber(VERSION.major)
"""
Template(interactive::Bool=false; kwargs...) -> Template
Template(; interactive::Bool=false, kwargs...) -> Template
Records common information used to generate a package.
@ -26,10 +26,10 @@ Records common information used to generate a package.
- `disable_default_plugins::Vector{DataType}=DataType[]`: Default plugins to disable.
The default plugins are [`Readme`](@ref), [`License`](@ref), [`Tests`](@ref), and [`Gitignore`](@ref).
To override a default plugin instead of disabling it altogether, supply it via `plugins`.
- `interactive::Bool=false`: When set, creates the template interactively from user input,
using the previous keywords as a starting point.
- `fast::Bool=false`: Only applicable when `interactive` is set.
Skips prompts for any unsupplied keywords except `user` and `plugins`.
## Interactive Usage
- `interactive::Bool=false`: When set, creates the template interactively, filling unset keywords with user input.
- `fast::Bool=false`: Skips prompts for any unsupplied keywords except `user` and `plugins`.
"""
struct Template
user::String