'Helper method to be passed to re.sub,
for handling all substitutions.
Args:
match (re.Matchobject): Match for substitution.
Returns:
text (str): Processed text.'

def sub_text(self, match):

    cdict = match.groupdict()
    if cdict['htmlchars']:
        return cgi.escape(cdict['htmlchars'])
    if cdict['lineend']:
        return '<br>'
    elif (cdict['space'] == ' DCTB '):
        return ('    ' * self.tabstop)
    elif (cdict['space'] or cdict['spacestart']):
        text = match.group().replace(' DCTB ', ('&nbsp;' * self.tabstop))
        text = text.replace('    ', '&nbsp;')
        return text
    return None
