-
Notifications
You must be signed in to change notification settings - Fork 1
/
transcription_to_latex.py
30 lines (23 loc) · 1.13 KB
/
transcription_to_latex.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import os
from lxml import etree
from django.conf import settings as django_settings
class TranscriptionConverter(object):
def __init__(self):
self.parser = etree.XMLParser(resolve_entities=False)
def convert_transcription(self, transcription):
tree = etree.XML(transcription, self.parser)
siglum = tree.xpath('//tei:title[@type="document"]/@n',
namespaces={'tei': 'http://www.tei-c.org/ns/1.0'})[0]
xsl_path = os.path.join(django_settings.BASE_DIR, 'collation', 'xslt', 'transcription_to_latex.xsl')
if os.path.exists(xsl_path):
xsl_string = open(xsl_path, mode='r', encoding='utf-8').read()
xsl_string = xsl_string.replace('<?xml version="1.0" encoding="UTF-8"?>', '')
xsl_tree = etree.XML(xsl_string)
xsl_transformer = etree.XSLT(xsl_tree)
else:
raise
result = xsl_transformer(tree)
latex = str(result)
latex = latex.replace('xͮ', '\\XVE{}').replace('ŋͮ', '\\NGVE{}')
latex = latex.replace('xͮ', '\\XVE{}').replace('ŋͮ', '\\NGVE{}')
return (latex, siglum)