diff --git a/src/build.jl b/src/build.jl index 38ea103c2..fc034cce4 100644 --- a/src/build.jl +++ b/src/build.jl @@ -1,4 +1,4 @@ -const PY = begin +function PY() if "PYTHON3" ∈ keys(ENV) ENV["PYTHON3"] else @@ -9,7 +9,7 @@ const PY = begin end end end -const PIP = begin +function PIP() if "PIP3" ∈ keys(ENV) ENV["PIP3"] else @@ -20,7 +20,7 @@ const PIP = begin end end end -const NODE = begin +function NODE() if "NODE" ∈ keys(ENV) ENV["NODE"] else @@ -39,8 +39,28 @@ 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 @@ -48,35 +68,26 @@ Minification - 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 diff --git a/src/converter/html/prerender.jl b/src/converter/html/prerender.jl index a76ba9973..dbbc819ea 100644 --- a/src/converter/html/prerender.jl +++ b/src/converter/html/prerender.jl @@ -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)) diff --git a/src/manager/extras.jl b/src/manager/extras.jl index 75d86cfa1..ad33d0c77 100644 --- a/src/manager/extras.jl +++ b/src/manager/extras.jl @@ -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." diff --git a/src/manager/post_processing.jl b/src/manager/post_processing.jl index 1f14e268d..7e7516548 100644 --- a/src/manager/post_processing.jl +++ b/src/manager/post_processing.jl @@ -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 @@ -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) @@ -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 diff --git a/src/manager/write_page.jl b/src/manager/write_page.jl index effea7740..02f30fb63 100644 --- a/src/manager/write_page.jl +++ b/src/manager/write_page.jl @@ -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""=>"") diff --git a/test/global/postprocess.jl b/test/global/postprocess.jl index 7042def37..e911b5166 100644 --- a/test/global/postprocess.jl +++ b/test/global/postprocess.jl @@ -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) @@ -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""" @@ -73,7 +73,7 @@ if F.FD_CAN_PRERENDER; @testset "prerender" begin # @test occursin("""M""", jskx) end - if F.FD_CAN_HIGHLIGHT; @testset "highlight" begin + if F.FD_CAN_HIGHLIGHT(); @testset "highlight" begin hs = raw"""