diff --git a/.github/workflows/python.yml b/.github/workflows/python.yml index ce53a9d..b9f40e2 100644 --- a/.github/workflows/python.yml +++ b/.github/workflows/python.yml @@ -11,6 +11,7 @@ jobs: runs-on: ${{ matrix.os }} strategy: matrix: + variant: [main, onepass] os: [macos-11.0, ubuntu-18.04, windows-2019, self-hosted-linux-arm64, self-hosted-macos-arm64] python: [3.8, 3.9, '3.10'] steps: @@ -49,7 +50,7 @@ jobs: run: bash ./prebuild.sh - name: Build native module - working-directory: ./python + working-directory: ./python/${{ matrix.feature }} run: cargo build --release - name: Install Python build tools (macOS x64) @@ -71,7 +72,7 @@ jobs: - name: Pack and publish package shell: bash - working-directory: ./python + working-directory: ./python/${{ matrix.feature }} run: | cat << 'EOF' > "$HOME/.pypirc" [pypi] diff --git a/CHANGELOG.md b/CHANGELOG.md index de42f1c..2fda78a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## 0.9.2 - Fix Node.js dependency version. +- Create onepass variant for Python. ## 0.9.1 diff --git a/format b/format index 112d6c8..fc67fde 100755 --- a/format +++ b/format @@ -4,7 +4,7 @@ set -Eeuxo pipefail pushd "$(dirname "$0")" >/dev/null -npx prettier@2.3.2 -w 'version' 'bench/*.{js,json}' 'bench/runners/*.{js,json}' 'bench/runners/*/*.{js,json}' 'gen/*.{ts,json}' +npx prettier@2.3.2 -w 'version' 'bench/*.{js,json}' 'bench/runners/*.{js,json}' 'bench/runners/*/*.{js,json}' 'gen/*.{ts,json}' 'nodejs/*.{js,json,ts}' for dir in \ bench/runners/minify-html \ @@ -16,7 +16,8 @@ for dir in \ fuzz/process \ java \ nodejs \ - python \ + python/main \ + python/onepass \ ruby \ rust/main \ rust/onepass \ diff --git a/python/README.md b/python/README.md deleted file mode 120000 index 32d46ee..0000000 --- a/python/README.md +++ /dev/null @@ -1 +0,0 @@ -../README.md \ No newline at end of file diff --git a/python/.cargo/config b/python/main/.cargo/config similarity index 100% rename from python/.cargo/config rename to python/main/.cargo/config diff --git a/python/.gitignore b/python/main/.gitignore similarity index 100% rename from python/.gitignore rename to python/main/.gitignore diff --git a/python/Cargo.toml b/python/main/Cargo.toml similarity index 91% rename from python/Cargo.toml rename to python/main/Cargo.toml index 7e41865..806c0cc 100644 --- a/python/Cargo.toml +++ b/python/main/Cargo.toml @@ -15,7 +15,7 @@ name = "minify_html" crate-type = ["cdylib"] [dependencies] -minify-html = { path = "../rust/main" } +minify-html = { path = "../../rust/main" } [dependencies.pyo3] version = "0.13.0" features = ["extension-module"] diff --git a/python/main/README.md b/python/main/README.md new file mode 120000 index 0000000..fe84005 --- /dev/null +++ b/python/main/README.md @@ -0,0 +1 @@ +../../README.md \ No newline at end of file diff --git a/python/minify_html.pyi b/python/main/minify_html.pyi similarity index 100% rename from python/minify_html.pyi rename to python/main/minify_html.pyi diff --git a/python/src/lib.rs b/python/main/src/lib.rs similarity index 100% rename from python/src/lib.rs rename to python/main/src/lib.rs diff --git a/python/onepass/.cargo/config b/python/onepass/.cargo/config new file mode 100644 index 0000000..d47f983 --- /dev/null +++ b/python/onepass/.cargo/config @@ -0,0 +1,11 @@ +[target.x86_64-apple-darwin] +rustflags = [ + "-C", "link-arg=-undefined", + "-C", "link-arg=dynamic_lookup", +] + +[target.aarch64-apple-darwin] +rustflags = [ + "-C", "link-arg=-undefined", + "-C", "link-arg=dynamic_lookup", +] diff --git a/python/onepass/.gitignore b/python/onepass/.gitignore new file mode 100644 index 0000000..ffcf2b4 --- /dev/null +++ b/python/onepass/.gitignore @@ -0,0 +1,10 @@ +/Cargo.lock +/target/ + +__pycache__/ +/venv/ + +# Used by Python setuptools. +/build/ +/dist/ +/*.egg-info/ diff --git a/python/onepass/Cargo.toml b/python/onepass/Cargo.toml new file mode 100644 index 0000000..0431c24 --- /dev/null +++ b/python/onepass/Cargo.toml @@ -0,0 +1,21 @@ +[package] +publish = false +name = "minify_html_onepass" +description = "Even faster version of minify-html" +license = "MIT" +homepage = "https://github.com/wilsonzlin/minify-html" +readme = "README.md" +repository = "https://github.com/wilsonzlin/minify-html.git" +version = "0.9.1" +authors = ["Wilson Lin "] +edition = "2018" + +[lib] +name = "minify_html_onepass" +crate-type = ["cdylib"] + +[dependencies] +minify-html-onepass = { path = "../../rust/onepass" } +[dependencies.pyo3] +version = "0.13.0" +features = ["extension-module"] diff --git a/python/onepass/README.md b/python/onepass/README.md new file mode 120000 index 0000000..7e686e2 --- /dev/null +++ b/python/onepass/README.md @@ -0,0 +1 @@ +../../rust/onepass/README.md \ No newline at end of file diff --git a/python/onepass/minify_html_onepass.pyi b/python/onepass/minify_html_onepass.pyi new file mode 100644 index 0000000..5f26cbf --- /dev/null +++ b/python/onepass/minify_html_onepass.pyi @@ -0,0 +1,5 @@ +def minify( + code: str, + minify_css: bool = False, + minify_js: bool = False, +) -> str: ... diff --git a/python/onepass/src/lib.rs b/python/onepass/src/lib.rs new file mode 100644 index 0000000..dd089f3 --- /dev/null +++ b/python/onepass/src/lib.rs @@ -0,0 +1,24 @@ +use minify_html_onepass::{Cfg, Error, in_place as minify_html_native}; +use pyo3::prelude::*; +use pyo3::exceptions::PySyntaxError; +use pyo3::wrap_pyfunction; +use std::str::from_utf8_unchecked; + +#[pyfunction(py_args="*", minify_js="false", minify_css="false")] +fn minify(code: String, minify_js: bool, minify_css: bool) -> PyResult { + let mut code = code.into_bytes(); + match minify_html_native(&mut code, &Cfg { + minify_js, + minify_css, + }) { + Ok(out_len) => Ok(unsafe { from_utf8_unchecked(&code[0..out_len]).to_string() }), + Err(Error { error_type, position }) => Err(PySyntaxError::new_err(format!("{} [Character {}]", error_type.message(), position))), + } +} + +#[pymodule] +fn minify_html_onepass(_py: Python, m: &PyModule) -> PyResult<()> { + m.add_wrapped(wrap_pyfunction!(minify))?; + + Ok(()) +} diff --git a/rust/onepass/README.md b/rust/onepass/README.md index f2d8413..63d4cd2 100644 --- a/rust/onepass/README.md +++ b/rust/onepass/README.md @@ -14,12 +14,11 @@ An HTML minifier that provides the functionality of [minify-html](https://github The API is different compared to minify-html; refer to per-package documentation for more details. -- [in.wilsonl.minifyhtmlonepass](https://search.maven.org/artifact/in.wilsonl.minifyhtmlonepass/minify-html-onepass) -- [@minify-html/onepass](https://www.npmjs.com/package/@minify-html/onepass) - [minify-html-onepass](https://pypi.org/project/minify-html-onepass) -- [minify_html_onepass](https://rubygems.org/gems/minify_html_onepass) - [minify-html-onepass](https://crates.io/crates/minify-html-onepass) +If you don't see your preferred language here and the main library supports it, raise an issue. + ## Parsing In addition to the [minify-html rules](https://github.com/wilsonzlin/minify-html/blob/master/notes/Parsing.md), the onepass variant has additional requirements: diff --git a/version b/version index fb5e462..5cd6e8d 100755 --- a/version +++ b/version @@ -101,7 +101,8 @@ for (const f of [ "cli/Cargo.toml", "nodejs/Cargo.toml", "java/Cargo.toml", - "python/Cargo.toml", + "python/main/Cargo.toml", + "python/onepass/Cargo.toml", "ruby/Cargo.toml", "wasm/Cargo.toml", ]) {