---
title: pages
description: Read source documents, metadata, and output destinations
---
`fastship.pages` reads a directory of markdown files and notebooks into [`Page`](pages.md#page) objects. Each page holds its markdown, frontmatter settings, and HTML output path. For example, `api/01_config.ipynb` becomes a page with destination `api/config.html`.

Create a [`Page`](pages.md#page) directly when you already have markdown, or use [`scan`](pages.md#scan) to read all documents under a directory:

```python
page = Page('api/01_config.md', '# Configuration\n\nProject settings.')
page.dest  # 'api/config.html'

pages = scan('ship')
[(p.rel, p.dest) for p in pages]  # source and output paths
```

Scanning converts notebooks to markdown using their saved outputs; it does not run their cells unless you supply a processor that does so. Markdown `{python}` blocks, however, execute during scanning. Hidden files and directories, whose names begin with `.` or `_`, are skipped, as are paths matching the `exclude` globs that [`source_files`](pages.md#source_files) takes.

This module reads sources and determines destinations; it does not render or write the site. `fastship.site` uses the collected pages to build navigation, resolve cross-page links, and write the output.

<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

## Frontmatter

Frontmatter is a YAML block at the start of a document, enclosed by `---` lines. It supplies settings such as a page title, sort order, and old URLs to redirect.

`frontmatter` returns the parsed settings and the remaining document body. YAML numbers and lists retain their types.

This tutorial declares a numeric sort order and one old URL in `aliases`.

```python
text = """---
title: End-To-End Walkthrough
order: 1
aliases:
  - /tutorial.html
---

Build a package from scratch, starting with a single notebook.
"""
meta,body = frontmatter(text)
meta
```

```output
{'title': 'End-To-End Walkthrough', 'order': 1, 'aliases': ['/tutorial.html']}
```

Without frontmatter, the settings are empty and the document body is retained.

```python
bare,barebody = frontmatter('# Hand written\n\nNo frontmatter here.\n')
test_eq(bare, {})
barebody
```

```output
'# Hand written\n\nNo frontmatter here.\n'
```

## Pages

A page’s output keeps its source directory but drops a leading numeric prefix from the filename. For example, `api/01_config.md` produces `api/config.html`. This lets authors order source files without including those numbers in public URLs.

Pass `nm` to choose a different output name without changing the source filename.

[source](https://github.com/AnswerDotAI/fastship/blob/main/fastship/pages.py#L49)

### Page

```python
def Page(
    rel:str, # Path of the document relative to the source root
    text:str, # Its Markdown, frontmatter included
    meta:dict=None, # Frontmatter; read from `text`, through `snake_keys`, when not given
    nm:str=None, # Output name without extension; derived from `output-file` or `rel` when not given
):
```

*A source document, its metadata, and its output destination*

```python
p = Page('tutorials/01_tutorial.md', text)
test_eq(p.dest, 'tutorials/tutorial.html')
test_eq(p.title, 'End-To-End Walkthrough')
p
```

```output
tutorials/01_tutorial.md -> tutorials/tutorial.html
```

Without a frontmatter title, a page takes its title from a leading level-1 heading, as Quarto does. HTML comments before the heading, such as a generated-file notice, are skipped. A page with neither uses its output name. Pass `nm="configuration"` to produce `api/configuration.html` from `api/01_config.md`.

```python
q = Page('explanations/why_nbdev.md', '<!-- Generated from notes.json -->\n# Why nbdev\n\nSome prose.\n')
test_eq(q.title, 'Why nbdev')
cfg = Page('api/01_config.md', 'Settings live here.\n', nm='configuration')
test_eq((cfg.dest, cfg.title), ('api/configuration.html', 'configuration'))
q
```

```output
explanations/why_nbdev.md -> explanations/why_nbdev.html
```

`title` applies [`dashes`](core.md#dashes) to a frontmatter title or heading. Navigation, listings and feeds then show the same dashes as the page itself.

```python
llms = Page('posts/llmstxt.md', '---\ntitle: /llms.txt---a proposal\n---\n')
test_eq(llms.title, '/llms.txt—a proposal')
```

## Scanning a source tree

A site's source files are every file below its root except hidden paths, `ship.toml`, and paths matching the site's `exclude` globs. A path is hidden when a component begins with `.` or `_`, which covers `.git`, `.ipynb_checkpoints` and `_docs`. Each source file becomes either a page or an asset: [`scan`](pages.md#scan) reads the Markdown documents and notebooks, and [`Site.build`](site.md#site.build) copies the rest, such as images, stylesheets and downloads, without rendering them and at the same relative path.

[`source_files`](pages.md#source_files) lists them. It does not enter hidden or excluded directories.

[source](https://github.com/AnswerDotAI/fastship/blob/main/fastship/pages.py#L84)

### source\_files

```python
def source_files(
    root:str, # Directory of source documents
    exclude=None, # Glob patterns, relative to `root`, of paths to leave out
):
```

*Paths relative to `root` of the files a site publishes: not hidden, not `ship.toml`, and not matching `exclude`*

[source](https://github.com/AnswerDotAI/fastship/blob/main/fastship/pages.py#L80)

### hidden

```python
def hidden(
    rel
):
```

*Is any part of `rel` a dotfile or underscore-prefixed?*

This source tree holds three Markdown documents, a notebook and an image, plus `ship.toml` and a `_docs` directory left over from an earlier build, which are not source files.

```python
_src_td = tempfile.TemporaryDirectory()
src = Path(_src_td.name)
mkdoc(src/'getting_started.md', {'title':'Getting Started'})
mkdoc(src/'api/01_config.md', {'title':'config', 'order':1})
mkdoc(src/'tutorials/tutorial.md', {'title':'End-To-End Walkthrough', 'aliases':['/tutorial.html']})
nb = dict2nb(cells=[mk_cell('# Notebook page', 'markdown')], metadata={}, nbformat=4, nbformat_minor=5)
write_nb(nb, src/'api/02_notebook.ipynb')
mkdoc(src/'_docs/stale.md', {'title':'Left over from an earlier build'})
(src/'ship.toml').write_text('title = "fastship"\n')
(src/'images').mkdir()
(src/'images/logo.svg').write_text('<svg/>')
files = source_files(src)
test_eq(files, ['api/01_config.md', 'api/02_notebook.ipynb', 'getting_started.md', 'images/logo.svg', 'tutorials/tutorial.md'])
files
```

```output
['api/01_config.md', 'api/02_notebook.ipynb', 'getting_started.md', 'images/logo.svg', 'tutorials/tutorial.md']
```

`exclude` patterns are matched against paths relative to the root. A pattern that matches a directory leaves out everything under it, so `tutorials` drops the walkthrough. In these patterns `*` also matches `/`, so `*.svg` matches SVG files at any depth.

```python
test_eq(source_files(src, ['tutorials', '*.svg']), ['api/01_config.md', 'api/02_notebook.ipynb', 'getting_started.md'])
```

[`scan`](pages.md#scan) reads the Markdown documents and notebooks among the source files. Markdown `{python}` blocks execute during reading. Notebook conversion uses saved content and outputs. A caller can supply `nbproc` to process notebooks first, such as Fastship’s generated API documentation and directives.

A notebook's frontmatter is a leading Markdown cell that starts with `---`.

[source](https://github.com/AnswerDotAI/fastship/blob/main/fastship/pages.py#L207)

### qmd2md

```python
def qmd2md(
    path:str, # `.qmd` document to read and optionally execute
    execute:bool=True, # Execute `{python}` blocks unless disabled by frontmatter or directives?
):
```

*Read a `.qmd` file, executing `{python}` blocks with directive support*

[source](https://github.com/AnswerDotAI/fastship/blob/main/fastship/pages.py#L142)

### ipynb2md

```python
def ipynb2md(
    path:str, # Notebook to convert
    nbproc=None, # Optional callable that processes the notebook in place
    html1st:bool=True, # Prefer HTML outputs over Markdown?
):
```

*Convert a notebook to document Markdown with code-cell publishing directives*

[source](https://github.com/AnswerDotAI/fastship/blob/main/fastship/pages.py#L128)

### nb2md

```python
def nb2md(
    nb, # In-memory `Notebook` object (already read and optionally processed)
    name:str='dialog', # Dialog name
    html1st:bool=True, # Prefer HTML outputs over Markdown?
):
```

*Convert an in-memory `Notebook` directly to document Markdown without JSON serialization*

[source](https://github.com/AnswerDotAI/fastship/blob/main/fastship/pages.py#L97)

### DocProc

```python
def DocProc(
    local_libs=None, # Package names whose symbol links emit root-relative `/...` URLs for `Linker`
    strip_libs=None, # Package names stripped for bare symbol lookups (defaults to `local_libs`)
    extra=None, # Optional live `{mod: {sym: (doc_path, py_path)}}` map for hand-written `.py` modules
    incl_libs=None, # Optional package names restricting entry-point loading
):
```

*Picklable notebook processor for site and README builds: runs [`FilterDefaults`](processors.md#filterdefaults) with local symbol links.*

[source](https://github.com/AnswerDotAI/fastship/blob/main/fastship/pages.py#L276)

### scan

```python
def scan(
    root:str, # Directory of source documents
    nbproc=None, # Optional callable applied to notebooks before conversion
    exclude=None, # Glob patterns of paths to leave out; see `source_files`
    mdproc=None, # Optional callable `(text, rel) -> text` applied to every page's Markdown
    execute:bool=True, # Execute `{python}` blocks in `.qmd` files?
    n_workers:int=1, # Parallel notebook preprocessing workers (>1 uses `forkserver`/`spawn` for `.ipynb` files)
):
```

*Every Markdown, Quarto Markdown, and notebook document under `root`, as [`Page`](pages.md#page)s*

[source](https://github.com/AnswerDotAI/fastship/blob/main/fastship/pages.py#L259)

### discover\_pages

```python
def discover_pages(
    root:str, # Directory of source documents
    exclude=None, # Glob patterns of paths to leave out; see `source_files`
):
```

*Discover page metadata and destinations under `root` without rendering or executing notebooks, `.qmd`, or render scripts*

[source](https://github.com/AnswerDotAI/fastship/blob/main/fastship/pages.py#L229)

### script\_pages

```python
def script_pages(
    root:str, # Source directory
    exclude=None, # Glob patterns of paths to leave out
    mdproc=None, # Optional callable `(text, rel) -> text` applied to generated Markdown pages
    execute:bool=True, # Execute `{python}` blocks in `*.qmd.py` output?
):
```

*Run double-suffix render scripts under `root`: `*.md.py`/`*.qmd.py` as [`Page`](pages.md#page)s, others as [`GeneratedFile`](pages.md#generatedfile)s*

[source](https://github.com/AnswerDotAI/fastship/blob/main/fastship/pages.py#L219)

### render\_script

```python
def render_script(
    path
):
```

*Execute a double-suffix render script (`*.md.py`, `*.qmd.py`, `*.svg.py`) and return its output with optional YAML frontmatter*

```python
seen = []
def mark_nb(nb): seen.append(len(nb.cells))
pages = scan(src, nbproc=mark_nb)
test_eq(seen, [1])
pages
```

```output
[api/01_config.md -> api/config.html, api/02_notebook.ipynb -> api/notebook.html, getting_started.md -> getting_started.html, tutorials/tutorial.md -> tutorials/tutorial.html]
```

Each scanned page already has the destination and title needed for navigation.

```python
{o.dest: o.title for o in pages}
```

```output
{'api/config.html': 'config',
 'api/notebook.html': 'Notebook page',
 'getting_started.html': 'Getting Started',
 'tutorials/tutorial.html': 'End-To-End Walkthrough'}
```

The same pages supply redirect destinations for their declared aliases.

```python
{a: o.dest for o in pages for a in o.aliases}
```

```output
{'/tutorial.html': 'tutorials/tutorial.html'}
```

Quarto Markdown (`.qmd`) sources execute `{python}` fences during scanning, honoring ` #|  ` directives (`hide`, `eval`, `echo`, `output: asis|false`, `code-fold`) and frontmatter `execute: enabled: false`. During execution, the document's directory is temporarily placed on `sys.path` so `{python}` blocks (and render scripts) can import sibling `_*.py` helper modules, which [`hidden`](pages.md#hidden) excludes from the built site. Plain `.md` sources never execute `{python}` fences at build time.

````python
(src/'api/_helper.py').write_text("def fmt(k, v): return f'| {k} | {v} |'\n")
mkdoc(src/'api/01_config.qmd', {'title':'config', 'order':1}, body=r'''
```{python}
#| echo: false
from _helper import fmt
name,score = 'Ada',42
```

```{python}
#| echo: false
#| output: asis
print(f"| Name | Score |\n| --- | ---: |\n{fmt(name, score)}")
```

```python
raise RuntimeError('This is a displayed example')
```

Literal {{name}} remains prose.
''')
mkdoc(src/'api/03_static.md', {'title':'static'}, body='```{python}\nraise RuntimeError("never runs in .md")\n```\n')
computed = next(o for o in scan(src) if o.rel=='api/01_config.qmd')
static = next(o for o in scan(src) if o.rel=='api/03_static.md')
test_eq(computed.meta, {'title':'config', 'order':1})
test_eq(static.order, 3)
assert '| Ada | 42 |' in computed.text
assert '```{python}' not in computed.text
assert '```python' in computed.text and 'raise RuntimeError' in computed.text
assert '{{name}}' in computed.text
assert 'never runs in .md' in static.text
print(computed.text)
````

````output
---
title: config
order: 1
---


| Name | Score |
| --- | ---: |
| Ada | 42 |

```python
raise RuntimeError('This is a displayed example')
```

Literal {{name}} remains prose.
````

## Generated files

A build can include files produced by another tool. For example, a render script such as `images/circles.svg.py` can produce SVG output and pass it to the builder as a [`GeneratedFile`](pages.md#generatedfile).

The object records the source path, output path, and text to write. The site builder excludes the source script from copied assets. It rejects generated destinations that collide with pages, copied assets, or other generated files.

[source](https://github.com/AnswerDotAI/fastship/blob/main/fastship/pages.py#L301)

### GeneratedFile

```python
def GeneratedFile(
    src:str, # Path of the source it was generated from, relative to the source root
    dest:str, # Path to write, relative to the site root
    content:str, # File contents as text
):
```

*A file produced in memory, written verbatim to the output*

This generated file writes `images/circles.svg` and excludes `images/circles.svg.py` from copied assets.

```python
gen = GeneratedFile('images/circles.svg.py', 'images/circles.svg', '<svg/>\n')
test_eq(gen.src, 'images/circles.svg.py')
test_eq(gen.dest, 'images/circles.svg')
gen
```

```output
images/circles.svg.py -> images/circles.svg
```
