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

Un/flatten table cells #29

Merged
merged 4 commits into from
Mar 9, 2024
Merged
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
29 changes: 29 additions & 0 deletions page-ensure-textequiv-conf.xsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:pc="http://schema.primaresearch.org/PAGE/gts/pagecontent/2019-07-15" xmlns:exslt="http://exslt.org/common" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0" extension-element-prefixes="exslt">
<xsl:output omit-xml-declaration="no" indent="yes" method="xml" encoding="utf-8"/>
<xsl:strip-space elements="*"/>
<xsl:preserve-space elements="pc:Unicode"/>
<xsl:template match="//pc:TextEquiv">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:if test="not(@conf)">
<xsl:attribute name="conf">
<xsl:choose>
<xsl:when test="../*/pc:TextEquiv/@conf">
<xsl:value-of select="sum(../*/pc:TextEquiv/@conf) div count(../*/pc:TextEquiv)"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="1.0"/>
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
</xsl:if>
<xsl:apply-templates select="node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@*|node()" name="identity">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
30 changes: 30 additions & 0 deletions page-flatten-tableregions.xsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:pc="http://schema.primaresearch.org/PAGE/gts/pagecontent/2019-07-15">
<xsl:output method="xml" version="1.0" omit-xml-declaration="no" encoding="UTF-8" indent="yes"/>
<xsl:preserve-space elements="*"/>
<!-- LAREX cannot present/edit recursive regions, but TableRegion necessitates TextRegion for content
so this converts TableRegion/TextRegion children to siblings -->
<xsl:template match="pc:TableRegion">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:apply-templates select="*[not(local-name()='TextRegion')]"/>
</xsl:copy>
<xsl:apply-templates select="pc:TextRegion">
<xsl:with-param name="custom"><xsl:value-of select="concat('parent:',@id)"/></xsl:with-param>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="@* | node() | text()">
<xsl:param name="custom"/>
<xsl:copy>
<xsl:if test="$custom">
<xsl:attribute name="custom">
<xsl:value-of select="string($custom)"/>
</xsl:attribute>
</xsl:if>
<xsl:apply-templates select="@* | node() | text()"/>
</xsl:copy>
</xsl:template>

</xsl:stylesheet>

63 changes: 63 additions & 0 deletions page-rename-id-clashes.xsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:pc="http://schema.primaresearch.org/PAGE/gts/pagecontent/2019-07-15">
<xsl:output omit-xml-declaration="no" indent="yes" method="xml" encoding="utf-8"/>
<!-- rename segments with @id clashes -->
<xsl:key name="segments" match="pc:TextRegion|pc:TableRegion|pc:TextLine|pc:Word" use="@id" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="pc:TextRegion[generate-id() != generate-id(key('segments', @id)[1])]">
<xsl:variable name="newID" select="generate-id()" />
<xsl:element name="pc:TextRegion">
<xsl:attribute name="id">
<xsl:value-of select="$newID" />
</xsl:attribute>
<xsl:copy-of select="@*[not(local-name()='id')]"/>
<xsl:copy-of select="*|text()"/>
</xsl:element>
</xsl:template>
<xsl:template match="pc:TextLine[generate-id() != generate-id(key('segments', @id)[1])]">
<xsl:variable name="newID" select="generate-id()" />
<xsl:element name="pc:TextLine">
<xsl:attribute name="id">
<xsl:value-of select="$newID" />
</xsl:attribute>
<xsl:copy-of select="@*[not(local-name()='id')]"/>
<xsl:copy-of select="*|text()"/>
</xsl:element>
</xsl:template>
<xsl:template match="pc:Word[generate-id() != generate-id(key('segments', @id)[1])]">
<xsl:variable name="newID" select="generate-id()" />
<xsl:element name="pc:Word">
<xsl:attribute name="id">
<xsl:value-of select="$newID" />
</xsl:attribute>
<xsl:copy-of select="@*[not(local-name()='id')]"/>
<xsl:copy-of select="*|text()"/>
</xsl:element>
</xsl:template>
<xsl:template match="pc:Glyph[generate-id() != generate-id(key('segments', @id)[1])]">
<xsl:variable name="newID" select="generate-id()" />
<xsl:element name="pc:Glyph">
<xsl:attribute name="id">
<xsl:value-of select="$newID" />
</xsl:attribute>
<xsl:copy-of select="@*[not(local-name()='id')]"/>
<xsl:copy-of select="*|text()"/>
</xsl:element>
</xsl:template>
<xsl:template match="pc:TableRegion[generate-id() != generate-id(key('segments', @id)[1])]">
<xsl:variable name="newID" select="generate-id()" />
<xsl:element name="pc:TableRegion">
<xsl:attribute name="id">
<xsl:value-of select="$newID" />
</xsl:attribute>
<xsl:copy-of select="@*[not(local-name()='id')]"/>
<xsl:copy-of select="*|text()"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
28 changes: 28 additions & 0 deletions page-unflatten-tableregions.xsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:pc="http://schema.primaresearch.org/PAGE/gts/pagecontent/2019-07-15">
<xsl:output method="xml" version="1.0" omit-xml-declaration="no" encoding="UTF-8" indent="yes"/>
<xsl:preserve-space elements="*"/>
<!-- LAREX cannot present/edit recursive regions, but TableRegion necessitates TextRegion for content
so this moves TextRegion elements previously moved from TableRegion back to TableRegion/TextRegion -->
<xsl:template match="pc:TableRegion">
<xsl:variable name="tabid" select="@id"/>
<xsl:copy>
<xsl:apply-templates select="@* | node() | text()"/>
<xsl:for-each select="../pc:TextRegion[concat('parent:',$tabid)=@custom]">
<xsl:copy>
<xsl:apply-templates select="@*[not(starts-with(.,'parent:'))]"/>
<xsl:apply-templates select="node() | text()"/>
</xsl:copy>
</xsl:for-each>
</xsl:copy>
</xsl:template>
<xsl:template match="pc:TextRegion[starts-with(@custom,'parent:')]"/>
<xsl:template match="@* | node() | text()">
<xsl:copy>
<xsl:apply-templates select="@* | node() | text()"/>
</xsl:copy>
</xsl:template>

</xsl:stylesheet>

2 changes: 1 addition & 1 deletion xsl-transform
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ if ((pretty)); then
output="$(echo "$output" | xmlstarlet fo -s 2 -)"
fi
if ((diff)); then
diff -u "$file" <(echo "$output")
diff -u <(cat "$file" | if ((pretty)); then xmlstarlet fo -s 2 -; fi) <(echo "$output")
elif ((inplace)); then
echo "$output" > "$file"
else
Expand Down