Skip to content

Commit

Permalink
generate prefigure assets using prefigure namespace (#864)
Browse files Browse the repository at this point in the history
* generate prefigure assets using prefigure namespace

* format

* add test for prefigure

* update changelog

* format

---------

Co-authored-by: Oscar Levin <[email protected]>
  • Loading branch information
davidaustinm and oscarlevin authored Nov 22, 2024
1 parent 41f08b3 commit 8b480a7
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 47 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ Instructions: Add a subsection under `[UNRELEASED]` for additions, fixes, change

## [UNRELEASED]

### Fixed

- prefigure graphics were not not being recognized.

## [2.9.1] - 2024-11-09

### Changed
Expand Down
2 changes: 1 addition & 1 deletion pretext/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@
"latex-image": ".//latex-image",
"sageplot": ".//sageplot",
"asymptote": ".//asymptote",
"prefigure": ".//prefigure",
"prefigure": ".//pf:prefigure",
"youtube": ".//video[@youtube]",
"codelens": ".//program[@interactive = 'codelens']",
"datafile": ".//datafile",
Expand Down
3 changes: 2 additions & 1 deletion pretext/project/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,7 @@ def generate_asset_table(self) -> pt.AssetTable:
ex: {latex-image: {img1: <hash>, img_another: <hash>}, asymptote: {asy_img_1: <hash>}}.
"""
asset_hash_dict: pt.AssetTable = {}
ns = {"pf": "https://prefigure.org"}
for asset in constants.ASSET_TO_XPATH.keys():
if asset == "webwork":
# WeBWorK must be regenerated every time *any* of the ww exercises change.
Expand All @@ -445,7 +446,7 @@ def generate_asset_table(self) -> pt.AssetTable:
# 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]
constants.ASSET_TO_XPATH[asset], namespaces=ns
)
assert isinstance(source_assets, t.List)
if len(source_assets) == 0:
Expand Down
34 changes: 0 additions & 34 deletions tests/examples/projects/asymptote/source/main.ptx

This file was deleted.

59 changes: 59 additions & 0 deletions tests/examples/projects/graphics/source/main.ptx
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?xml version="1.0" encoding="UTF-8"?>

<pretext xmlns:xi="http://www.w3.org/2001/XInclude">
<book>
<title>Asymptote</title>
<chapter xml:id="ch-foo">
<title>foo</title>
<image xml:id="test">
<asymptote>
size(4cm,6cm);
pen canadared=rgb(235/256,45/256,55/256);
real flagwidth=4, flagheight=2;
path flag_outline=scale(flagwidth,flagheight)*unitsquare;
path cbar1=scale(1,2)*unitsquare, cbar2=shift(3,0)*cbar1;
path mapleleafleft=
(0,-102) --(-5,-102)--(-2,-56) {dir(87)}..{dir(190)}
(-8,-53) --(-51,-61)--(-45,-45){dir(70)}..{dir(141)}
(-46,-41)--(-94,-3) --(-82,1) {dir(25)}..{dir(108)}
(-81,6) --(-90,34) --(-63,29) {dir(348)}..{dir(67)}
(-59,30) --(-54,43) --(-33,20) {dir(313)}..{dir(101)}
(-27,23) --(-38,76) --(-21,62) {dir(330)}..{dir(63)}
(-16,67) --(0,100);
path mapleleafright=reflect((0,0),(0,1))*reverse(mapleleafleft);
path mapleleaf=mapleleafleft--mapleleafright--cycle;
filldraw(flag_outline,white,black);
fill(cbar1,canadared);
fill(cbar2,canadared);
fill(shift(2,1)*scale(.008)*mapleleaf,canadared);
draw(flag_outline);
</asymptote>
</image>

<image width="60%">
<prefigure label="pftest"
xmlns="https://prefigure.org">
<diagram dimensions="(300,300)" margins="5">
<definition>f(t,y) = (y[1], -pi*y[0]-0.3*y[1])</definition>
<coordinates bbox="[-1,-3,6,3]">
<grid-axes xlabel="t"/>
<de-solve function="f" t0="0" t1="bbox[2]"
y0="(0,2)" name="oscillator"
N="200"/>
<plot-de-solution at="x" solution="oscillator"
axes="(t,y0)" />
<plot-de-solution at="xprime" solution="oscillator"
axes="(t,y1)" stroke="red"
tactile-dash="9 9"/>
<legend at="legend" anchor="(bbox[2], bbox[3])"
alignment="sw" scale="0.9" opacity="0.5">
<item ref="x"><m>x(t)</m></item>
<item ref="xprime"><m>x'(t)</m></item>
</legend>
</coordinates>
</diagram>
</prefigure>
</image>
</chapter>
</book>
</pretext>
27 changes: 16 additions & 11 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,23 +123,28 @@ def test_init(tmp_path: Path, script_runner: ScriptRunner) -> None:
assert (tmp_path / constants.PROJECT_RESOURCES[resource]).exists()


def test_generate_asymptote(tmp_path: Path, script_runner: ScriptRunner) -> None:
asy_path = tmp_path / "asymptote"
shutil.copytree(EXAMPLES_DIR / "projects" / "asymptote", asy_path)
def test_generate_graphics(tmp_path: Path, script_runner: ScriptRunner) -> None:
graphics_path = tmp_path / "graphics"
shutil.copytree(EXAMPLES_DIR / "projects" / "graphics", graphics_path)
assert script_runner.run(
[PTX_CMD, "-v", "debug", "generate", "asymptote"], cwd=asy_path
[PTX_CMD, "-v", "debug", "generate", "asymptote"], cwd=graphics_path
).success
assert (asy_path / "generated-assets" / "asymptote" / "test.html").exists()
os.remove(asy_path / "generated-assets" / "asymptote" / "test.html")
assert (graphics_path / "generated-assets" / "asymptote" / "test.html").exists()
os.remove(graphics_path / "generated-assets" / "asymptote" / "test.html")
assert script_runner.run(
[PTX_CMD, "-v", "debug", "generate", "-x", "test"], cwd=asy_path
[PTX_CMD, "-v", "debug", "generate", "-x", "test"], cwd=graphics_path
).success
assert (asy_path / "generated-assets" / "asymptote" / "test.html").exists()
os.remove(asy_path / "generated-assets" / "asymptote" / "test.html")
assert (graphics_path / "generated-assets" / "asymptote" / "test.html").exists()
os.remove(graphics_path / "generated-assets" / "asymptote" / "test.html")
assert script_runner.run(
[PTX_CMD, "-v", "debug", "generate", "asymptote", "-t", "web"], cwd=asy_path
[PTX_CMD, "-v", "debug", "generate", "asymptote", "-t", "web"],
cwd=graphics_path,
).success
os.remove(asy_path / "generated-assets" / "asymptote" / "test.html")
os.remove(graphics_path / "generated-assets" / "asymptote" / "test.html")
assert script_runner.run(
[PTX_CMD, "-v", "debug", "generate", "prefigure"], cwd=graphics_path
).success
assert (graphics_path / "generated-assets" / "prefigure" / "pftest.svg").exists()


# @pytest.mark.skip(
Expand Down

0 comments on commit 8b480a7

Please sign in to comment.