Skip to content

Commit ea7dfce

Browse files
committed
Drop HTML comments from every source file
Comment stripping and the `<!-- keep: ... -->` marker applied only to template.md. They now apply to every included file too, so an authoring note in any source stays out of the generated README unless a keep marker precedes it. No source has comments today, so README.md is unchanged.
1 parent 1b191f1 commit ea7dfce

2 files changed

Lines changed: 23 additions & 5 deletions

File tree

‎CONTRIBUTING.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ directly. Edit the source files and rebuild:
1414
lists the files to append with `<!-- include: <path> -->` directives, in order.
1515
To add a source file, place an `include` directive where it belongs.
1616

17-
HTML comments in `template.md` are dropped from the generated page. A
18-
`<!-- keep: ... -->` line marks the comment right after it to be emitted
19-
verbatim (this is how the generated-file banner reaches the top of `README.md`).
17+
HTML comments are dropped from the generated page. A `<!-- keep: ... -->` line
18+
marks the comment right after it to be emitted verbatim (this is how the
19+
generated-file banner reaches the top of `README.md`).
2020

2121
Cross-file Markdown links are written file-relative (e.g. `pairing.md#pairing`
2222
from a top-level file, `../../messaging.md#...` from a role file) and rewritten

‎tools/build-readme.py‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
88
template.md is the assembly root: the head of the document followed by
99
`<!-- include: <path> -->` directives naming the source files to append, in
10-
order. HTML comments in template.md are dropped from the generated page. A
10+
order. HTML comments in any source file are dropped from the generated page. A
1111
`<!-- keep: ... -->` line marks the comment right after it to be emitted
1212
verbatim, which is how the generated-file banner reaches the top of README.md.
1313
"""
@@ -87,13 +87,31 @@ def add_inline(chunk):
8787
return pieces
8888

8989

90+
def strip_comments(text):
91+
"""Drop HTML comments from a source file, keeping only a comment that a
92+
`<!-- keep: ... -->` line immediately precedes."""
93+
out, pos, keep_next = [], 0, False
94+
for m in COMMENT_RE.finditer(text):
95+
out.append(text[pos : m.start()])
96+
pos = m.end()
97+
inner = m.group(0)[4:-3].strip()
98+
if inner.startswith("keep:"):
99+
keep_next = True
100+
elif keep_next:
101+
out.append(m.group(0)) # emit this comment verbatim
102+
keep_next = False
103+
# otherwise the comment is dropped
104+
out.append(text[pos:])
105+
return "".join(out)
106+
107+
90108
def generate(reader=_fs_read):
91109
rendered = [] # (repo-relative path or "", stripped text)
92110
for kind, val in pieces_from_template(reader):
93111
if kind == "inline":
94112
rendered.append(("", val.strip("\n")))
95113
else:
96-
rendered.append((val, reader(val).strip("\n")))
114+
rendered.append((val, strip_comments(reader(val)).strip("\n")))
97115

98116
# Every heading's page anchor. Two headings sharing a slug would give GitHub
99117
# ambiguous anchors, so require them disambiguated at the source instead.

0 commit comments

Comments
 (0)