"""
attributed text parser
"""

import re

scanner = [
    r"(?P<strong>''')",
    r"(?P<em>'')",
    r"(?P<u>__)",
    ]
scanner = re.compile('|'.join(scanner))

textAttribtues = ['em', 'strong', 'u']


class Parser:
    """ Attributed text parser """
    
    def __init__(self):
        self.attributes = {}
        self.output = []
            
    def scan(self, line):
        pos = 0
        for match in scanner.finditer(line):
            # Add text before match
            if pos < match.start():
                self.addText(line[pos:match.start()])
            self.replace(match)
            pos = match.end()
                
        # Add text after match
        if pos < len(line) -1:
            self.addText(line[pos:])
    
    def replace(self, match):
        """ Handle key or call handler for key """
        key, val = self.getKeyval(match)
        if key in textAttribtues:
            self.updateAttribtues(key)
        else:
            handler = getattr(self, '_repl_' + key)
            handler(match)
    
    def updateAttribtues(self, key):
        """ Update text attributes state """
        if key in self.attributes:
            del self.attributes[key]
        else:
            self.attributes[key] = 1

    def addText(self, text):
        attributes = self.attributes.keys()
        attributes.sort()
        self.output.append((text, attributes))

    def getKeyval(self, match):
        # Get match name
        for key, val in match.groupdict().items():
            if val:
                return key, val
                
