Skip to content

Commit

Permalink
Initialize global variables lazily instead of during precompilation. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikekre authored Oct 25, 2021
1 parent d5fd301 commit da7c927
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 49 deletions.
75 changes: 43 additions & 32 deletions src/build.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const PY = begin
function PY()
if "PYTHON3" keys(ENV)
ENV["PYTHON3"]
else
Expand All @@ -9,7 +9,7 @@ const PY = begin
end
end
end
const PIP = begin
function PIP()
if "PIP3" keys(ENV)
ENV["PIP3"]
else
Expand All @@ -20,7 +20,7 @@ const PIP = begin
end
end
end
const NODE = begin
function NODE()
if "NODE" keys(ENV)
ENV["NODE"]
else
Expand All @@ -39,44 +39,55 @@ Pre-rendering
require `katex.min.js`
- For highlights, we need `node` and also to have the `highlight.js` installed via `npm`.
=#
const FD_CAN_PRERENDER = shell_try(`$NODE -v`)
const FD_CAN_HIGHLIGHT = shell_try(`$NODE -e "require('$(HIGHLIGHTJS[])')"`)
function FD_CAN_PRERENDER()
r = shell_try(`$(NODE()) -v`)
if !r
@warn """Couldn't find node.js: `$(NODE()) -v` failed. Setting `prerender=false`.
Note: node is required for pre-rendering KaTeX and highlight.js, but it is not necessary to run Franklin (cf docs)."""
end
return r
end
let r = nothing # Hack to only run the checks once per call to Franklin.optimize since this is called multiple times
global function FD_CAN_HIGHLIGHT(; force::Bool=false)
if force || r === nothing
r = FD_CAN_PRERENDER() && shell_try(`$(NODE()) -e "require('$(HIGHLIGHTJS[])')"`)
if !r
@warn """Couldn't find highlight.js: `$(NODE()) -e "require('$(HIGHLIGHTJS[])')"` failed. Will not prerender code blocks.
Note: highlight.js is required for pre-rendering highlight.js, but is not necessary to run Franklin (cf docs)."""
end
end
return r
end
end

#=
Minification
- We use `css_html_js_minify`. To use it, you need python3 and to install it.
- Here we check there is python3, and pip3, and then if we fail to import, we try to
use pip3 to install it.
=#
const FD_HAS_PY3 = shell_try(`$([e for e in split(PY)]) -V`)
const FD_HAS_PIP3 = shell_try(`$([e for e in split(PIP)]) -V`)
const FD_CAN_MINIFY = FD_HAS_PY3 && FD_HAS_PIP3 &&
(success(`$([e for e in split(PY)]) -c "import css_html_js_minify"`) ||
success(`$([e for e in split(PIP)]) install css_html_js_minify`))
#=
Information to the user
- what Franklin couldn't find
- that the user should do a `build` step after installing
=#
FD_CAN_HIGHLIGHT || begin
FD_CAN_PRERENDER || begin
println("""✘ Couldn't find node.js (`$NODE -v` failed).
→ It is required for pre-rendering KaTeX and highlight.js but is not necessary to run Franklin (cf docs).""")
end
println("""✘ Couldn't find highlight.js (`$NODE -e "require('$(HIGHLIGHTJS[])')"` failed).
→ It is required for pre-rendering highlight.js but is not necessary to run Franklin (cf docs).""")
end
function FD_HAS_PY3()
r = shell_try(`$([e for e in split(PY())]) -V`)
if !r
@warn """Couldn't find python3 (`$([e for e in split(PY())]) -V` failed). Will not minify.
FD_CAN_MINIFY || begin
if FD_HAS_PY3
println("✘ Couldn't find css_html_js_minify (`$([e for e in split(PY)]) -m \"import css_html_js_minify\"` failed).\n" *
"""→ It is required for minification but is not necessary to run Franklin (cf docs).""")
else
println("""✘ Couldn't find python3 (`$([e for e in split(PY)]) -V` failed).
→ It is required for minification but not necessary to run Franklin (cf docs).""")
Note: python3 is required for minification, but not necessary to run Franklin (cf docs)."""
end
return r
end
FD_HAS_PIP3() = shell_try(`$([e for e in split(PIP())]) -V`)

