Skip to content

Commit

Permalink
folia2html: Implemented support for outputting based on other text cl…
Browse files Browse the repository at this point in the history
…asses #30
  • Loading branch information
proycon committed Mar 3, 2021
1 parent 6736104 commit 28f4864
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 9 deletions.
2 changes: 1 addition & 1 deletion foliatools/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""FoLiA-tools contains various Python-based command line tools for working with FoLiA XML (Format for Linguistic Annotation)"""

VERSION = "2.4.7"
VERSION = "2.4.8"
7 changes: 4 additions & 3 deletions foliatools/folia2html.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<xsl:stylesheet version="1.0" xmlns="http://www.w3.org/1999/xhtml" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:imdi="http://www.mpi.nl/IMDI/Schema/IMDI" xmlns:folia="http://ilk.uvt.nl/folia" xmlns:exsl="http://exslt.org/common" xmlns:dc="http://purl.org/dc/elements/1.1/">

<xsl:param name="css"></xsl:param>
<xsl:param name="textclass">current</xsl:param>

<xsl:output method="html" encoding="UTF-8" omit-xml-declaration="yes" doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" indent="yes" />

Expand Down Expand Up @@ -538,12 +539,12 @@

<xsl:variable name="ancestors_ok">not(ancestor::folia:original) and not(ancestor::folia:suggestion) and not(ancestor::folia:alt) and not(ancestor::folia:altlayers)</xsl:variable><!-- Checks if all ancestors are authoritative -->
<xsl:variable name="ancestors_nosubtoken">not(ancestor::folia:morpheme) and not(ancestor::folia:phoneme)</xsl:variable><!-- Checks if all ancestors are authoritative -->
<xsl:variable name="textclass_current">(not(@class) or (@class='current'))</xsl:variable>
<xsl:variable name="textclass_ok">((($textclass="current") and not(@class)) or (@class=$textclass))</xsl:variable>

<xsl:template match="folia:w">
<xsl:variable name="wid" select="@xml:id" />
<xsl:if test="$ancestors_ok">
<span id="{@xml:id}"><xsl:attribute name="class">word<xsl:if test="//folia:wref[@id=$wid and not(ancestor::folia:altlayers)]"> sh</xsl:if><xsl:if test=".//folia:correction or .//folia:errordetection"> cor</xsl:if></xsl:attribute><span class="t"><xsl:value-of select="string(.//folia:t[$ancestors_ok and $ancestors_nosubtoken and not(ancestor::folia:str) and (not(@class) or @class = 'current')])"/></span><xsl:call-template name="inlineannotations" /></span>
<span id="{@xml:id}"><xsl:attribute name="class">word<xsl:if test="//folia:wref[@id=$wid and not(ancestor::folia:altlayers)]"> sh</xsl:if><xsl:if test=".//folia:correction or .//folia:errordetection"> cor</xsl:if></xsl:attribute><span class="t"><xsl:value-of select="string(.//folia:t[$ancestors_ok and $ancestors_nosubtoken and not(ancestor::folia:str) and $textclass_ok])"/></span><xsl:call-template name="inlineannotations" /></span>
<xsl:choose>
<xsl:when test="@space = 'no'"></xsl:when>
<xsl:otherwise>
Expand All @@ -560,7 +561,7 @@
<!-- Test presence of text in deeper structure elements, if they exist we don't
render this text but rely on the text in the deeper structure -->
<!-- Next, check if text element is authoritative (ancestors_ok) and have the proper class -->
<xsl:if test="not(following-sibling::*//folia:t[$textclass_current and $ancestors_ok and $ancestors_nosubtoken and not(ancestor::folia:str)])"><xsl:if test="$textclass_current and $ancestors_ok and $ancestors_nosubtoken and not(ancestor::folia:str)"><xsl:apply-templates /></xsl:if></xsl:if>
<xsl:if test="not(following-sibling::*//folia:t[$textclass_ok and $ancestors_ok and $ancestors_nosubtoken and not(ancestor::folia:str)])"><xsl:if test="$textclass_current and $ancestors_ok and $ancestors_nosubtoken and not(ancestor::folia:str)"><xsl:apply-templates /></xsl:if></xsl:if>
</xsl:template>


Expand Down
13 changes: 9 additions & 4 deletions foliatools/xslt.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def usage():
print(" -E [extension] Set extension (default: xml)",file=sys.stderr)
print(" -q Ignore errors",file=sys.stderr)
print(" -s [url] Associate a CSS Stylesheet (URL, may be relative)",file=sys.stderr)
print(" -t [textclass] Text class to output",file=sys.stderr)



Expand All @@ -52,6 +53,7 @@ class settings:
outputextension = 'UNDEFINED'
usage = "UNDEFINED"
css = ""
textclass = "current"

def processdir(d, outputfilename = None):
print("Searching in " + d, file=sys.stderr)
Expand All @@ -64,10 +66,11 @@ def processdir(d, outputfilename = None):

def process(inputfilename, outputfilename=None):
try:
kwargs = {}
if settings.css:
kwargs = {'css': settings.css}
else:
kwargs = {}
kwargs['css'] = settings.css
if settings.textclass:
kwargs['textclass'] = settings.textclass
transform(settings.xsltfilename, inputfilename, outputfilename, settings.encoding, **kwargs)
except Exception as e:
if settings.ignoreerrors:
Expand All @@ -78,7 +81,7 @@ def process(inputfilename, outputfilename=None):

def main(xsltfilename, outputextension, usagetext):
try:
opts, args = getopt.getopt(sys.argv[1:], "o:E:hrqs:", ["help"])
opts, args = getopt.getopt(sys.argv[1:], "o:E:hrqs:t:", ["help"])
except getopt.GetoptError as err:
print(str(err), file=sys.stderr)
usage()
Expand Down Expand Up @@ -109,6 +112,8 @@ def main(xsltfilename, outputextension, usagetext):
settings.ignoreerrors = True
elif o == '-s':
settings.css = a
elif o == '-t':
settings.textclass = a
else:
raise Exception("No such option: " + o)

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def read(fname):

setup(
name = "FoLiA-tools",
version = "2.4.7", #also change in __init__.py
version = "2.4.8", #also change in __init__.py
author = "Maarten van Gompel",
author_email = "proycon@anaproy.nl",
description = ("FoLiA-tools contains various Python-based command line tools for working with FoLiA XML (Format for Linguistic Annotation)"),
Expand Down

0 comments on commit 28f4864

Please sign in to comment.