| 1 |
#!/usr/bin/python3 |
| 2 |
# SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 |
|
| 4 |
# Extracts translatable strings from HTML files in the following forms: |
| 5 |
# |
| 6 |
# <tag translate>String</tag> |
| 7 |
# <tag translate context="value">String</tag> |
| 8 |
# |
| 9 |
# Note that some of the use of the translated may not support all the strings |
| 10 |
# depending on the code actually using these strings to translate the HTML. |
| 11 |
|
| 12 |
|
| 13 |
import argparse |
| 14 |
import collections.abc |
| 15 |
import json |
| 16 |
import pathlib |
| 17 |
from collections import defaultdict |
| 18 |
from html.parser import HTMLParser |
| 19 |
|
| 20 |
PO_HEADER = r"""msgid "" |
| 21 |
msgstr "" |
| 22 |
"Project-Id-Version: PACKAGE_VERSION\n" |
| 23 |
"MIME-Version: 1.0\n" |
| 24 |
"Content-Type: text/plain; charset=UTF-8\n" |
| 25 |
"Content-Transfer-Encoding: 8bit\n" |
| 26 |
"X-Generator: Cockpit html2po\n" |
| 27 |
""" |
| 28 |
|
| 29 |
|
| 30 |
class MyHTMLParser(HTMLParser): |
| 31 |
current_attr: tuple[str | None, str] | None = None |
| 32 |
filename: str |
| 33 |
|
| 34 |
def __init__(self, filename: str): |
| 35 |
self.strings = defaultdict[tuple[str | None, str], set[str]](set) |
| 36 |
self.filename = filename |
| 37 |
super().__init__() |
| 38 |
|
| 39 |
def handle_starttag(self, tag, attrs): |
| 40 |
translatable = False |
| 41 |
context = None |
| 42 |
|
| 43 |
# attrs => (key, value) where value needs to be split |
| 44 |
for (attr, value) in attrs: |
| 45 |
if attr == 'translate-context': |
| 46 |
context = value |
| 47 |
elif attr == 'context': |
| 48 |
context = value |
| 49 |
elif attr == 'translate' and (value is None or 'yes' in value.split(' ')): |
| 50 |
translatable = True |
| 51 |
|
| 52 |
if not translatable: |
| 53 |
return |
| 54 |
|
| 55 |
lineno = self.getpos()[0] |
| 56 |
filename = f'{self.filename}:{lineno}' |
| 57 |
self.current_attr = (context, filename) |
| 58 |
|
| 59 |
def handle_data(self, data: str): |
| 60 |
if self.current_attr and data: |
| 61 |
context, filename = self.current_attr |
| 62 |
self.strings[context, data].add(filename) |
| 63 |
self.current_attr = None |
| 64 |
|
| 65 |
|
| 66 |
def main() -> None: |
| 67 |
parser = argparse.ArgumentParser(prog='html2po', |
| 68 |
description='Extracts translatable strings from HTML files') |
| 69 |
parser.add_argument('-d', '--directory', help='Base directory for input files') |
| 70 |
parser.add_argument('-o', '--output', help='Output file') |
| 71 |
parser.add_argument('files', nargs='+', help='One or more input files', type=pathlib.Path, metavar='FILE') |
| 72 |
|
| 73 |
args = parser.parse_args() |
| 74 |
strings = defaultdict[tuple[str | None, str], set[str]](set) |
| 75 |
|
| 76 |
files: collections.abc.Iterable[pathlib.Path] = args.files |
| 77 |
for file in files: |
| 78 |
# Qualify the filename if necessary |
| 79 |
full_path = args.directory / file if args.directory else file |
| 80 |
|
| 81 |
htmlparser = MyHTMLParser(str(file)) |
| 82 |
with open(full_path, 'r') as fp: |
| 83 |
htmlparser.feed(fp.read()) |
| 84 |
for ctx_msgid, filename in htmlparser.strings.items(): |
| 85 |
strings[ctx_msgid].update(filename) |
| 86 |
|
| 87 |
with open(args.output, 'w') as fp: |
| 88 |
fp.write(PO_HEADER) |
| 89 |
for (context, msgid), filenames in strings.items(): |
| 90 |
fp.write(f"\n#: {' '.join(filenames)}\n") |
| 91 |
# json.dumps() to escape any quotes in translation text or context |
| 92 |
if context: |
| 93 |
fp.write(f'msgctxt {json.dumps(context)}\n') |
| 94 |
|
| 95 |
fp.write(f'msgid {json.dumps(msgid)}\n') |
| 96 |
fp.write('msgstr ""\n') |
| 97 |
|
| 98 |
|
| 99 |
if __name__ == "__main__": |
| 100 |
main() |