tools: Add multi-language support to blockreplace.py

Suggested-by: Rusty Russell <@rustyrussell>
This commit is contained in:
Christian Decker
2022-12-05 13:45:10 +01:00
committed by Rusty Russell
parent 744d111cea
commit a31575ca0b
2 changed files with 30 additions and 7 deletions

View File

@@ -6,15 +6,37 @@
# requiring a separate template. The markers are currently for # requiring a separate template. The markers are currently for
# reStructuredText only, but more can be added. # reStructuredText only, but more can be added.
from enum import Enum
import argparse import argparse
import os import os
import sys import sys
import textwrap import textwrap
def replace(filename, blockname, content): class Language(str, Enum):
start = f".. block_start {blockname}" md = 'md'
stop = f".. block_end {blockname}" rst = 'rst'
c = 'c'
comment_style = {
Language.md: (
"<!-- block_start {blockname} -->",
"<!-- block_end {blockname} -->",
),
Language.rst: (
".. block_start {blockname}",
".. block_end {blockname}",
),
Language.c: (
"/* block_start {blockname} */",
"/* block_end {blockname} */",
),
}
def replace(filename, blockname, language, content):
start, stop = comment_style[language]
tempfile = f"{filename}.tmp" tempfile = f"{filename}.tmp"
@@ -24,7 +46,7 @@ def replace(filename, blockname, content):
while lines != []: while lines != []:
l = lines.pop(0) l = lines.pop(0)
o.write(l) o.write(l)
if l.strip() == start: if l.strip() == start.format(blockname=blockname):
break break
o.write(content) o.write(content)
@@ -32,7 +54,7 @@ def replace(filename, blockname, content):
# Skip lines until we get the end marker # Skip lines until we get the end marker
while lines != []: while lines != []:
l = lines.pop(0) l = lines.pop(0)
if l.strip() == stop: if l.strip() == stop.format(blockname=blockname):
o.write(l) o.write(l)
break break
@@ -50,12 +72,13 @@ def main(args):
) )
parser.add_argument('filename') parser.add_argument('filename')
parser.add_argument('blockname') parser.add_argument('blockname')
parser.add_argument('--language', type=Language)
parser.add_argument('--indent', dest="indent", default="") parser.add_argument('--indent', dest="indent", default="")
args = parser.parse_args() args = parser.parse_args()
content = sys.stdin.read() content = sys.stdin.read()
content = textwrap.indent(content, args.indent) content = textwrap.indent(content, args.indent)
replace(args.filename, args.blockname, content) replace(args.filename, args.blockname, args.language, content)
if __name__ == "__main__": if __name__ == "__main__":

View File

@@ -200,5 +200,5 @@ doc/index.rst: $(MANPAGES:=.md)
find doc -maxdepth 1 -name '*\.[0-9]\.md' | \ find doc -maxdepth 1 -name '*\.[0-9]\.md' | \
cut -b 5- | LC_ALL=C sort | \ cut -b 5- | LC_ALL=C sort | \
sed 's/\(.*\)\.\(.*\).*\.md/\1 <\1.\2.md>/' | \ sed 's/\(.*\)\.\(.*\).*\.md/\1 <\1.\2.md>/' | \
python3 devtools/blockreplace.py doc/index.rst manpages --indent " " \ python3 devtools/blockreplace.py doc/index.rst manpages --language=rst --indent " " \
) )