Coder Social home page Coder Social logo

ekinops / zundler Goto Github PK

View Code? Open in Web Editor NEW

This project forked from adrianvollmer/zundler

0.0 0.0 0.0 80.69 MB

Bundle assets of distributed HTML docs into one self-contained HTML file

Home Page: https://adrianvollmer.github.io/Zundler/

License: MIT License

JavaScript 34.44% Python 58.69% CSS 4.74% Makefile 1.90% HTML 0.23%

zundler's Introduction

Zundler

Bundle assets of distributed HTML docs into one self-contained HTML file.

Warning This is still experimental. Expect breakages! Feel free to open an issue. Please provide everything that is necessary to reproduce the problem.

Naively building one HTML file out of a directory of HTML files that total dozens of megabytes quickly overwhelm modern browsers. Zundler solves this by taking the following approach:

  • Embed all assets that are referenced in CSS files, such as imports and fonts
  • Endow all HTML files with extra scripts that embed these resources at run time:
    • Convert script tags with a src attribute referencing a local JavaScript file to inline scripts
    • Convert link tags with an href attribute referencing a local CSS file to inline styles
    • Make all img tags with a src attribute referencing local images use a data URI
    • Monkey patch the JavaScript function fetch so requests to local files are turned into queries of the "virtual file tree" (see next item)
    • Monkey patch the JavaScript class URLSearchParams so GET requests to local files can be handeled; this is needed for Sphinx' search function, for example
  • Create a JSON structure (the "global context") out of all files in the directory (the "virtual file tree") and other data
  • Gzip the global context and base64-encode it
  • Bundle it all into a scuffolding file with this structure:
    <!DOCTYPE html><html>
    <head><style>{style}</style></head>
    <body>{body}
    <script>window.global_context = {zipped_global_context}</script>
    <script>{pako} //# sourceURL=pako.js</script>
    <script>{init_js} //# sourceURL=init.js</script>
    </body></html>
    The global context is then unzipped using the Pako library, an iframe is created and the document is bootstrapped from the virtual file tree.

The output file is usually smaller than the sum of all input files despite some resources being embedded redundantly and the 33% overhead of the base64 encoding.

Limitations

This approach is quite hacky, but it might work well enough for some purposes.

  • Some scripts may break as the execution flow is different than some scripts expect
  • HTML forms with method="GET" to local HTML files only work if the receiving code uses URLSearchParams, as same-origin policies forbid reading GET parameters otherwise
  • Opening links in a new tab won't work
  • Redirections won't work (yet)
  • Support of themes or extensions that require jQuery is wonky; fortunately Sphinx stopped supporting jQuery in version 6

Installation

Zundler is a Python package and can be installed like any other Python package.

The recommended way is to run this:

$ pip install git+https://github.com/AdrianVollmer/Zundler@main

If you prefer an alternative like pipx, poetry or manually handled venvs, you probably know what to do.

An entry point called zundler will appear in ~/.local/bin.

Bundling Sphinx docs

The Zundler package provides a Sphinx extension that adds an appropriate builder. The builder is a thin wrapper around the HTML builder, which runs zundler at the end. It can be used with sphinx-build -b zundler or, if there is a suitable Makefile, with make zundler.

Jupyter-Books can be built with jupyter-book build --custom-builder zundler --builder custom .. You may have to add a render priority for Zundler like here:

sphinx:
  config:
    nb_mime_priority_overrides: [
      ["zundler", "application/vnd.jupyter.widget-view+json", 10],
      ["zundler", "application/javascript", 20],
      ["zundler", "text/html", 30],
      ["zundler", "image/svg+xml", 40],
      ["zundler", "image/png", 50],
      ["zundler", "image/jpeg", 60],
      ["zundler", "text/markdown", 70],
      ["zundler", "text/latex", 80],
      ["zundler", "text/plain", 90]
    ]

Demos

Some demo documents are available here: https://adrianvollmer.github.io/Zundler

Copyright

Adrian Vollmer, 2022. MIT licensed; see LICENSE for details.

zundler's People

Contributors

adrianvollmer avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.