about summary refs log tree commit diff homepage
path: root/atom-encode-ids
blob: 79ac5099cdac6eeb4b869b2671e4c0b393042b34 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/usr/bin/env python3
import re
from urllib.parse import quote
from sys import stdin, stdout

ID_LINE = re.compile(r'(?P<open> +<id>)(?P<url>.+)(?P<close></id>)\n')


def encode_id(line: str) -> str:
    match = re.fullmatch(ID_LINE, line)
    if match is None: return line
    return '{open}{iri}{close}\n'.format(iri=quote(match.group('url'), ':/#'),
                                         **match.groupdict())


stdout.writelines(map(encode_id, stdin.readlines()))