Build a project theme ===================== A theme gives the same content two reading forms: a website and a book. This tutorial builds an Orbit theme with a shared visual identity, an HTML partial and macro, and a modular PDF template. It needs no plugin or compiler. 1. Start with a real project ---------------------------- Use Guidedog 1.0.0-beta.9 or newer. Run this from the directory that will contain ``orbit``: .. code-block:: sh guidedog quickstart orbit -q -p "Orbit Field Notes" -a "Orbit contributors" -v 1.0 Rename the generated ``orbit/_templates/book.typ`` to ``base-book.typ`` in the same directory. Keep the generated ``theme.typ`` beside it; the renamed book template supplies the tested page layout, cover, running heads, contents, code styles and optimized paragraph breaking. Download :download:`conf.toml <../_static/repository/theme/conf.toml>`, :download:`index.rst <../_static/repository/theme/index.rst>` and :download:`reading.rst <../_static/repository/theme/chapters/reading.rst>`. Save the first two in ``orbit`` and the third in ``orbit/chapters``. The finished project will have these files: .. code-block:: text orbit/ conf.toml index.rst chapters/reading.rst _templates/ layout.html macros/ui.html partials/edition.html theme.typ # Shared PDF theme generated by quickstart. discussion.typ # Discussion layout generated by quickstart. article.typ # Standalone article layout generated by quickstart. base-book.typ # Generated by quickstart; renamed, not replaced. book.typ palette.typ _static/ guidedog.css # Generated by quickstart. guidedog.js # Generated by quickstart. typography.js # Generated by quickstart. theme.css 2. Set the contract ------------------- The downloaded configuration selects the local template and static directories. ``html_css_files`` adds our stylesheet after the default stylesheet. ``html_context`` supplies a project value to Jinja. The explicit PDF filename makes the output easy to find. .. literalinclude:: ../_static/repository/theme/conf.toml :language: toml :caption: orbit/conf.toml All paths in this tutorial are project files. HTML templates are found through ``templates_path``. The PDF entry point is always ``_templates/book.typ`` under the source directory. Changing ``templates_path`` does not move it. 3. Extend the HTML layout ------------------------- Save :download:`layout.html <../_static/repository/theme/_templates/layout.html>` as ``orbit/_templates/layout.html``. Replace the generated file completely: .. literalinclude:: ../_static/repository/theme/_templates/layout.html :language: html+jinja :caption: orbit/_templates/layout.html ``!layout.html`` selects Guidedog's built-in layout. Without ``!``, a template named ``layout.html`` would try to extend itself. ``super()`` keeps the parent's block content. Navigation, search, theme controls and accessible landmarks remain in the base layout. Our blocks add a label, metadata and an edition footer. A theme changes presentation. A build plugin changes source text, complete HTML or generated assets. Use :doc:`../plugins` when a template cannot express the required operation. Ordinary layout changes need no native code. 4. Give repeated markup one home -------------------------------- Create ``orbit/_templates/macros`` and ``orbit/_templates/partials``. Save :download:`ui.html <../_static/repository/theme/_templates/macros/ui.html>` and :download:`edition.html <../_static/repository/theme/_templates/partials/edition.html>` at the matching paths: .. literalinclude:: ../_static/repository/theme/_templates/macros/ui.html :language: html+jinja :caption: orbit/_templates/macros/ui.html .. literalinclude:: ../_static/repository/theme/_templates/partials/edition.html :language: html+jinja :caption: orbit/_templates/partials/edition.html A macro takes explicit inputs. An included partial uses the current page's context. Escape plain project values with ``|e``. Use ``pathto`` for document links: a hard-coded ``chapters/reading.html`` would break on a nested page or with another HTML builder. 5. Design for the reader's theme -------------------------------- Save :download:`theme.css <../_static/repository/theme/_static/theme.css>` as ``orbit/_static/theme.css``: .. literalinclude:: ../_static/repository/theme/_static/theme.css :language: css :caption: orbit/_static/theme.css The built-in theme follows the system scheme by default, and the existing control selects system, light or dark and saves the choice. Explicit dark rules and the system media query use the same tokens. Typography and spacing stay shared. No new JavaScript is needed. Avoid a fixed ink color on a background whose theme can change. ``_static/typography.js`` optimizes desktop English paragraphs with Knuth-Plass line breaking and Liang hyphenation, while ``guidedog.css`` falls back cleanly to ``text-wrap: pretty`` when JavaScript is disabled or on narrow screens; see :doc:`../topics/typography`. Diagrams with ``:class: graphviz-adaptive`` use the selected scheme in current Guidedog builds. Ordinary graphs retain their own colors. Older hosts render the same DOT in its light palette. See :doc:`../topics/diagrams` for semantic node classes and print behavior. 6. Wrap the PDF book -------------------- Save :download:`palette.typ <../_static/repository/theme/_templates/palette.typ>` and :download:`book.typ <../_static/repository/theme/_templates/book.typ>` in ``orbit/_templates``. These are new files; keep ``base-book.typ`` beside them. .. literalinclude:: ../_static/repository/theme/_templates/palette.typ :language: typst :caption: orbit/_templates/palette.typ .. literalinclude:: ../_static/repository/theme/_templates/book.typ :language: typst :caption: orbit/_templates/book.typ The host calls ``book`` with metadata and the chapter content. The wrapper forwards those values to the generated base and supplies our palette. Passing ``body`` into the styled function keeps its rules in scope. The web stylesheet does not affect PDF, and the Typst template does not affect HTML. The base handles language-aware hyphenation and optimized line breaking. The wrapper keeps those rules, uses the matching blue accent and adds page numbers to document cross-references. PDF always uses the light, print palette. 7. Build and inspect both forms ------------------------------- Run from the same parent directory as step 1: .. code-block:: sh guidedog build html orbit -W guidedog build pdf orbit -W guidedog build dirhtml orbit -W guidedog build singlehtml orbit -W guidedog serve orbit Open the URL printed by ``serve``. Check the chapter and generated search page, not only the front page. Try light, dark and system modes. Resize the window. Use the keyboard to reach the navigation and search. Change the footer partial, save, then refresh: the preview rebuilds its template dependencies. Open ``orbit/_build/pdf/orbit-notes-en.pdf`` in a PDF reader. Guidedog adds the language suffix to the configured filename. Inspect the contents, chapter opening, equation, table, code and diagram. Look for clipped labels, loose paragraphs and missing glyphs. The other outputs are below ``_build/html``, ``_build/dirhtml`` and ``_build/singlehtml``. Stop the server with ``Ctrl+C``. 8. Reuse and maintain the theme ------------------------------- Create another project with ``guidedog quickstart``. Copy the finished ``_templates`` directory and ``_static/theme.css`` into it. Add ``html_css_files = ["theme.css"]``, the accent option and the ``edition_label`` context value to its configuration. Change the footer's document link to a page that exists in that project. Keep the new project's metadata and source tree. The copied PDF base makes the theme self-contained. When we upgrade Guidedog, we create a scratch quickstart project and compare its templates with our saved base. We adopt the changes we need, then rebuild every target. The built-in HTML layout can change too. Small block overrides are easier to maintain than a second complete layout. For every variable and block, see :doc:`../templates`. If the build reports a missing ``base-book.typ``, finish the rename in step 1. If the label is blank, set ``edition_label`` in ``html_context``. If CSS is missing, check both ``html_static_path`` and ``html_css_files``. Read the file, line and correction in the diagnostic before changing unrelated settings.