mdhtml2docx 0.1.5__tar.gz → 0.1.7__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.
- {mdhtml2docx-0.1.5 → mdhtml2docx-0.1.7}/PKG-INFO +28 -29
- {mdhtml2docx-0.1.5 → mdhtml2docx-0.1.7}/README.md +24 -25
- mdhtml2docx-0.1.7/mdhtml2docx/__init__.py +3 -0
- mdhtml2docx-0.1.7/mdhtml2docx/_modidx.py +27 -0
- mdhtml2docx-0.1.7/mdhtml2docx/hilite.py +37 -0
- mdhtml2docx-0.1.7/mdhtml2docx/mdhtml2docx.py +619 -0
- {mdhtml2docx-0.1.5 → mdhtml2docx-0.1.7}/mdhtml2docx/styles.py +33 -10
- mdhtml2docx-0.1.7/mdhtml2docx/wml.py +41 -0
- {mdhtml2docx-0.1.5 → mdhtml2docx-0.1.7}/mdhtml2docx.egg-info/PKG-INFO +28 -29
- {mdhtml2docx-0.1.5 → mdhtml2docx-0.1.7}/mdhtml2docx.egg-info/SOURCES.txt +2 -0
- mdhtml2docx-0.1.7/mdhtml2docx.egg-info/entry_points.txt +2 -0
- {mdhtml2docx-0.1.5 → mdhtml2docx-0.1.7}/mdhtml2docx.egg-info/requires.txt +1 -2
- {mdhtml2docx-0.1.5 → mdhtml2docx-0.1.7}/pyproject.toml +6 -8
- mdhtml2docx-0.1.7/tests/test_convert.py +336 -0
- mdhtml2docx-0.1.5/mdhtml2docx/__init__.py +0 -8
- mdhtml2docx-0.1.5/mdhtml2docx/hilite.py +0 -24
- mdhtml2docx-0.1.5/mdhtml2docx/mdhtml2docx.py +0 -769
- mdhtml2docx-0.1.5/mdhtml2docx/wml.py +0 -30
- mdhtml2docx-0.1.5/tests/test_convert.py +0 -639
- {mdhtml2docx-0.1.5 → mdhtml2docx-0.1.7}/LICENSE +0 -0
- {mdhtml2docx-0.1.5 → mdhtml2docx-0.1.7}/mdhtml2docx/templates/reference.docx +0 -0
- {mdhtml2docx-0.1.5 → mdhtml2docx-0.1.7}/mdhtml2docx.egg-info/dependency_links.txt +0 -0
- {mdhtml2docx-0.1.5 → mdhtml2docx-0.1.7}/mdhtml2docx.egg-info/top_level.txt +0 -0
- {mdhtml2docx-0.1.5 → mdhtml2docx-0.1.7}/setup.cfg +0 -0
- {mdhtml2docx-0.1.5 → mdhtml2docx-0.1.7}/tests/test_check_docx.py +0 -0
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: mdhtml2docx
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.7
|
|
4
4
|
Summary: Convert MDHTML to Word docx files
|
|
5
5
|
Author-email: Jeremy Howard <j@fast.ai>
|
|
6
6
|
License-Expression: Apache-2.0
|
|
7
7
|
Project-URL: Homepage, https://github.com/AnswerDotAI/mdhtml2docx
|
|
8
|
+
Project-URL: Documentation, https://AnswerDotAI.github.io/mdhtml2docx/
|
|
8
9
|
Classifier: Programming Language :: Python :: 3
|
|
9
10
|
Classifier: Programming Language :: Python :: 3 :: Only
|
|
10
|
-
Requires-Python: >=3.
|
|
11
|
+
Requires-Python: >=3.11
|
|
11
12
|
Description-Content-Type: text/markdown
|
|
12
13
|
License-File: LICENSE
|
|
13
|
-
Requires-Dist: mdhtml>=0.1.
|
|
14
|
-
Requires-Dist: fast5ever>=0.1.5
|
|
14
|
+
Requires-Dist: mdhtml>=0.1.44
|
|
15
15
|
Requires-Dist: oxml>=0.1.4
|
|
16
16
|
Provides-Extra: dev
|
|
17
17
|
Requires-Dist: lxml; extra == "dev"
|
|
@@ -23,35 +23,34 @@ Dynamic: license-file
|
|
|
23
23
|
|
|
24
24
|
# mdhtml2docx
|
|
25
25
|
|
|
26
|
-
|
|
26
|
+
Write [mdhtml](https://github.com/AnswerDotAI/mdhtml) documents as Word docx files.
|
|
27
27
|
|
|
28
|
-
`
|
|
28
|
+
`mdhtml2docx` consumes mdhtml's semantic `Document` API. The shared importer handles HTML5 parsing, raw-data decoding, and document structure. The shared reference index validates and plans references. `oxml` provides WordprocessingML construction, reference editing, DOCX packaging, and atomic saving. XML construction uses namespace-bound factories such as `e.tcW(type='dxa', w=2400)`, backed by fastcore's XML builder. Conversion requires neither lxml nor a .NET or Office runtime.
|
|
29
29
|
|
|
30
|
-
MDHTML accepts the full HTML vocabulary. This exporter supports the elements and annotations listed below. It preserves the text of unknown inline elements and recurses through block containers.
|
|
30
|
+
MDHTML accepts the full HTML vocabulary. This exporter supports the elements and annotations listed below. It preserves the text of unknown inline elements and recurses through block containers. HTML parsing and repair belong to `mdhtml`.
|
|
31
31
|
|
|
32
32
|
## Usage
|
|
33
33
|
|
|
34
34
|
```python
|
|
35
|
-
from mdhtml import
|
|
35
|
+
from mdhtml import parse_md
|
|
36
36
|
from mdhtml2docx import mdhtml2docx
|
|
37
37
|
|
|
38
|
-
|
|
39
|
-
warnings = mdhtml2docx(
|
|
38
|
+
document = parse_md(markdown_text)
|
|
39
|
+
warnings = mdhtml2docx(document, 'out.docx')
|
|
40
40
|
```
|
|
41
41
|
|
|
42
|
-
`mdhtml2docx`
|
|
42
|
+
`mdhtml2docx` accepts a semantic `Document` or symbolic MDHTML, writes a docx, and returns warning strings. Normal HTML5 repair applies when importing MDHTML; the input need not be well-formed XML.
|
|
43
43
|
|
|
44
|
-
|
|
44
|
+
Import existing MDHTML through the same document API:
|
|
45
45
|
|
|
46
46
|
```python
|
|
47
|
-
from mdhtml import
|
|
47
|
+
from mdhtml import Document
|
|
48
48
|
|
|
49
|
-
document =
|
|
50
|
-
document.children[0].attrs['custom-style'] = 'Contract Title'
|
|
49
|
+
document = Document.from_mdhtml(html)
|
|
51
50
|
warnings = mdhtml2docx(document, 'out.docx')
|
|
52
51
|
```
|
|
53
52
|
|
|
54
|
-
|
|
53
|
+
The importer groups body-position text and phrasing content into paragraphs. The exporter reads ordinary dictionary snapshots of the semantic blocks, inlines, and note definitions. Operation arguments remain inert unless an operation renderer selects them.
|
|
55
54
|
|
|
56
55
|
## Supported content
|
|
57
56
|
|
|
@@ -69,6 +68,8 @@ A `details` div becomes its label as a bold line above the body. It does not bec
|
|
|
69
68
|
|
|
70
69
|
Math spans and blocks become native Word math zones (`m:oMath`). These contain the source text unchanged, whether TeX, UnicodeMath, or AsciiMath. Math build-up and rendering are left to downstream tools. Pandoc reads the zones back as math.
|
|
71
70
|
|
|
71
|
+
An id-bearing display equation receives a live `Equation N` caption below its math zone. Unlabelled equations remain unnumbered.
|
|
72
|
+
|
|
72
73
|
When [fastpylight](https://github.com/AnswerDotAI/fastpylight) is installed, code blocks with a language use syntax coloring. Tokens receive `Hl*` character styles such as `Hl Keyword`. Their colors come from the reference theme described below. You can restyle them in Word.
|
|
73
74
|
|
|
74
75
|
## Styling
|
|
@@ -112,13 +113,7 @@ The converter accepts MDHTML raw data with `data-format="docx"`:
|
|
|
112
113
|
|
|
113
114
|
A ```` ```{=docx} ```` fenced block in Markdown, or inline code followed by `{=docx}`, produces this element. The payload is parsed as WordprocessingML and inserted verbatim. Block payloads contain elements such as `w:p` or `w:tbl`. Inline payloads contain elements such as `w:r`. The prefixes `w`, `r`, `wp`, `a`, `pic`, and `m` are predeclared.
|
|
114
115
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
- No `data-encoding`: use the literal payload.
|
|
118
|
-
- `data-encoding="html"`: perform one character-reference decoding pass.
|
|
119
|
-
- `data-encoding="base64"`: decode a base64-encoded UTF-8 payload.
|
|
120
|
-
|
|
121
|
-
Malformed payloads and unknown encodings are dropped with a warning. Raw data for other formats is skipped silently.
|
|
116
|
+
The shared mdhtml importer decodes payloads according to its raw-data contract. This exporter parses decoded `docx` payloads as WordprocessingML. Invalid encodings and malformed XML produce warnings. Other formats are skipped.
|
|
122
117
|
|
|
123
118
|
For example, insert a page break with:
|
|
124
119
|
|
|
@@ -128,6 +123,8 @@ For example, insert a page break with:
|
|
|
128
123
|
|
|
129
124
|
## Cross-references
|
|
130
125
|
|
|
126
|
+
Bibliographies and citations use mdhtml's shared Rust preparation. `::: bibliography` entries contain citation-author terms with ids and optional `year` attributes, followed by their formatted definitions. Citations use `[@key]`, narrative `{ref=text}`, author-suppressing `-@`, and per-item prefix/suffix text. `cite_style='numeric'` selects numbers in bibliography order; `'author-year'` selects author-year text. None inherits frontmatter. Citations become static linked text and bibliography entries retain their bookmarks. Section, page, figure and table cross-references remain native Word fields. Re-export the original document to change citation style.
|
|
127
|
+
|
|
131
128
|
Markdown references such as `[@sec-payment]` become live Word REF fields. MDHTML represents them as `a` elements with `data-ref`.
|
|
132
129
|
|
|
133
130
|
The default field is `REF <bookmark> \w \h`. It displays the full-context paragraph number, such as "3.(c)(iii)", as a hyperlink. The converter sets `updateFields` in `settings.xml` so Word refreshes fields on open.
|
|
@@ -148,7 +145,7 @@ A missing target id raises a conversion error. A reference type with no defined
|
|
|
148
145
|
|
|
149
146
|
Number fields require numbered headings. If your reference docx already numbers its heading styles, the converter leaves that numbering unchanged.
|
|
150
147
|
|
|
151
|
-
|
|
148
|
+
The shared reference index resolves `number_headings` against document metadata. Numeric heading references infer decimal numbering when no scheme is supplied. You can select a scheme explicitly:
|
|
152
149
|
|
|
153
150
|
- `'legal'` uses 1. / (a) / (i) numbering.
|
|
154
151
|
- `'decimal'` uses 1. / 1.1. / 1.1.1. numbering.
|
|
@@ -158,15 +155,17 @@ The named schemes come from mdhtml's `SCHEMES`. The custom dictionary uses the s
|
|
|
158
155
|
|
|
159
156
|
A reference-list entry ending in `.xml` can also supply styles and numbering. It contains raw `w:style`, `w:abstractNum`, and `w:num` elements. Each contributor's ids and references are remapped before merging, so separate contributors can reuse the same original numbering ids; later styles still win.
|
|
160
157
|
|
|
161
|
-
### Figures and
|
|
158
|
+
### Figures, tables, and equations
|
|
159
|
+
|
|
160
|
+
Figures, captioned tables, and id-bearing display equations use independent live SEQ fields. A figure's caption appears below its body as "Figure 1: caption". A table's caption appears above it as "Table 1: caption". Equations receive an "Equation 1" caption below the math zone. Labels come from `reftypes`; changing a label never merges the counters.
|
|
162
161
|
|
|
163
|
-
|
|
162
|
+
When the element has an id, the converter bookmarks its label, number, and authored caption separately. The shared reference planner supplies prefix words; each Word REF field supplies only the live number. Thus `[@fig-plot]` displays "Figure 1", while `[-@tbl-stages]` displays only the number.
|
|
164
163
|
|
|
165
|
-
|
|
164
|
+
Figure and table `{ref=text}` references use a separate bookmark containing the authored caption text. Generated bookmark names are reserved through the same collision-safe allocator as authored targets, including forward references. Equation references support full labels, bare numbers, and pages. `{ref=text}` to equation source is not supported by the Word exporter.
|
|
166
165
|
|
|
167
|
-
`fig` and `
|
|
166
|
+
`fig`, `tbl`, and `eq` are built-in reference types alongside `sec`. Grouped references use the shared planner's plural prefixes and separators, such as "Figures 1 and 2".
|
|
168
167
|
|
|
169
|
-
Reference targets
|
|
168
|
+
Reference targets include headings, paragraphs, figures, tables, equations, spans, and definition terms with ids. Paragraphs support text and page references; spans and definition terms support text and page references. Target and variant validation belongs to mdhtml.
|
|
170
169
|
|
|
171
170
|
## Validation
|
|
172
171
|
|
|
@@ -1,34 +1,33 @@
|
|
|
1
1
|
# mdhtml2docx
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Write [mdhtml](https://github.com/AnswerDotAI/mdhtml) documents as Word docx files.
|
|
4
4
|
|
|
5
|
-
`
|
|
5
|
+
`mdhtml2docx` consumes mdhtml's semantic `Document` API. The shared importer handles HTML5 parsing, raw-data decoding, and document structure. The shared reference index validates and plans references. `oxml` provides WordprocessingML construction, reference editing, DOCX packaging, and atomic saving. XML construction uses namespace-bound factories such as `e.tcW(type='dxa', w=2400)`, backed by fastcore's XML builder. Conversion requires neither lxml nor a .NET or Office runtime.
|
|
6
6
|
|
|
7
|
-
MDHTML accepts the full HTML vocabulary. This exporter supports the elements and annotations listed below. It preserves the text of unknown inline elements and recurses through block containers.
|
|
7
|
+
MDHTML accepts the full HTML vocabulary. This exporter supports the elements and annotations listed below. It preserves the text of unknown inline elements and recurses through block containers. HTML parsing and repair belong to `mdhtml`.
|
|
8
8
|
|
|
9
9
|
## Usage
|
|
10
10
|
|
|
11
11
|
```python
|
|
12
|
-
from mdhtml import
|
|
12
|
+
from mdhtml import parse_md
|
|
13
13
|
from mdhtml2docx import mdhtml2docx
|
|
14
14
|
|
|
15
|
-
|
|
16
|
-
warnings = mdhtml2docx(
|
|
15
|
+
document = parse_md(markdown_text)
|
|
16
|
+
warnings = mdhtml2docx(document, 'out.docx')
|
|
17
17
|
```
|
|
18
18
|
|
|
19
|
-
`mdhtml2docx`
|
|
19
|
+
`mdhtml2docx` accepts a semantic `Document` or symbolic MDHTML, writes a docx, and returns warning strings. Normal HTML5 repair applies when importing MDHTML; the input need not be well-formed XML.
|
|
20
20
|
|
|
21
|
-
|
|
21
|
+
Import existing MDHTML through the same document API:
|
|
22
22
|
|
|
23
23
|
```python
|
|
24
|
-
from mdhtml import
|
|
24
|
+
from mdhtml import Document
|
|
25
25
|
|
|
26
|
-
document =
|
|
27
|
-
document.children[0].attrs['custom-style'] = 'Contract Title'
|
|
26
|
+
document = Document.from_mdhtml(html)
|
|
28
27
|
warnings = mdhtml2docx(document, 'out.docx')
|
|
29
28
|
```
|
|
30
29
|
|
|
31
|
-
|
|
30
|
+
The importer groups body-position text and phrasing content into paragraphs. The exporter reads ordinary dictionary snapshots of the semantic blocks, inlines, and note definitions. Operation arguments remain inert unless an operation renderer selects them.
|
|
32
31
|
|
|
33
32
|
## Supported content
|
|
34
33
|
|
|
@@ -46,6 +45,8 @@ A `details` div becomes its label as a bold line above the body. It does not bec
|
|
|
46
45
|
|
|
47
46
|
Math spans and blocks become native Word math zones (`m:oMath`). These contain the source text unchanged, whether TeX, UnicodeMath, or AsciiMath. Math build-up and rendering are left to downstream tools. Pandoc reads the zones back as math.
|
|
48
47
|
|
|
48
|
+
An id-bearing display equation receives a live `Equation N` caption below its math zone. Unlabelled equations remain unnumbered.
|
|
49
|
+
|
|
49
50
|
When [fastpylight](https://github.com/AnswerDotAI/fastpylight) is installed, code blocks with a language use syntax coloring. Tokens receive `Hl*` character styles such as `Hl Keyword`. Their colors come from the reference theme described below. You can restyle them in Word.
|
|
50
51
|
|
|
51
52
|
## Styling
|
|
@@ -89,13 +90,7 @@ The converter accepts MDHTML raw data with `data-format="docx"`:
|
|
|
89
90
|
|
|
90
91
|
A ```` ```{=docx} ```` fenced block in Markdown, or inline code followed by `{=docx}`, produces this element. The payload is parsed as WordprocessingML and inserted verbatim. Block payloads contain elements such as `w:p` or `w:tbl`. Inline payloads contain elements such as `w:r`. The prefixes `w`, `r`, `wp`, `a`, `pic`, and `m` are predeclared.
|
|
91
92
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
- No `data-encoding`: use the literal payload.
|
|
95
|
-
- `data-encoding="html"`: perform one character-reference decoding pass.
|
|
96
|
-
- `data-encoding="base64"`: decode a base64-encoded UTF-8 payload.
|
|
97
|
-
|
|
98
|
-
Malformed payloads and unknown encodings are dropped with a warning. Raw data for other formats is skipped silently.
|
|
93
|
+
The shared mdhtml importer decodes payloads according to its raw-data contract. This exporter parses decoded `docx` payloads as WordprocessingML. Invalid encodings and malformed XML produce warnings. Other formats are skipped.
|
|
99
94
|
|
|
100
95
|
For example, insert a page break with:
|
|
101
96
|
|
|
@@ -105,6 +100,8 @@ For example, insert a page break with:
|
|
|
105
100
|
|
|
106
101
|
## Cross-references
|
|
107
102
|
|
|
103
|
+
Bibliographies and citations use mdhtml's shared Rust preparation. `::: bibliography` entries contain citation-author terms with ids and optional `year` attributes, followed by their formatted definitions. Citations use `[@key]`, narrative `{ref=text}`, author-suppressing `-@`, and per-item prefix/suffix text. `cite_style='numeric'` selects numbers in bibliography order; `'author-year'` selects author-year text. None inherits frontmatter. Citations become static linked text and bibliography entries retain their bookmarks. Section, page, figure and table cross-references remain native Word fields. Re-export the original document to change citation style.
|
|
104
|
+
|
|
108
105
|
Markdown references such as `[@sec-payment]` become live Word REF fields. MDHTML represents them as `a` elements with `data-ref`.
|
|
109
106
|
|
|
110
107
|
The default field is `REF <bookmark> \w \h`. It displays the full-context paragraph number, such as "3.(c)(iii)", as a hyperlink. The converter sets `updateFields` in `settings.xml` so Word refreshes fields on open.
|
|
@@ -125,7 +122,7 @@ A missing target id raises a conversion error. A reference type with no defined
|
|
|
125
122
|
|
|
126
123
|
Number fields require numbered headings. If your reference docx already numbers its heading styles, the converter leaves that numbering unchanged.
|
|
127
124
|
|
|
128
|
-
|
|
125
|
+
The shared reference index resolves `number_headings` against document metadata. Numeric heading references infer decimal numbering when no scheme is supplied. You can select a scheme explicitly:
|
|
129
126
|
|
|
130
127
|
- `'legal'` uses 1. / (a) / (i) numbering.
|
|
131
128
|
- `'decimal'` uses 1. / 1.1. / 1.1.1. numbering.
|
|
@@ -135,15 +132,17 @@ The named schemes come from mdhtml's `SCHEMES`. The custom dictionary uses the s
|
|
|
135
132
|
|
|
136
133
|
A reference-list entry ending in `.xml` can also supply styles and numbering. It contains raw `w:style`, `w:abstractNum`, and `w:num` elements. Each contributor's ids and references are remapped before merging, so separate contributors can reuse the same original numbering ids; later styles still win.
|
|
137
134
|
|
|
138
|
-
### Figures and
|
|
135
|
+
### Figures, tables, and equations
|
|
136
|
+
|
|
137
|
+
Figures, captioned tables, and id-bearing display equations use independent live SEQ fields. A figure's caption appears below its body as "Figure 1: caption". A table's caption appears above it as "Table 1: caption". Equations receive an "Equation 1" caption below the math zone. Labels come from `reftypes`; changing a label never merges the counters.
|
|
139
138
|
|
|
140
|
-
|
|
139
|
+
When the element has an id, the converter bookmarks its label, number, and authored caption separately. The shared reference planner supplies prefix words; each Word REF field supplies only the live number. Thus `[@fig-plot]` displays "Figure 1", while `[-@tbl-stages]` displays only the number.
|
|
141
140
|
|
|
142
|
-
|
|
141
|
+
Figure and table `{ref=text}` references use a separate bookmark containing the authored caption text. Generated bookmark names are reserved through the same collision-safe allocator as authored targets, including forward references. Equation references support full labels, bare numbers, and pages. `{ref=text}` to equation source is not supported by the Word exporter.
|
|
143
142
|
|
|
144
|
-
`fig` and `
|
|
143
|
+
`fig`, `tbl`, and `eq` are built-in reference types alongside `sec`. Grouped references use the shared planner's plural prefixes and separators, such as "Figures 1 and 2".
|
|
145
144
|
|
|
146
|
-
Reference targets
|
|
145
|
+
Reference targets include headings, paragraphs, figures, tables, equations, spans, and definition terms with ids. Paragraphs support text and page references; spans and definition terms support text and page references. Target and variant validation belongs to mdhtml.
|
|
147
146
|
|
|
148
147
|
## Validation
|
|
149
148
|
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# Autogenerated by fastship
|
|
2
|
+
|
|
3
|
+
d = { 'settings': { 'branch': 'main',
|
|
4
|
+
'lib_path': 'mdhtml2docx',
|
|
5
|
+
'git_url': 'https://github.com/AnswerDotAI/mdhtml2docx',
|
|
6
|
+
'doc_host': 'https://AnswerDotAI.github.io',
|
|
7
|
+
'doc_baseurl': '/mdhtml2docx'},
|
|
8
|
+
'syms': { 'mdhtml2docx.mdhtml2docx': { 'mdhtml2docx.mdhtml2docx': ('api/mdhtml2docx.html', 'mdhtml2docx/mdhtml2docx.py'),
|
|
9
|
+
'mdhtml2docx.mdhtml2docx.mdhtml2docx': ( 'api/mdhtml2docx.html#mdhtml2docx',
|
|
10
|
+
'mdhtml2docx/mdhtml2docx.py'),
|
|
11
|
+
'mdhtml2docx.mdhtml2docx.mustache_fields': ( 'api/mdhtml2docx.html#mustache_fields',
|
|
12
|
+
'mdhtml2docx/mdhtml2docx.py')},
|
|
13
|
+
'mdhtml2docx.styles': { 'mdhtml2docx.styles': ('api/styles.html', 'mdhtml2docx/styles.py'),
|
|
14
|
+
'mdhtml2docx.styles.style_id': ('api/styles.html#style_id', 'mdhtml2docx/styles.py'),
|
|
15
|
+
'mdhtml2docx.styles.ref_path': ('api/styles.html#ref_path', 'mdhtml2docx/styles.py'),
|
|
16
|
+
'mdhtml2docx.styles.theme_styles': ('api/styles.html#theme_styles', 'mdhtml2docx/styles.py'),
|
|
17
|
+
'mdhtml2docx.styles.theme_ref': ('api/styles.html#theme_ref', 'mdhtml2docx/styles.py')},
|
|
18
|
+
'mdhtml2docx.hilite': { 'mdhtml2docx.hilite': ('api/hilite.html', 'mdhtml2docx/hilite.py'),
|
|
19
|
+
'mdhtml2docx.hilite.segments': ('api/hilite.html#segments', 'mdhtml2docx/hilite.py')},
|
|
20
|
+
'mdhtml2docx.wml': { 'mdhtml2docx.wml': ('api/wml.html', 'mdhtml2docx/wml.py'),
|
|
21
|
+
'mdhtml2docx.wml.E': ('api/wml.html#e', 'mdhtml2docx/wml.py'),
|
|
22
|
+
'mdhtml2docx.wml.E.attr_value': ('api/wml.html#e.attr_value', 'mdhtml2docx/wml.py'),
|
|
23
|
+
'mdhtml2docx.wml.e': ('api/wml.html#e', 'mdhtml2docx/wml.py'),
|
|
24
|
+
'mdhtml2docx.wml.twips': ('api/wml.html#twips', 'mdhtml2docx/wml.py'),
|
|
25
|
+
'mdhtml2docx.wml.parse_tracks': ('api/wml.html#parse_tracks', 'mdhtml2docx/wml.py')}}}
|
|
26
|
+
|
|
27
|
+
docs = {'API': ['mdhtml2docx.mdhtml2docx', 'mdhtml2docx.styles', 'mdhtml2docx.hilite', 'mdhtml2docx.wml']}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"""Optional syntax tokenization for code blocks via `fastpylight`.
|
|
2
|
+
|
|
3
|
+
When `fastpylight` is absent or the language is unknown, `segments` returns `None` so callers fall back to plain runs. Colors live in the reference document's `Hl*` character styles (`mdhtml2docx.styles.theme_styles`).
|
|
4
|
+
"""
|
|
5
|
+
# - order: 3
|
|
6
|
+
# - title: Syntax tokenization
|
|
7
|
+
|
|
8
|
+
# theme_colors is probed too: a pre-theme_colors fastpylight is treated as absent, not half-working
|
|
9
|
+
try: from fastpylight import tokenize, theme_colors # chkstyle: ignore
|
|
10
|
+
except ImportError: tokenize = None
|
|
11
|
+
|
|
12
|
+
__all__ = ['segments']
|
|
13
|
+
|
|
14
|
+
def segments(
|
|
15
|
+
code: str, # Source code text to tokenize
|
|
16
|
+
lang: str | None, # Language identifier (e.g. `'python'`, `'rust'`)
|
|
17
|
+
) -> list[tuple[str, str | None]] | None: # `(text, scope)` pairs, or `None` when `fastpylight` or `lang` is unavailable
|
|
18
|
+
"""Split `code` into `(text, scope)` pairs using `fastpylight` byte-offset tokens, or `None` when unavailable.
|
|
19
|
+
|
|
20
|
+
```{python}
|
|
21
|
+
segs = segments('x = 1', 'python')
|
|
22
|
+
assert segs is None or ''.join(t for t, _ in segs) == 'x = 1'
|
|
23
|
+
```
|
|
24
|
+
"""
|
|
25
|
+
if tokenize is None or not lang: return None
|
|
26
|
+
try: toks = tokenize(code, lang)
|
|
27
|
+
except ValueError: return None
|
|
28
|
+
b = code.encode()
|
|
29
|
+
out, pos = [], 0
|
|
30
|
+
def emit(s, e, scope=None):
|
|
31
|
+
if e > s: out.append((b[s:e].decode(), scope))
|
|
32
|
+
for s, e, scope in toks:
|
|
33
|
+
emit(pos, s)
|
|
34
|
+
emit(s, e, scope)
|
|
35
|
+
pos = e
|
|
36
|
+
emit(pos, len(b))
|
|
37
|
+
return out
|