markdowntei.py
1.42 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
'Markdown to TEI converter'
import re
import metadata
from tei import Tei, Utterance
from splitter import split_sessions
class MarkdownTei:
'Converter from Markdown to TEI'
def __init__(self):
self.who = 'komentarz'
def convert(self, filename, output):
'Convert file to TEI'
self.who = 'komentarz'
data = list(filter(None, [self._utterance(par)
for par in self._read(filename)]))
sessions = split_sessions(data)
if len(sessions) > 1:
for index, session in enumerate(sessions):
filemeta = metadata.metadata(
session[0:10], f'{filename}:{index+1}')
metadata.validate(filemeta)
tei = Tei(session, filemeta)
tei.save(output)
else:
filemeta = metadata.metadata(data[0:10], filename)
metadata.validate(filemeta)
tei = Tei(sessions[0], filemeta)
tei.save(output)
@ staticmethod
def _read(filename):
return filter(None, [par.strip() for par in open(filename)])
def _utterance(self, par):
match = re.match(r'\*\((.*)\)\*', par)
if match:
return Utterance(who='komentarz', u=match.group(1))
match = re.match(r'\*\*(.*):\*\*', par)
if match:
self.who = match.group(1)
return None
return Utterance(who=self.who, u=par)