odix 0.1.0__py3-none-any.whl

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.
Files changed (120) hide show
  1. odix/Impressio/__init__.py +36 -0
  2. odix/Impressio/impressio.py +305 -0
  3. odix/Ordinatio/__init__.py +31 -0
  4. odix/Ordinatio/book.py +122 -0
  5. odix/Ordinatio/chapter.py +45 -0
  6. odix/Ordinatio/loader.py +105 -0
  7. odix/Ordinatio/ordinatio.py +33 -0
  8. odix/Ordinatio/principium.py +46 -0
  9. odix/Scriptorium/__init__.py +40 -0
  10. odix/Scriptorium/compiler.py +58 -0
  11. odix/Scriptorium/language.py +29 -0
  12. odix/Scriptorium/syntax.py +465 -0
  13. odix/Scriptorium/visitor.py +78 -0
  14. odix/Scriptorium/visitors/__init__.py +17 -0
  15. odix/Scriptorium/visitors/markup.py +333 -0
  16. odix/Scriptorium/writer.py +206 -0
  17. odix/Tabula/Lexer/__init__.py +29 -0
  18. odix/Tabula/Lexer/exceptions.py +21 -0
  19. odix/Tabula/Lexer/lexer.py +215 -0
  20. odix/Tabula/Lexer/reader.py +32 -0
  21. odix/Tabula/Lexer/reader_type.py +28 -0
  22. odix/Tabula/Lexer/token.py +33 -0
  23. odix/Tabula/Lexer/token_type.py +57 -0
  24. odix/Tabula/Parser/__init__.py +25 -0
  25. odix/Tabula/Parser/exceptions.py +21 -0
  26. odix/Tabula/Parser/parser.py +193 -0
  27. odix/Tabula/Parser/rules/__init__.py +24 -0
  28. odix/Tabula/Parser/rules/blocks/__init__.py +17 -0
  29. odix/Tabula/Parser/rules/blocks/bibliography.py +63 -0
  30. odix/Tabula/Parser/rules/blocks/caption.py +107 -0
  31. odix/Tabula/Parser/rules/blocks/cells.py +47 -0
  32. odix/Tabula/Parser/rules/blocks/code_block.py +57 -0
  33. odix/Tabula/Parser/rules/blocks/directive.py +62 -0
  34. odix/Tabula/Parser/rules/blocks/figure.py +68 -0
  35. odix/Tabula/Parser/rules/blocks/footnote.py +59 -0
  36. odix/Tabula/Parser/rules/blocks/headings.py +80 -0
  37. odix/Tabula/Parser/rules/blocks/image.py +59 -0
  38. odix/Tabula/Parser/rules/blocks/list_item.py +56 -0
  39. odix/Tabula/Parser/rules/blocks/lists.py +49 -0
  40. odix/Tabula/Parser/rules/blocks/math_block.py +73 -0
  41. odix/Tabula/Parser/rules/blocks/page_break.py +53 -0
  42. odix/Tabula/Parser/rules/blocks/paragraphs.py +49 -0
  43. odix/Tabula/Parser/rules/blocks/quotes.py +49 -0
  44. odix/Tabula/Parser/rules/blocks/reference.py +59 -0
  45. odix/Tabula/Parser/rules/blocks/rows.py +46 -0
  46. odix/Tabula/Parser/rules/blocks/tables.py +97 -0
  47. odix/Tabula/Parser/rules/dispatchers/__init__.py +17 -0
  48. odix/Tabula/Parser/rules/dispatchers/blocks.py +93 -0
  49. odix/Tabula/Parser/rules/dispatchers/inline.py +134 -0
  50. odix/Tabula/Parser/rules/inline/__init__.py +17 -0
  51. odix/Tabula/Parser/rules/inline/asterisk.py +42 -0
  52. odix/Tabula/Parser/rules/inline/bold.py +58 -0
  53. odix/Tabula/Parser/rules/inline/citation.py +53 -0
  54. odix/Tabula/Parser/rules/inline/code.py +69 -0
  55. odix/Tabula/Parser/rules/inline/colon.py +57 -0
  56. odix/Tabula/Parser/rules/inline/cross_reference.py +53 -0
  57. odix/Tabula/Parser/rules/inline/emphasis.py +74 -0
  58. odix/Tabula/Parser/rules/inline/hyphen.py +42 -0
  59. odix/Tabula/Parser/rules/inline/inline_code.py +57 -0
  60. odix/Tabula/Parser/rules/inline/italic.py +45 -0
  61. odix/Tabula/Parser/rules/inline/math.py +53 -0
  62. odix/Tabula/Parser/rules/inline/module.py +42 -0
  63. odix/Tabula/Parser/rules/inline/plus.py +42 -0
  64. odix/Tabula/Parser/rules/inline/sequence.py +126 -0
  65. odix/Tabula/Parser/rules/inline/strike.py +50 -0
  66. odix/Tabula/Parser/rules/inline/text.py +42 -0
  67. odix/Tabula/Parser/rules/inline/underline.py +50 -0
  68. odix/Tabula/Parser/rules/inline/underscore.py +42 -0
  69. odix/Tabula/__init__.py +38 -0
  70. odix/Tabula/io/deserializer.py +247 -0
  71. odix/Tabula/io/serializer.py +95 -0
  72. odix/Tabula/nodes/__init__.py +81 -0
  73. odix/Tabula/nodes/bibliography.py +25 -0
  74. odix/Tabula/nodes/block.py +31 -0
  75. odix/Tabula/nodes/bold.py +25 -0
  76. odix/Tabula/nodes/caption.py +25 -0
  77. odix/Tabula/nodes/cell.py +25 -0
  78. odix/Tabula/nodes/citation.py +35 -0
  79. odix/Tabula/nodes/code_block.py +63 -0
  80. odix/Tabula/nodes/cross_reference.py +38 -0
  81. odix/Tabula/nodes/document.py +52 -0
  82. odix/Tabula/nodes/figure.py +25 -0
  83. odix/Tabula/nodes/footnote.py +35 -0
  84. odix/Tabula/nodes/horizontal_rule.py +25 -0
  85. odix/Tabula/nodes/image.py +41 -0
  86. odix/Tabula/nodes/inline.py +31 -0
  87. odix/Tabula/nodes/inline_code.py +34 -0
  88. odix/Tabula/nodes/italic.py +25 -0
  89. odix/Tabula/nodes/label.py +36 -0
  90. odix/Tabula/nodes/link.py +35 -0
  91. odix/Tabula/nodes/list.py +35 -0
  92. odix/Tabula/nodes/list_item.py +25 -0
  93. odix/Tabula/nodes/math_block.py +41 -0
  94. odix/Tabula/nodes/math_inline.py +41 -0
  95. odix/Tabula/nodes/metadata.py +135 -0
  96. odix/Tabula/nodes/node.py +184 -0
  97. odix/Tabula/nodes/page_break.py +25 -0
  98. odix/Tabula/nodes/paragraph.py +32 -0
  99. odix/Tabula/nodes/quote.py +25 -0
  100. odix/Tabula/nodes/reference.py +41 -0
  101. odix/Tabula/nodes/row.py +25 -0
  102. odix/Tabula/nodes/section.py +70 -0
  103. odix/Tabula/nodes/strike.py +25 -0
  104. odix/Tabula/nodes/table.py +25 -0
  105. odix/Tabula/nodes/text.py +54 -0
  106. odix/Tabula/nodes/underline.py +25 -0
  107. odix/Tabula/tabula.py +123 -0
  108. odix/Tabula/visitor.py +67 -0
  109. odix/Typus/__init__.py +40 -0
  110. odix/Typus/document_style.py +127 -0
  111. odix/Typus/loader.py +102 -0
  112. odix/Typus/margins.py +51 -0
  113. odix/Typus/typus.py +51 -0
  114. odix/__init__.py +52 -0
  115. odix/cli.py +98 -0
  116. odix-0.1.0.dist-info/METADATA +1010 -0
  117. odix-0.1.0.dist-info/RECORD +120 -0
  118. odix-0.1.0.dist-info/WHEEL +4 -0
  119. odix-0.1.0.dist-info/entry_points.txt +2 -0
  120. odix-0.1.0.dist-info/licenses/LICENSE +674 -0
