54 lines
1.3 KiB
Python
54 lines
1.3 KiB
Python
#!/usr/bin/env python3
|
|
|
|
# parse CLI arguments
|
|
import argparse
|
|
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("file", help="File to open")
|
|
parser.add_argument("-i", "--internal", help="Output internal version", action="store_true")
|
|
|
|
args = parser.parse_args()
|
|
|
|
# read file
|
|
from pathlib import Path
|
|
txt = Path(args.file).read_text()
|
|
|
|
# redaction syntax
|
|
import re
|
|
inlinepat = re.compile(r"\(\(\((([^>]*?):::)?(.*?)\)\)\)", re.MULTILINE|re.DOTALL)
|
|
tokenpat = re.compile(r"\[redact(:::(.*?))?\](.*?)\[\/redact\]", re.MULTILINE|re.DOTALL)
|
|
|
|
# match handling
|
|
def subredact(match):
|
|
_, reason, content = match.groups()
|
|
|
|
if reason:
|
|
reason = reason.strip()
|
|
else:
|
|
reason = ""
|
|
|
|
if args.internal:
|
|
retval = content.lstrip(" ").rstrip()
|
|
# retval = re.sub(r"(\s*[+\-\*]\s+)?(.*)", r"\1*\2*", retval, re.MULTILINE)
|
|
|
|
retval = f"*{retval}*"
|
|
retval = re.sub(r"\n(\s*[+\-\*]\s+)?", r"*\n\1*", retval, re.MULTILINE)
|
|
|
|
else:
|
|
retval = f"{reason} \\textcolor{{violet}}{{\\faIcon{{lock}}[intern]}}"
|
|
|
|
# DEBUG output
|
|
# print("GROUPS", match.groups())
|
|
# print("REASON", reason.strip())
|
|
# print("CONTENT", content.strip())
|
|
# print("RET", retval)
|
|
# print("~~~~~~")
|
|
|
|
return retval
|
|
|
|
# matching
|
|
txt = tokenpat.sub(subredact, txt)
|
|
txt = inlinepat.sub(subredact, txt)
|
|
|
|
# pipe to stdout
|
|
print(txt)
|