Skip to content

Commit 35c9028

Browse files
authored
Webwork files (#1154)
* first try * add per-problem webwork representation file checking * format * fix types * update to latest core commit
1 parent 2fb785c commit 35c9028

3 files changed

Lines changed: 48 additions & 12 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ Instructions: Add a subsection under `[Unreleased]` for additions, fixes, change
99

1010
## [Unreleased]
1111

12+
### Changed
13+
14+
- WeBWorK representations now use individual files for each problem. Added checking for these files as they are needed in the build process.
15+
1216
## [2.39.0] - 2026-05-10
1317

1418
Includes updates to core through commit: [b169423](https://github.com/PreTeXtBook/pretext/commit/b16942370f7710784c73c28349c62d4f368b3825)

pretext/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
VERSION = get_version("pretext", Path(__file__).parent.parent)
2020

2121

22-
CORE_COMMIT = "b16942370f7710784c73c28349c62d4f368b3825"
22+
CORE_COMMIT = "79697cb952a34018efb660b4d1c37706081d4799"
2323

2424

2525
def activate() -> None:

pretext/project/__init__.py

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,10 @@ class Target(pxml.BaseXmlModel, tag="target", search_mode=SearchMode.UNORDERED):
138138
#
139139
# A path to the root source for this target, relative to the project's `source` path.
140140
source: Path = pxml.attr(default=Path("main.ptx"))
141-
# Cache of assembled source element.
141+
# Cache of assembled "version only" source element.
142142
_source_element: t.Optional[ET._Element] = None
143+
# Cache of assembled with assembly-ids.
144+
_source_element_with_ids: t.Optional[ET._Element] = None
143145
# A path to the publication file for this target, relative to the project's `publication` path. This is mostly validated by `post_validate`.
144146
publication: Path = pxml.attr(default=None)
145147
latex_engine: LatexEngine = pxml.attr(
@@ -403,6 +405,7 @@ def source_element(self) -> ET._Element:
403405
Caches the result for future calls.
404406
"""
405407
if self._source_element is None:
408+
406409
log.debug(
407410
f"Parsing source element for target {self.name}",
408411
)
@@ -416,6 +419,24 @@ def source_element(self) -> ET._Element:
416419
log.debug(f"Using cached source_element for target {self.name}")
417420
return self._source_element
418421

422+
def source_element_with_ids(self) -> ET._Element:
423+
"""
424+
Returns the root element for the assembled source, after processing with assembly-id method. Caches the result for future calls.
425+
"""
426+
if self._source_element_with_ids is None:
427+
log.debug(
428+
f"Parsing source element with assembly ids for target {self.name}",
429+
)
430+
self._source_element_with_ids = core.assembly_internal(
431+
xml=self.source_abspath(),
432+
pub_file=self.publication_abspath().as_posix(),
433+
stringparams=self.stringparams.copy(),
434+
method="assembly-id",
435+
).getroot()
436+
else:
437+
log.debug(f"Using cached source_element_with_ids for target {self.name}")
438+
return self._source_element_with_ids
439+
419440
def publication_abspath(self) -> Path:
420441
return self._project.publication_abspath() / self.publication
421442

@@ -598,18 +619,29 @@ def ensure_webwork_reps(self) -> None:
598619
"""
599620
Ensures that the webwork representation file is present if the source contains webwork problems. This is needed to build or generate other assets.
600621
"""
601-
# NB: need to include `text()` as well as `@*|*` in the xpath, since some webwork problems are only included as text/pg source.
602-
if self.source_element().xpath(".//webwork[@*|*|text()]"):
622+
# NB: need to include `text()` as well as `@copy|@source|*` in the xpath, since some webwork problems are only included as text/pg source.
623+
if self.source_element().xpath(".//webwork[@copy|@source|*|text()]"):
603624
log.debug("Source contains webwork problems")
604-
if not (
605-
self.generated_dir_abspath() / "webwork" / "webwork-representations.xml"
606-
).exists():
607-
log.debug("Webwork representations file does not exist, generating")
608-
self.generate_assets(
609-
requested_asset_types=["webwork"], only_changed=False
610-
)
625+
# Get list of all assembly-ids for webwork (this will be the assembly-id of the parent of the webwork element).
626+
webwork_assembly_ids = self.source_element_with_ids().xpath(
627+
".//webwork[@copy|@source|*|text()]/parent::*/@assembly-id"
628+
)
629+
assert isinstance(webwork_assembly_ids, list)
630+
for id in webwork_assembly_ids:
631+
assert isinstance(id, str)
632+
filename = id + ".xml"
633+
if not (self.generated_dir_abspath() / "webwork" / filename).exists():
634+
log.debug(
635+
f'At least one WeBWorK representation file (for webwork problem with id "{id}") does not exist, generating'
636+
)
637+
self.generate_assets(
638+
requested_asset_types=["webwork"], only_changed=False
639+
)
640+
break
611641
else:
612-
log.debug("Webwork representations file exists, not generating")
642+
log.debug(
643+
"All WeBWorK representation files already exist, not generating"
644+
)
613645
else:
614646
log.debug("Source does not contain webwork problems")
615647

0 commit comments

Comments
 (0)