function FD_CAN_MINIFY()
r = FD_HAS_PY3() && FD_HAS_PIP3()
r || return r
r = (success(`$([e for e in split(PY())]) -c "import css_html_js_minify"`) ||
success(`$([e for e in split(PIP())]) install css_html_js_minify`))
if !r
@warn """Couldn't find css_html_js_minify (`$([e for e in split(PY())]) -m "import css_html_js_minify"` failed). Will not minify.
all((FD_CAN_HIGHLIGHT, FD_CAN_PRERENDER, FD_CAN_MINIFY, FD_HAS_PY3, FD_HAS_PIP3)) || begin
println("→ After installing any missing component, please re-build the package (cf docs).")
Note: css_html_js_minify is required for minification, but is not necessary to run Franklin (cf docs)."""
end
return r
end
2 changes: 1 addition & 1 deletion src/converter/html/prerender.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ function js2html(hs::String, jsbuffer::IOBuffer, matches::Vector{RegexMatch},
splitter::String)::String
# run it redirecting the output to a buffer
outbuffer = IOBuffer()
run(pipeline(`$NODE -e "$(String(take!(jsbuffer)))"`, stdout=outbuffer))
run(pipeline(`$(NODE()) -e "$(String(take!(jsbuffer)))"`, stdout=outbuffer))

# read the buffer and split it using $splitter
out = String(take!(outbuffer))
Expand Down
2 changes: 1 addition & 1 deletion src/manager/extras.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function lunr()::Nothing
start = time()
msg = rpad("→ Building the Lunr index...", 35)
print(msg)
run(`$NODE $(splitdir(buildindex)[2])`)
run(`$(NODE()) $(splitdir(buildindex)[2])`)
print_final(msg, start)
catch e
@warn "There was an error building the Lunr index."
Expand Down
15 changes: 4 additions & 11 deletions src/manager/post_processing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,10 @@ function optimize(;
#
# Prerendering
#
if prerender && !FD_CAN_PRERENDER
@warn "I couldn't find node and so will not be able to pre-render javascript."
if prerender && !FD_CAN_PRERENDER(; force=true)
prerender = false
end
if prerender && !FD_CAN_HIGHLIGHT
@warn "I couldn't load 'highlight.js' so will not be able to pre-render code blocks. " *
"You can install it with `npm install highlight.js`."
end
prerender && !FD_CAN_HIGHLIGHT(; force=true)
if !isempty(prepath)
GLOBAL_VARS["prepath"] = dpair(prepath)
end
Expand Down Expand Up @@ -104,7 +100,7 @@ function optimize(;
# Minification
#
if minify && (succ || no_fail_prerender)
if FD_CAN_MINIFY
if FD_CAN_MINIFY()
start = time()
mmsg = rpad("→ Minifying *.[html|css] files...", 35)
print(mmsg)
Expand All @@ -114,13 +110,10 @@ function optimize(;
py_script = read(path_to, String)
write(FD_PY_MIN_NAME, py_script)
# run it
succ = success(`$([e for e in split(PY)]) $FD_PY_MIN_NAME`)
succ = success(`$([e for e in split(PY())]) $FD_PY_MIN_NAME`)
# remove the script file
rm(FD_PY_MIN_NAME)
print_final(mmsg, start)
else
@warn "I didn't find css_html_js_minify, you can install it via " *
"pip. The output will not be minified."
end
end

Expand Down
2 changes: 1 addition & 1 deletion src/manager/write_page.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function postprocess_page(pg)
pg = js_prerender_katex(pg)
end
# Code (HIGHLIGHT.JS)
if locvar(:hascode)::Bool && FD_CAN_HIGHLIGHT
if locvar(:hascode)::Bool && FD_CAN_HIGHLIGHT()
pg = js_prerender_highlight(pg)
# remove script
pg = replace(pg, r"<script.*?(?:highlight\.pack\.js|initHighlightingOnLoad).*?<\/script>"=>"")
Expand Down
6 changes: 3 additions & 3 deletions test/global/postprocess.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
@test isfile(joinpath("__site", "css", "franklin.css"))

# ---------------
if Franklin.FD_CAN_MINIFY
if Franklin.FD_CAN_MINIFY()
presize1 = stat(joinpath("__site", "css", "basic.css")).size
presize2 = stat(joinpath("__site", "index.html")).size
optimize(prerender=false)
Expand All @@ -51,7 +51,7 @@
@test occursin("=\"/prependme/libs/katex/katex.min.css", index)
end

if F.FD_CAN_PRERENDER; @testset "prerender" begin
if F.FD_CAN_PRERENDER(); @testset "prerender" begin
@testset "katex" begin
hs = raw"""
<!doctype html>
Expand All @@ -73,7 +73,7 @@ if F.FD_CAN_PRERENDER; @testset "prerender" begin
# @test occursin("""<span class=\"katex-display\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><mi>M</mi>""", jskx)
end

if F.FD_CAN_HIGHLIGHT; @testset "highlight" begin
if F.FD_CAN_HIGHLIGHT(); @testset "highlight" begin
hs = raw"""
<!doctype html>
<html lang=en>
Expand Down

0 comments on commit da7c927

Please sign in to comment.