migrate

Convert Quarto sources and settings for Fastship

ship-migrate-quarto converts a Quarto site or nbdev project in place. Read the migration instructions below before running it.

Python callers use migrate_quarto(path) and receive the source-to-destination rename mapping. Pass extra_moves for additional files to rename.

Source conversion

Renaming a document also requires updating references to it. A page beside guide.qmd refers to it differently from a page in a subdirectory: guide.qmd versus ../guide.qmd. Migration calculates replacement paths from the documentation root and from each source file’s directory.

The current implementation performs text replacement rather than parsing links. Matching text outside link destinations can also change.

The Markdown copy of each page also moves. Quarto published page.html.md beside page.html; Fastship publishes page.md. Hand-written indexes such as llms.txt often link these copies, so migration rewrites .html.md targets to .md in Markdown sources and .txt files.

source

def repl_html_md_links(
    txt
):

Rewrite Quarto .html.md Markdown-copy targets to Fastship's .md

source

def repl_qmd_links(
    txt, moves, src, root
):

Rewrite relative and root-relative .qmd link targets to .md

Quarto lets authors mark a block of content for a particular output format—for example, HTML for the website or Markdown for a README. Migration replaces Quarto's .content-visible divs with mdhtml.fill mustache sections ({{#html}}, {{#md}}, {{^html}}, {{^md}}), keeping any remaining attributes as an inner div and preserving Markdown rendering inside both formats.

Migration also moves Quarto callout title="..." attributes onto a leading ## Title heading inside the callout when no heading is present.

source

repl_callout_titles

def repl_callout_titles(
    s
):

Move Quarto callout title="..." attributes into a leading ## Title heading inside the callout

source

repl_content_visible

def repl_content_visible(
    s
):

