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

Quick dirty html build #2329

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
39 changes: 30 additions & 9 deletions pretext/pretext.py
Original file line number Diff line number Diff line change
Expand Up @@ -3438,6 +3438,12 @@ def _parse_runestone_services(et):

return (rs_js, rs_css, rs_cdn_url, rs_version)

# Update stringparams with Runestone Services information
def _set_runestone_stringparams(stringparams, rs_js, rs_css, rs_version):
stringparams["rs-js"] = rs_js
stringparams["rs-css"] = rs_css
stringparams["rs-version"] = rs_version

# A helper function to query the latest Runestone
# Services file, while failing gracefully

Expand Down Expand Up @@ -3487,9 +3493,7 @@ def _runestone_services(stringparams):
rs_version = "dev"
services_xml = None
# Return, plus side-effect
stringparams["rs-js"] = rs_js
stringparams["rs-css"] = rs_css
stringparams["rs-version"] = rs_version
_set_runestone_stringparams(stringparams, rs_js, rs_css, rs_version)
return (rs_js, rs_css, rs_cdn_url, rs_version, services_xml)

# Otherwise, we have a URL pointing to the Runestone server/CDN
Expand Down Expand Up @@ -3547,9 +3551,7 @@ def _runestone_services(stringparams):
rs_js, rs_css, rs_cdn_url, rs_version = _parse_runestone_services(services)

# Return, plus side-effect
stringparams["rs-js"] = rs_js
stringparams["rs-css"] = rs_css
stringparams["rs-version"] = rs_version
_set_runestone_stringparams(stringparams, rs_js, rs_css, rs_version)
return (rs_js, rs_css, rs_cdn_url, rs_version, services_xml)


