diff --git a/ulo/fix-rdf-file.py b/ulo/fix-rdf-file.py new file mode 100755 index 0000000000000000000000000000000000000000..a6712460e9ac58b893fc56effd64c66a47a33715 --- /dev/null +++ b/ulo/fix-rdf-file.py @@ -0,0 +1,36 @@ +#! /usr/bin/env python3 + + +from urllib import parse +import re +import sys + + +def fix_quoted(s: str) -> str: + payload = s.strip('"') + fixed = parse.quote(payload) + return '"%s"' % fixed + + +def print_fixed_line(line: str): + # https://stackoverflow.com/questions/249791 + regex = r"(\"(?:[^\"]|\\.)*\")" + matches = re.findall(regex, line) + + for match in matches: + fixed = fix_quoted(match) + line = line.replace(match, fixed) + + print(line, end='') + + +def main(): + for line in sys.stdin: + print_fixed_line(line) + + +if __name__ == '__main__': + try: + main() + except (KeyboardInterrupt, SystemExit, BrokenPipeError): + pass