summaryrefslogtreecommitdiff
path: root/docs/conf.py
blob: 86ddcf67261fb7a297e22a6d842421e6ae2c31aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env python3
# Configuration file for the Sphinx documentation builder.
#
# This file only contains a selection of the most common options. For a full
# list see the documentation:
# https://www.sphinx-doc.org/en/master/usage/configuration.html

import re
from pathlib import Path

# -- Path setup --------------------------------------------------------------


# -- Project information -----------------------------------------------------

project = 'TinyUSB'
copyright = '2025, Ha Thach'
author = 'Ha Thach'


# -- General configuration ---------------------------------------------------

extensions = [
    'sphinx.ext.autodoc',
    'sphinx.ext.intersphinx',
    'sphinx.ext.todo',
    'sphinx_autodoc_typehints',
]

templates_path = ['_templates']

exclude_patterns = ['_build']


# -- Options for HTML output -------------------------------------------------

html_theme = 'furo'
html_title = 'TinyUSB'
html_logo = 'assets/logo.svg'
html_favicon = 'assets/logo.svg'
html_theme_options = {
    'sidebar_hide_name': True,
}
html_static_path = ['_static']
html_css_files = ['custom.css']

todo_include_todos = True

# pre-process path in README.rst
def preprocess_readme():
    """Modify figure paths in README.rst for Sphinx builds."""
    src = Path(__file__).parent.parent / "README.rst"
    tgt = Path(__file__).parent.parent / "README_processed.rst"
    if src.exists():
        content = src.read_text(encoding='utf-8')
        # if the matching is inside a table, keep the table cell width by adding the same number of spaces in the end of the line
        # match pattern: | ... `... <docs/...>`_ ... |
        # change into:   | ... `... <...>`_ ...      |
        def _rewrite_table_line(line):
            if not (line.startswith('|') and line.rstrip().endswith('|')):
                return line

            rewritten = re.sub(r"<docs/([^>]+)>", r"<\1>", line)
            delta = len(line) - len(rewritten) - 1  # -1 for rst->html

            if delta > 0:
                last_pipe = rewritten.rfind('|')
                if last_pipe >= 0:
                    rewritten = rewritten[:last_pipe] + (' ' * delta) + rewritten[last_pipe:]

            return rewritten

        content = ''.join(_rewrite_table_line(line) for line in content.splitlines(keepends=True))

        content = re.sub(r"docs/", r"", content)
        content = re.sub(r"\.rst\b", r".html", content)
        if not content.endswith("\n"):
            content += "\n"
        tgt.write_text(content, encoding='utf-8')

preprocess_readme()