dtclib 0.1.0__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.
dtclib-0.1.0/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Matthias Gatto
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,11 @@
1
+ include Makefile
2
+ include pyproject.toml
3
+ include *.h
4
+ include README.md
5
+ include LICENSE
6
+ recursive-include tests *.py
7
+ include tests/html/test_attr.html
8
+ include tests/html/test_doctype.html
9
+ recursive-include examples *.py
10
+ recursive-exclude * __pycache__
11
+ recursive-exclude * *.pyc
dtclib-0.1.0/Makefile ADDED
@@ -0,0 +1,44 @@
1
+ VENV := .venv
2
+ PYTHON := $(VENV)/bin/python
3
+ PIP := $(VENV)/bin/pip
4
+
5
+ JSON_C_CFLAGS := $(shell pkg-config --cflags json-c)
6
+ JSON_C_LDFLAGS := $(shell pkg-config --libs json-c)
7
+
8
+ .PHONY: all venv pip-dev tests-py tests-c json-cli tests dist clean
9
+
10
+ all: json-cli
11
+
12
+ venv:
13
+ @test -d $(VENV) || python3 -m venv $(VENV)
14
+
15
+ json-cli: json-cli.c dtc-json-c.h dtclib.h
16
+ $(CC) json-cli.c -std=gnu23 $(JSON_C_CFLAGS) -I./ -o json-cli $(JSON_C_LDFLAGS)
17
+
18
+ pip-dev: venv
19
+ $(PIP) install -e '.[dev]' --force-reinstall
20
+
21
+ tests-py: pip-dev
22
+ $(PYTHON) -m pytest
23
+
24
+ tests-c: json-cli
25
+ @for f in tests/html/*.html; do \
26
+ if [ -f "$$f" ]; then \
27
+ echo "Testing $$f"; \
28
+ ./json-cli -f "$$f" > /dev/null || exit 1; \
29
+ fi; \
30
+ done
31
+
32
+ tests: tests-py tests-c
33
+
34
+ dist: venv
35
+ rm -rf build dist *.egg-info
36
+ $(PIP) install -q --upgrade build
37
+ $(PYTHON) -m build
38
+
39
+ clean:
40
+ rm -f json-cli
41
+ rm -rf build dist
42
+ rm -rf *.egg-info
43
+ find . -name '*.so' -delete
44
+ find . -type d -name __pycache__ -exec rm -rf {} +
dtclib-0.1.0/PKG-INFO ADDED
@@ -0,0 +1,159 @@
1
+ Metadata-Version: 2.4
2
+ Name: dtclib
3
+ Version: 0.1.0
4
+ Summary: Dom Tree Containerized: parse HTML into a JSON-like tree of dicts and lists
5
+ Author: Matthias Gatto
6
+ License-Expression: MIT
7
+ Project-URL: Homepage, https://github.com/cosmo-ray/DTClib
8
+ Project-URL: Repository, https://github.com/cosmo-ray/DTClib
9
+ Project-URL: Issues, https://github.com/cosmo-ray/DTClib/issues
10
+ Keywords: html,parser,dom,tree,json,c,header-only
11
+ Classifier: Development Status :: 4 - Beta
12
+ Classifier: Intended Audience :: Developers
13
+ Classifier: Operating System :: OS Independent
14
+ Classifier: Programming Language :: C
15
+ Classifier: Programming Language :: Python :: 3
16
+ Classifier: Programming Language :: Python :: 3 :: Only
17
+ Classifier: Programming Language :: Python :: 3.9
18
+ Classifier: Programming Language :: Python :: 3.10
19
+ Classifier: Programming Language :: Python :: 3.11
20
+ Classifier: Programming Language :: Python :: 3.12
21
+ Classifier: Programming Language :: Python :: 3.13
22
+ Classifier: Programming Language :: Python :: 3.14
23
+ Classifier: Topic :: Text Processing :: Markup :: HTML
24
+ Requires-Python: >=3.9
25
+ Description-Content-Type: text/markdown
26
+ License-File: LICENSE
27
+ Provides-Extra: dev
28
+ Requires-Dist: pytest>=8.3.5; extra == "dev"
29
+ Provides-Extra: browser
30
+ Requires-Dist: requests>=2.32.0; extra == "browser"
31
+ Requires-Dist: csonpath>=0.21.0; extra == "browser"
32
+ Dynamic: license-file
33
+
34
+ # DTClib
35
+
36
+ [![tests](https://github.com/cosmo-ray/DTClib/actions/workflows/test.yaml/badge.svg)](https://github.com/cosmo-ray/DTClib/actions/workflows/test.yaml)
37
+ [![pypi](https://img.shields.io/pypi/v/dtclib.svg)](https://pypi.org/project/dtclib/)
38
+ [![license: MIT](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)
39
+
40
+ **D**om **T**ree **C**ontainer — take your HTML and put it into a JSON-like tree,
41
+ or a Python `dict`/`list` tree, well, into any container.
42
+
43
+ DTClib is a header-only C parser for HTML-like documents. It is
44
+ backend-agnostic: the same parser can target different output representations
45
+ by defining a small set of backend macros.
46
+
47
+ Backends shipped in this repository:
48
+
49
+ - `dtc-python.h` — outputs Python `list`/`dict` objects via a C extension.
50
+ - `dtc-json-c.h` — outputs [`json-c`](https://github.com/json-c/json-c) objects.
51
+
52
+ ## Install
53
+
54
+ ```sh
55
+ pip install dtclib
56
+ ```
57
+
58
+ Prebuilt wheels are available for Linux (glibc and musl, x86_64), macOS
59
+ (arm64) and Windows (amd64, built with MinGW-w64). On other platforms the
60
+ extension is compiled from the source distribution, so a C compiler is
61
+ required.
62
+
63
+ ## Python API
64
+
65
+ ```python
66
+ import dtclib
67
+
68
+ tree = dtclib.parse('<p class="x">hi <b>there</b></p>')
69
+ ```
70
+
71
+ The result is a list of nodes, where each element is either:
72
+
73
+ - a `str` — a text node,
74
+ - a `dict` with the keys:
75
+ - `name` (`str`) — the tag name (`!doctype` for a doctype),
76
+ - `attributes` (`dict`, optional) — attribute name to value; a valueless
77
+ attribute such as `<input disabled>` maps to `True`,
78
+ - `content` (`list`, optional) — child nodes. Void elements
79
+ (`br`, `img`, `input`, …) have no `content` key.
80
+
81
+ Example:
82
+
83
+ ```python
84
+ >>> import dtclib
85
+ >>> dtclib.parse('<p class="x">hi <b>there</b></p>')
86
+ [{'name': 'p', 'attributes': {'class': 'x'}, 'content': ['hi ', {'name': 'b', 'content': ['there']}]}]
87
+ ```
88
+
89
+ On a parse error, `dtclib.parse` raises `ValueError` with line/column
90
+ information:
91
+
92
+ ```python
93
+ >>> dtclib.parse('<div><p></div>')
94
+ Traceback (most recent call last):
95
+ ...
96
+ ValueError: parse error at line 0 col 9: trying to close the wrong tag got 'div' instead of 'p' :(
97
+ ```
98
+
99
+ Comments (`<!-- ... -->`) are skipped. Unknown tag names are kept as text
100
+ instead of failing the parse.
101
+
102
+ ## C CLI (json-c backend)
103
+
104
+ Requires `json-c` and a C compiler:
105
+
106
+ ```sh
107
+ make json-cli
108
+ ./json-cli -f tests/html/gol.html
109
+ ```
110
+
111
+ ## Use the parser in your own backend
112
+
113
+ `dtclib.h` is header-only. Define the `DTC_*` macros expected by the parser
114
+ (see `dtc-python.h` or `dtc-json-c.h` for reference implementations), then:
115
+
116
+ ```c
117
+ #define DTCLIB_PREFIX mybackend
118
+ #include "dtclib.h"
119
+ ```
120
+
121
+ ## Example browser: cliowser
122
+
123
+ `examples/cliowser.py` is a tiny interactive text-mode browser that fetches a
124
+ page, parses it with `dtclib`, and uses
125
+ [`csonpath`](https://pypi.org/project/csonpath/) to inspect the tree:
126
+
127
+ ```sh
128
+ pip install 'dtclib[browser]'
129
+ python examples/cliowser.py https://example.com
130
+ ```
131
+
132
+ Commands inside the prompt:
133
+
134
+ - `p` — print page (URL, title, extracted text)
135
+ - `l` — list links found on the page
136
+ - `i` — show tag statistics (Python) and hrefs/srcs (csonpath)
137
+ - `r` — dump the raw parsed tree as JSON
138
+ - `m` — toggle between plain text and markdown rendering
139
+ - `g <n>` — navigate to link number `n`
140
+ - `b` — go back to the previous page
141
+ - `q` — quit
142
+
143
+ ## Development
144
+
145
+ ```sh
146
+ make pip-dev # creates .venv and installs the extension in editable mode
147
+ make tests-py # python tests
148
+ make tests-c # C smoke tests (needs json-c)
149
+ make tests # both
150
+ make dist # sdist + wheel in dist/
151
+ ```
152
+
153
+ Releases are cut with `./do-release X.Y.Z` then tagged; the `pypi.yaml`
154
+ workflow builds the wheels and publishes the tagged version with the
155
+ `PYPI_TOKEN` repository secret.
156
+
157
+ ## License
158
+
159
+ MIT — see [LICENSE](LICENSE).
dtclib-0.1.0/README.md ADDED
@@ -0,0 +1,126 @@
1
+ # DTClib
2
+
3
+ [![tests](https://github.com/cosmo-ray/DTClib/actions/workflows/test.yaml/badge.svg)](https://github.com/cosmo-ray/DTClib/actions/workflows/test.yaml)
4
+ [![pypi](https://img.shields.io/pypi/v/dtclib.svg)](https://pypi.org/project/dtclib/)
5
+ [![license: MIT](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)
6
+
7
+ **D**om **T**ree **C**ontainer — take your HTML and put it into a JSON-like tree,
8
+ or a Python `dict`/`list` tree, well, into any container.
9
+
10
+ DTClib is a header-only C parser for HTML-like documents. It is
11
+ backend-agnostic: the same parser can target different output representations
12
+ by defining a small set of backend macros.
13
+
14
+ Backends shipped in this repository:
15
+
16
+ - `dtc-python.h` — outputs Python `list`/`dict` objects via a C extension.
17
+ - `dtc-json-c.h` — outputs [`json-c`](https://github.com/json-c/json-c) objects.
18
+
19
+ ## Install
20
+
21
+ ```sh
22
+ pip install dtclib
23
+ ```
24
+
25
+ Prebuilt wheels are available for Linux (glibc and musl, x86_64), macOS
26
+ (arm64) and Windows (amd64, built with MinGW-w64). On other platforms the
27
+ extension is compiled from the source distribution, so a C compiler is
28
+ required.
29
+
30
+ ## Python API
31
+
32
+ ```python
33
+ import dtclib
34
+
35
+ tree = dtclib.parse('<p class="x">hi <b>there</b></p>')
36
+ ```
37
+
38
+ The result is a list of nodes, where each element is either:
39
+
40
+ - a `str` — a text node,
41
+ - a `dict` with the keys:
42
+ - `name` (`str`) — the tag name (`!doctype` for a doctype),
43
+ - `attributes` (`dict`, optional) — attribute name to value; a valueless
44
+ attribute such as `<input disabled>` maps to `True`,
45
+ - `content` (`list`, optional) — child nodes. Void elements
46
+ (`br`, `img`, `input`, …) have no `content` key.
47
+
48
+ Example:
49
+
50
+ ```python
51
+ >>> import dtclib
52
+ >>> dtclib.parse('<p class="x">hi <b>there</b></p>')
53
+ [{'name': 'p', 'attributes': {'class': 'x'}, 'content': ['hi ', {'name': 'b', 'content': ['there']}]}]
54
+ ```
55
+
56
+ On a parse error, `dtclib.parse` raises `ValueError` with line/column
57
+ information:
58
+
59
+ ```python
60
+ >>> dtclib.parse('<div><p></div>')
61
+ Traceback (most recent call last):
62
+ ...
63
+ ValueError: parse error at line 0 col 9: trying to close the wrong tag got 'div' instead of 'p' :(
64
+ ```
65
+
66
+ Comments (`<!-- ... -->`) are skipped. Unknown tag names are kept as text
67
+ instead of failing the parse.
68
+
69
+ ## C CLI (json-c backend)
70
+
71
+ Requires `json-c` and a C compiler:
72
+
73
+ ```sh
74
+ make json-cli
75
+ ./json-cli -f tests/html/gol.html
76
+ ```
77
+
78
+ ## Use the parser in your own backend
79
+
80
+ `dtclib.h` is header-only. Define the `DTC_*` macros expected by the parser
81
+ (see `dtc-python.h` or `dtc-json-c.h` for reference implementations), then:
82
+
83
+ ```c
84
+ #define DTCLIB_PREFIX mybackend
85
+ #include "dtclib.h"
86
+ ```
87
+
88
+ ## Example browser: cliowser
89
+
90
+ `examples/cliowser.py` is a tiny interactive text-mode browser that fetches a
91
+ page, parses it with `dtclib`, and uses
92
+ [`csonpath`](https://pypi.org/project/csonpath/) to inspect the tree:
93
+
94
+ ```sh
95
+ pip install 'dtclib[browser]'
96
+ python examples/cliowser.py https://example.com
97
+ ```
98
+
99
+ Commands inside the prompt:
100
+
101
+ - `p` — print page (URL, title, extracted text)
102
+ - `l` — list links found on the page
103
+ - `i` — show tag statistics (Python) and hrefs/srcs (csonpath)
104
+ - `r` — dump the raw parsed tree as JSON
105
+ - `m` — toggle between plain text and markdown rendering
106
+ - `g <n>` — navigate to link number `n`
107
+ - `b` — go back to the previous page
108
+ - `q` — quit
109
+
110
+ ## Development
111
+
112
+ ```sh
113
+ make pip-dev # creates .venv and installs the extension in editable mode
114
+ make tests-py # python tests
115
+ make tests-c # C smoke tests (needs json-c)
116
+ make tests # both
117
+ make dist # sdist + wheel in dist/
118
+ ```
119
+
120
+ Releases are cut with `./do-release X.Y.Z` then tagged; the `pypi.yaml`
121
+ workflow builds the wheels and publishes the tagged version with the
122
+ `PYPI_TOKEN` repository secret.
123
+
124
+ ## License
125
+
126
+ MIT — see [LICENSE](LICENSE).
@@ -0,0 +1,128 @@
1
+ #ifndef DTCLIB_JSON_C_H_
2
+ #define DTCLIB_JSON_C_H_
3
+
4
+ #include "json.h"
5
+ #include <string.h>
6
+
7
+ #define DTC_UNLIKELY(args...) args
8
+
9
+ #define DTC_NULL NULL
10
+
11
+ #define DTC_IS_NULL(c) !(c)
12
+
13
+ #define DTCLIB_PREFIX json_c
14
+
15
+ #define DTC_NEW_ARRAY() json_object_new_array()
16
+
17
+ static int DTC_TAG_NAME(struct json_object *obj_cnt, char name[static 1],
18
+ size_t name_l)
19
+ {
20
+ json_object_object_add(obj_cnt, "name", json_object_new_string_len(name, name_l));
21
+ return 0;
22
+ }
23
+
24
+ static struct json_object *DTC_NEW_OBJECT_OBJECT(struct json_object *obj_cnt, char name[static 1])
25
+ {
26
+ json_object *r = json_object_new_object();
27
+ json_object_object_add(obj_cnt, name, r);
28
+ return r;
29
+ }
30
+
31
+ static struct json_object *DTC_STORE_STRL_KEYL(struct json_object *obj_cnt,
32
+ size_t nl, char name[static nl],
33
+ size_t vl, char val[static vl])
34
+ {
35
+ char tmp_buf[16];
36
+ char *tmp = nl + 1 < sizeof tmp_buf ? tmp_buf : malloc(nl+1);
37
+ memcpy(tmp, name, nl);
38
+ tmp[nl] = 0;
39
+ json_object *r = json_object_new_string_len(val, vl - 1);
40
+ if (r)
41
+ json_object_object_add(obj_cnt, tmp, r);
42
+ if (tmp != tmp_buf)
43
+ free(tmp);
44
+ return r;
45
+ }
46
+
47
+ static struct json_object *DTC_STORE_BOOL_KEYL(struct json_object *obj_cnt,
48
+ size_t nl, char name[static nl],
49
+ _Bool val)
50
+ {
51
+ char tmp_buf[16];
52
+ char *tmp = nl + 1 < sizeof tmp_buf ? tmp_buf : malloc(nl+1);
53
+ if (!tmp)
54
+ return NULL;
55
+ memcpy(tmp, name, nl);
56
+ tmp[nl] = 0;
57
+ json_object *r = json_object_new_boolean(val);
58
+ if (r)
59
+ json_object_object_add(obj_cnt, tmp, r);
60
+
61
+ if (tmp != tmp_buf)
62
+ free(tmp);
63
+ return r;
64
+ }
65
+
66
+ static struct json_object *DTC_STORE_STRL_KEY(struct json_object *obj_cnt,
67
+ char key[static 1],
68
+ size_t vl, char val[static vl])
69
+ {
70
+ json_object *r = json_object_new_string_len(val, vl - 1);
71
+ if (!r)
72
+ return NULL;
73
+ json_object_object_add(obj_cnt, key, r);
74
+ return r;
75
+ }
76
+
77
+ static struct json_object *DTC_STORE_ARRAY(struct json_object *obj_cnt,
78
+ char key[static 1])
79
+ {
80
+ json_object *r = json_object_new_array();
81
+ if (DTC_UNLIKELY(!r))
82
+ return NULL;
83
+ json_object_object_add(obj_cnt, key, r);
84
+ return r;
85
+ }
86
+
87
+
88
+ static struct json_object *DTC_PUSH_STRL(struct json_object *parent_array, size_t vl, char val[static vl])
89
+ {
90
+ json_object *r = json_object_new_string_len(val, vl - 1);
91
+ if (DTC_UNLIKELY(!r))
92
+ return NULL;
93
+ size_t s = json_object_array_length(parent_array);
94
+ if (DTC_UNLIKELY(json_object_array_put_idx(parent_array, s, r) < 0)) {
95
+ json_object_put(r);
96
+ return NULL;
97
+ }
98
+ return r;
99
+ }
100
+
101
+ static struct json_object *DTC_NEW_OBJECT(struct json_object *parent_array)
102
+ {
103
+ json_object *r = json_object_new_object();
104
+ if (DTC_UNLIKELY(!r))
105
+ return NULL;
106
+ size_t s = json_object_array_length(parent_array);
107
+ if (DTC_UNLIKELY(json_object_array_put_idx(parent_array, s, r) < 0)) {
108
+ json_object_put(r);
109
+ return NULL;
110
+ }
111
+ return r;
112
+ }
113
+
114
+ static int DTC_POP(struct json_object *parent_array)
115
+ {
116
+ size_t size = json_object_array_length(parent_array);
117
+ if (DTC_UNLIKELY(size == 0))
118
+ return -1;
119
+ return json_object_array_del_idx(parent_array, size - 1, 1);
120
+ }
121
+
122
+ #define DTC_FREE json_object_put
123
+
124
+ #define DTC_PTR struct json_object *
125
+
126
+ #include "dtclib.h"
127
+
128
+ #endif
@@ -0,0 +1,166 @@
1
+ #ifndef DTCLIB_PYTHON_H_
2
+ #define DTCLIB_PYTHON_H_
3
+
4
+ #include <Python.h>
5
+ #include <string.h>
6
+
7
+ /* Python holds the GIL during parsing, so a non-thread-local error buffer is
8
+ * safe and avoids depending on C11 thread_local support. */
9
+ #define DTC_NO_THREAD
10
+
11
+ #define DTC_UNLIKELY(args...) args
12
+
13
+ #define DTC_NULL NULL
14
+
15
+ #define DTC_IS_NULL(c) !(c)
16
+
17
+ #define DTCLIB_PREFIX python
18
+
19
+ #define DTC_PTR PyObject *
20
+
21
+ #define DTC_FREE Py_DECREF
22
+
23
+ #define DTC_NEW_ARRAY() PyList_New(0)
24
+
25
+ static PyObject *dtc_python_new_object(PyObject *parent_array)
26
+ {
27
+ PyObject *r = PyDict_New();
28
+ if (DTC_UNLIKELY(!r))
29
+ return NULL;
30
+ if (DTC_UNLIKELY(PyList_Append(parent_array, r) < 0)) {
31
+ Py_DECREF(r);
32
+ return NULL;
33
+ }
34
+ Py_DECREF(r);
35
+ return r;
36
+ }
37
+ #define DTC_NEW_OBJECT(parent_array) dtc_python_new_object(parent_array)
38
+
39
+ static int dtc_python_tag_name(PyObject *obj_cnt, char name[static 1],
40
+ size_t name_l)
41
+ {
42
+ PyObject *s = PyUnicode_FromStringAndSize(name, name_l);
43
+ if (DTC_UNLIKELY(!s))
44
+ return -1;
45
+ int ret = PyDict_SetItemString(obj_cnt, "name", s);
46
+ Py_DECREF(s);
47
+ return ret;
48
+ }
49
+ #define DTC_TAG_NAME(obj_cnt, name, name_l) dtc_python_tag_name(obj_cnt, name, name_l)
50
+
51
+ static PyObject *dtc_python_new_object_object(PyObject *obj_cnt, char name[static 1])
52
+ {
53
+ PyObject *r = PyDict_New();
54
+ if (DTC_UNLIKELY(!r))
55
+ return NULL;
56
+ if (DTC_UNLIKELY(PyDict_SetItemString(obj_cnt, name, r) < 0)) {
57
+ Py_DECREF(r);
58
+ return NULL;
59
+ }
60
+ Py_DECREF(r);
61
+ return r;
62
+ }
63
+ #define DTC_NEW_OBJECT_OBJECT(obj_cnt, name) dtc_python_new_object_object(obj_cnt, name)
64
+
65
+ static PyObject *dtc_python_store_strl_keyl(PyObject *obj_cnt,
66
+ size_t nl, char name[static nl],
67
+ size_t vl, char val[static vl])
68
+ {
69
+ PyObject *key = PyUnicode_FromStringAndSize(name, nl);
70
+ if (DTC_UNLIKELY(!key))
71
+ return NULL;
72
+ PyObject *value = PyUnicode_FromStringAndSize(val, vl - 1);
73
+ if (DTC_UNLIKELY(!value)) {
74
+ Py_DECREF(key);
75
+ return NULL;
76
+ }
77
+ int ret = PyDict_SetItem(obj_cnt, key, value);
78
+ Py_DECREF(key);
79
+ Py_DECREF(value);
80
+ if (DTC_UNLIKELY(ret < 0))
81
+ return NULL;
82
+ return value;
83
+ }
84
+ #define DTC_STORE_STRL_KEYL(obj_cnt, nl, name, vl, val) \
85
+ dtc_python_store_strl_keyl(obj_cnt, nl, name, vl, val)
86
+
87
+ static PyObject *dtc_python_store_bool_keyl(PyObject *obj_cnt,
88
+ size_t nl, char name[static nl],
89
+ _Bool val)
90
+ {
91
+ PyObject *key = PyUnicode_FromStringAndSize(name, nl);
92
+ if (DTC_UNLIKELY(!key))
93
+ return NULL;
94
+ PyObject *value = PyBool_FromLong(val ? 1 : 0);
95
+ if (DTC_UNLIKELY(!value)) {
96
+ Py_DECREF(key);
97
+ return NULL;
98
+ }
99
+ int ret = PyDict_SetItem(obj_cnt, key, value);
100
+ Py_DECREF(key);
101
+ Py_DECREF(value);
102
+ if (DTC_UNLIKELY(ret < 0))
103
+ return NULL;
104
+ return value;
105
+ }
106
+ #define DTC_STORE_BOOL_KEYL(obj_cnt, nl, name, val) \
107
+ dtc_python_store_bool_keyl(obj_cnt, nl, name, val)
108
+
109
+ static PyObject *dtc_python_store_strl_key(PyObject *obj_cnt,
110
+ char key[static 1],
111
+ size_t vl, char val[static vl])
112
+ {
113
+ PyObject *value = PyUnicode_FromStringAndSize(val, vl - 1);
114
+ if (DTC_UNLIKELY(!value))
115
+ return NULL;
116
+ int ret = PyDict_SetItemString(obj_cnt, key, value);
117
+ Py_DECREF(value);
118
+ if (DTC_UNLIKELY(ret < 0))
119
+ return NULL;
120
+ return value;
121
+ }
122
+ #define DTC_STORE_STRL_KEY(obj_cnt, key, vl, val) \
123
+ dtc_python_store_strl_key(obj_cnt, key, vl, val)
124
+
125
+ static PyObject *dtc_python_store_array(PyObject *obj_cnt,
126
+ char key[static 1])
127
+ {
128
+ PyObject *r = PyList_New(0);
129
+ if (DTC_UNLIKELY(!r))
130
+ return NULL;
131
+ if (DTC_UNLIKELY(PyDict_SetItemString(obj_cnt, key, r) < 0)) {
132
+ Py_DECREF(r);
133
+ return NULL;
134
+ }
135
+ Py_DECREF(r);
136
+ return r;
137
+ }
138
+ #define DTC_STORE_ARRAY(obj_cnt, key) dtc_python_store_array(obj_cnt, key)
139
+
140
+ static PyObject *dtc_python_push_strl(PyObject *parent_array, size_t vl,
141
+ char val[static vl])
142
+ {
143
+ PyObject *r = PyUnicode_FromStringAndSize(val, vl - 1);
144
+ if (DTC_UNLIKELY(!r))
145
+ return NULL;
146
+ if (DTC_UNLIKELY(PyList_Append(parent_array, r) < 0)) {
147
+ Py_DECREF(r);
148
+ return NULL;
149
+ }
150
+ Py_DECREF(r);
151
+ return r;
152
+ }
153
+ #define DTC_PUSH_STRL(parent_array, vl, val) dtc_python_push_strl(parent_array, vl, val)
154
+
155
+ static int dtc_python_pop(PyObject *parent_array)
156
+ {
157
+ Py_ssize_t size = PyList_GET_SIZE(parent_array);
158
+ if (DTC_UNLIKELY(size <= 0))
159
+ return -1;
160
+ return PyList_SetSlice(parent_array, size - 1, size, NULL);
161
+ }
162
+ #define DTC_POP(parent_array) dtc_python_pop(parent_array)
163
+
164
+ #include "dtclib.h"
165
+
166
+ #endif
@@ -0,0 +1,50 @@
1
+ #include "dtc-python.h"
2
+
3
+ static PyObject *dtclib_parse(PyObject *self, PyObject *args)
4
+ {
5
+ const char *html;
6
+ struct dtc_error_ctx errctx = {};
7
+ PyObject *ret;
8
+
9
+ (void)self;
10
+
11
+ if (!PyArg_ParseTuple(args, "s", &html))
12
+ return NULL;
13
+
14
+ ret = dtc_python_parse((char *)html, &errctx);
15
+ if (!ret) {
16
+ if (PyErr_Occurred())
17
+ return NULL;
18
+ PyErr_Format(PyExc_ValueError,
19
+ "parse error at line %d col %d: %s",
20
+ errctx.line, errctx.col,
21
+ errctx.err ? errctx.err : "unknown error");
22
+ return NULL;
23
+ }
24
+
25
+ if (PyErr_Occurred()) {
26
+ Py_DECREF(ret);
27
+ return NULL;
28
+ }
29
+
30
+ return ret;
31
+ }
32
+
33
+ static PyMethodDef dtclib_methods[] = {
34
+ {"parse", dtclib_parse, METH_VARARGS,
35
+ "Parse HTML string and return a Python list/dict structure."},
36
+ {NULL, NULL, 0, NULL}
37
+ };
38
+
39
+ static struct PyModuleDef dtclib_module = {
40
+ PyModuleDef_HEAD_INIT,
41
+ "dtclib",
42
+ "DTClib Python backend",
43
+ -1,
44
+ dtclib_methods
45
+ };
46
+
47
+ PyMODINIT_FUNC PyInit_dtclib(void)
48
+ {
49
+ return PyModule_Create(&dtclib_module);
50
+ }