#! /usr/bin/env python3

# edelfelt-txt-to-hrt
#
# Convert Edelfelt's letters from plain text to HRT.
#
# Usage: edelfelt-txt-to-hrt letter1.txt [letter2.txt ...] > letters.hrt
#
# The letters need to have been converted from .docx to plain text
# (UTF-8) with e.g. unoconv.


import re
import sys


class Converter():

    def __init__(self):
        pass

    def main(self):
        if len(sys.argv) > 1:
            for fname in sys.argv[1:]:
                with open(fname, 'r', encoding='utf-8-sig') as inf:
                    self.process_input(inf)
        else:
            self.process_input(sys.stdin)

    def process_input(self, inf):
        """Process text input stream inf and output HRT."""

        def escape(s, quotes=False):
            """Convert &, <, > and " (if quotes) in s to XML entities"""
            s = (s.replace('&', '&amp;')
                 .replace('<', '&lt;')
                 .replace('>', '&gt;'))
            if quotes:
                s = s.replace('"', '&quot;')
            return s

        text_open = False
        signum = year = letternum = page = ''
        for line in inf:
            line = line.strip()
            if not line:
                continue
            line = escape(line)
            mo = re.match(r'Brev (slsa?367_([0-9]{4})_([0-9]+))', line)
            if mo:
                # Line containing letter id begins a new letter
                if text_open:
                    print('</text>')
                text_open = False
                # Extract information from letter id, to be added as
                # text attributes
                year = mo.group(2)
                letternum = mo.group(3)
                signum = f'SLSA 367_brev_{year}_{letternum}'
            else:
                mo = re.match(r'Sida ([0-9]+)', line)
                if mo:
                    # Page break
                    page = mo.group(1)
                    if text_open:
                        # If a text is open, add page tags
                        print('</page>')
                        print(f'<page n="{page}">')
                    continue
                elif not text_open:
                    # Text not yet opened, so open it
                    print(f'<text signum="{signum}" year="{year}"'
                          f' letternum="{letternum}"'
                          f' title="{escape(line, True)}"'
                          f' datefrom="{year}0101" dateto="{year}1231">')
                    print(f'<page n="{page}">')
                    print(escape(line))
                    text_open = True
                else:
                    print(escape(line))
        # Close page and text
        print('</page>')
        print('</text>')


if __name__ == '__main__':
    Converter().main()
