Files
carbon-lang/proposals/scripts/update_proposal_list.py
T
Jon Meow f2d48f1bd0 Move the jekyll sidebar to a python script for consistent generation. (#205)
One implication here is that both proposals/README.md and the website should use basically the same format for their proposal lists. Another subtle change is that the website sidebar was previously sorted by *filename*, and is now sorted by *title*, which seems better because it's something that readers can see.

Enumerating why files change:

- gen_sidebar.py is the centerpiece, with a couple helpful functions for re-use elsewhere
- Delete the old sidebar html includes
- Modify the Makefile to handle gen_sidebar.py reasonably well (let's be honest... I'm not great at Makefiles)
- Move proposal listing out to its own file (proposals.py) for re-use
- Add a few PYTHONPATH things + `__init__.py` files to get modules importing correctly (may be a better way at this, I've hit a wall though).
2020-12-04 08:26:25 -08:00

55 lines
1.8 KiB
Python
Executable File

#!/usr/bin/env python3
"""Updates the list of proposals in proposals/README.md."""
__copyright__ = """
Part of the Carbon Language project, under the Apache License v2.0 with LLVM
Exceptions. See /LICENSE for license information.
SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
"""
import io
import os
import re
import sys
# To support direct runs, ensure the pythonpath has the repo root.
_PYTHONPATH = os.path.realpath(os.path.join(os.path.dirname(__file__), "../.."))
if _PYTHONPATH not in sys.path:
sys.path.insert(0, _PYTHONPATH)
from proposals.scripts import proposals
if __name__ == "__main__":
proposals_path = proposals.get_path()
with io.StringIO() as out:
out.write("<!-- Generated by ./scripts/update_proposal_list.py -->\n\n")
results = out.getvalue()
for title, filename in proposals.get_list(proposals_path):
indent = ""
if filename.endswith("decision.md"):
indent = " "
out.write("%s- [%s](%s)\n" % (indent, title, filename))
toc = out.getvalue()
# Replace the README content if needed.
readme_path = os.path.join(proposals_path, "README.md")
with open(readme_path) as f:
old_content = f.read()
proposals_re = re.compile(
r"(.*<!-- proposals -->)(?:.*)(<!-- endproposals -->)",
re.DOTALL | re.MULTILINE,
)
if not proposals_re.match(old_content):
print(
"ERROR: proposals/README.md is missing the <!-- proposals --> ... "
"<!-- endproposals --> marker."
)
sys.exit(1)
new_content = proposals_re.sub(r"\1\n%s\n\2" % toc, old_content)
if old_content != new_content:
print("Updating proposals/README.md")
with open(readme_path, "w") as f:
f.write(new_content)