-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy path__init__.py
More file actions
1503 lines (1375 loc) · 68.5 KB
/
__init__.py
File metadata and controls
1503 lines (1375 loc) · 68.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import typing as t
from enum import Enum
import hashlib
import logging
import multiprocessing
import shutil
import tempfile
import pickle
from pathlib import Path
from lxml import etree as ET
from pydantic import (
field_validator,
model_validator,
ConfigDict,
HttpUrl,
PrivateAttr,
ValidationInfo,
)
import pydantic_xml as pxml
from pydantic_xml.element.element import SearchMode
from .xml import Executables, LegacyProject, LatexEngine
from .. import constants
from .. import core
from .. import codechat
from .. import utils
from .. import types as pt # PreTeXt types
from ..resources import resource_base_path
from .. import VERSION
log = logging.getLogger("ptxlogger")
class Format(str, Enum):
HTML = "html"
LATEX = "latex"
PDF = "pdf"
EPUB = "epub"
KINDLE = "kindle"
BRAILLE = "braille"
WEBWORK = "webwork"
CUSTOM = "custom"
# The CLI only needs two values from the publication file. Therefore, this class ignores the vast majority of a publication file's contents, loading and validating only a (small) relevant subset.
class PublicationSubset(
pxml.BaseXmlModel, tag="publication", search_mode=SearchMode.UNORDERED
):
external: Path = pxml.wrapped("source/directories", pxml.attr())
generated: Path = pxml.wrapped("source/directories", pxml.attr())
class BrailleMode(str, Enum):
EMBOSS = "emboss"
ELECTRONIC = "electronic"
class Compression(str, Enum):
ZIP = "zip"
# This class defines the possibilities of the `Target.platform`` attribute.
class Platform(str, Enum):
# A typical HTML build, meant for self-hosting with no server configuration, features, or assumptions.
WEB = "web"
# Build output meant for hosting on a Runestone server.
RUNESTONE = "runestone"
# Author can specify a method for asymptote generation.
class AsyMethod(str, Enum):
LOCAL = "local"
SERVER = "server"
# See `Target.server`.
class ServerName(str, Enum):
SAGE = "sage"
# Short for Asymptote.
ASY = "asy"
# Possible servers to add: Jing, WeBWorK.
# Allow the author to specify a server instead of a local executable for asset generation. See `Target.server`.
class Server(pxml.BaseXmlModel, tag="server"):
model_config = ConfigDict(extra="forbid")
name: ServerName = pxml.attr()
url: HttpUrl = pxml.attr()
class Target(pxml.BaseXmlModel, tag="target", search_mode=SearchMode.UNORDERED):
"""
Representation of a target for a PreTeXt project: a specific
build targeting a format such as HTML, LaTeX, etc.
"""
model_config = ConfigDict(extra="forbid", str_strip_whitespace=True)
# Provide access to the containing project.
_project: "Project" = PrivateAttr()
# These two attribute are required; everything else is optional.
name: str = pxml.attr()
format: Format = pxml.attr()
# These attributes have simple validators.
#
# A path to the root source for this target, relative to the project's `source` path.
source: Path = pxml.attr(default=Path("main.ptx"))
# A path to the publication file for this target, relative to the project's `publication` path. This is mostly validated by `post_validate`.
publication: Path = pxml.attr(default=None)
latex_engine: LatexEngine = pxml.attr(
name="latex-engine", default=LatexEngine.XELATEX
)
braille_mode: BrailleMode = pxml.attr(
name="braille-mode", default=BrailleMode.EMBOSS
)
stringparams: t.Dict[str, str] = pxml.element(default={})
# Specify whether this target should be included in a deployment
deploy: t.Optional[str] = pxml.attr(name="deploy", default=None)
# A non-default path to the subdirectory of your deployment where this target will live.
deploy_dir: t.Optional[Path] = pxml.attr(name="deploy-dir", default=None)
# Case-check different combos of `deploy` / `deploy-dir`
def to_deploy(self) -> bool:
deploy = self.deploy
if deploy is None:
# didn't specify `deploy`, so deploy iff there's a `deploy_dir`
return self.deploy_dir is not None
else:
# specified `deploy` attr, so deploy iff choice isn't "no"
return deploy.lower() != "no"
# These attributes have complex validators.
# Note that in each case, since we may not have validated the properties we refer to in values, we should use `values.get` instead of `values[]`.
#
# The platform; only valid for an HTML target. See `Platform`. Define this before the other complex validators, since several depend on this value being set.
platform: t.Optional[Platform] = pxml.attr(
default=None,
# Always run this, so we can provide a non-optional value for an HTML target.
validate_default=True,
)
@field_validator("platform")
@classmethod
def platform_validator(
cls, v: t.Optional[Platform], info: ValidationInfo
) -> t.Optional[Platform]:
if info.data.get("format") == Format.HTML:
# For the HTML format, default to the web platform.
if v is None:
return Platform.WEB
else:
if v is not None:
raise ValueError(
"Only the HTML format supports the platform attribute."
)
return v
# We validate compression before output_filename to use its value to check if we can have an output_filename.
compression: t.Optional[Compression] = pxml.attr(default=None)
# Compression is only supported for HTML and WeBWorK formats.
@field_validator("compression")
@classmethod
def compression_validator(
cls, v: t.Optional[Compression], info: ValidationInfo
) -> t.Optional[Compression]:
if (
info.data.get("format") not in (Format.HTML, Format.WEBWORK)
and v is not None
):
raise ValueError("Only the HTML and WeBWorK formats support compression.")
if (
info.data.get("format") == Format.HTML
and info.data.get("platform") == Platform.RUNESTONE
):
raise ValueError(
"The HTML format for the Runestone platform does not allow compression."
)
return v
# A path to the output directory for this target, relative to the project's `output` path.
output_dir: Path = pxml.attr(
name="output-dir",
default=None,
# Make the default value for output be `self.name`. Specifying a `default_factory` won't work, since it's a `@classmethod`. So, use a validator (which has access to the object), replacing `None` with `self.name`.
validate_default=True,
)
@field_validator(
"output_dir",
# Run this before Pydantic's validation, since the default value isn't allowed.
mode="before",
)
@classmethod
def output_dir_validator(cls, v: t.Optional[Path], info: ValidationInfo) -> Path:
# When the format is Runestone, this is overwritten in `post_validate`. Make sure it's not specified.
if (
info.data.get("format") == Format.HTML
and info.data.get("platform") == Platform.RUNESTONE
and v is not None
):
raise ValueError("The Runestone format's output-dir must not be specified.")
return (
# If the `name` isn't set, then we can't build a valid `output_dir`. However, we want to avoid issuing two validation errors in this case, so supply a dummy name instead, since this name will never be used (this Target won't validate).
Path(v)
if v is not None
else Path(info.data.get("name", "no-name-provided"))
)
# A path to the output filename for this target, relative to the `output_dir`. The HTML target cannot specify this (since the HTML output is a directory of files, not a single file.)
output_filename: t.Optional[str] = pxml.attr(
name="output-filename", default=None, validate_default=True
)
@field_validator("output_filename")
@classmethod
def output_filename_validator(
cls, v: t.Optional[str], info: ValidationInfo
) -> t.Optional[str]:
# See if `output-filename` is allowed.
if v is not None:
# uncompressed WeBWorK always produces multiple files, so `output-filename` makes no sense.
if (
info.data.get("format") == Format.WEBWORK
and info.data.get("compression") is None
):
raise ValueError(
"The output-filename must not be present when the format is Webwork."
)
# For the HTML format, non-zipped or Runestone output produces multiple files.
if info.data.get("format") == Format.HTML and (
info.data.get("platform") == Platform.RUNESTONE
or info.data.get("compression") is None
):
raise ValueError(
"The output-filename must not be present when the format is HTML."
)
# Verify that this is just a file name, without any prefixed path.
assert v is None or Path(v).name == v
return v
# The method for generating asymptote files. Overrides the project's `asy_method` if specified.
asy_method: t.Optional[AsyMethod] = pxml.attr(name="asy-method", default=None)
# See `Server`. Each server name (`sage`, `asy`) may be specified only once. If specified, the CLI will use the server for asset generation instead of a local executable, unless @asy-method is set to "local". Settings for a given server name here override settings at the project level.
server: t.List[Server] = pxml.element(default=[])
@field_validator("server")
@classmethod
def server_validator(cls, v: t.List[Server]) -> t.List[Server]:
# Ensure the names are unique.
if len(set([server.name for server in v])) != len(v):
raise ValueError("Server names must not be repeated.")
return v
# A path to custom XSL for this target, relative to the project's `xsl` path.
xsl: t.Optional[Path] = pxml.attr(default=None)
# If the `format == Format.CUSTOM`, then `xsl` must be defined.
@field_validator("xsl")
@classmethod
def xsl_validator(
cls, v: t.Optional[Path], info: ValidationInfo
) -> t.Optional[Path]:
if v is None and info.data.get("format") == Format.CUSTOM:
raise ValueError("A custom format requires a value for xsl.")
return v
# Allow specifying `_project` in the constructor. (Since it's private, pydantic ignores it by default).
def __init__(self, **kwargs: t.Any):
super().__init__(**kwargs)
if "_project" in kwargs:
self._project = kwargs["_project"]
# Since we now have the project, perform validation.
self.post_validate()
# Perform validation which requires the parent `Project` object. This can't be placed in a Pydantic validator, since `self._project` isn't set until after validation finishes. So, this must be manually called after that's done.
def post_validate(self) -> None:
# If no publication file is specified, assume either `publication.ptx` (if it exists) or the CLI's template `publication.ptx` (which always exists). If a publication file is specified, ensure that it exists.
#
# Select a default publication file if it's not provided.
if self.publication is None:
self.publication = Path("publication.ptx")
# If this publication file doesn't exist, ...
if not self.publication_abspath().exists():
# ... then use the CLI's built-in template file.
# TODO: this is wrong, since the returned path is only valid inside the context manager. Instead, need to enter the context here, then exit it when this class is deleted (also problematic).
with resource_base_path() / "templates" / "publication.ptx" as self.publication:
pass
# Otherwise, verify that the provided publication file exists. TODO: It is silly to check that all publication files exist. We warn when they don't. If the target we are calling has a non-existent publication file, then that error will be caught anyway.
else:
p_full = self.publication_abspath()
if not p_full.exists():
log.warning(
f'The target "{self.name}" has a specified publication file that does not exist: {p_full}'
)
# Pass `Project.asy_method` to `Target.asy_method` if it's not specified.
self.asy_method = self.asy_method or self._project.asy_method
# Merge `Project.server` with `self.server`; entries in `self.server` take precedence.
self_server_names = [server.name for server in self.server]
for server in self._project.server:
if server.name not in self_server_names:
self.server.append(server)
# For the Runestone format, determine the `<document-id>`, which specifies the `output_dir`.
if self.format == Format.HTML and self.platform == Platform.RUNESTONE:
# We expect `d_list == ["document-id contents here"]`.
d_list = self.source_element().xpath("/pretext/docinfo/document-id/text()")
if isinstance(d_list, list):
if len(d_list) != 1:
raise ValueError(
"Only one <document-id> is allowed in a PreTeXt document."
)
d = d_list[0]
assert isinstance(d, str)
# Use the correct number of `../` to undo the project's `output-dir`, so the output from the build is located in the correct directory of `published/document-id`.
self.output_dir = Path(
f"{'../'*len(self._project.output_dir.parents)}published/{d}"
)
else:
raise ValueError(
"The <document-id> must be defined for the Runestone format."
)
def source_abspath(self) -> Path:
return self._project.source_abspath() / self.source
def source_element(self) -> ET._Element:
source_doc = ET.parse(self.source_abspath())
for _ in range(25):
source_doc.xinclude()
return source_doc.getroot()
def publication_abspath(self) -> Path:
return self._project.publication_abspath() / self.publication
def output_dir_abspath(self) -> Path:
return self._project.output_dir_abspath() / self.output_dir
def output_dir_relpath(self) -> Path:
return self._project.output_dir / self.output_dir
def deploy_dir_abspath(self) -> Path:
if self.deploy_dir is None:
return self._project.stage_abspath() / self.name
return self._project.stage_abspath() / self.deploy_dir
def deploy_dir_relpath(self) -> Path:
if self.deploy_dir is None:
return self._project.stage / self.name
return self._project.stage / self.deploy_dir
def xsl_abspath(self) -> t.Optional[Path]:
if self.xsl is None:
return None
return self._project.xsl_abspath() / self.xsl
def _read_publication_file_subset(self) -> PublicationSubset:
p_bytes = self.publication_abspath().read_bytes()
return PublicationSubset.from_xml(p_bytes)
def external_dir(self) -> Path:
return self._read_publication_file_subset().external
def external_dir_abspath(self) -> Path:
return (self.source_abspath().parent / self.external_dir()).resolve()
def generated_dir(self) -> Path:
return self._read_publication_file_subset().generated
def generated_dir_abspath(self) -> Path:
return (self.source_abspath().parent / self.generated_dir()).resolve()
def ensure_asset_directories(self, asset: t.Optional[str] = None) -> None:
self.external_dir_abspath().mkdir(parents=True, exist_ok=True)
self.generated_dir_abspath().mkdir(parents=True, exist_ok=True)
if asset is not None:
# make directories for each asset type that would be generated from "asset":
for asset_dir in constants.ASSET_TO_DIR[asset]:
(self.generated_dir_abspath() / asset_dir).mkdir(
parents=True, exist_ok=True
)
def ensure_output_directory(self) -> None:
log.debug(
f"Ensuring output directory for {self.name}: {self.output_dir_abspath()}"
)
self.output_dir_abspath().mkdir(parents=True, exist_ok=True)
def load_asset_table(self) -> pt.AssetTable:
"""
Loads the asset table from a pickle file in the generated assets directory
based on the target name.
"""
try:
with open(
self.generated_dir_abspath() / f".{self.name}_assets.pkl", "rb"
) as f:
return pickle.load(f)
except Exception:
return {}
def generate_asset_table(self) -> pt.AssetTable:
"""
Returns a hash table (dictionary) with keys the assets present in the current target's source, each having a value that is a dictionary of xml:ids mapped to the hash of the assets below that xmlid of that type.
ex: {latex-image: {img1: <hash>, img_another: <hash>}, asymptote: {asy_img_1: <hash>}}.
"""
asset_hash_dict: pt.AssetTable = {}
for asset in constants.ASSET_TO_XPATH.keys():
if asset == "webwork":
# WeBWorK must be regenerated every time *any* of the ww exercises change.
ww = self.source_element().xpath(".//webwork[@*|*]")
assert isinstance(ww, t.List)
if len(ww) == 0:
# Only generate a hash if there are actually ww exercises in the source
continue
asset_hash_dict[asset] = {}
h = hashlib.sha256()
for node in ww:
assert isinstance(node, ET._Element)
h.update(ET.tostring(node).strip())
asset_hash_dict["webwork"][""] = h.digest()
else:
# everything else can be updated individually.
# get all the nodes for the asset attribute
source_assets = self.source_element().xpath(
constants.ASSET_TO_XPATH[asset]
)
assert isinstance(source_assets, t.List)
if len(source_assets) == 0:
# Only generate a hash if there are actually assets of this type in the source
continue
# We will have a dictionary of id's that we will get their own hash:
asset_hash_dict[asset] = {}
hash_ids = {}
for node in source_assets:
assert isinstance(node, ET._Element)
# assign the xml:id of the youngest ancestor of the node with an xml:id as the node's id (or "" if none)
ancestor_xmlids = node.xpath("ancestor::*/@xml:id")
assert isinstance(ancestor_xmlids, t.List)
id = str(ancestor_xmlids[-1]) if len(ancestor_xmlids) > 0 else ""
assert isinstance(id, str)
# create a new hash object when id is first encountered
if id not in hash_ids:
hash_ids[id] = hashlib.sha256()
# update the hash with the node's xml:
hash_ids[id].update(ET.tostring(node).strip())
# and update the value of the hash for that asset/id pair
asset_hash_dict[asset][id] = hash_ids[id].digest()
return asset_hash_dict
def save_asset_table(self, asset_table: pt.AssetTable) -> None:
"""
Saves the asset_table to a pickle file in the generated assets directory
based on the target name.
"""
with open(self.generated_dir_abspath() / f".{self.name}_assets.pkl", "wb") as f:
pickle.dump(asset_table, f)
def ensure_webwork_reps(self) -> None:
"""
Ensures that the webwork representation file is present if the source contains webwork problems. This is needed to build or generate other assets.
"""
if self.source_element().xpath(".//webwork[@*|*]"):
log.debug("Source contains webwork problems")
if not (
self.generated_dir_abspath() / "webwork" / "webwork-representations.xml"
).exists():
log.debug("Webwork representations file does not exist, generating")
self.generate_assets(
requested_asset_types=["webwork"], only_changed=False
)
else:
log.debug("Webwork representations file exists, not generating")
else:
log.debug("Source does not contain webwork problems")
def ensure_play_button(self) -> None:
try:
core.play_button(dest_dir=(self.generated_dir_abspath() / "play-button"))
log.debug("Play button generated")
except Exception as e:
log.warning(f"Failed to generate play button: {e}")
def clean_output(self) -> None:
# refuse to clean if output is not a subdirectory of the project or contains source/publication
if self._project.abspath() not in self.output_dir_abspath().parents:
log.warning(
"Refusing to clean output directory that isn't a proper subdirectory of the project."
)
# handle request to clean directory that does not exist
elif not self.output_dir_abspath().exists():
log.warning(
f"Directory {self.output_dir_abspath()} already does not exist, nothing to clean."
)
# destroy the output directory
else:
log.warning(
f"Destroying directory {self.output_dir_abspath()} to clean previously built files."
)
shutil.rmtree(self.output_dir_abspath())
def build(
self,
clean: bool = False,
generate: bool = True,
xmlid: t.Optional[str] = None,
no_knowls: bool = False,
) -> None:
# Check for xml syntax errors and quit if xml invalid:
if not utils.xml_syntax_is_valid(self.source_abspath()):
raise RuntimeError("XML syntax for source file is invalid")
if not utils.xml_syntax_is_valid(self.publication_abspath(), "publication"):
raise RuntimeError("XML syntax for publication file is invalid")
# Validate xml against schema; continue with warning if invalid:
utils.xml_source_validates_against_schema(self.source_abspath())
# Clean output upon request
if clean:
self.clean_output()
# Ensure the asset directories exist.
self.ensure_asset_directories()
# verify that a webwork_representations.xml file exists if it is needed; generated if needed.
self.ensure_webwork_reps()
# Generate needed assets unless requested not to.
if generate:
self.generate_assets(xmlid=xmlid)
# Ensure the output directories exist.
self.ensure_output_directory()
# Modify stringparams for no_knowls
if no_knowls:
self.stringparams["debug.skip-knowls"] = "yes"
# Proceed with the build
with tempfile.TemporaryDirectory(prefix="pretext_") as tmp_xsl_str:
tmp_xsl_path = Path(tmp_xsl_str)
# if custom xsl, copy it into a temporary directory (different from the building temporary directory)
if (txp := self.xsl_abspath()) is not None:
log.info(f"Building with custom xsl {txp}")
utils.copy_custom_xsl(txp, tmp_xsl_path)
custom_xsl = tmp_xsl_path / txp.name
else:
custom_xsl = None
# warn if "publisher" is one of the string-param keys:
if "publisher" in self.stringparams:
log.warning(
"You specified a publication file via a stringparam. "
+ "This is ignored in favor of the publication file given by the "
+ "<publication> element in the project manifest."
)
log.info(f"Preparing to build into {self.output_dir_abspath()}.")
# The core expects `out_file` to be the full path, not just a file name, if it's not None.
out_file = (
(self.output_dir_abspath() / self.output_filename).as_posix()
if self.output_filename is not None
else None
)
# The copy allows us to modify string params without affecting the original,
# and avoids issues with core modifying string params
stringparams_copy = self.stringparams.copy()
if self.format == Format.HTML:
if self.platform == Platform.RUNESTONE:
# The validator guarantees this.
assert self.compression is None
assert self.output_filename is None
# This is equivalent to setting `<platform host="runestone">` in the publication file.
stringparams_copy.update({"host-platform": "runestone"})
core.html(
xml=self.source_abspath(),
pub_file=self.publication_abspath().as_posix(),
stringparams=stringparams_copy,
xmlid_root=xmlid,
file_format=self.compression or "html",
extra_xsl=custom_xsl,
out_file=out_file,
dest_dir=self.output_dir_abspath().as_posix(),
)
codechat.map_path_to_xml_id(
self.source_abspath(),
self._project.abspath(),
self.output_dir_abspath().as_posix(),
)
elif self.format == Format.PDF:
core.pdf(
xml=self.source_abspath(),
pub_file=self.publication_abspath().as_posix(),
stringparams=stringparams_copy,
extra_xsl=custom_xsl,
out_file=out_file,
dest_dir=self.output_dir_abspath().as_posix(),
method=self.latex_engine,
)
elif self.format == Format.LATEX:
core.latex(
xml=self.source_abspath(),
pub_file=self.publication_abspath().as_posix(),
stringparams=stringparams_copy,
extra_xsl=custom_xsl,
out_file=out_file,
dest_dir=self.output_dir_abspath().as_posix(),
)
utils.manage_directories(
self.output_dir_abspath(),
external_abs=self.external_dir_abspath(),
generated_abs=self.generated_dir_abspath(),
)
elif self.format == Format.EPUB:
utils.npm_install()
core.epub(
xml_source=self.source_abspath(),
pub_file=self.publication_abspath().as_posix(),
out_file=out_file,
dest_dir=self.output_dir_abspath().as_posix(),
math_format="svg",
stringparams=stringparams_copy,
)
elif self.format == Format.KINDLE:
utils.npm_install()
core.epub(
xml_source=self.source_abspath(),
pub_file=self.publication_abspath().as_posix(),
out_file=out_file,
dest_dir=self.output_dir_abspath().as_posix(),
math_format="kindle",
stringparams=stringparams_copy,
)
elif self.format == Format.BRAILLE:
log.warning(
"Braille output is still experimental, and requires additional libraries from liblouis (specifically the file2brl software)."
)
utils.npm_install()
core.braille(
xml_source=self.source_abspath(),
pub_file=self.publication_abspath().as_posix(),
out_file=out_file,
dest_dir=self.output_dir_abspath().as_posix(),
page_format=self.braille_mode,
stringparams=stringparams_copy,
)
elif self.format == Format.WEBWORK:
core.webwork_sets(
xml_source=self.source_abspath(),
pub_file=self.publication_abspath().as_posix(),
stringparams=stringparams_copy,
dest_dir=self.output_dir_abspath().as_posix(),
tgz=self.compression,
)
elif self.format == Format.CUSTOM:
# Need to add the publication file to string params since xsltproc function doesn't include pubfile.
stringparams_copy["publisher"] = self.publication_abspath().as_posix()
core.xsltproc(
xsl=custom_xsl,
xml=self.source_abspath(),
result=out_file,
output_dir=self.output_dir_abspath().as_posix(),
stringparams=stringparams_copy,
)
utils.manage_directories(
self.output_dir_abspath(),
external_abs=self.external_dir_abspath(),
generated_abs=self.generated_dir_abspath(),
)
else:
log.critical(f"Unknown format {self.format}")
def generate_assets(
self,
requested_asset_types: t.Optional[t.List[str]] = None,
all_formats: bool = False,
only_changed: bool = True,
xmlid: t.Optional[str] = None,
pymupdf: bool = False,
) -> None:
"""
Generates assets for the current target. Options:
- requested_asset_types: optional list of which assets to generate (latex-image, sagemath, asymptote, etc). Default will generate all asset types found in target.
- all_formats: boolean to decide whether the output format of the assets will be just those that the target format uses (default/False) or all possible output formats for that asset (True).
- only_changed: boolean. When True (default), function will only generate assets that have changed since last generation. When False, all assets will be built (hash table will be ignored).
- xmlid: optional string to specify the root of the subtree of the xml document to generate assets within.
- pymupdf: temporary boolean to test alternative image generation with pymupdf instead of external programs.
"""
# Start by getting the assets that need to be generated for the particular target. This will either be all of them, or just the asset type that was specifically requested.
if requested_asset_types is None or "ALL" in requested_asset_types:
requested_asset_types = list(constants.ASSET_TO_XPATH.keys())
log.debug(f"Assets generation requested for: {requested_asset_types}.")
requested_asset_types = [
asset
for asset in requested_asset_types
if asset in constants.ASSETS_BY_FORMAT[self.format]
]
log.debug(
f"Based on format {self.format}, assets to be generated are: {requested_asset_types}."
)
# We always build the asset hash table, even if only_changed=True: this tells us which assets need to be built, and how to update the saved asset hash table at the end of the method.
# utils.clean_asset_table purges any saved assets that are no longer in the target.
source_asset_table = self.generate_asset_table()
saved_asset_table = utils.clean_asset_table(
self.load_asset_table(), source_asset_table
)
log.debug(f"Starting asset table: {source_asset_table}")
# Throw away any asset types that were not requested:
source_asset_table = {
asset: source_asset_table[asset]
for asset in source_asset_table
if asset in requested_asset_types
}
# Throw away requested assets if they are not in source:
requested_asset_types = [asset for asset in source_asset_table]
log.debug(
f"Based on what is in your source, the assets that will be considered are {requested_asset_types}."
)
# For each asset type, we need to keep track of whether to build all of the assets (possibly only below the xml:id given) or generate some one-at-a-time.
# Cases when we would build all:
# 1. only_change=False,
# 2. this is the first time building that asset type (in which case, there will be no instances of that asset in saved_asset_table), or
# 3. There are lots of assets that have changed (so it would be more efficient to call core once).
# We first we create a list of asset types that we will generate all instances of
full_generate = []
partial_generate = []
if not only_changed:
full_generate = requested_asset_types.copy()
else:
for asset in requested_asset_types:
if asset not in saved_asset_table:
full_generate.append(asset)
else:
partial_generate.append(asset)
# (at least for now, later we might move some of these to full_generate
# Now we repeatedly pass through the source asset table, and purge any assets that we shouldn't build for any reason.
# If we limit by xml:id, only look for assets below that id in the source tree
if xmlid is not None:
log.debug(f"Limiting asset generation to assets below xml:id={xmlid}.")
# Keep webwork if only there is a webwork below the xmlid:
ww_nodes = self.source_element().xpath(f"//*[@xml:id='{xmlid}']//webwork")
assert isinstance(ww_nodes, t.List)
if len(ww_nodes) == 0:
source_asset_table.pop("webwork", None)
# All other assets: we only need to keep the assets whose id is not above the xmlid (we would have used the xmlid as their id if there wasn't any other xmlid below it):
# Get list of xml:ids below 'xmlid':
id_list = self.source_element().xpath(f"//*[@xml:id='{xmlid}']//@xml:id")
assert isinstance(id_list, t.List)
# Filter by non-webwork assets whose id is in ID list:
# Note: if an id = "", that means that no ancestor of that asset had an id, which means that it would not be a child of the xml:id we are subsetting.
log.debug(f"id list: {id_list}")
for asset in source_asset_table.copy():
if asset != "webwork":
source_asset_table[asset] = {
id: source_asset_table[asset][id]
for id in source_asset_table[asset]
if id in id_list
}
if len(source_asset_table[asset]) == 0:
source_asset_table.pop(asset, None)
log.debug(f"Eligible assets are: {source_asset_table}")
# Prune the list of assets based on what is left
full_generate = [
asset for asset in full_generate if asset in source_asset_table
]
partial_generate = [
asset for asset in partial_generate if asset in source_asset_table
]
log.debug(
f"Partial generate assets are: {partial_generate}, full generate assets are {full_generate}"
)
# TODO: check which assets can be generated based on the user's system (and executables).
# Now for any asset type in `partial_generate`, we looks for the assets that need to be regenerated because they don't match the previous hash.
for asset in partial_generate.copy():
log.debug(
f"Checking whether any {asset} assets have changed and need to be regenerated."
)
source_asset_table[asset] = {
id: source_asset_table[asset][id]
for id in source_asset_table[asset]
if saved_asset_table.get(asset, {}).get(id, None)
!= source_asset_table[asset][id]
}
# If there are no assets of that type left, remove it from our list:
log.debug(f"no {asset} assets have changed.")
if len(source_asset_table[asset]) == 0:
partial_generate.remove(asset)
log.debug(
f"Assets to be regenerated: all: {full_generate}, partial: {partial_generate}"
)
# Create a dictionary with `asset: [ids]` that will be built. For assets that will be built fully, `[ids] = [xmlid]` (where `xmlid` could be `None`)
assets_to_generate = {}
for asset in full_generate:
assets_to_generate[asset] = [xmlid]
for asset in partial_generate:
assets_to_generate[asset] = [id for id in source_asset_table[asset]]
log.debug(f"Assets to be generated: {assets_to_generate}")
# Now further limit the assets to be built by those that have changed since the last build, if only_changed is true. Either way create a dictionary of asset: [ids] to be built, where asset:[] means to generate all of them.
# TODO: check if there are too many individual assets to make generating individually is worthwhile.
# Now we have the correct list of assets we want to build.
# We proceed to generate the assets that were requested.
for asset in assets_to_generate:
self.ensure_asset_directories(asset)
# Check if all formats are requested and modify accordingly.
asset_formats = constants.ASSET_FORMATS[self.format]
if all_formats:
for asset in assets_to_generate:
asset_formats[asset] = ["all"]
# We will keep track of the assets that were successful to update cache at the end.
successful_assets: t.List[t.Tuple[str, t.Optional[str]]] = []
# The copy allows us to modify string params without affecting the original,
# and avoids issues with core modifying string params
stringparams_copy = self.stringparams.copy()
# generate assets by calling appropriate core functions :
if "webwork" in assets_to_generate:
try:
core.webwork_to_xml(
xml_source=self.source_abspath(),
pub_file=self.publication_abspath().as_posix(),
stringparams=stringparams_copy,
xmlid_root=xmlid,
abort_early=False,
dest_dir=(self.generated_dir_abspath() / "webwork").as_posix(),
server_params=None,
)
successful_assets.append(("webwork", None))
except Exception as e:
log.error(
"Unable to generate webwork. If you already have a webwork-representations.xml file, this might result in unpredictable behavior."
)
log.warning(e)
log.debug(e, exc_info=True)
if "latex-image" in assets_to_generate:
for id in assets_to_generate["latex-image"]:
log.debug(f"Generating latex-image assets for {id}")
try:
for outformat in asset_formats["latex-image"]:
core.latex_image_conversion(
xml_source=self.source_abspath(),
pub_file=self.publication_abspath().as_posix(),
stringparams=stringparams_copy,
xmlid_root=id,
dest_dir=self.generated_dir_abspath() / "latex-image",
outformat=outformat,
method=self.latex_engine,
pyMuPDF=pymupdf,
)
successful_assets.append(("latex-image", id))
except Exception as e:
log.error(f"Unable to generate some latex-image assets:\n {e}")
log.debug(e, exc_info=True)
if "asymptote" in assets_to_generate:
for id in assets_to_generate["asymptote"]:
try:
for outformat in asset_formats["asymptote"]:
core.asymptote_conversion(
xml_source=self.source_abspath(),
pub_file=self.publication_abspath().as_posix(),
stringparams=stringparams_copy,
xmlid_root=id,
dest_dir=self.generated_dir_abspath() / "asymptote",
outformat=outformat,
method=self.asy_method,
)
successful_assets.append(("asymptote", id))
except Exception as e:
log.error(f"Unable to generate some asymptote elements: \n{e}")
log.debug(e, exc_info=True)
if "sageplot" in assets_to_generate:
for id in assets_to_generate["sageplot"]:
try:
for outformat in asset_formats["sageplot"]:
core.sage_conversion(
xml_source=self.source_abspath(),
pub_file=self.publication_abspath().as_posix(),
stringparams=stringparams_copy,
xmlid_root=id,
dest_dir=self.generated_dir_abspath() / "sageplot",
outformat=outformat,
)
successful_assets.append(("sageplot", id))
except Exception as e:
log.error(f"Unable to generate some sageplot images:\n {e}")
log.debug(e, exc_info=True)
if "interactive" in assets_to_generate:
# Ensure playwright is installed:
utils.playwright_install()
for id in assets_to_generate["interactive"]:
try:
core.preview_images(
xml_source=self.source_abspath(),
pub_file=self.publication_abspath().as_posix(),
stringparams=stringparams_copy,
xmlid_root=id,
dest_dir=self.generated_dir_abspath() / "preview",
)
successful_assets.append(("interactive", id))
except Exception as e:
log.error(f"Unable to generate some interactive previews: \n{e}")
log.debug(e, exc_info=True)
if "youtube" in assets_to_generate:
for id in assets_to_generate["youtube"]:
try:
core.youtube_thumbnail(
xml_source=self.source_abspath(),
pub_file=self.publication_abspath().as_posix(),
stringparams=stringparams_copy,
xmlid_root=id,
dest_dir=self.generated_dir_abspath() / "youtube",
)
successful_assets.append(("youtube", id))
except Exception as e:
log.error(f"Unable to generate some youtube thumbnails: \n{e}")
log.debug(e, exc_info=True)
# youtube also requires the play button.
self.ensure_play_button()
if "mermaid" in assets_to_generate:
for id in assets_to_generate["mermaid"]:
try:
core.mermaid_images(
xml_source=self.source_abspath(),
pub_file=self.publication_abspath().as_posix(),
stringparams=stringparams_copy,
xmlid_root=id,
dest_dir=self.generated_dir_abspath() / "mermaid",
)
successful_assets.append(("mermaid", id))
except Exception as e:
log.error(f"Unable to generate some mermaid images: \n{e}")
log.debug(e, exc_info=True)
if "codelens" in assets_to_generate:
for id in assets_to_generate["codelens"]:
try:
core.tracer(
xml_source=self.source_abspath(),
pub_file=self.publication_abspath().as_posix(),
stringparams=stringparams_copy,
xmlid_root=id,
dest_dir=self.generated_dir_abspath() / "trace",
)
successful_assets.append(("codelens", id))
except Exception as e:
log.error(f"Unable to generate some codelens traces: \n{e}")
log.debug(e, exc_info=True)
if "datafile" in assets_to_generate:
for id in assets_to_generate["datafile"]:
log.debug(f"Generating datafile assets for {id}")
try:
core.datafiles_to_xml(
xml_source=self.source_abspath(),
pub_file=self.publication_abspath().as_posix(),
stringparams=stringparams_copy,
xmlid_root=id,
dest_dir=self.generated_dir_abspath() / "datafile",
)
successful_assets.append(("datafile", id))
except Exception as e:
log.error(f"Unable to generate some datafiles:\n {e}")
log.debug(e, exc_info=True)
# Finally, also generate the qrcodes for interactive and youtube assets:
# NOTE: we do not currently check for success of this for saving assets to the asset cache.
if "interactive" in assets_to_generate or "youtube" in assets_to_generate:
for id in set(
assets_to_generate.get("interactive", [])
+ assets_to_generate.get("youtube", [])
):
try:
core.qrcode(
xml_source=self.source_abspath(),
pub_file=self.publication_abspath().as_posix(),
stringparams=stringparams_copy,
xmlid_root=id,
dest_dir=self.generated_dir_abspath() / "qrcode",
)
except Exception as e:
log.error(f"Unable to generate some qrcodes:\n {e}", exc_info=True)
log.debug(e, exc_info=True)
# Delete temporary directories left behind by core:
try:
core.release_temporary_directories()
except Exception as e:
log.error(
"Unable to release temporary directories. Please report this error to pretext-support"
)
log.debug(e, exc_info=True)
# After all assets are generated, update the asset cache:
log.debug(f"Updated these assets successfully: {successful_assets}")