ym-handcalcs 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.
- ym_handcalcs-0.1.0/LICENSE +201 -0
- ym_handcalcs-0.1.0/PKG-INFO +101 -0
- ym_handcalcs-0.1.0/README.md +87 -0
- ym_handcalcs-0.1.0/pyproject.toml +31 -0
- ym_handcalcs-0.1.0/src/ym_handcalcs/__init__.py +17 -0
- ym_handcalcs-0.1.0/src/ym_handcalcs/block.py +75 -0
- ym_handcalcs-0.1.0/src/ym_handcalcs/breaking.py +133 -0
- ym_handcalcs-0.1.0/src/ym_handcalcs/flowables.py +829 -0
- ym_handcalcs-0.1.0/src/ym_handcalcs/fonts.py +121 -0
- ym_handcalcs-0.1.0/src/ym_handcalcs/markers.py +60 -0
- ym_handcalcs-0.1.0/src/ym_handcalcs/pdf.py +79 -0
- ym_handcalcs-0.1.0/src/ym_handcalcs/renderer.py +490 -0
- ym_handcalcs-0.1.0/src/ym_handcalcs/styles.py +279 -0
- ym_handcalcs-0.1.0/src/ym_handcalcs/symbols.py +233 -0
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: ym-handcalcs
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: ym_handcalcs: renders handcalcs to ReportLab, and the `_hc` block for ymprint.
|
|
5
|
+
Author-email: Connor Ferster <connor@structuralpython.com>
|
|
6
|
+
Requires-Python: >=3.14
|
|
7
|
+
Description-Content-Type: text/markdown
|
|
8
|
+
License-Expression: Apache-2.0
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Requires-Dist: handcalcs>=2.0.0rc1
|
|
11
|
+
Requires-Dist: reportlab>=4.5.1
|
|
12
|
+
Project-URL: Homepage, https://github.com/StructuralPython/ym-handcalcs
|
|
13
|
+
|
|
14
|
+
# ym-handcalcs
|
|
15
|
+
|
|
16
|
+
Render [handcalcs](https://github.com/connorferster/handcalcs) calculations in
|
|
17
|
+
[YMPrint](https://github.com/StructuralPython/ymprint) documents.
|
|
18
|
+
|
|
19
|
+
ym-handcalcs is a handcalcs renderer that produces ReportLab flowables, plus the `_hc`
|
|
20
|
+
block that puts them in a YMPrint document: write Python, get typeset calculations
|
|
21
|
+
with symbolic, substituted and computed values.
|
|
22
|
+
|
|
23
|
+
## Installation
|
|
24
|
+
|
|
25
|
+
Install it with YMPrint's `hc` extra:
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
pip install "ymprint[hc]"
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
ym-handcalcs needs handcalcs 2.0, which is currently a pre-release. pip installs it
|
|
32
|
+
automatically; uv only installs a pre-release you name, so add it with `--with`:
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
uv tool install "ymprint[hc]" --with "handcalcs>=2.0.0rc1"
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
YMPrint registers the `_hc` block automatically when ym-handcalcs is installed.
|
|
39
|
+
|
|
40
|
+
## The `_hc` block
|
|
41
|
+
|
|
42
|
+
```yaml
|
|
43
|
+
_vars:
|
|
44
|
+
w_DL: 2.5
|
|
45
|
+
w_LL: 4.0
|
|
46
|
+
|
|
47
|
+
Beam design:
|
|
48
|
+
- _hc: |
|
|
49
|
+
## Factored load
|
|
50
|
+
L = 6.0 # span, m
|
|
51
|
+
w_f = 1.25 * w_DL + 1.5 * w_LL
|
|
52
|
+
M_f = w_f * L**2 / 8
|
|
53
|
+
- The factored moment is {{ M_f }} kN·m.
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
The source runs in the document's `_vars`, like a `_py` block: it can use values from
|
|
57
|
+
`_vars` and earlier blocks, and later blocks (and `{{ }}` interpolation) can use its
|
|
58
|
+
results. As in handcalcs, `## ...` comments are headings, `# ...` comments are prose,
|
|
59
|
+
inline comments annotate their line, and `# hc:` commands work as usual.
|
|
60
|
+
|
|
61
|
+
The value is either the source as a string, or a mapping:
|
|
62
|
+
|
|
63
|
+
| Option | Default | Meaning |
|
|
64
|
+
| --- | --- | --- |
|
|
65
|
+
| `source` | (required) | The Python source to run and render. |
|
|
66
|
+
| `theme_color` | `"#0B5F66"` | A dark `#RRGGBB` colour; the whole palette is derived from it. |
|
|
67
|
+
| `base_size` | the document's body text size | Font size, in points, that every calc style scales from. |
|
|
68
|
+
|
|
69
|
+
Give a block an id to use it more than once in a mapping: `_hc_loads`, `_hc_moment`.
|
|
70
|
+
|
|
71
|
+
## Using the renderer directly
|
|
72
|
+
|
|
73
|
+
The renderer works without YMPrint. It returns a ReportLab stylesheet and a story:
|
|
74
|
+
|
|
75
|
+
```python
|
|
76
|
+
from handcalcs import HandCalcs
|
|
77
|
+
from ym_handcalcs import ReportLabRenderer, build_pdf
|
|
78
|
+
from ym_handcalcs.fonts import ensure_dejavu_registered
|
|
79
|
+
|
|
80
|
+
ensure_dejavu_registered() # YMPrint registers the DejaVu fonts itself
|
|
81
|
+
result = HandCalcs(renderer=ReportLabRenderer(theme_color="#7A1F1F"))("a = 2\nb = a * 3\n")
|
|
82
|
+
result["story"] # list of flowables
|
|
83
|
+
build_pdf(result, "calcs.pdf", title="Calcs")
|
|
84
|
+
|
|
85
|
+
# or straight to a PDF
|
|
86
|
+
HandCalcs(renderer=ReportLabRenderer())("a = 2\nb = a * 3\n", destination="calcs.pdf")
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
## Development
|
|
90
|
+
|
|
91
|
+
```bash
|
|
92
|
+
uv sync
|
|
93
|
+
uv run pytest
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
Regenerate the golden files with `HANDCALCS_UPDATE_GOLDEN=1 uv run pytest` and review the diff.
|
|
97
|
+
|
|
98
|
+
## License
|
|
99
|
+
|
|
100
|
+
Apache-2.0. See [LICENSE](LICENSE).
|
|
101
|
+
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
# ym-handcalcs
|
|
2
|
+
|
|
3
|
+
Render [handcalcs](https://github.com/connorferster/handcalcs) calculations in
|
|
4
|
+
[YMPrint](https://github.com/StructuralPython/ymprint) documents.
|
|
5
|
+
|
|
6
|
+
ym-handcalcs is a handcalcs renderer that produces ReportLab flowables, plus the `_hc`
|
|
7
|
+
block that puts them in a YMPrint document: write Python, get typeset calculations
|
|
8
|
+
with symbolic, substituted and computed values.
|
|
9
|
+
|
|
10
|
+
## Installation
|
|
11
|
+
|
|
12
|
+
Install it with YMPrint's `hc` extra:
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
pip install "ymprint[hc]"
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
ym-handcalcs needs handcalcs 2.0, which is currently a pre-release. pip installs it
|
|
19
|
+
automatically; uv only installs a pre-release you name, so add it with `--with`:
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
uv tool install "ymprint[hc]" --with "handcalcs>=2.0.0rc1"
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
YMPrint registers the `_hc` block automatically when ym-handcalcs is installed.
|
|
26
|
+
|
|
27
|
+
## The `_hc` block
|
|
28
|
+
|
|
29
|
+
```yaml
|
|
30
|
+
_vars:
|
|
31
|
+
w_DL: 2.5
|
|
32
|
+
w_LL: 4.0
|
|
33
|
+
|
|
34
|
+
Beam design:
|
|
35
|
+
- _hc: |
|
|
36
|
+
## Factored load
|
|
37
|
+
L = 6.0 # span, m
|
|
38
|
+
w_f = 1.25 * w_DL + 1.5 * w_LL
|
|
39
|
+
M_f = w_f * L**2 / 8
|
|
40
|
+
- The factored moment is {{ M_f }} kN·m.
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
The source runs in the document's `_vars`, like a `_py` block: it can use values from
|
|
44
|
+
`_vars` and earlier blocks, and later blocks (and `{{ }}` interpolation) can use its
|
|
45
|
+
results. As in handcalcs, `## ...` comments are headings, `# ...` comments are prose,
|
|
46
|
+
inline comments annotate their line, and `# hc:` commands work as usual.
|
|
47
|
+
|
|
48
|
+
The value is either the source as a string, or a mapping:
|
|
49
|
+
|
|
50
|
+
| Option | Default | Meaning |
|
|
51
|
+
| --- | --- | --- |
|
|
52
|
+
| `source` | (required) | The Python source to run and render. |
|
|
53
|
+
| `theme_color` | `"#0B5F66"` | A dark `#RRGGBB` colour; the whole palette is derived from it. |
|
|
54
|
+
| `base_size` | the document's body text size | Font size, in points, that every calc style scales from. |
|
|
55
|
+
|
|
56
|
+
Give a block an id to use it more than once in a mapping: `_hc_loads`, `_hc_moment`.
|
|
57
|
+
|
|
58
|
+
## Using the renderer directly
|
|
59
|
+
|
|
60
|
+
The renderer works without YMPrint. It returns a ReportLab stylesheet and a story:
|
|
61
|
+
|
|
62
|
+
```python
|
|
63
|
+
from handcalcs import HandCalcs
|
|
64
|
+
from ym_handcalcs import ReportLabRenderer, build_pdf
|
|
65
|
+
from ym_handcalcs.fonts import ensure_dejavu_registered
|
|
66
|
+
|
|
67
|
+
ensure_dejavu_registered() # YMPrint registers the DejaVu fonts itself
|
|
68
|
+
result = HandCalcs(renderer=ReportLabRenderer(theme_color="#7A1F1F"))("a = 2\nb = a * 3\n")
|
|
69
|
+
result["story"] # list of flowables
|
|
70
|
+
build_pdf(result, "calcs.pdf", title="Calcs")
|
|
71
|
+
|
|
72
|
+
# or straight to a PDF
|
|
73
|
+
HandCalcs(renderer=ReportLabRenderer())("a = 2\nb = a * 3\n", destination="calcs.pdf")
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## Development
|
|
77
|
+
|
|
78
|
+
```bash
|
|
79
|
+
uv sync
|
|
80
|
+
uv run pytest
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
Regenerate the golden files with `HANDCALCS_UPDATE_GOLDEN=1 uv run pytest` and review the diff.
|
|
84
|
+
|
|
85
|
+
## License
|
|
86
|
+
|
|
87
|
+
Apache-2.0. See [LICENSE](LICENSE).
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "ym-handcalcs"
|
|
3
|
+
dynamic = ["version", "description"]
|
|
4
|
+
readme = "README.md"
|
|
5
|
+
authors = [
|
|
6
|
+
{ name = "Connor Ferster", email = "connor@structuralpython.com" }
|
|
7
|
+
]
|
|
8
|
+
license = "Apache-2.0"
|
|
9
|
+
license-files = ["LICENSE"]
|
|
10
|
+
requires-python = ">=3.14"
|
|
11
|
+
dependencies = [
|
|
12
|
+
"handcalcs>=2.0.0rc1",
|
|
13
|
+
"reportlab>=4.5.1",
|
|
14
|
+
]
|
|
15
|
+
|
|
16
|
+
[project.urls]
|
|
17
|
+
Homepage = "https://github.com/StructuralPython/ym-handcalcs"
|
|
18
|
+
|
|
19
|
+
[build-system]
|
|
20
|
+
requires = ["flit_core>=3.12,<4"]
|
|
21
|
+
build-backend = "flit_core.buildapi"
|
|
22
|
+
|
|
23
|
+
[tool.flit.module]
|
|
24
|
+
name = "ym_handcalcs"
|
|
25
|
+
|
|
26
|
+
[dependency-groups]
|
|
27
|
+
dev = [
|
|
28
|
+
"numpy>=2.3.0",
|
|
29
|
+
"pytest>=9.0.3",
|
|
30
|
+
"ymprint>=0.6.0",
|
|
31
|
+
]
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"""
|
|
2
|
+
ym_handcalcs: renders handcalcs to ReportLab, and the `_hc` block for ymprint.
|
|
3
|
+
|
|
4
|
+
from handcalcs import HandCalcs
|
|
5
|
+
from ym_handcalcs import ReportLabRenderer
|
|
6
|
+
|
|
7
|
+
result = HandCalcs(renderer=ReportLabRenderer())(source)
|
|
8
|
+
result["stylesheet"] # reportlab.lib.styles.StyleSheet1
|
|
9
|
+
result["story"] # list of flowables
|
|
10
|
+
"""
|
|
11
|
+
__version__ = "0.1.0"
|
|
12
|
+
|
|
13
|
+
from .fonts import Fonts, FontFamily, ensure_dejavu_registered
|
|
14
|
+
from .styles import DEFAULT_THEME, Palette, build_stylesheet
|
|
15
|
+
from .renderer import ReportLabRenderer
|
|
16
|
+
from .pdf import build_pdf
|
|
17
|
+
from .block import BLOCK_CODE, convert_hc_block
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"""
|
|
2
|
+
The ``_hc`` ymprint block: runs Python source in the document's variables and
|
|
3
|
+
renders it with handcalcs.
|
|
4
|
+
|
|
5
|
+
_hc: |
|
|
6
|
+
L = 6.0 # span
|
|
7
|
+
M = w * L**2 / 8
|
|
8
|
+
|
|
9
|
+
_hc_beam:
|
|
10
|
+
source: |
|
|
11
|
+
...
|
|
12
|
+
theme_color: "#0B5F66"
|
|
13
|
+
base_size: 9.5
|
|
14
|
+
|
|
15
|
+
Like ``_py``, the source runs in the document's ``_vars``, so it can use
|
|
16
|
+
values from ``_vars`` and earlier blocks, and later blocks can use its results.
|
|
17
|
+
ymprint registers this block itself when ym_handcalcs is installed
|
|
18
|
+
(``pip install ymprint[hc]``).
|
|
19
|
+
"""
|
|
20
|
+
from __future__ import annotations
|
|
21
|
+
|
|
22
|
+
from typing import Any
|
|
23
|
+
|
|
24
|
+
from handcalcs import HandCalcs, get_option
|
|
25
|
+
from handcalcs.parsing import line_values
|
|
26
|
+
|
|
27
|
+
from .renderer import ReportLabRenderer
|
|
28
|
+
|
|
29
|
+
BLOCK_CODE = "_hc"
|
|
30
|
+
OPTIONS = {"source", "theme_color", "base_size"}
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def block_options(block_key: str, block_value: Any) -> dict:
|
|
34
|
+
"""The block's options as a dict; a bare string is the source."""
|
|
35
|
+
if isinstance(block_value, str):
|
|
36
|
+
return {"source": block_value}
|
|
37
|
+
if not isinstance(block_value, dict) or "source" not in block_value:
|
|
38
|
+
raise ValueError(
|
|
39
|
+
f"{block_key}: expected the Python source as a string, or a mapping with a "
|
|
40
|
+
f"'source' key (and optionally {', '.join(sorted(OPTIONS - {'source'}))})."
|
|
41
|
+
)
|
|
42
|
+
unknown = set(block_value) - OPTIONS
|
|
43
|
+
if unknown:
|
|
44
|
+
raise ValueError(
|
|
45
|
+
f"{block_key}: unknown option(s) {', '.join(sorted(unknown))}. "
|
|
46
|
+
f"Choose from: {', '.join(sorted(OPTIONS))}."
|
|
47
|
+
)
|
|
48
|
+
return block_value
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def run_source(source: str, namespace: dict):
|
|
52
|
+
"""Run 'source' in 'namespace'; returns the recorded line values (or None)."""
|
|
53
|
+
if not get_option("line_values", True):
|
|
54
|
+
exec(source, namespace)
|
|
55
|
+
return None
|
|
56
|
+
return line_values.run(source, namespace)
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def convert_hc_block(block_key: str, block_value: Any, context: dict) -> list:
|
|
60
|
+
options = block_options(block_key, block_value)
|
|
61
|
+
source = options["source"]
|
|
62
|
+
namespace = context["vars"]
|
|
63
|
+
try:
|
|
64
|
+
values = run_source(source, namespace)
|
|
65
|
+
except Exception as e:
|
|
66
|
+
# The author's own code raised: report it like a failing _py block.
|
|
67
|
+
from ymprint.errors import PythonBlockError
|
|
68
|
+
|
|
69
|
+
raise PythonBlockError(block_key, source, e) from e
|
|
70
|
+
renderer = ReportLabRenderer(
|
|
71
|
+
theme_color=options.get("theme_color"),
|
|
72
|
+
base_size=options.get("base_size", context["styles"]["ymprint"].body.size),
|
|
73
|
+
)
|
|
74
|
+
result = HandCalcs(renderer=renderer).render_run(source, namespace, values)
|
|
75
|
+
return list(result["story"])
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Math-aware line breaking for rendered paragraph markup.
|
|
3
|
+
|
|
4
|
+
ReportLab breaks a paragraph at any space and never at a non-breaking space
|
|
5
|
+
(U+00A0). ``ranked_breaks`` gives every space in a rendered expression a
|
|
6
|
+
*rank*: low ranks are good places to break (before a top-level ``+`` or
|
|
7
|
+
``and``), high ranks poor ones (inside nested brackets), and ``None`` never
|
|
8
|
+
(the space after an operator, so an operator always starts the continuation
|
|
9
|
+
line). ``with_breaks(markup, max_rank)`` keeps the spaces up to ``max_rank``
|
|
10
|
+
and makes every other one non-breaking.
|
|
11
|
+
|
|
12
|
+
The rank is ``bracket_depth * CLASSES + operator_class``, so every break
|
|
13
|
+
outside brackets beats every break inside them, and within one bracket level
|
|
14
|
+
looser-binding operators win:
|
|
15
|
+
|
|
16
|
+
- class 0: relations, comparisons and boolean words (``=``, ``→``, ``<``,
|
|
17
|
+
``≤``, ``and``, ``or``, ``in``, ``is``, ``not``) and the spaces between plain
|
|
18
|
+
words (block headers);
|
|
19
|
+
- class 1: ``+``, ``-`` and the space after a comma (collections, arguments);
|
|
20
|
+
- class 2: ``·``, ``/``, ``//``, ``%``.
|
|
21
|
+
|
|
22
|
+
Tags (``<font color="...">``) are skipped, so their attribute spaces are never
|
|
23
|
+
touched.
|
|
24
|
+
"""
|
|
25
|
+
from __future__ import annotations
|
|
26
|
+
|
|
27
|
+
import re
|
|
28
|
+
from typing import Optional
|
|
29
|
+
|
|
30
|
+
NBSP = " "
|
|
31
|
+
CLASSES = 3
|
|
32
|
+
OPERATOR_CLASS = {
|
|
33
|
+
"=": 0, "→": 0, "and": 0, "or": 0, "in": 0, "is": 0, "not": 0,
|
|
34
|
+
"+": 1, "-": 1, "−": 1,
|
|
35
|
+
"·": 2, "/": 2, "//": 2, "%": 2, "*": 2,
|
|
36
|
+
}
|
|
37
|
+
OPENERS = "([{"
|
|
38
|
+
CLOSERS = ")]}"
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def _visible(markup: str) -> list[tuple[int, str]]:
|
|
42
|
+
"""``(index, char)`` for every character of ``markup`` outside a tag."""
|
|
43
|
+
chars, in_tag = [], False
|
|
44
|
+
for idx, char in enumerate(markup):
|
|
45
|
+
if char == "<":
|
|
46
|
+
in_tag = True
|
|
47
|
+
elif char == ">" and in_tag:
|
|
48
|
+
in_tag = False
|
|
49
|
+
elif not in_tag:
|
|
50
|
+
chars.append((idx, char))
|
|
51
|
+
return chars
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def _token_after(chars: list[tuple[int, str]], pos: int) -> str:
|
|
55
|
+
"""The visible token (up to the next space) starting at ``chars[pos]``."""
|
|
56
|
+
token = []
|
|
57
|
+
for _, char in chars[pos:]:
|
|
58
|
+
if char in (" ", NBSP):
|
|
59
|
+
break
|
|
60
|
+
token.append(char)
|
|
61
|
+
return "".join(token)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def ranked_breaks(markup: str) -> dict[int, Optional[int]]:
|
|
65
|
+
"""Map the index of every visible space in ``markup`` to its break rank (or None)."""
|
|
66
|
+
chars = _visible(markup)
|
|
67
|
+
ranks: dict[int, Optional[int]] = {}
|
|
68
|
+
depth = 0
|
|
69
|
+
previous_token = ""
|
|
70
|
+
for pos, (idx, char) in enumerate(chars):
|
|
71
|
+
if char in OPENERS:
|
|
72
|
+
depth += 1
|
|
73
|
+
elif char in CLOSERS:
|
|
74
|
+
depth = max(0, depth - 1)
|
|
75
|
+
if char != " ":
|
|
76
|
+
continue
|
|
77
|
+
before = "".join(c for _, c in chars[max(0, pos - 3):pos])
|
|
78
|
+
token_before = previous_token
|
|
79
|
+
token_after = _token_after(chars, pos + 1)
|
|
80
|
+
previous_token = token_after
|
|
81
|
+
if token_before in OPERATOR_CLASS and not before.endswith(","):
|
|
82
|
+
ranks[idx] = None # keep an operator with its right operand
|
|
83
|
+
elif token_after in OPERATOR_CLASS:
|
|
84
|
+
ranks[idx] = depth * CLASSES + OPERATOR_CLASS[token_after]
|
|
85
|
+
elif before.endswith(","):
|
|
86
|
+
ranks[idx] = depth * CLASSES + 1
|
|
87
|
+
else:
|
|
88
|
+
ranks[idx] = depth * CLASSES
|
|
89
|
+
return ranks
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def _first_token(markup: str) -> str:
|
|
93
|
+
chars = _visible(markup)
|
|
94
|
+
return _token_after(chars, 0)
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def max_rank(markup: str) -> int:
|
|
98
|
+
ranks = [rank for rank in ranked_breaks(markup).values() if rank is not None]
|
|
99
|
+
return max(ranks, default=0)
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def with_breaks(markup: str, allowed: int) -> str:
|
|
103
|
+
"""Keep the spaces of rank <= ``allowed`` and make every other one non-breaking."""
|
|
104
|
+
ranks = ranked_breaks(markup)
|
|
105
|
+
out = list(markup)
|
|
106
|
+
for idx, rank in ranks.items():
|
|
107
|
+
if rank is None or rank > allowed:
|
|
108
|
+
out[idx] = NBSP
|
|
109
|
+
return "".join(out)
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def unbreakable(markup: str) -> str:
|
|
113
|
+
return with_breaks(markup, -1)
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
_ITEM_SPLIT = re.compile(r", ")
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
def elide_collections(markup: str, max_items: int = 10, edge: int = 3) -> str:
|
|
120
|
+
"""
|
|
121
|
+
Shorten every bracketed collection with more than ``max_items`` items to its
|
|
122
|
+
first and last ``edge`` items around an ellipsis. Only collections whose
|
|
123
|
+
items contain no nested brackets or tags are touched.
|
|
124
|
+
"""
|
|
125
|
+
def shorten(match: re.Match) -> str:
|
|
126
|
+
opener, body, closer = match.group(1), match.group(2), match.group(3)
|
|
127
|
+
items = _ITEM_SPLIT.split(body)
|
|
128
|
+
if len(items) <= max_items:
|
|
129
|
+
return match.group(0)
|
|
130
|
+
kept = items[:edge] + ["…"] + items[-edge:]
|
|
131
|
+
return f"{opener}{', '.join(kept)}{closer}"
|
|
132
|
+
|
|
133
|
+
return re.sub(r"([\[{(])([^\[\]{}()<>]*)([\]})])", shorten, markup)
|