Skip to content
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

Replace html-css-js-minify with minify_jll, eliminate Python3 dependency #1038

Closed
wants to merge 11 commits into from
3 changes: 2 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
REPL = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
TOML = "fa267f1f-6049-4f14-aa54-33bafae1ed76"
minify_jll = "7dd700f0-7fa2-5578-8ac1-d309908db794"

[compat]
DocStringExtensions = "0.8, 0.9"
Expand All @@ -31,7 +32,7 @@ LiveServer = "0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0"
NodeJS = "0.6, 1"
OrderedCollections = "1"
TOML = "1.0"
julia = "1.3"
julia = "1.6"

[extras]
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
Expand Down
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Go to [Franklin's main website](https://franklinjl.org). For users already famil
### Important notes

* if, upon deployment, your website doesn't seem to apply CSS, you likely forgot a step, please [see here](https://franklinjl.org/workflow/deploy/#creating_a_repo_on_github).
* it is currently recommended to **switch off** pre-rendering and minification (you can do this by passing `prerender=false, minify=false` to the `optimize(...)` call in your deploy script), the corresponding logic will be removed in future version of Franklin as it's tricky to maintain and caused too many issues.
* it is currently recommended to **switch off** pre-rendering (you can do this by passing `prerender=false` to the `optimize(...)` call in your deploy script), the corresponding logic will be removed in future version of Franklin as it's tricky to maintain and caused too many issues.
* if you're getting warning messages about some JS library not being found on your computer, you can safely ignore this.

## Key features
Expand Down Expand Up @@ -149,5 +149,4 @@ While Franklin broadly supports standard Markdown there are a few things that ma

* KaTeX is [MIT licensed](https://github.com/KaTeX/KaTeX/blob/master/LICENSE),
* Node's is essentially [MIT licensed](https://github.com/nodejs/node/blob/master/LICENSE),
* css-html-js-minify is [LGPL licensed](https://github.com/juancarlospaco/css-html-js-minify/blob/master/LICENCE.lgpl.txt),
* highlight.js is [BSD licensed](https://github.com/highlightjs/highlight.js/blob/master/LICENSE).
10 changes: 2 additions & 8 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,7 @@ You can also inspect the file `menu1.md` which offers more examples of what Fran
## Installing optional extras

Franklin allows a post-processing step to compress HTML and CSS and pre-render code blocks and math environments.
This requires a couple of dependencies:
@@tlist
* [`python3`](https://www.python.org/downloads/) for the minification of the site,
* [`node.js`](https://nodejs.org/en/) for the pre-rendering of KaTeX and code highlighting.
@@
Minifcation is handled via Taco de Wolff's [Minify](https://pkg.go.dev/github.com/tdewolff/Minify/cmd/minify) package, which is already included. Pre-rendering of KaTeX and code highlighting requires [`node.js`](https://nodejs.org/en/) as an external dependency.
You will then need to install `highlight.js`, which you should do from Julia using the [NodeJS.jl](https://github.com/davidanthoff/NodeJS.jl) package:

```julia-repl
Expand All @@ -68,8 +64,6 @@ julia> run(`sudo $(npm_cmd()) install highlight.js`)

**Note**: a key advantage of using `NodeJS` for this instead of using `npm` yourself is that it puts the libraries in the "right place" for Julia to find them.

Assuming you have `python3`, Franklin will try to install the python package [`css_html_js_minify`](https://github.com/juancarlospaco/css-html-js-minify) if via `pip3`.

\note{
You **don't have to** install these libraries and you can safely ignore any message suggesting you install those. Also note that if you do want this but it doesn't work locally due to some `node` weirdness or related, things will likely still work if you use GitHub to deploy.
You **don't have to** install the external libraries and you can safely ignore any message suggesting you install those. Also note that if you do want this but it doesn't work locally due to some `node` weirdness or related, things will likely still work if you use GitHub to deploy.
}
4 changes: 2 additions & 2 deletions docs/workflow/deploy.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ Deploying the website is trivial on an existing web server, via GitHub or Gitlab
The contents of the `__site` folder can simply be deployed to a path on an existing server as follows.
Supposing you wish your site to appear at `http://my.example.com/path/to/my/franklin/site/`. You would take the following steps:

* Prepare the `__site` directory by running `optimize(prepath="/path/to/my/franklin/site/", minify=false)`.
Franklin.jl does not use relative links, so this step is needed to ensure that the links between site elements are correct. (The `minify = false` argument is optional but is currently recommended.)
* Prepare the `__site` directory by running `optimize(prepath="/path/to/my/franklin/site/")`.
Franklin.jl does not use relative links, so this step is needed to ensure that the links between site elements are correct.

* Copy the contents of the `__site` directory to the target location using your chosen method (for example, using [rsync](https://en.wikipedia.org/wiki/Rsync)).

Expand Down
2 changes: 0 additions & 2 deletions docs/workflow/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,6 @@ Those two steps _may_ lead to faster loading pages.
Note that in order to run them, you will need a couple of external dependencies as mentioned in the [installation section](/index.html#installing_optional_extras).

\note{
The minifier script (an external python lib) used is far from perfect and can break your website, generally speaking, if things work locally but not when you deploy, try disabling it with `optimize(minify=false)`.

Also note that if you use GitHub or GitLab pages, the minification is not needed as these platforms compress the pages themselves.
}

Expand Down
3 changes: 2 additions & 1 deletion src/Franklin.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ using OrderedCollections
using Pkg
using DocStringExtensions: SIGNATURES, TYPEDEF
using TOML
using minify_jll

import Logging
import LiveServer
Expand Down Expand Up @@ -162,7 +163,7 @@ end

# -----------------------------------------------------------------------------

include("build.jl") # check if user has Node/minify
include("build.jl") # check if user has Node

include("regexes.jl")

Expand Down
29 changes: 11 additions & 18 deletions src/build.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#=
This function currently sits unused, but may prove useful when some future python
dependency may be added for extended Franklin.jl functionality.
function PY()
if "PYTHON3" ∈ keys(ENV)
ENV["PYTHON3"]
Expand All @@ -12,6 +15,8 @@ function PY()
end
end
end
This function currently sits unused, but may prove useful when some future python
dependency may be added for extended Franklin.jl functionality.
function PIP()
if "PIP3" ∈ keys(ENV)
ENV["PIP3"]
Expand All @@ -23,6 +28,7 @@ function PIP()
end
end
end
=#
function NODE()
if "NODE" ∈ keys(ENV)
ENV["NODE"]
Expand All @@ -31,7 +37,7 @@ function NODE()
end
end

# highligh.js library; can be overridden from the outside which is useful for testing
# highlight.js library; can be overridden from the outside which is useful for testing
const HIGHLIGHTJS = Ref{String}("highlight.js")

shell_try(com)::Bool = try success(com); catch; false; end
Expand Down Expand Up @@ -66,11 +72,10 @@ let r = nothing # Hack to only run the checks once per call to Franklin.optimize
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
This function currently sits unused, but may prove useful when some future python
dependency may be added for extended Franklin.jl functionality.
Here we check there is python3, and pip3, and then if we fail to import, we try to
use pip3 to install it.
=#
function FD_HAS_PY3()
r = shell_try(`$([e for e in split(PY())]) -V`)
if !r
Expand All @@ -81,16 +86,4 @@ function FD_HAS_PY3()
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.

Note: css_html_js_minify is required for minification, but is not necessary to run Franklin (cf docs)."""
end
return r
end
=#
37 changes: 18 additions & 19 deletions src/manager/post_processing.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const FD_PY_MIN_NAME = ".__py_tmp_minscript.py"

"""
$(SIGNATURES)

Expand All @@ -16,8 +14,8 @@ Does a full pass followed by a pre-rendering and minification step.
not update `/stable/` and `/dev/`.
* `prerender=true`: whether to pre-render katex and highlight.js (requires
`node.js`)
* `minify=true`: whether to minify output (requires `python3` and
`css_html_js_minify`)
* `minify=true`: whether to minify output (html, css, js, json, svg, and
xml files are supported)
* `sig=false`: whether to return an integer indicating success (see
[`publish`](@ref))
* `clear=false`: whether to clear the output dir and thereby regenerate
Expand Down Expand Up @@ -100,21 +98,22 @@ function optimize(;
# Minification
#
if minify && (succ || no_fail_prerender)
if FD_CAN_MINIFY()
start = time()
mmsg = rpad("→ Minifying *.[html|css] files...", 35)
print(mmsg)
# copy the script to the current dir
path_to = joinpath(dirname(pathof(Franklin)),
"scripts", "minify.py")
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`)
# remove the script file
rm(FD_PY_MIN_NAME)
print_final(mmsg, start)
end
start = time()
mmsg = rpad("→ Minifying *.[html|css] files...", 35)
print(mmsg)
for (rootpath, _, files) in walkdir("__site")
if rootpath != "__site/assets"
files = filter(endswith(r"^.*\.(html|css|js|json|svg|xml)$"), files)
for file in files
filepath = joinpath(rootpath, file)
if !success(`$(minify_jll.minify()) -o $(filepath) $(filepath)`)
succ = false
break
end
end
end
end
print_final(mmsg, start)
end

# if not dev or preview, the /stable/ path was overwritten and we copy that
Expand Down
29 changes: 0 additions & 29 deletions src/scripts/minify.py

This file was deleted.

2 changes: 0 additions & 2 deletions test/global/postprocess.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,11 @@
@test isfile(joinpath("__site", "css", "franklin.css"))

# ---------------
if Franklin.FD_CAN_MINIFY()
presize1 = stat(joinpath("__site", "css", "basic.css")).size
presize2 = stat(joinpath("__site", "index.html")).size
optimize(prerender=false)
@test stat(joinpath("__site", "css", "basic.css")).size < presize1
@test stat(joinpath("__site", "index.html")).size < presize2
end
# ---------------
# verify all links (XXX)
# Franklin.verify_links()
Expand Down