Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

codechat wip #747

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions pretext/project/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1457,3 +1457,44 @@ def generate_boilerplate(
):
project_resource_path.write_text(requirements_txt)
log.info(f"Generated `{project_resource_path}`\n")

def generate_mapping(self) -> None:
"""
Generates a JSON string that describes how to map target/xmlid pairs to the path of
the relevant html file output
"""
parser = ET.XMLParser(huge_tree=True)
ptx_common_path = core.resources.path("xsl", "pretext-common.xsl")
enhance_xslt_path = core.resources.path("xsl", "utilities", "pretext-enhanced-source.xsl")
with tempfile.TemporaryDirectory(prefix="pretext_") as tmp_dir:
with open(Path(tmp_dir)/"filename.xsl", mode="w") as f:
print(f'''
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:import href="{ptx_common_path}"/>
<xsl:output method="xml"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[@unique-id]">
<xsl:copy>
<xsl:attribute name="output-filename">
<xsl:apply-templates select="." mode="containing-filename"/>
</xsl:attribute>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
''', file=f)
for target in [t for t in self.targets if t.format == 'html']:
# run pretext-enhanced-source.xsl on target
source_tree = ET.parse(target.source_abspath(), parser=parser)
source_tree.xinclude()
enhance_xslt_tree = ET.parse(enhance_xslt_path)
enhance_xslt = ET.XSLT(enhance_xslt_tree)
enhanced_tree = enhance_xslt(source_tree)
filename_xslt_tree = ET.parse(Path(tmp_dir)/"filename.xsl", parser=parser)
filename_xslt = ET.XSLT(filename_xslt_tree)
filename_tree = filename_xslt(enhanced_tree)
print(filename_tree)
Loading