@@ -0,0 +1,36 @@
1
+ # Odix - Open-source publishing system for technical books
2
+ # Copyright (C) 2026 José Manuel Naveiro
3
+ #
4
+ # This file is part of Odix.
5
+ #
6
+ # Odix is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU General Public License as published
8
+ # by the Free Software Foundation, either version 3 of the License,
9
+ # or (at your option) any later version.
10
+ #
11
+ # Odix is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU General Public License
17
+ # along with Odix. If not, see <https://www.gnu.org/licenses/>.
18
+
19
+ """
20
+ Impressio
21
+ =========
22
+
23
+ Renders a publication into one or more output formats.
24
+
25
+ Public API
26
+ ==========
27
+
28
+ Impressio
29
+ Renders a styled publication.
30
+ """
31
+
32
+ from .impressio import Impressio
33
+
34
+ __all__ = [
35
+ "Impressio",
36
+ ]
@@ -0,0 +1,305 @@
1
+ # Odix - Open-source publishing system for technical books
2
+ # Copyright (C) 2026 José Manuel Naveiro
3
+ #
4
+ # This file is part of Odix.
5
+ #
6
+ # Odix is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU General Public License as published
8
+ # by the Free Software Foundation, either version 3 of the License,
9
+ # or (at your option) any later version.
10
+ #
11
+ # Odix is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU General Public License
17
+ # along with Odix. If not, see <https://www.gnu.org/licenses/>.
18
+
19
+ from __future__ import annotations
20
+
21
+ from pathlib import Path
22
+
23
+ from odix.ordinatio import Book, Chapter, Principium
24
+
25
+ from ..scriptorium import Compiler, Language
26
+ from ..tabula import Tabula
27
+ from ..typus import Typus
28
+
29
+
30
+ class Impressio:
31
+ """Publishes an Odix book as a LaTeX document."""
32
+
33
+ def __init__(
34
+ self,
35
+ book: Book,
36
+ typus: Typus,
37
+ ) -> None:
38
+ self._book = book
39
+ self._typus = typus
40
+
41
+ self._FONTS = {
42
+ "computer-modern": None,
43
+ "palatino": "mathpazo",
44
+ "helvetica": "helvet",
45
+ "times": "mathptmx",
46
+ }
47
+
48
+ @property
49
+ def book(self) -> Book:
50
+ """Returns the book to publish."""
51
+ return self._book
52
+
53
+ @property
54
+ def typus(self) -> Typus:
55
+ """Returns the publication style."""
56
+ return self._typus
57
+
58
+ def _render_principium(
59
+ self,
60
+ principium: Principium,
61
+ ) -> str:
62
+ """Renders a principium as LaTeX."""
63
+
64
+ tabula = Tabula(principium.source)
65
+
66
+ compiler = Compiler(
67
+ Language.LATEX,
68
+ )
69
+
70
+ return compiler.compile(
71
+ tabula.ast,
72
+ )
73
+
74
+ def _render_chapter(
75
+ self,
76
+ chapter: Chapter,
77
+ ) -> str:
78
+ """Renders a chapter and its principia as LaTeX."""
79
+
80
+ parts = [
81
+ rf"\chapter{{{chapter.title}}}",
82
+ "",
83
+ ]
84
+
85
+ for principium in chapter.principia:
86
+ parts.append(self._render_principium(principium))
87
+
88
+ return "\n".join(parts)
89
+
90
+ def _render_titlepage(self) -> str:
91
+ """Renders the book metadata as a LaTeX title page."""
92
+
93
+ metadata = self._book.metadata
94
+
95
+ parts = [
96
+ r"\begin{titlepage}",
97
+ r"\centering",
98
+ "",
99
+ r"\vspace*{3cm}",
100
+ "",
101
+ rf"{{\Huge\bfseries {metadata.title}\par}}",
102
+ ]
103
+
104
+ if metadata.subtitle:
105
+ parts.extend(
106
+ [
107
+ "",
108
+ r"\vspace{1cm}",
109
+ rf"{{\Large {metadata.subtitle}\par}}",
110
+ ]
111
+ )
112
+
113
+ parts.extend(
114
+ [
115
+ "",
116
+ r"\vfill",
117
+ ]
118
+ )
119
+
120
+ if metadata.author:
121
+ parts.append(rf"{{\Large {metadata.author}\par}}")
122
+
123
+ if metadata.edition:
124
+ parts.extend(
125
+ [
126
+ "",
127
+ r"\vspace{0.5cm}",
128
+ rf"{{\large {metadata.edition}\par}}",
129
+ ]
130
+ )
131
+
132
+ if metadata.date:
133
+ parts.extend(
134
+ [
135
+ "",
136
+ r"\vspace{0.5cm}",
137
+ rf"{{\large {metadata.date}\par}}",
138
+ ]
139
+ )
140
+
141
+ parts.extend(
142
+ [
143
+ "",
144
+ r"\end{titlepage}",
145
+ ]
146
+ )
147
+
148
+ return "\n".join(parts)
149
+
150
+ def _render_bibliography(self) -> str:
151
+ """Renders the bibliography as LaTeX."""
152
+
153
+ bibliography = self._book.bibliography
154
+
155
+ if bibliography is None:
156
+ return ""
157
+
158
+ file = Path(bibliography.file).stem
159
+
160
+ return "\n".join(
161
+ [
162
+ rf"\bibliographystyle{{{bibliography.style}}}",
163
+ rf"\bibliography{{{file}}}",
164
+ ]
165
+ )
166
+
167
+ def _render_body(self) -> str:
168
+ """Renders the complete body of the book."""
169
+
170
+ chapters = [self._render_chapter(chapter) for chapter in self._book.chapters]
171
+
172
+ return "\n\n".join(chapters)
173
+
174
+ def _render_preamble(self) -> str:
175
+ """Renders the LaTeX preamble from Typus."""
176
+
177
+ document = self._typus.document
178
+ margins = document.margins
179
+ language = document.language
180
+
181
+ options = [
182
+ document.font_size,
183
+ document.page_size,
184
+ ]
185
+
186
+ if document.orientation != "portrait":
187
+ options.append(document.orientation)
188
+
189
+ if document.twoside:
190
+ options.append("twoside")
191
+
192
+ document_class = (
193
+ rf"\documentclass[{','.join(options)}]" rf"{{{document.document_class}}}"
194
+ )
195
+
196
+ packages = "\n".join(
197
+ rf"\usepackage{{{package}}}" for package in document.packages
198
+ )
199
+
200
+ font_package = self._FONTS[document.font]
201
+
202
+ if font_package is not None:
203
+ packages += "\n" + rf"\usepackage{{{font_package}}}"
204
+
205
+ geometry = (
206
+ f"\\usepackage["
207
+ f"top={margins.top},"
208
+ f"bottom={margins.bottom},"
209
+ f"left={margins.left},"
210
+ f"right={margins.right}"
211
+ f"]{{geometry}}"
212
+ )
213
+
214
+ babel = rf"\usepackage[{language}]{{babel}}"
215
+
216
+ style = "\n".join(
217
+ [
218
+ r"\setcounter{secnumdepth}{1}",
219
+ r"\setstretch{1.25}",
220
+ "",
221
+ r"\definecolor{chaptercolor}{HTML}{24527A}",
222
+ r"\definecolor{primary}{HTML}{24527A}",
223
+ r"\definecolor{secondary}{HTML}{4F81A1}",
224
+ r"\definecolor{codebg}{HTML}{F4F6F8}",
225
+ "",
226
+ r"\titleformat{\chapter}",
227
+ r" {\Huge\bfseries\color{chaptercolor}}",
228
+ r" {\thechapter}",
229
+ r" {1em}",
230
+ r" {}",
231
+ ]
232
+ )
233
+
234
+ return "\n".join(
235
+ [
236
+ document_class,
237
+ "",
238
+ packages,
239
+ geometry,
240
+ babel,
241
+ "",
242
+ style,
243
+ ]
244
+ )
245
+
246
+ def _render_document(self) -> str:
247
+ """Renders the complete LaTeX document."""
248
+
249
+ preamble = self._render_preamble()
250
+ metadata = self._render_titlepage()
251
+ body = self._render_body()
252
+ bibliography = self._render_bibliography()
253
+
254
+ document = [
255
+ preamble,
256
+ "",
257
+ r"\begin{document}",
258
+ "",
259
+ metadata,
260
+ "",
261
+ ]
262
+
263
+ if self._typus.document.chapters_table_of_contents:
264
+ document.extend(
265
+ [
266
+ r"\setcounter{tocdepth}{1}",
267
+ r"\tableofcontents",
268
+ "",
269
+ ]
270
+ )
271
+
272
+ document.append(body)
273
+
274
+ if bibliography:
275
+ document.extend(
276
+ [
277
+ "",
278
+ bibliography,
279
+ ]
280
+ )
281
+
282
+ document.extend(
283
+ [
284
+ "",
285
+ r"\end{document}",
286
+ "",
287
+ ]
288
+ )
289
+
290
+ return "\n".join(document)
291
+
292
+ def publish(
293
+ self,
294
+ path: str | Path,
295
+ ) -> None:
296
+ """Publishes the book to a LaTeX file."""
297
+
298
+ path = Path(path)
299
+
300
+ latex = self._render_document()
301
+
302
+ path.write_text(
303
+ latex,
304
+ encoding="utf-8",
305
+ )
@@ -0,0 +1,31 @@
1
+ # Odix - Open-source publishing system for technical books
2
+ # Copyright (C) 2026 José Manuel Naveiro
3
+ #
4
+ # This file is part of Odix.
5
+ #
6
+ # Odix is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU General Public License as published
8
+ # by the Free Software Foundation, either version 3 of the License,
9
+ # or (at your option) any later version.
10
+ #
11
+ # Odix is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU General Public License
17
+ # along with Odix. If not, see <https://www.gnu.org/licenses/>.
18
+
19
+ from .book import Bibliography, Book, Metadata
20
+ from .chapter import Chapter
21
+ from .ordinatio import Ordinatio
22
+ from .principium import Principium
23
+
24
+ __all__ = [
25
+ "Bibliography",
26
+ "Book",
27
+ "Chapter",
28
+ "Metadata",
29
+ "Ordinatio",
30
+ "Principium",
31
+ ]
odix/Ordinatio/book.py ADDED
@@ -0,0 +1,122 @@
1
+ # Odix - Open-source publishing system for technical books
2
+ # Copyright (C) 2026 José Manuel Naveiro
3
+ #
4
+ # This file is part of Odix.
5
+ #
6
+ # Odix is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU General Public License as published
8
+ # by the Free Software Foundation, either version 3 of the License,
9
+ # or (at your option) any later version.
10
+ #
11
+ # Odix is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU General Public License
17
+ # along with Odix. If not, see <https://www.gnu.org/licenses/>.
18
+
19
+ from __future__ import annotations
20
+
21
+ from collections.abc import Iterable
22
+
23
+ from .chapter import Chapter
24
+
25
+
26
+ class Metadata:
27
+ """Metadata of an Odix publication."""
28
+
29
+ def __init__(
30
+ self,
31
+ title: str,
32
+ author: str | None = None,
33
+ subtitle: str | None = None,
34
+ date: str | None = None,
35
+ edition: str | None = None,
36
+ ) -> None:
37
+ self._title = title
38
+ self._author = author
39
+ self._subtitle = subtitle
40
+ self._date = date
41
+ self._edition = edition
42
+
43
+ @property
44
+ def title(self) -> str:
45
+ """Returns the book title."""
46
+ return self._title
47
+
48
+ @property
49
+ def author(self) -> str | None:
50
+ """Returns the book author."""
51
+ return self._author
52
+
53
+ @property
54
+ def subtitle(self) -> str | None:
55
+ """Returns the book subtitle."""
56
+ return self._subtitle
57
+
58
+ @property
59
+ def date(self) -> str | None:
60
+ """Returns the publication date."""
61
+ return self._date
62
+
63
+ @property
64
+ def edition(self) -> str | None:
65
+ """Returns the book edition."""
66
+ return self._edition
67
+
68
+
69
+ class Bibliography:
70
+ """Bibliography configuration of an Odix publication."""
71
+
72
+ def __init__(
73
+ self,
74
+ file: str,
75
+ style: str = "plain",
76
+ ) -> None:
77
+ self._file = file
78
+ self._style = style
79
+
80
+ @property
81
+ def file(self) -> str:
82
+ """Returns the bibliography file."""
83
+ return self._file
84
+
85
+ @property
86
+ def style(self) -> str:
87
+ """Returns the bibliography style."""
88
+ return self._style
89
+
90
+
91
+ class Book:
92
+ """A book of an Odix publication."""
93
+
94
+ def __init__(
95
+ self,
96
+ metadata: Metadata,
97
+ chapters: Iterable[Chapter],
98
+ bibliography: Bibliography | None = None,
99
+ ) -> None:
100
+ self._metadata = metadata
101
+ self._chapters = list(chapters)
102
+ self._bibliography = bibliography
103
+
104
+ @property
105
+ def metadata(self) -> Metadata:
106
+ """Returns the book metadata."""
107
+ return self._metadata
108
+
109
+ @property
110
+ def title(self) -> str:
111
+ """Returns the book title."""
112
+ return self._metadata.title
113
+
114
+ @property
115
+ def chapters(self) -> list[Chapter]:
116
+ """Returns the chapters contained in the book."""
117
+ return self._chapters
118
+
119
+ @property
120
+ def bibliography(self) -> Bibliography | None:
121
+ """Returns the book bibliography."""
122
+ return self._bibliography
@@ -0,0 +1,45 @@
1
+ # Odix - Open-source publishing system for technical books
2
+ # Copyright (C) 2026 José Manuel Naveiro
3
+ #
4
+ # This file is part of Odix.
5
+ #
6
+ # Odix is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU General Public License as published
8
+ # by the Free Software Foundation, either version 3 of the License,
9
+ # or (at your option) any later version.
10
+ #
11
+ # Odix is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU General Public License
17
+ # along with Odix. If not, see <https://www.gnu.org/licenses/>.
18
+
19
+ from __future__ import annotations
20
+
21
+ from collections.abc import Iterable
22
+
23
+ from .principium import Principium
24
+
25
+
26
+ class Chapter:
27
+ """A chapter of an Odix book."""
28
+
29
+ def __init__(
30
+ self,
31
+ title: str,
32
+ principia: Iterable[Principium],
33
+ ) -> None:
34
+ self._title = title
35
+ self._principia = list(principia)
36
+
37
+ @property
38
+ def title(self) -> str:
39
+ """Returns the chapter title."""
40
+ return self._title
41
+
42
+ @property
43
+ def principia(self) -> list[Principium]:
44
+ """Returns the principia contained in the chapter."""
45
+ return self._principia
@@ -0,0 +1,105 @@
1
+ # Odix - Open-source publishing system for technical books
2
+ # Copyright (C) 2026 José Manuel Naveiro
3
+ #
4
+ # This file is part of Odix.
5
+ #
6
+ # Odix is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU General Public License as published
8
+ # by the Free Software Foundation, either version 3 of the License,
9
+ # or (at your option) any later version.
10
+ #
11
+ # Odix is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU General Public License
17
+ # along with Odix. If not, see <https://www.gnu.org/licenses/>.
18
+
19
+ from __future__ import annotations
20
+
21
+ from pathlib import Path
22
+
23
+ import yaml
24
+
25
+ from .book import Bibliography, Book, Metadata
26
+ from .chapter import Chapter
27
+ from .principium import Principium
28
+
29
+
30
+ class Loader:
31
+ """Loads an Odix book from a YAML file."""
32
+
33
+ @classmethod
34
+ def load(cls, path: str | Path) -> Book:
35
+ """Loads a book from a YAML file."""
36
+ path = Path(path)
37
+
38
+ data = yaml.safe_load(path.read_text(encoding="utf-8"))
39
+
40
+ if data is None:
41
+ raise ValueError("The YAML file is empty.")
42
+
43
+ if "metadata" not in data:
44
+ raise ValueError("The book configuration is missing 'metadata'.")
45
+
46
+ metadata_data = data["metadata"]
47
+
48
+ if "title" not in metadata_data:
49
+ raise ValueError("The metadata configuration is missing 'title'.")
50
+
51
+ if "chapters" not in data:
52
+ raise ValueError("The book configuration is missing 'chapters'.")
53
+
54
+ metadata = Metadata(
55
+ title=metadata_data["title"],
56
+ author=metadata_data.get("author"),
57
+ subtitle=metadata_data.get("subtitle"),
58
+ date=metadata_data.get("date"),
59
+ edition=metadata_data.get("edition"),
60
+ )
61
+
62
+ bibliography = None
63
+
64
+ if "bibliography" in data:
65
+ bibliography_data = data["bibliography"]
66
+
67
+ if "file" not in bibliography_data:
68
+ raise ValueError("The bibliography configuration is missing 'file'.")
69
+
70
+ bibliography = Bibliography(
71
+ file=bibliography_data["file"],
72
+ style=bibliography_data.get("style", "plain"),
73
+ )
74
+
75
+ chapters = []
76
+
77
+ for chapter_data in data["chapters"]:
78
+ if "title" not in chapter_data:
79
+ raise ValueError("The chapter configuration is missing 'title'.")
80
+
81
+ if "principia" not in chapter_data:
82
+ raise ValueError("The chapter configuration is missing " "'principia'.")
83
+
84
+ principia = []
85
+
86
+ for principium_path in chapter_data["principia"]:
87
+ source = path.parent / principium_path
88
+
89
+ if not source.exists():
90
+ raise FileNotFoundError(source)
91
+
92
+ principia.append(Principium(source))
93
+
94
+ chapters.append(
95
+ Chapter(
96
+ title=chapter_data["title"],
97
+ principia=principia,
98
+ )
99
+ )
100
+
101
+ return Book(
102
+ metadata=metadata,
103
+ bibliography=bibliography,
104
+ chapters=chapters,
105
+ )
@@ -0,0 +1,33 @@
1
+ # Odix - Open-source publishing system for technical books
2
+ # Copyright (C) 2026 José Manuel Naveiro
3
+ #
4
+ # This file is part of Odix.
5
+ #
6
+ # Odix is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU General Public License as published
8
+ # by the Free Software Foundation, either version 3 of the License,
9
+ # or (at your option) any later version.
10
+ #
11
+ # Odix is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU General Public License
17
+ # along with Odix. If not, see <https://www.gnu.org/licenses/>.
18
+
19
+ from __future__ import annotations
20
+
21
+ from pathlib import Path
22
+
23
+ from .book import Book
24
+ from .loader import Loader
25
+
26
+
27
+ class Ordinatio:
28
+ """Public interface for loading Odix book structures."""
29
+
30
+ @classmethod
31
+ def from_file(cls, path: str | Path) -> Book:
32
+ """Loads a book structure from a YAML file."""
33
+ return Loader.load(path)