| 1 |
# Copyright Mozilla Foundation |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
# you may not use this file except in compliance with the License. |
| 5 |
# You may obtain a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
# See the License for the specific language governing permissions and |
| 13 |
# limitations under the License. |
| 14 |
|
| 15 |
from __future__ import annotations |
| 16 |
|
| 17 |
from collections import defaultdict |
| 18 |
from collections.abc import Iterator |
| 19 |
|
| 20 |
from lxml import etree |
| 21 |
|
| 22 |
from ...message import Markup, Message, PatternMessage, VariableRef |
| 23 |
from ..data import Entry, Metadata |
| 24 |
from .common import attrib_as_metadata, element_as_metadata, pretty_name, xliff_ns |
| 25 |
|
| 26 |
|
| 27 |
def parse_trans_unit(unit: etree._Element) -> Entry[Message, str]: |
| 28 |
id = unit.attrib.get("id", None) |
| 29 |
if id is None: |
| 30 |
raise ValueError(f'Missing "id" attribute for <trans-unit>: {unit}') |
| 31 |
meta = attrib_as_metadata(unit, None, ("id",)) |
| 32 |
if unit.text and not unit.text.isspace(): |
| 33 |
raise ValueError(f"Unexpected text in <trans-unit>: {unit.text}") |
| 34 |
|
| 35 |
target = None |
| 36 |
note = None |
| 37 |
seen: dict[str, int] = defaultdict(int) |
| 38 |
for el in unit: |
| 39 |
if isinstance(el, etree._Comment): |
| 40 |
meta.append(Metadata("comment()", el.text)) |
| 41 |
else: |
| 42 |
name = pretty_name(el, el.tag) |
| 43 |
if name == "target": |
| 44 |
if target: |
| 45 |
raise ValueError(f"Duplicate <target> in <trans-unit> {id}: {unit}") |
| 46 |
target = el |
| 47 |
meta += attrib_as_metadata(el, "target") |
| 48 |
elif name == "note" and note is None and el.text: |
| 49 |
note = el |
| 50 |
note_attrib = attrib_as_metadata(el, "note") |
| 51 |
if note_attrib: |
| 52 |
meta += note_attrib |
| 53 |
elif el != unit[-1]: |
| 54 |
# If there are elements after this <note>, |
| 55 |
# add a marker for its relative position. |
| 56 |
meta.append(Metadata("note", "")) |
| 57 |
seen[name] += 1 |
| 58 |
else: |
| 59 |
idx = seen[name] + 1 |
| 60 |
base = f"{name}[{idx}]" if idx > 1 else name |
| 61 |
meta += element_as_metadata(el, base, True) |
| 62 |
seen[name] = idx |
| 63 |
if el.tail and not el.tail.isspace(): |
| 64 |
raise ValueError(f"Unexpected text in <trans-unit>: {el.tail}") |
| 65 |
|
| 66 |
comment = "" if note is None else note.text or "" |
| 67 |
msg = PatternMessage([] if target is None else list(parse_pattern(target))) |
| 68 |
return Entry((id,), msg, comment, meta) |
| 69 |
|
| 70 |
|
| 71 |
def parse_pattern(el: etree._Element) -> Iterator[str | Markup]: |
| 72 |
if el.text: |
| 73 |
yield el.text |
| 74 |
for child in el: |
| 75 |
q = etree.QName(child.tag) |
| 76 |
ns = q.namespace |
| 77 |
name = q.localname if not ns or ns in xliff_ns else q.text |
| 78 |
options: dict[str, str | VariableRef] = dict(child.attrib) |
| 79 |
if name in ("x", "bx", "ex"): |
| 80 |
yield Markup("standalone", name, options) |
| 81 |
elif isinstance(child.tag, str): |
| 82 |
yield Markup("open", name, options) |
| 83 |
yield from parse_pattern(child) |
| 84 |
yield Markup("close", name) |
| 85 |
if child.tail: |
| 86 |
yield child.tail |