Convert Quarto ::: {.content-visible when-format=... / unless-format=...} to {{#html}} / {{#md}} / {{^html}} / {{^md}}

Fastship reads Quarto's frontmatter keys as they are, such as toc-depth, page-layout and categories, so migration leaves them alone. It changes one thing: Fastship shows a page's description under its title, where Quarto shows subtitle, so a subtitle, or a legacy summary, becomes the description. A page that already has a description keeps it, and its subtitle is reported as unsupported.

source

repl_frontmatter_keys

def repl_frontmatter_keys(
    s
):

Move Quarto's subtitle, or a legacy summary, to the description Fastship shows, unless the page has one

Documentation conversion must not rewrite executable notebook code or discard saved outputs. migrate_md_file applies the text processors to a Markdown, QMD, or text file in place, while convert_quarto_nb converts only raw frontmatter and Markdown cells.

source

migrate_md_file

def migrate_md_file(
    path, procs
):

Apply procs to a Markdown, QMD or text file in-place

read_quarto_config reads a YAML mapping and rejects scalars and lists. quarto_config copies basic website and HTML settings (title, description, site-url, favicon, open-graph, twitter-card, image, toc, toc-depth, output) and enables math = "dollars".

source

quarto_config

def quarto_config(
    qy, root:str='.', overrides=None
):

Convert basic Quarto website and HTML keys (title, description, site-url, favicon, open-graph, twitter-card, image, toc, output) to Fastship settings.

source

read_quarto_config

def read_quarto_config(
    path
):

Read a Quarto YAML mapping; missing or empty files give empty settings

source

parse_quarto_config

def parse_quarto_config(
    text:str, path:str='_quarto.yml'
):

Parse Quarto YAML text into a mapping; empty text gives empty settings

Applying the migration

source

migrate_quarto_source

def migrate_quarto_source(
    path, procs
):

Convert one Markdown/QMD document or notebook in place using procs.

source

convert_quarto_nb

def convert_quarto_nb(
    nb, procs, path:str='notebook.ipynb'
):

Convert a leading raw YAML frontmatter cell to Markdown and apply procs to Markdown cells in place.

migrate_quarto_source applies the text processors to a Markdown/QMD file or notebook Markdown cells, converting any leading raw frontmatter cell in a notebook to Markdown without touching code cells or saved outputs.

source

migrate_pyproject

def migrate_pyproject(
    proj_root:pathlib._local.Path, extra_cfg:dict=None, write_table:bool=True
)->bool:

Migrate [project.entry-points.nbdev] -> fastship (preserving custom providers) and [tool.nbdev] -> [tool.fastship]

For a standalone Quarto site, migration writes ship.toml unless the site already has Fastship settings. For nbdev, it merges basic Quarto and nbdev.yml site settings into [tool.fastship] in pyproject.toml.

extra_moves maps source Paths to destination Paths, using filesystem paths just like the returned rename map. These pairs join document renames for conflict checks, link updates, and renaming. Extra files outside the document formats are not read or converted.

source

migrate_quarto

def migrate_quarto(
    path, # Source directory or project root
    extra_moves=None, # Additional source-to-destination Path pairs
):

Convert Quarto or nbdev sources and basic settings in place, returning the source rename mapping.

The notebook below contains a saved result and code that must not execute during migration.

migration_tmp = tempfile.TemporaryDirectory()
root = Path(migration_tmp.name)/'docs'
root.mkdir()
(root/'_quarto.yml').write_text('''project:
  output-dir: ../_site
website:
  title: Demo
''')
(root/'guide.qmd').write_text('''---
subtitle: A guide
---

[Notebook](index.ipynb)

::: {.content-visible when-format="html"}
Web note
:::

::: {.callout-note title="Heads up"}
Check links after migrating.
:::
''')
code = mk_cell('raise RuntimeError("do not run")')
code.outputs = [dict(output_type='stream', name='stdout', text='Saved result\n')]
book = new_nb([mk_cell('See [Guide](guide.qmd) and [API](api.html.md)', 'markdown'), code])
write_nb(book, root/'index.ipynb')

Migration renames the static .qmd guide, moves subtitle to description, converts .content-visible and callout title=, and writes basic settings to ship.toml:

moves = migrate_quarto(root)
test_eq(moves, {root/'guide.qmd': root/'guide.md'})
guide = (root/'guide.md').read_text()
test_eq(frontmatter(guide)[0]['description'], 'A guide')
assert '{{#html}}\nWeb note\n{{/html}}' in guide
assert '## Heads up\n\nCheck links after migrating.' in guide

Notebook Markdown links change, while executable code and saved outputs remain intact.

migrated = read_nb(root/'index.ipynb')
test_eq(migrated.cells[0].source, 'See [Guide](guide.md) and [API](api.md)')
test_eq(migrated.cells[1].source, code.source)
test_eq(migrated.cells[1].outputs, code.outputs)

Quarto notebooks usually keep frontmatter in a raw first cell. Migration converts it to Markdown, the only form Fastship reads, and converts its keys like any other frontmatter.

post = new_nb([mk_cell('---\ntitle: Post\n---', 'raw'), mk_cell('Text.', 'markdown')])
with capture_output() as fm_report: assert convert_quarto_nb(post, [repl_frontmatter_keys], 'post.ipynb')
test_eq(post.cells[0].cell_type, 'markdown')
test_eq(frontmatter(post.cells[0].source)[0]['title'], 'Post')
assert 'converted raw frontmatter cell' in fm_report.stdout

A second migration has no files to rename and preserves the existing Fastship configuration.

before = (root/'ship.toml').read_text()
test_eq(migrate_quarto(root), {})
test_eq((root/'ship.toml').read_text(), before)
test_eq(tomllib.loads(before)['title'], 'Demo')

The converted configuration preserves the output directory relative to the project.

cfg = tomllib.loads(before)
test_eq(cfg['output'], '../_site')
migration_tmp.cleanup()

Callers can supply additional renames. Their contents are not converted, but document links follow the new filenames. Existing destinations are rejected before source changes.

with tempfile.TemporaryDirectory() as d:
    root = Path(d)
    manifest = root/'pyproject.toml'
    manifest.write_text('[tool.fastship]\ntitle="Keep me"\n')
    old,new = root/'old.bin',root/'new.bin'
    old.write_bytes(b'opaque payload')
    new.touch()
    (root/'guide.qmd').write_text('[Asset](old.bin)')
    with expect_fail(FileExistsError): migrate_quarto(root, extra_moves={old:new})
    test_eq((root/'guide.qmd').read_text(), '[Asset](old.bin)')
    new.unlink()
    moves = migrate_quarto(root, extra_moves={old:new})
    test_eq(moves[old], new)
    test_eq(new.read_bytes(), b'opaque payload')
    test_eq((root/'guide.md').read_text(), '[Asset](new.bin)')
    assert not (root/'ship.toml').exists()
    test_eq(manifest.read_toml()['tool']['fastship']['title'], 'Keep me')

A destination collision must stop migration before an existing file can be overwritten.

with tempfile.TemporaryDirectory() as d:
    p = Path(d)
    (p/'_quarto.yml').touch()
    (p/'page.qmd').touch()
    (p/'page.md').touch()
    with expect_fail(FileExistsError, contains='page.md'): migrate_quarto(p)

Malformed YAML must fail before any source rename. This differs from a valid YAML value of the wrong type.

with tempfile.TemporaryDirectory() as d:
    p = Path(d)
    (p/'guide.qmd').write_text('# Guide')
    (p/'_quarto.yml').write_text('website: [invalid')
    with expect_fail(yaml.YAMLError): migrate_quarto(p)
    assert (p/'guide.qmd').exists() and not (p/'guide.md').exists()

Valid YAML is not always valid configuration. Lists, booleans, and numbers must fail before source changes, whether they came from Quarto or an existing Fastship configuration.

for invalid in ('[]', 'false', '0'):
    with expect_fail(ValueError): parse_ship_cfg(invalid)
    with expect_fail(ValueError, contains='mapping'): parse_quarto_config(invalid)
with tempfile.TemporaryDirectory() as d:
    nbs = Path(d)
    (nbs/'guide.qmd').write_text('# Guide')
    (nbs/'_quarto.yml').write_text('website:\n  title: Demo\n')
    (nbs/'ship.toml').write_text('[]')
    with expect_fail(ValueError): migrate_quarto(nbs)
    assert (nbs/'guide.qmd').exists() and (nbs/'_quarto.yml').exists()
    (nbs/'ship.toml').unlink()
    (nbs/'_quarto.yml').write_text('[]')
    with expect_fail(ValueError, contains='mapping'): migrate_quarto(nbs)
    assert (nbs/'guide.qmd').exists() and not (nbs/'guide.md').exists()

Command line

source

ship_migrate_quarto

def ship_migrate_quarto(
    path:str='.', # Source directory or project root containing `_quarto.yml` or `nbdev.yml`
):

Convert a Quarto or Quarto-nbdev project to Fastship in place; commit or back up first

ship-migrate-quarto [path] calls ship_migrate_quarto; the source path defaults to the current directory. See ship-migrate-quarto --help for usage and the instructions below for the migration workflow.

Navbars, sidebars, drafts, freeze, CSS, bibliographies, profiles, and render scripts are left for manual review.

Migration guide

ship-migrate-quarto handles the mechanical Quarto and nbdev conversions in place (commit or back up first):

Review and update these manually after running ship-migrate-quarto: