Commit 07d08bc1 authored by Wendell Piez's avatar Wendell Piez

Removing HTML Tweak application (added to HTMLevator)

parent cb47419d
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsw="http://coko.foundation/xsweet"
xpath-default-namespace="http://www.w3.org/1999/xhtml"
xmlns="http://www.w3.org/1999/xhtml"
exclude-result-prefixes="#all">
<xsl:output method="xml" indent="no" omit-xml-declaration="yes"/>
<!-- Stylesheet to rewrite style and class on HTML
via a template cascade - i.e., more than a single transformation
can be performed over a single element.
Ultimately this could support a little language kinda like:
where { font-size: 18pt } .FreeForm
remove { font-size } .FreeForm
add { color: red } .FreeFormNew
<where>
<match>
<style>font-size: 18pt</style>
<class>FreeForm</class>
</match>
<remove>
<style>font-size</style>
<class>FreeForm</class>
</remove>
<add>
<class>FreeFormNew</class>
<style>color: red</style>
</add>
</where>
providing mappings across @class (considered as NMTOKENS) and @style (considered as CSS property sets)
-->
<!-- How to use (until we implement a next-layer-up):
match using appropriate key for class (name), style (property or property-value)
call execute-tweak with replacement values.
Note it is possible to pass in multiple values for parameters using , and ; within style and class settings.
i.e.
<with-param name="removeStyleProperties">font-size; font-weight</with-param>
removes both the properties named.
Note also that control of style values is by property for removal (i.e., remove any/all 'font-size' property),
but by property-value pair for addition (i.e., add 'font-size: larger").
Not that you should be adding @style!
-->
<!-- Implementation of rule given above. -->
<xsl:template priority="12" match="key('elements-by-propertyValue','font-size: 18pt')
[. intersect key('elements-by-class', 'Freeform')]" >
<xsl:call-template name="makeTweak">
<xsl:with-param name="removeStyleProperties" >font-size</xsl:with-param>
<xsl:with-param name="addStylePropertyValues" >color: red</xsl:with-param>
<xsl:with-param name="removeClass" >FreeForm</xsl:with-param>
<xsl:with-param name="addClass" >FreeFormNew</xsl:with-param>
</xsl:call-template>
</xsl:template>
<xsl:template priority="11" match="key('elements-by-propertyValue','font-family: Arial Unicode MS')">
<xsl:call-template name="makeTweak">
<xsl:with-param name="removeStyleProperties">font-family</xsl:with-param>
</xsl:call-template>
</xsl:template>
<xsl:template priority="10" match="key('elements-by-propertyValue','text-indent: 36pt')">
<xsl:call-template name="makeTweak">
<xsl:with-param name="addClass" select="'indented'"/>
</xsl:call-template>
</xsl:template>
<!-- Infrastructure should not require modification. -->
<xsl:include href="html-tweak-lib.xsl"/>
</xsl:stylesheet>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsw="http://coko.foundation/xsweet"
xpath-default-namespace="http://www.w3.org/1999/xhtml"
xmlns="http://www.w3.org/1999/xhtml"
exclude-result-prefixes="#all">
<!-- See html-tweak-demo.xsl for an example of how to use this stylesheet fragment. -->
<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*"/>
</xsl:copy>
</xsl:template>
<xsl:template name="makeTweak">
<!-- for class, a string with , delimiters; for style, a string with ; delimiters -->
<xsl:param name="addClass" select="()" as="xs:string?"/>
<xsl:param name="removeClass" select="()" as="xs:string?"/>
<xsl:param name="addStylePropertyValues" select="()" as="xs:string?"/>
<xsl:param name="removeStyleProperties" select="()" as="xs:string?"/>
<xsl:variable name="alreadyTweaked">
<xsl:next-match/>
</xsl:variable>
<xsl:for-each select="$alreadyTweaked/*"><!-- change context to results of applying templates
which we expect to be a single (html) element -->
<xsl:copy>
<xsl:copy-of select="@* except (@class | @style)"/>
<xsl:call-template name="tweakStyle">
<xsl:with-param name="removeProperties" select="tokenize($removeStyleProperties, '\s*;\s*')"/>
<xsl:with-param name="addPropertyValues" select="tokenize($addStylePropertyValues,'\s*;\s*')"/>
</xsl:call-template>
<xsl:call-template name="tweakClass">
<xsl:with-param name="remove" select="tokenize($removeClass,'\s*,\s*')"/>
<xsl:with-param name="add" select="tokenize($addClass, '\s*,\s*')"/>
</xsl:call-template>
<xsl:copy-of select="child::node()"/>
</xsl:copy>
</xsl:for-each>
</xsl:template>
<xsl:template name="tweakClass">
<xsl:param name="remove" select="()" as="xs:string*"/>
<xsl:param name="add" select="()" as="xs:string*"/>
<xsl:if test="exists( ($add, xsw:classes(.)[not(.= $remove)] (: either $add or classes other than $remove :)))">
<xsl:attribute name="class" select="xsw:classes(.)[not(.=$remove)], $add" separator="&#32;"/>
</xsl:if>
</xsl:template>
<xsl:template name="tweakStyle">
<!-- $removeProperties are expected as 'font-size', 'text-indent' -->
<!-- $addPropertyValues are expected as 'font-size: 12pt', 'text-indent: 36pt' -->
<xsl:param name="removeProperties" select="()" as="xs:string*"/>
<xsl:param name="addPropertyValues" select="()" as="xs:string*"/>
<xsl:variable name="oldPropertyValues" select="xsw:style-propertyValues(.)"/>
<xsl:variable name="newPropertyValues"
select="$oldPropertyValues[not(replace(.,':.*$','') = $removeProperties)],
$addPropertyValues"/>
<xsl:if test="exists($newPropertyValues)">
<xsl:attribute name="style">
<xsl:for-each select="$newPropertyValues">
<xsl:sort data-type="text"/>
<xsl:if test="position() gt 1">; </xsl:if>
<xsl:value-of select="."/>
</xsl:for-each>
</xsl:attribute>
</xsl:if>
</xsl:template>
<xsl:function name="xsw:classes" as="xs:string*">
<xsl:param name="e" as="element()"/>
<xsl:sequence select="tokenize($e/@class/normalize-space(.),'\s')"/>
</xsl:function>
<xsl:function name="xsw:style-properties" as="xs:string*">
<!-- Returns 'font-family','font-size','color','text-indent' whatever
properties are defined on @style -->
<xsl:param name="e" as="element()"/>
<xsl:sequence select="for $propVal in xsw:style-propertyValues($e)
return substring-before($propVal,':')"/>
</xsl:function>
<xsl:function name="xsw:style-propertyValues" as="xs:string*">
<!-- Returns 'font-family: Helvetica','font-size: 10pt' whatever
properties are defined on @style -->
<xsl:param name="e" as="element()"/>
<xsl:sequence select="tokenize($e/@style/normalize-space(.),'\s*;\s*')"/>
</xsl:function>
<!--<xsl:function name="xsw:hasClass" as="xs:boolean">
<xsl:param name="e" as="element()"/>
<xsl:param name="c" as="xs:string"/>
<xsl:sequence select="xsw:classes($e) = $c"/>
</xsl:function>-->
<xsl:key name="elements-by-class" match="*[matches(@class,'\S')]" use="xsw:classes(.)"/>
<xsl:key name="elements-by-property" match="*[matches(@style,'\S')]" use="xsw:style-properties(.)"/>
<xsl:key name="elements-by-propertyValue" match="*[matches(@style,'\S')]" use="xsw:style-propertyValues(.)"/>
</xsl:stylesheet>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<htmlTweak>
<where>
<match><class>Default</class></match>
<remove><class>Default</class></remove>
</where>
<where>
<match><style>margin-bottom</style></match>
<remove><style>margin-bottom</style></remove>
</where>
<where>
<match><style>font-family: Helvetica</style></match>
<remove><style>font-family</style></remove>
</where>
<where>
<match>
<style>font-size: 18pt</style>
<class>FreeForm</class>
</match>
<remove>
<style>font-size</style>
<class>FreeForm</class>
</remove>
<add>
<class>FreeFormNew</class>
<style>color: red</style>
</add>
</where>
<where>
<match>
<style>text-indent: 36pt</style>
</match>
<add>
<class>Indented</class>
</add>
</where>
</htmlTweak>
<?xml version="1.0" encoding="UTF-8"?>
<p:declare-step version="1.0"
xmlns:p="http://www.w3.org/ns/xproc"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:c="http://www.w3.org/ns/xproc-step"
xmlns:xsw="http://coko.foundation/xsweet"
type="xsw:html-tweak" name="html-tweak">
<!-- Implementing a mapping to modify an HTML document wrt @style and @class -->
<p:input port="source" primary="true"/>
<p:input port="htmlTweakMap"/>
<p:input port="parameters" kind="parameter"/>
<p:output port="result" primary="true">
<p:pipe port="result" step="final"/>
</p:output>
<p:output port="htmlTweakXSLT" primary="false">
<p:pipe port="result" step="html-tweak-xsl"/>
</p:output>
<p:serialization port="result" indent="true" omit-xml-declaration="true"/>
<p:serialization port="htmlTweakXSLT" indent="true" />
<!-- First we step aside and generate a transformation from a spec... -->
<p:xslt name="html-tweak-xsl">
<p:input port="source">
<p:pipe port="htmlTweakMap" step="html-tweak"/>
</p:input>
<p:input port="stylesheet">
<p:document href="make-html-tweak-xslt.xsl"/>
</p:input>
</p:xslt>
<!-- Now running the resulting XSLT on the html file input. -->
<p:xslt name="html-tweaked">
<p:input port="source">
<p:pipe port="source" step="html-tweak"/>
</p:input>
<p:input port="stylesheet">
<p:pipe port="result" step="html-tweak-xsl"/>
</p:input>
</p:xslt>
<p:identity name="final"/>
</p:declare-step>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsw="http://coko.foundation/xsweet"
exclude-result-prefixes="#all">
<xsl:output indent="yes"/>
<!-- Stylesheet transforms a rough transformation spec for HTML
(affecting @style and @class only) into an XSLT executable.
So you can write simple maps for your HTML and have the XSLT written for you behind the scenes.
-->
<!--
<where>
<match>
<style>font-size: 18pt</style>
<class>FreeForm</class>
</match>
<remove>
<style>font-size</style>
<class>FreeForm</class>
</remove>
<add>
<class>FreeFormNew</class>
<style>color: red</style>
</add>
</where>
-->
<xsl:namespace-alias stylesheet-prefix="xsw" result-prefix="xsl"/>
<xsl:param name="debug-mode" as="xs:string">silent</xsl:param>
<xsl:template match="/*">
<!-- xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsw="http://coko.foundation/xsweet"
xmlns="http://www.w3.org/1999/xhtml"
-->
<xsw:stylesheet version="2.0" exclude-result-prefixes="#all">
<!--<xsw:template match="node() | @*">
<xsw:copy>
<xsw:apply-templates select="node() | @*"/>
</xsw:copy>
</xsw:template>
<xsl:apply-templates select="div[@class='grouped']/div[@class='hX']/*" mode="xslt-produce"/>
<xsl:if test="not($debug-mode='silent')">
<xsw:variable name="in">
<xsl:copy-of select="div"/>
</xsw:variable>
</xsl:if>
-->
<xsl:apply-templates mode="xslt-produce"/>
<xsw:include href="{resolve-uri('html-tweak-lib.xsl',document-uri(document('')) )}"/>
</xsw:stylesheet>
</xsl:template>
<!-- Template writes XSLT templates -->
<!--
<where>
<match>
<style>font-size: 18pt</style>
<class>FreeForm</class>
</match>
<remove>
<style>font-size</style>
<class>FreeForm</class>
</remove>
<add>
<class>FreeFormNew</class>
<style>color: red</style>
</add>
</where>
-->
<xsl:template match="where" mode="xslt-produce">
<xsw:template priority="{count(.|following-sibling::where)}">
<xsl:attribute name="match">
<xsl:apply-templates mode="keyPattern" select="match/*[1]"/>
<xsl:for-each select="match/*[1]/following-sibling::*">
<xsl:text>[. intersect </xsl:text>
<xsl:apply-templates mode="keyPattern" select="."/>
<xsl:text>]</xsl:text>
</xsl:for-each>
</xsl:attribute>
<!--match="key('elements-by-propertyValue','font-size: 18pt')
[. intersect key('elements-by-class', 'Freeform')]" >-->
<xsw:call-template name="makeTweak">
<!--<xsw:with-param name="removeStyleProperties" >font-size</xsw:with-param>
<xsw:with-param name="addStylePropertyValues" >color: red</xsw:with-param>
<xsw:with-param name="removeClass" >FreeForm</xsw:with-param>
<xsw:with-param name="addClass" >FreeFormNew</xsw:with-param>-->
<xsl:apply-templates select="(add | remove)/(class | style)" mode="tweakParams"/>
</xsw:call-template>
</xsw:template>
</xsl:template>
<xsl:template match="match/style[matches(.,':')]" priority="2" mode="keyPattern">
<xsl:text>key('elements-by-propertyValue','</xsl:text>
<xsl:apply-templates/>
<xsl:text>')</xsl:text>
</xsl:template>
<xsl:template match="match/style" priority="1" mode="keyPattern">
<xsl:text>key('elements-by-property','</xsl:text>
<xsl:apply-templates/>
<xsl:text>')</xsl:text>
</xsl:template>
<xsl:template match="match/class" mode="keyPattern">
<xsl:text>key('elements-by-class','</xsl:text>
<xsl:apply-templates/>
<xsl:text>')</xsl:text>
</xsl:template>
<xsl:template match="remove/class" mode="tweakParams">
<xsw:with-param name="removeClass">
<xsl:apply-templates/>
</xsw:with-param>
</xsl:template>
<xsl:template match="add/class" mode="tweakParams">
<xsw:with-param name="addClass">
<xsl:apply-templates/>
</xsw:with-param>
</xsl:template>
<xsl:template match="remove/style" mode="tweakParams">
<xsw:with-param name="removeStyleProperties">
<xsl:apply-templates/>
</xsw:with-param>
</xsl:template>
<xsl:template match="add/style" mode="tweakParams">
<xsw:with-param name="addStylePropertyValues">
<xsl:apply-templates/>
</xsw:with-param>
</xsl:template>
</xsl:stylesheet>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment