doclang 0.7.2__tar.gz → 0.7.4__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {doclang-0.7.2 → doclang-0.7.4}/CHANGELOG.md +9 -0
- {doclang-0.7.2/doclang.egg-info → doclang-0.7.4}/PKG-INFO +1 -1
- {doclang-0.7.2 → doclang-0.7.4}/doclang/_packaging.py +22 -18
- {doclang-0.7.2 → doclang-0.7.4}/doclang/backends/saxonche.py +4 -3
- {doclang-0.7.2 → doclang-0.7.4}/doclang/doclang.sch +28 -28
- {doclang-0.7.2 → doclang-0.7.4}/doclang/doclang.xsd +1 -1
- {doclang-0.7.2 → doclang-0.7.4}/doclang/packaging.py +4 -1
- doclang-0.7.4/doclang/utils.py +127 -0
- {doclang-0.7.2 → doclang-0.7.4}/doclang/xsd_validation.py +4 -3
- {doclang-0.7.2 → doclang-0.7.4/doclang.egg-info}/PKG-INFO +1 -1
- {doclang-0.7.2 → doclang-0.7.4}/pyproject.toml +1 -1
- doclang-0.7.2/doclang/utils.py +0 -66
- {doclang-0.7.2 → doclang-0.7.4}/LICENSE +0 -0
- {doclang-0.7.2 → doclang-0.7.4}/MANIFEST.in +0 -0
- {doclang-0.7.2 → doclang-0.7.4}/README.md +0 -0
- {doclang-0.7.2 → doclang-0.7.4}/doclang/README.md +0 -0
- {doclang-0.7.2 → doclang-0.7.4}/doclang/__init__.py +0 -0
- {doclang-0.7.2 → doclang-0.7.4}/doclang/_schemas.py +0 -0
- {doclang-0.7.2 → doclang-0.7.4}/doclang/backends/__init__.py +0 -0
- {doclang-0.7.2 → doclang-0.7.4}/doclang/backends/_svrl.py +0 -0
- {doclang-0.7.2 → doclang-0.7.4}/doclang/cli.py +0 -0
- {doclang-0.7.2 → doclang-0.7.4}/doclang/schematron.py +0 -0
- {doclang-0.7.2 → doclang-0.7.4}/doclang/validation.py +0 -0
- {doclang-0.7.2 → doclang-0.7.4}/doclang/version.py +0 -0
- {doclang-0.7.2 → doclang-0.7.4}/doclang.egg-info/SOURCES.txt +0 -0
- {doclang-0.7.2 → doclang-0.7.4}/doclang.egg-info/dependency_links.txt +0 -0
- {doclang-0.7.2 → doclang-0.7.4}/doclang.egg-info/entry_points.txt +0 -0
- {doclang-0.7.2 → doclang-0.7.4}/doclang.egg-info/requires.txt +0 -0
- {doclang-0.7.2 → doclang-0.7.4}/doclang.egg-info/top_level.txt +0 -0
- {doclang-0.7.2 → doclang-0.7.4}/setup.cfg +0 -0
|
@@ -1,3 +1,12 @@
|
|
|
1
|
+
## [v0.7.4](https://github.com/doclang-project/doclang/releases/tag/v0.7.4)
|
|
2
|
+
|
|
3
|
+
* fix element head rules for all their elements (#214)
|
|
4
|
+
|
|
5
|
+
## [v0.7.3](https://github.com/doclang-project/doclang/releases/tag/v0.7.3)
|
|
6
|
+
|
|
7
|
+
* refuse symbolic links when packing pages and assets (#171)
|
|
8
|
+
* reject DTD and entity references in DocLang validation (#170)
|
|
9
|
+
|
|
1
10
|
## [v0.7.2](https://github.com/doclang-project/doclang/releases/tag/v0.7.2)
|
|
2
11
|
|
|
3
12
|
* expand lxml version support (#158)
|
|
@@ -58,6 +58,11 @@ def _require_directory(path: Path, *, label: str) -> None:
|
|
|
58
58
|
raise PackagingError(f"{label} not found or not a directory: {path}")
|
|
59
59
|
|
|
60
60
|
|
|
61
|
+
def _reject_symlink(path: Path, *, label: str) -> None:
|
|
62
|
+
if path.is_symlink():
|
|
63
|
+
raise PackagingError(f"{label} must not be a symbolic link: {path}")
|
|
64
|
+
|
|
65
|
+
|
|
61
66
|
def _validate_archive_relative_path(path: str, *, label: str) -> None:
|
|
62
67
|
if not path or path.startswith("/") or "\\" in path:
|
|
63
68
|
raise PackagingError(f"Invalid {label} path: {path!r}")
|
|
@@ -66,12 +71,22 @@ def _validate_archive_relative_path(path: str, *, label: str) -> None:
|
|
|
66
71
|
raise PackagingError(f"Invalid {label} path: {path!r}")
|
|
67
72
|
|
|
68
73
|
|
|
69
|
-
def
|
|
74
|
+
def _copy_file(source: Path, destination: Path, *, label: str) -> None:
|
|
75
|
+
_reject_symlink(source, label=label)
|
|
76
|
+
_require_file(source, label=label)
|
|
77
|
+
destination.parent.mkdir(parents=True, exist_ok=True)
|
|
78
|
+
shutil.copy2(source, destination)
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def _copy_tree_into(source: Path, destination: Path, *, label: str) -> None:
|
|
82
|
+
_reject_symlink(source, label=label)
|
|
83
|
+
_require_directory(source, label=label)
|
|
70
84
|
destination.mkdir(parents=True, exist_ok=True)
|
|
71
85
|
for item in source.iterdir():
|
|
72
86
|
target = destination / item.name
|
|
87
|
+
_reject_symlink(item, label=label)
|
|
73
88
|
if item.is_dir():
|
|
74
|
-
|
|
89
|
+
_copy_tree_into(item, target, label=label)
|
|
75
90
|
else:
|
|
76
91
|
shutil.copy2(item, target)
|
|
77
92
|
|
|
@@ -88,24 +103,18 @@ def _place_pages(stage: Path, pages: PagesInput) -> None:
|
|
|
88
103
|
if not isinstance(page_number, int) or page_number < 1:
|
|
89
104
|
raise PackagingError(f"Page numbers must be positive integers, got {page_number!r}")
|
|
90
105
|
source_path = Path(source)
|
|
91
|
-
_require_file(source_path, label="Page file")
|
|
92
106
|
destination = pages_dir / f"{page_number}{source_path.suffix}"
|
|
93
|
-
|
|
94
|
-
shutil.copy2(source_path, destination)
|
|
107
|
+
_copy_file(source_path, destination, label="Page file")
|
|
95
108
|
return
|
|
96
109
|
|
|
97
110
|
if isinstance(pages, str | Path):
|
|
98
|
-
|
|
99
|
-
_require_directory(source_dir, label="Pages directory")
|
|
100
|
-
_copy_tree_into(source_dir, pages_dir)
|
|
111
|
+
_copy_tree_into(Path(pages), pages_dir, label="Pages directory")
|
|
101
112
|
return
|
|
102
113
|
|
|
103
114
|
for index, source in enumerate(pages, start=1):
|
|
104
115
|
source_path = Path(source)
|
|
105
|
-
_require_file(source_path, label="Page file")
|
|
106
116
|
destination = pages_dir / f"{index}{source_path.suffix}"
|
|
107
|
-
|
|
108
|
-
shutil.copy2(source_path, destination)
|
|
117
|
+
_copy_file(source_path, destination, label="Page file")
|
|
109
118
|
|
|
110
119
|
|
|
111
120
|
def _place_assets(stage: Path, assets: AssetsInput) -> None:
|
|
@@ -113,16 +122,11 @@ def _place_assets(stage: Path, assets: AssetsInput) -> None:
|
|
|
113
122
|
if isinstance(assets, Mapping):
|
|
114
123
|
for archive_path, source in assets.items():
|
|
115
124
|
_validate_archive_relative_path(archive_path, label="asset")
|
|
116
|
-
source_path = Path(source)
|
|
117
|
-
_require_file(source_path, label="Asset file")
|
|
118
125
|
destination = assets_dir / archive_path
|
|
119
|
-
|
|
120
|
-
shutil.copy2(source_path, destination)
|
|
126
|
+
_copy_file(Path(source), destination, label="Asset file")
|
|
121
127
|
return
|
|
122
128
|
|
|
123
|
-
|
|
124
|
-
_require_directory(source_dir, label="Assets directory")
|
|
125
|
-
_copy_tree_into(source_dir, assets_dir)
|
|
129
|
+
_copy_tree_into(Path(assets), assets_dir, label="Assets directory")
|
|
126
130
|
|
|
127
131
|
|
|
128
132
|
def _write_opc_metadata(stage: Path) -> None:
|
|
@@ -13,7 +13,7 @@ from lxml import etree
|
|
|
13
13
|
|
|
14
14
|
from doclang.backends._svrl import _svrl_failed_asserts_to_violations
|
|
15
15
|
from doclang.schematron import SchematronViolation, _require_saxonche_backend
|
|
16
|
-
from doclang.utils import _ensure_namespace
|
|
16
|
+
from doclang.utils import _ensure_namespace, _parse_doclang_document, _write_xml_without_dtd
|
|
17
17
|
|
|
18
18
|
# ISO Schematron transpiler - converts .sch to XSLT 3.0
|
|
19
19
|
_ISO_SCHEMATRON_TRANSPILER = """<?xml version="1.0" encoding="UTF-8"?>
|
|
@@ -139,13 +139,14 @@ class SaxoncheValidator:
|
|
|
139
139
|
print(f"Using Schematron file: {schema_path}")
|
|
140
140
|
|
|
141
141
|
with open(xml_path, "rb") as f:
|
|
142
|
-
xml_doc =
|
|
142
|
+
xml_doc = _parse_doclang_document(f)
|
|
143
143
|
|
|
144
144
|
if allow_empty_namespace:
|
|
145
145
|
xml_doc = _ensure_namespace(xml_doc)
|
|
146
146
|
|
|
147
147
|
with tempfile.NamedTemporaryFile(mode="wb", suffix=".xml", delete=True) as tmp:
|
|
148
|
-
|
|
148
|
+
# Never re-emit a DOCTYPE: Saxon would otherwise expand entities.
|
|
149
|
+
_write_xml_without_dtd(xml_doc, tmp)
|
|
149
150
|
tmp.flush()
|
|
150
151
|
tmp_xml_path = tmp.name
|
|
151
152
|
|
|
@@ -10,6 +10,8 @@
|
|
|
10
10
|
<!-- ============================================ -->
|
|
11
11
|
<!-- NOTE: Element head order is enforced by XSD element_head group -->
|
|
12
12
|
<!-- Schematron only validates structural tokens (ldiv for lists, cell tokens for tables) -->
|
|
13
|
+
<!-- Rule contexts are single paths (e.g. *[self::dl:a or self::dl:b], not dl:a | dl:b), so that every backend applies -->
|
|
14
|
+
<!-- them the same, including ones that only apply the first branch of a union -->
|
|
13
15
|
<!-- ============================================ -->
|
|
14
16
|
|
|
15
17
|
<!-- ============================================ -->
|
|
@@ -32,7 +34,7 @@
|
|
|
32
34
|
<!-- ============================================ -->
|
|
33
35
|
|
|
34
36
|
<sch:pattern id="table-structure">
|
|
35
|
-
<sch:rule context="dl:table
|
|
37
|
+
<sch:rule context="*[self::dl:table or self::dl:index][*]">
|
|
36
38
|
<sch:let name="first-non-header" value="*[not(self::dl:label or self::dl:thread or self::dl:xref or self::dl:href or self::dl:layer or self::dl:location or self::dl:caption or self::dl:description or self::dl:summary or self::dl:custom)][1]"/>
|
|
37
39
|
|
|
38
40
|
<sch:assert test="not($first-non-header) or
|
|
@@ -51,7 +53,7 @@
|
|
|
51
53
|
<!-- ============================================ -->
|
|
52
54
|
|
|
53
55
|
<sch:pattern id="table-rectangular-grid">
|
|
54
|
-
<sch:rule context="dl:table
|
|
56
|
+
<sch:rule context="*[self::dl:table or self::dl:index][dl:nl]">
|
|
55
57
|
<!-- Define cell-starting tokens (tokens that begin a new cell) -->
|
|
56
58
|
<sch:let name="cell-tokens" value="dl:fcel | dl:ecel | dl:ched | dl:rhed | dl:corn | dl:srow | dl:lcel | dl:ucel | dl:xcel"/>
|
|
57
59
|
|
|
@@ -77,10 +79,10 @@
|
|
|
77
79
|
<!-- ============================================ -->
|
|
78
80
|
|
|
79
81
|
<sch:pattern id="element-head-placement">
|
|
80
|
-
<sch:rule context="dl:text
|
|
81
|
-
dl:
|
|
82
|
-
dl:
|
|
83
|
-
dl:
|
|
82
|
+
<sch:rule context="*[self::dl:text or self::dl:heading or self::dl:code or self::dl:formula or self::dl:caption or
|
|
83
|
+
self::dl:description or self::dl:summary or self::dl:page_header or self::dl:page_footer or
|
|
84
|
+
self::dl:footnote or self::dl:picture or self::dl:marker or self::dl:field_region or
|
|
85
|
+
self::dl:field_heading or self::dl:field_item or self::dl:key or self::dl:value or self::dl:group]">
|
|
84
86
|
<sch:let name="header-elements" value="dl:label | dl:thread | dl:xref | dl:href | dl:layer | dl:location | dl:caption | dl:description | dl:summary | dl:custom"/>
|
|
85
87
|
|
|
86
88
|
<sch:let name="text-before-header" value="text()[following-sibling::*[self::dl:label or self::dl:thread or self::dl:xref or self::dl:href or self::dl:layer or self::dl:location or self::dl:caption or self::dl:description or self::dl:summary or self::dl:custom]]"/>
|
|
@@ -90,6 +92,20 @@
|
|
|
90
92
|
Found non-whitespace text before element head: '<sch:value-of select="normalize-space(string-join($text-before-header, ''))"/>'
|
|
91
93
|
</sch:assert>
|
|
92
94
|
</sch:rule>
|
|
95
|
+
|
|
96
|
+
<!-- containers: only their own head (before the first body element) is checked here; -->
|
|
97
|
+
<!-- the heads of their items and cells are checked by the virtual text rules -->
|
|
98
|
+
<sch:rule context="*[self::dl:list or self::dl:table or self::dl:index]">
|
|
99
|
+
<sch:let name="first-body" value="*[not(self::dl:label or self::dl:thread or self::dl:xref or self::dl:href or self::dl:layer or self::dl:location or self::dl:caption or self::dl:description or self::dl:summary or self::dl:custom)][1]"/>
|
|
100
|
+
|
|
101
|
+
<sch:let name="text-before-header" value="text()[following-sibling::*[self::dl:label or self::dl:thread or self::dl:xref or self::dl:href or self::dl:layer or self::dl:location or self::dl:caption or self::dl:description or self::dl:summary or self::dl:custom]
|
|
102
|
+
[not($first-body) or . << $first-body]]"/>
|
|
103
|
+
|
|
104
|
+
<sch:assert test="every $t in $text-before-header satisfies normalize-space($t) = ''">
|
|
105
|
+
Property elements in the element head (label, thread, xref, href, layer, location, caption, description, summary, custom) must appear before any non-whitespace text content.
|
|
106
|
+
Found non-whitespace text before element head: '<sch:value-of select="normalize-space(string-join($text-before-header, ''))"/>'
|
|
107
|
+
</sch:assert>
|
|
108
|
+
</sch:rule>
|
|
93
109
|
</sch:pattern>
|
|
94
110
|
|
|
95
111
|
<!-- ============================================ -->
|
|
@@ -238,14 +254,8 @@
|
|
|
238
254
|
|
|
239
255
|
<sch:let name="header-elements" value="$item-content[self::dl:label or self::dl:thread or self::dl:xref or self::dl:href or self::dl:layer or self::dl:location or self::dl:caption or self::dl:description or self::dl:summary or self::dl:custom]"/>
|
|
240
256
|
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
else 0"/>
|
|
244
|
-
|
|
245
|
-
<sch:let name="text-before-header" value="if ($first-header-index > 0)
|
|
246
|
-
then for $i in 1 to ($first-header-index - 1)
|
|
247
|
-
return $item-content[$i][self::text()][normalize-space(.) != '']
|
|
248
|
-
else ()"/>
|
|
257
|
+
<!-- compared by position in the document (not by value: e.g. empty elements would all be equal) -->
|
|
258
|
+
<sch:let name="text-before-header" value="$item-content[self::text()][normalize-space(.) != ''][. << $header-elements[1]]"/>
|
|
249
259
|
|
|
250
260
|
<sch:assert test="empty($text-before-header)">
|
|
251
261
|
In list items (virtual text), property elements in the element head (label, thread, xref, href, layer, location, caption, description, summary, custom) must appear before any non-whitespace text content.
|
|
@@ -261,12 +271,8 @@
|
|
|
261
271
|
<!-- ============================================ -->
|
|
262
272
|
|
|
263
273
|
<sch:pattern id="table-virtual-text-element-head">
|
|
264
|
-
<sch:rule context="dl:
|
|
265
|
-
dl:
|
|
266
|
-
dl:table/dl:lcel | dl:table/dl:ucel | dl:table/dl:xcel |
|
|
267
|
-
dl:index/dl:fcel | dl:index/dl:ecel | dl:index/dl:ched |
|
|
268
|
-
dl:index/dl:rhed | dl:index/dl:corn | dl:index/dl:srow |
|
|
269
|
-
dl:index/dl:lcel | dl:index/dl:ucel | dl:index/dl:xcel">
|
|
274
|
+
<sch:rule context="*[self::dl:fcel or self::dl:ecel or self::dl:ched or self::dl:rhed or self::dl:corn or self::dl:srow or
|
|
275
|
+
self::dl:lcel or self::dl:ucel or self::dl:xcel][parent::dl:table or parent::dl:index]">
|
|
270
276
|
<sch:let name="next-token" value="following-sibling::*[self::dl:fcel or self::dl:ecel or self::dl:ched or
|
|
271
277
|
self::dl:rhed or self::dl:corn or self::dl:srow or
|
|
272
278
|
self::dl:lcel or self::dl:ucel or self::dl:xcel or
|
|
@@ -278,14 +284,8 @@
|
|
|
278
284
|
|
|
279
285
|
<sch:let name="header-elements" value="$cell-content[self::dl:label or self::dl:thread or self::dl:xref or self::dl:href or self::dl:layer or self::dl:location or self::dl:caption or self::dl:description or self::dl:summary or self::dl:custom]"/>
|
|
280
286
|
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
else 0"/>
|
|
284
|
-
|
|
285
|
-
<sch:let name="text-before-header" value="if ($first-header-index > 0)
|
|
286
|
-
then for $i in 1 to ($first-header-index - 1)
|
|
287
|
-
return $cell-content[$i][self::text()][normalize-space(.) != '']
|
|
288
|
-
else ()"/>
|
|
287
|
+
<!-- compared by position in the document (not by value: e.g. empty elements would all be equal) -->
|
|
288
|
+
<sch:let name="text-before-header" value="$cell-content[self::text()][normalize-space(.) != ''][. << $header-elements[1]]"/>
|
|
289
289
|
|
|
290
290
|
<sch:assert test="empty($text-before-header)">
|
|
291
291
|
In table and index cells (virtual text), property elements in the element head (label, thread, xref, href, layer, location, caption, description, summary, custom) must appear before any non-whitespace text content.
|
|
@@ -34,9 +34,12 @@ def pack(
|
|
|
34
34
|
``assets`` may be a directory (copied into ``assets/``) or a mapping of
|
|
35
35
|
archive-relative asset path to source file.
|
|
36
36
|
|
|
37
|
+
Symbolic links are refused for ``pages`` and ``assets`` (including entries
|
|
38
|
+
inside directories) so pack does not follow link targets into the archive.
|
|
39
|
+
|
|
37
40
|
Returns the resolved path to the created archive.
|
|
38
41
|
|
|
39
|
-
Raises :class:`PackagingError` on packaging failure.
|
|
42
|
+
Raises :class:`PackagingError` on packaging failure (including symbolic links).
|
|
40
43
|
Raises :class:`~doclang.ValidationError` when ``validate=True`` and the
|
|
41
44
|
document fails validation.
|
|
42
45
|
"""
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Shared utility functions for DocLang validation.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
from typing import BinaryIO, Protocol, Union
|
|
7
|
+
|
|
8
|
+
from lxml import etree
|
|
9
|
+
|
|
10
|
+
from doclang.version import _resolve_version
|
|
11
|
+
|
|
12
|
+
_DOCLANG_NAMESPACE = "https://www.doclang.ai/ns/v0"
|
|
13
|
+
_VERSION = _resolve_version()
|
|
14
|
+
|
|
15
|
+
_DTD_REJECTED_MESSAGE = "DTD declarations and entity references are not allowed in DocLang documents"
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class _BinaryWriter(Protocol):
|
|
19
|
+
def write(self, data: bytes, /) -> int: ...
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def _safe_xml_parser() -> etree.XMLParser:
|
|
23
|
+
"""Return an lxml parser that does not expand entities or load external DTDs."""
|
|
24
|
+
return etree.XMLParser(
|
|
25
|
+
resolve_entities=False,
|
|
26
|
+
no_network=True,
|
|
27
|
+
load_dtd=False,
|
|
28
|
+
dtd_validation=False,
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def _contains_entity_nodes(node: etree._Element) -> bool:
|
|
33
|
+
for child in node:
|
|
34
|
+
if not isinstance(child.tag, str):
|
|
35
|
+
# Comments/PIs are skipped elsewhere; treat non-element nodes as unsafe
|
|
36
|
+
# when they are entity references (lxml exposes them as _Entity).
|
|
37
|
+
if type(child).__name__ == "_Entity":
|
|
38
|
+
return True
|
|
39
|
+
continue
|
|
40
|
+
if _contains_entity_nodes(child):
|
|
41
|
+
return True
|
|
42
|
+
return False
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def _reject_dtd_or_entities(xml_doc: etree._ElementTree) -> None:
|
|
46
|
+
"""Fail closed if the document declares a DTD or contains entity nodes."""
|
|
47
|
+
if (xml_doc.docinfo.doctype or "").strip() or _contains_entity_nodes(xml_doc.getroot()):
|
|
48
|
+
raise ValueError(_DTD_REJECTED_MESSAGE)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def _parse_xml(source: Union[str, Path, BinaryIO]) -> etree._ElementTree:
|
|
52
|
+
"""Parse XML with :func:`_safe_xml_parser` (no DTD/entity policy checks)."""
|
|
53
|
+
return etree.parse(source, parser=_safe_xml_parser())
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def _parse_doclang_document(source: Union[str, Path, BinaryIO]) -> etree._ElementTree:
|
|
57
|
+
"""Parse a DocLang document safely and reject DTD / entity payloads."""
|
|
58
|
+
xml_doc = _parse_xml(source)
|
|
59
|
+
_reject_dtd_or_entities(xml_doc)
|
|
60
|
+
return xml_doc
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def _write_xml_without_dtd(xml_doc: etree._ElementTree, out: _BinaryWriter) -> None:
|
|
64
|
+
"""Serialize ``xml_doc`` without preserving any DOCTYPE / internal subset."""
|
|
65
|
+
out.write(
|
|
66
|
+
etree.tostring(
|
|
67
|
+
xml_doc.getroot(),
|
|
68
|
+
encoding="utf-8",
|
|
69
|
+
xml_declaration=True,
|
|
70
|
+
)
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def _ensure_namespace(xml_doc: etree._ElementTree) -> etree._ElementTree:
|
|
75
|
+
"""
|
|
76
|
+
Ensure the document has the DocLang namespace.
|
|
77
|
+
If the root element has no namespace, add the default DocLang namespace.
|
|
78
|
+
|
|
79
|
+
Args:
|
|
80
|
+
xml_doc: The XML document tree
|
|
81
|
+
|
|
82
|
+
Returns:
|
|
83
|
+
The XML document tree with namespace added if it was missing
|
|
84
|
+
"""
|
|
85
|
+
root = xml_doc.getroot()
|
|
86
|
+
|
|
87
|
+
# Check if root element has a namespace
|
|
88
|
+
root_tag = str(root.tag)
|
|
89
|
+
if root_tag.startswith("{"):
|
|
90
|
+
# Already has a namespace
|
|
91
|
+
return xml_doc
|
|
92
|
+
|
|
93
|
+
# No namespace - add DocLang namespace
|
|
94
|
+
# Create a new root with namespace
|
|
95
|
+
new_root = etree.Element(f"{{{_DOCLANG_NAMESPACE}}}{root.tag}", nsmap={None: _DOCLANG_NAMESPACE})
|
|
96
|
+
|
|
97
|
+
# Copy attributes
|
|
98
|
+
for key, value in root.attrib.items():
|
|
99
|
+
new_root.set(key, value)
|
|
100
|
+
|
|
101
|
+
# Copy children recursively
|
|
102
|
+
def copy_element(source, target):
|
|
103
|
+
target.text = source.text
|
|
104
|
+
target.tail = source.tail
|
|
105
|
+
for child in source:
|
|
106
|
+
# Skip comments and processing instructions
|
|
107
|
+
if not isinstance(child.tag, str):
|
|
108
|
+
continue
|
|
109
|
+
|
|
110
|
+
child_tag = str(child.tag)
|
|
111
|
+
if child_tag.startswith("{"):
|
|
112
|
+
# Child already has namespace
|
|
113
|
+
new_child = etree.SubElement(target, child_tag)
|
|
114
|
+
else:
|
|
115
|
+
# Add namespace to child
|
|
116
|
+
new_child = etree.SubElement(target, f"{{{_DOCLANG_NAMESPACE}}}{child_tag}")
|
|
117
|
+
|
|
118
|
+
for key, value in child.attrib.items():
|
|
119
|
+
new_child.set(key, value)
|
|
120
|
+
|
|
121
|
+
copy_element(child, new_child)
|
|
122
|
+
|
|
123
|
+
copy_element(root, new_root)
|
|
124
|
+
|
|
125
|
+
# Create new document with namespaced root
|
|
126
|
+
new_doc = etree.ElementTree(new_root)
|
|
127
|
+
return new_doc
|
|
@@ -10,7 +10,7 @@ from typing import Any, Union
|
|
|
10
10
|
from lxml import etree
|
|
11
11
|
|
|
12
12
|
from doclang._schemas import _bundled_xsd_path
|
|
13
|
-
from doclang.utils import _ensure_namespace
|
|
13
|
+
from doclang.utils import _ensure_namespace, _parse_doclang_document, _parse_xml
|
|
14
14
|
|
|
15
15
|
|
|
16
16
|
def _validate_xsd_at(
|
|
@@ -29,11 +29,12 @@ def _validate_xsd_at(
|
|
|
29
29
|
"""
|
|
30
30
|
try:
|
|
31
31
|
with open(xsd_file, "rb") as f:
|
|
32
|
-
|
|
32
|
+
# Bundled/trusted schema: hardened parse only (no DocLang DTD policy).
|
|
33
|
+
schema_doc = _parse_xml(f)
|
|
33
34
|
schema = etree.XMLSchema(schema_doc)
|
|
34
35
|
|
|
35
36
|
with open(xml_file, "rb") as f:
|
|
36
|
-
xml_doc =
|
|
37
|
+
xml_doc = _parse_doclang_document(f)
|
|
37
38
|
|
|
38
39
|
if allow_empty_namespace:
|
|
39
40
|
xml_doc = _ensure_namespace(xml_doc)
|
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "doclang"
|
|
7
|
-
version = "0.7.
|
|
7
|
+
version = "0.7.4" # DO NOT EDIT MANUALLY, updated automatically
|
|
8
8
|
description = "DocLang reference toolkit"
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
requires-python = ">=3.10"
|
doclang-0.7.2/doclang/utils.py
DELETED
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
"""
|
|
2
|
-
Shared utility functions for DocLang validation.
|
|
3
|
-
"""
|
|
4
|
-
|
|
5
|
-
from lxml import etree
|
|
6
|
-
|
|
7
|
-
from doclang.version import _resolve_version
|
|
8
|
-
|
|
9
|
-
_DOCLANG_NAMESPACE = "https://www.doclang.ai/ns/v0"
|
|
10
|
-
_VERSION = _resolve_version()
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
def _ensure_namespace(xml_doc: etree._ElementTree) -> etree._ElementTree:
|
|
14
|
-
"""
|
|
15
|
-
Ensure the document has the DocLang namespace.
|
|
16
|
-
If the root element has no namespace, add the default DocLang namespace.
|
|
17
|
-
|
|
18
|
-
Args:
|
|
19
|
-
xml_doc: The XML document tree
|
|
20
|
-
|
|
21
|
-
Returns:
|
|
22
|
-
The XML document tree with namespace added if it was missing
|
|
23
|
-
"""
|
|
24
|
-
root = xml_doc.getroot()
|
|
25
|
-
|
|
26
|
-
# Check if root element has a namespace
|
|
27
|
-
root_tag = str(root.tag)
|
|
28
|
-
if root_tag.startswith("{"):
|
|
29
|
-
# Already has a namespace
|
|
30
|
-
return xml_doc
|
|
31
|
-
|
|
32
|
-
# No namespace - add DocLang namespace
|
|
33
|
-
# Create a new root with namespace
|
|
34
|
-
new_root = etree.Element(f"{{{_DOCLANG_NAMESPACE}}}{root.tag}", nsmap={None: _DOCLANG_NAMESPACE})
|
|
35
|
-
|
|
36
|
-
# Copy attributes
|
|
37
|
-
for key, value in root.attrib.items():
|
|
38
|
-
new_root.set(key, value)
|
|
39
|
-
|
|
40
|
-
# Copy children recursively
|
|
41
|
-
def copy_element(source, target):
|
|
42
|
-
target.text = source.text
|
|
43
|
-
target.tail = source.tail
|
|
44
|
-
for child in source:
|
|
45
|
-
# Skip comments and processing instructions
|
|
46
|
-
if not isinstance(child.tag, str):
|
|
47
|
-
continue
|
|
48
|
-
|
|
49
|
-
child_tag = str(child.tag)
|
|
50
|
-
if child_tag.startswith("{"):
|
|
51
|
-
# Child already has namespace
|
|
52
|
-
new_child = etree.SubElement(target, child_tag)
|
|
53
|
-
else:
|
|
54
|
-
# Add namespace to child
|
|
55
|
-
new_child = etree.SubElement(target, f"{{{_DOCLANG_NAMESPACE}}}{child_tag}")
|
|
56
|
-
|
|
57
|
-
for key, value in child.attrib.items():
|
|
58
|
-
new_child.set(key, value)
|
|
59
|
-
|
|
60
|
-
copy_element(child, new_child)
|
|
61
|
-
|
|
62
|
-
copy_element(root, new_root)
|
|
63
|
-
|
|
64
|
-
# Create new document with namespaced root
|
|
65
|
-
new_doc = etree.ElementTree(new_root)
|
|
66
|
-
return new_doc
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|