Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 38 additions & 3 deletions conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,54 @@ def doc_all_dirs():
return tuple(tools)


# matches the files generated by write_tool_docs() below
TOOL_STUB_PATTERN = re.compile(r'^\.\. _[^:\n]+:\n\n\.\. include:: /[^\n]+\n*$')


def write_tool_docs():
"""
Creates a file for each tool with the ".. include::" directives to pull in
the original documentation.
"""
generated = set()
for k in doc_all_dirs():
label = ('.. _{name}:\n\n').format(name=k[0])
include = ('.. include:: /{path}\n\n').format(path=k[1])
os.makedirs(os.path.join('docs/tools', os.path.dirname(k[0])),
mode=0o755, exist_ok=True)
with write_file_if_changed('docs/tools/{}.rst'.format(k[0])) as outfile:
path = os.path.join('docs', 'tools', *k[0].split('/')) + '.rst'
generated.add(os.path.normpath(path))
os.makedirs(os.path.dirname(path), mode=0o755, exist_ok=True)
with write_file_if_changed(path) as outfile:
outfile.write(label)
outfile.write(include)
remove_stale_tool_docs(generated)


def remove_stale_tool_docs(generated):
"""
Removes tool docs generated by previous runs that no longer have a source
file, e.g. after switching to a branch where a tool doesn't exist. Without
this, the stale files linger in the gitignored docs/tools directory and
break the build when their "include" directives point at missing files.
Only files matching the generated stub format are removed, so hand-written
files placed in docs/tools are left alone.
"""
tools_dir = os.path.join('docs', 'tools')
for root, _, files in os.walk(tools_dir, topdown=False):
for f in files:
if not f.endswith('.rst'):
continue
path = os.path.normpath(os.path.join(root, f))
if path in generated:
continue
try:
with open(path) as infile:
contents = infile.read()
except IOError:
continue
if TOOL_STUB_PATTERN.match(contents):
os.remove(path)
if root != tools_dir and not os.listdir(root):
os.rmdir(root)


write_tool_docs()
Expand Down
1 change: 1 addition & 0 deletions docs/changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ Template for new versions:
## Fixes

## Misc Improvements
- Core: docs files generated by previous builds are now cleaned up automatically, so switching branches no longer breaks the build with missing-file errors

## Documentation

Expand Down
11 changes: 9 additions & 2 deletions docs/sphinx_extensions/dfhack/tool_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,10 +297,17 @@ def register_index(app, tag, title):
app.add_index_to_domain(tag, index_class)

def init_tag_indices(app):
os.makedirs(os.path.join(dfhack.util.DOCS_ROOT, 'tags'), mode=0o755, exist_ok=True)
tags_dir = os.path.join(dfhack.util.DOCS_ROOT, 'tags')
os.makedirs(tags_dir, mode=0o755, exist_ok=True)
tag_groups = get_tags()
group_files = set('by{group}.rst'.format(group=group) for group in tag_groups)
for f in os.listdir(tags_dir):
# remove tag group files generated by previous runs whose tag groups
# no longer exist, e.g. after switching branches
if f.startswith('by') and f.endswith('.rst') and f not in group_files:
os.remove(os.path.join(tags_dir, f))
for tag_group in tag_groups:
group_file_path = os.path.join(dfhack.util.DOCS_ROOT, 'tags', 'by{group}.rst'.format(group=tag_group))
group_file_path = os.path.join(tags_dir, 'by{group}.rst'.format(group=tag_group))
with dfhack.util.write_file_if_changed(group_file_path) as topidx:
for tag_tuple in tag_groups[tag_group]:
tag, desc = tag_tuple[0], tag_tuple[1]
Expand Down
Loading