Build and Publish#

This page takes you from a fresh clone to a published site: install the tools, build the HTML, preview it, and publish it. Each stage links to the page that covers it in more detail.

Prerequisites#

  • Python 3.11 or newer. The Sphinx version in requirements.txt needs it.

  • Git, and a GitHub account if you want to publish.

  • Graphviz, with the dot command on your PATH. A post draws a diagram with the graphviz directive, so the build needs it. If you remove such posts you can also remove the sphinx.ext.graphviz extension.

Set up the environment#

Clone the site source (or your own copy of it) into a folder called docs, create a virtual environment inside it and install the dependencies:

git clone https://github.com/samkhalilian/docs.git docs
cd docs
py -3.11 -m venv .venv
.venv\Scripts\Activate.ps1
pip install -r requirements.txt
git clone https://github.com/samkhalilian/docs.git docs
cd docs
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt

A virtual environment only works on the operating system that created it, so WSL and Windows each need their own. If PowerShell refuses to run the activation script, allow scripts for the current session only: Set-ExecutionPolicy -Scope Process -ExecutionPolicy RemoteSigned.

Build the site#

With the virtual environment active, run Sphinx from the docs folder:

sphinx-build -M html source _build

-M html is Sphinx’s “make mode”: it reads the .rst files under source/ and writes the finished site to _build/html. ABlog is a Sphinx extension, so the same command also generates the blog archive, tag and category pages (see Project Structure and Configuration and Posts).

The build ends by copying the separately built Trade Engine documentation into _build/html/projects/trade_engine (see Projects). If you do not have that project you will see the warning trade_engine docs not built. The rest of the site builds normally, so the warning can be ignored.

Preview locally#

Serve the generated folder with Python’s built-in web server and open http://localhost:8000:

python -m http.server 8000 -d _build/html

Press Ctrl+C to stop the server. Browsers cache pages from http.server, so use Ctrl+F5 after each rebuild.

Sphinx only rebuilds the pages that changed. After changing conf.py, the navigation or the sidebars, or if the site looks stale, delete the _build folder and build again.

Write a page#

A page is an .rst file under source/ (see reStructuredText). Every page must be listed in a toctree, otherwise Sphinx warns that the document is not included in any toctree. For a page that is deliberately left out of every toctree, put :orphan: on the first line of the file, followed by a blank line. Blog posts are the exception: ABlog collects them from the post directive, so they need no toctree entry (see Posts).

Publish#

When _build/html looks correct, copy it into the Pages repository, commit and push. The exact commands, and how to avoid leaving stale pages behind, are in GitHub Pages.

Publish with the script#

The site is published from a separate checkout of the GitHub Pages repository, samkhalilian.github.io, which sits next to docs (..\samkhalilian.github.io). The publish.ps1 script in the docs folder builds the site, copies the output to that checkout, then commits and pushes:

.\publish.ps1                          # build, commit and push
.\publish.ps1 -Message "Add XVA page"  # custom commit message
.\publish.ps1 -NoPush                  # build and commit, but do not push

This is the complete script:

publish.ps1#
<#
.SYNOPSIS
    Build the Sphinx docs and publish them to the samkhalilian.github.io repository.

.EXAMPLE
    .\publish.ps1
    .\publish.ps1 -Message "Add XVA formula"
    .\publish.ps1 -NoPush
#>
[CmdletBinding()]
param(
    [string]$Message = "Publish refreshed docs site",
    [switch]$NoPush
)

$ErrorActionPreference = 'Stop'

$docs = $PSScriptRoot
$pages = Join-Path (Split-Path $docs -Parent) 'samkhalilian.github.io'
$sphinx = Join-Path $docs '.venv\Scripts\sphinx-build.exe'

if (-not (Test-Path $sphinx)) { throw "Missing $sphinx. Create the venv as described in README.rst." }
if (-not (Test-Path (Join-Path $pages '.git'))) { throw "Pages repo not found at $pages" }

Write-Host "Building docs..." -ForegroundColor Cyan
Push-Location $docs
try {
    & $sphinx -M html source _build
    if ($LASTEXITCODE -ne 0) { throw "Sphinx build failed (exit $LASTEXITCODE)" }
}
finally { Pop-Location }

Write-Host "Copying to $pages ..." -ForegroundColor Cyan
robocopy (Join-Path $docs '_build\html') $pages /E /XD .git /XF .buildinfo.bak /NFL /NDL /NJH /NJS /NP
# robocopy exit codes 0-7 are success
if ($LASTEXITCODE -ge 8) { throw "robocopy failed (exit $LASTEXITCODE)" }
$global:LASTEXITCODE = 0

Push-Location $pages
try {
    git add -A
    if (-not (git status --porcelain)) {
        Write-Host "No changes to publish." -ForegroundColor Yellow
        return
    }
    git commit -m $Message -m "Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>"
    if ($LASTEXITCODE -ne 0) { throw "git commit failed" }

    if ($NoPush) {
        Write-Host "Committed locally; skipped push (-NoPush)." -ForegroundColor Yellow
    }
    else {
        git push origin main
        if ($LASTEXITCODE -ne 0) { throw "git push failed" }
        Write-Host "Published: https://samkhalilian.github.io/" -ForegroundColor Green
    }
}
finally { Pop-Location }

The site is then live at https://samkhalilian.github.io/ within a minute or two.

Publish by hand#

To see each step, run the same commands yourself with the virtual environment active:

.venv\Scripts\Activate.ps1
sphinx-build -M html source _build
robocopy _build\html ..\samkhalilian.github.io /E /XD .git
cd ..\samkhalilian.github.io
git add -A
git commit -m "Publish refreshed docs site"
git push origin main
cd ..\docs

robocopy exit codes 0 to 7 mean success.