Expand Down Expand Up @@ -3655,9 +3657,26 @@ def html(xml, pub_file, stringparams, xmlid_root, file_format, extra_xsl, out_fi
# names for scratch directories
tmp_dir = get_temporary_directory()

add_runestone_services = True
# quick and dirty tries to use existing rs services
# on failure, will get new ones
if "html.quick-dirty" in stringparams:
try:
services_record_files = os.path.join(dest_dir, "_static", "_runestone-services.xml")
with open(services_record_files, 'r') as f:
services_xml = f.read()
services = ET.fromstring(services_xml)
rs_js, rs_css, rs_cdn_url, rs_version = _parse_runestone_services(services)
_set_runestone_stringparams(stringparams, rs_js, rs_css, rs_version)
log.info("Quick and dirty HTML is using old Runestone Services. Delete _static/_runestone-services.xml from output to update Runestone.")
add_runestone_services = False
except:
log.info("Quick and dirty HTML failed to use old Runestone Services. Will download Runestone.")

# interrogate Runestone server (or debugging switches) and populate
# NB: stringparams is augmented with Runestone Services information
_place_runestone_services(tmp_dir, stringparams)
if add_runestone_services:
_place_runestone_services(tmp_dir, stringparams)

# support publisher file, and subtree argument
if pub_file:
Expand All @@ -3672,10 +3691,12 @@ def html(xml, pub_file, stringparams, xmlid_root, file_format, extra_xsl, out_fi

# place managed directories - some of these (Asymptote HTML) are
# consulted during the XSL run and so need to be placed beforehand
copy_managed_directories(tmp_dir, external_abs=external_abs, generated_abs=generated_abs)
if "html.quick-dirty" not in stringparams:
copy_managed_directories(tmp_dir, external_abs=external_abs, generated_abs=generated_abs)

# place CSS and JS in scratch directory
copy_html_css_js(tmp_dir)
if "html.quick-dirty" not in stringparams:
copy_html_css_js(tmp_dir)

# Write output into temporary directory
log.info("converting {} to HTML in {}".format(xml, tmp_dir))
Expand Down
89 changes: 80 additions & 9 deletions xsl/pretext-assembly.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,63 @@ along with PreTeXt. If not, see <http://www.gnu.org/licenses/>.
</xsl:copy>
</xsl:template>


<!-- Pruning templates generate a subtree towards $target-node -->
<!-- that prunes as many other branches as possible while -->
<!-- retaining halfway decent output of the desire tree. -->
<xsl:template match="node()|@*" mode="pruning">
<xsl:param name="target-node"/>
<xsl:choose>
<!-- This node contains the target node, copy and keep going -->
<xsl:when test="count(descendant::*|$target-node) = count(descendant::*)">
<xsl:copy>
<xsl:apply-templates select="node()|@*" mode="pruning">
<xsl:with-param name="target-node" select="$target-node"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:when>
<!-- This node is target node, keep it all -->
<xsl:when test="count(.|$target-node) = 1">
<xsl:apply-templates select="." mode="pruning-keep"/>
</xsl:when>
<!-- Shift to aggressive pruning -->
<xsl:otherwise>
<xsl:apply-templates select="." mode="pruning-prune"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<!-- If we hit frontmatter or docinfo, preserve all the content -->
<!-- docinfo and bookinfo have important config info. frontmatter -->
<!-- has authors/subtitle/etc... -->
<xsl:template match="frontmatter|docinfo|bookinfo" mode="pruning">
<xsl:copy>
<xsl:apply-templates select="node()|@*" mode="pruning-keep"/>
</xsl:copy>
</xsl:template>

<!-- Preserve anything from here on down -->
<xsl:template match="node()|@*" mode="pruning-keep">
<xsl:copy>
<xsl:apply-templates select="node()|@*" mode="pruning-keep"/>
</xsl:copy>
</xsl:template>

<!-- Once aggressively pruning, only keep structural elements that give us -->
<!-- numbering and toc info. We could prune these and lose that, but -->
<!-- keeping them is cheap and looks a lot less broken on generated pages. -->
<xsl:template match="&STRUCTURAL;|@*" mode="pruning-prune">
<xsl:copy>
<xsl:apply-templates select="&STRUCTURAL;|title|@*" mode="pruning-prune">
</xsl:apply-templates>
</xsl:copy>
</xsl:template>

<!-- If we hit a title, grab all the contents so the ToC gets content-->
<xsl:template match="title" mode="pruning-prune">
<xsl:apply-templates select="." mode="pruning-keep"/>
</xsl:template>

<!-- These templates initiate and create several iterations of -->
<!-- the source tree via modal templates. Think of each as a -->
<!-- "pass" through the source. Generally this constructs the -->
Expand Down Expand Up @@ -288,15 +345,6 @@ along with PreTeXt. If not, see <http://www.gnu.org/licenses/>.
</xsl:variable>
<xsl:variable name="version" select="exsl:node-set($version-rtf)"/>

<xsl:variable name="commentary-rtf">
<xsl:apply-templates select="$version" mode="commentary"/>
</xsl:variable>
<xsl:variable name="commentaried" select="exsl:node-set($commentary-rtf)"/>

<!-- A global list of all "webwork" used for -->
<!-- efficient backward-compatible indentification -->
<xsl:variable name="all-webwork" select="$commentaried//webwork"/>

<!-- Support for versions mean there may be multiple instances of -->
<!-- the same structure in authored source, and conceivably they -->
<!-- might all be absent once a version has been selected. We are -->
Expand All @@ -307,6 +355,29 @@ along with PreTeXt. If not, see <http://www.gnu.org/licenses/>.
<xsl:variable name="version-docinfo" select="$version-root/docinfo"/>
<xsl:variable name="version-document-root" select="$version-root/*[not(self::docinfo)]"/>

<xsl:variable name="pruning-tree-rtf">
<xsl:choose>
<xsl:when test="$subtree != '' and $b-quick-dirty">
<xsl:apply-templates select="$version" mode="pruning">
<xsl:with-param name="target-node" select="//*[@id=$subtree]"/>
</xsl:apply-templates>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="$version"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="pruning-tree" select="exsl:node-set($pruning-tree-rtf)"/>

<xsl:variable name="commentary-rtf">
<xsl:apply-templates select="$pruning-tree" mode="commentary"/>
</xsl:variable>
<xsl:variable name="commentaried" select="exsl:node-set($commentary-rtf)"/>

<!-- A global list of all "webwork" used for -->
<!-- efficient backward-compatible indentification -->
<xsl:variable name="all-webwork" select="$commentaried//webwork"/>

<xsl:variable name="webwork-rtf">
<xsl:apply-templates select="$commentaried" mode="webwork"/>
</xsl:variable>
Expand Down
44 changes: 42 additions & 2 deletions xsl/pretext-common.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -11023,8 +11023,16 @@ http://andrewmccarthy.ie/2014/11/06/swung-dash-in-latex/
<!-- Comments without implementations have moved -->
<!-- to Schematron rules after residing here for -->
<!-- at least 16 months (one year plus grace) -->

<!-- -->
<!-- 2014-05-04 @filebase has been replaced in function by @xml:id -->
<xsl:call-template name="deprecation-message">
<xsl:with-param name="date-string" select="'2014-05-04'" />
<xsl:with-param name="message" select="'the &quot;filebase&quot; attribute is deprecated, convert to &quot;xml:id&quot;'" />
<xsl:with-param name="occurences" select="&quot;count(//@filebase)&quot;" />
</xsl:call-template>
<!-- -->
cite[@provisional]|xref[@provisional]
<!-- 2014-06-25 xref once had cite as a variant -->
<!-- 2015-01-28 once both circum and circumflex existed, circumflex won -->
<!-- -->
Expand All @@ -11033,10 +11041,42 @@ http://andrewmccarthy.ie/2014/11/06/swung-dash-in-latex/
<!-- 2017-07-05 sidebyside items that do not have captions, so ineffective -->
<!-- -->
<!-- 2015-02-08 naked tikz, asymptote, sageplot are no longer accomodated -->

<!-- tikz is generalized to latex-image-code -->
<xsl:if test="//tikz">
<xsl:call-template name="deprecation-message">
<xsl:with-param name="date-string" select="'2015/02/20'" />
<xsl:with-param name="message" select="'the &quot;tikz&quot; element is deprecated, convert to &quot;latex-image-code&quot; inside &quot;image&quot;'" />
<xsl:with-param name="occurences" select="count(//tikz)" />
</xsl:call-template>
</xsl:if>
<!-- 2015-02-20 tikz element is entirely abandoned -->
<!-- 2017-12-22 latex-image-code element is entirely abandoned -->
<!-- -->
<!-- Active deprecations follow -->
<!-- naked tikz, asymptote, sageplot are banned -->
<!-- typically these would be in a figure, but not necessarily -->
<xsl:if test="//figure/tikz or //figure/asymptote or //figure/sageplot">
<xsl:call-template name="deprecation-message">
<xsl:with-param name="date-string" select="'2015/02/08'" />
<xsl:with-param name="message" select="'&quot;tikz&quot;, &quot;asymptote&quot;, &quot;sageplot&quot;, elements must always be contained directly within an &quot;image&quot; element, rather than directly within a &quot;figure&quot; element'" />
<xsl:with-param name="occurences" select="count(//figure/tikz) + count(//figure/asymptote) + count(//figure/sageplot)" />
</xsl:call-template>
</xsl:if>
<!-- -->
<!-- xref once had variant called "cite" -->
<xsl:if test="//cite">
<xsl:call-template name="deprecation-message">
<xsl:with-param name="date-string" select="'2014/06/25'" />
<xsl:with-param name="message" select="'the &quot;cite&quot; element is deprecated, convert to &quot;xref&quot;'" />
<xsl:with-param name="occurences" select="count(//cite)" />
</xsl:call-template>
</xsl:if>
<!-- -->
<!-- 2017-12-22 latex-image-code to simply latex-image -->
<xsl:call-template name="deprecation-message">
<xsl:with-param name="occurences" select="$document-root//latex-image-code" />
<xsl:with-param name="date-string" select="'2017-08-25'" />
<xsl:with-param name="message" select="'the &quot;latex-image-code&quot; element has been replaced by the functionally equivalent &quot;latex-image&quot;'" />
</xsl:call-template>
<!-- -->
<!-- 2015-03-13 paragraph is renamed more accurately to paragraphs -->
<!-- 2017-07-16 removed all backwards compatibility and added empty template -->
Expand Down
25 changes: 21 additions & 4 deletions xsl/pretext-html.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ along with MathBook XML. If not, see <http://www.gnu.org/licenses/>.
<!-- Or make a thin customization layer and use 'select' to provide overrides -->
<!-- See more generally applicable parameters in pretext-common.xsl file -->

<!-- Fast HTML build that skips steps inessential for quickly checking -->
<!-- changes to a document or a portion of a document. -->
<!-- It will omit updating knowls, search index, theme, Runestone files,... -->
<!-- When combined with param subtree, it will only process that subtree. -->
<xsl:param name="html.quick-dirty" select="''"/>
<xsl:variable name="b-quick-dirty" select="not($html.quick-dirty = '')"/>

<!-- CSS and Javascript Directories -->
<!-- These are convenience variables to specify file prefixes -->
<!-- consistently. If you know what you are doing you could -->
Expand Down Expand Up @@ -261,15 +268,25 @@ along with MathBook XML. If not, see <http://www.gnu.org/licenses/>.
</xsl:call-template>
</xsl:if>
<!-- -->
<xsl:apply-templates select="$original" mode="generic-warnings"/>
<xsl:apply-templates select="$original" mode="element-deprecation-warnings"/>
<xsl:apply-templates select="$original" mode="parameter-deprecation-warnings"/>
<xsl:choose>
<!-- if working on a subtree, only warn on that part -->
<xsl:when test="$b-quick-dirty and $b-subsetting">
<xsl:apply-templates select="$pruning-tree" mode="generic-warnings"/>
<xsl:apply-templates select="$pruning-tree" mode="element-deprecation-warnings"/>
<xsl:apply-templates select="$pruning-tree" mode="parameter-deprecation-warnings"/>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="$original" mode="generic-warnings"/>
<xsl:apply-templates select="$original" mode="element-deprecation-warnings"/>
<xsl:apply-templates select="$original" mode="parameter-deprecation-warnings"/>
</xsl:otherwise>
</xsl:choose>
<!-- Usually no manifest is created -->
<xsl:call-template name="runestone-manifest"/>
<!-- A structured Table of Contents for a React app approach -->
<xsl:call-template name="doc-manifest"/>
<!-- build a search page (in development) -->
<xsl:if test="$has-native-search">
<xsl:if test="$has-native-search and not($b-quick-dirty)">
<xsl:call-template name="search-page-construction"/>
</xsl:if>
<!-- The main event -->
Expand Down