from cgi import escape from urllib import quote import re from .formats import BaseFormat, placeholder from .tokens import * _absolute_url_pattern = re.compile(r'^/|^\w+:/') HTML_SIZES = { -4: '6pt', -3: '8pt', -2: '9pt', -1: '10pt', 0: '12pt', 1: '14pt', 2: '18pt', 3: '24pt', 4: '30pt', 5: '36pt' } def make_absolute(url): #if isinstance(url,unicode): # url = url.encode('utf-8') # We trust the URL to have been made valid at a higher layer. # u = quote(url, '/:~#') assert '"' not in url, url if not _absolute_url_pattern.match(url): url = './'+url return url class HTMLFormat(BaseFormat): def __init__(self): self.pclass = None def text(self, text): yield escape(text) def tag(self, t, arg=None): if t == BOLD: return u'b' elif t == ITALIC: return u'i' elif t == MONOSPACE: return u'tt' elif t == SUPERSCRIPT: return u'sup' elif t == SUBSCRIPT: return u'sub' elif t == UNDERLINE: return u'u' elif t == STRIKE: return u'strike' elif t == CODEBLOCK: return u'pre' elif t == HEADING: return u'h%s' % (arg,) elif t == TABLE_HEADING: return u'th' elif t == CENTER: return u'center' else: assert False, t LISTMAP = {ORDERED: 'ol', UNORDERED: 'ul', BLOCKQUOTE: 'blockquote'} def start(self, t, arg=None): if t in self.LISTMAP: tag = self.LISTMAP[t] start = (arg * 2 - 2) * ' ' yield u'%s<%s>\n' % (start, tag) elif t in (ORDERED_ITEM, UNORDERED_ITEM): yield '%s
  • ' % ((arg * 2 - 1) * ' ') elif t == BLOCKQUOTE_LINE: yield '%s' % ((arg * 2 - 1) * ' ') elif t == LINK: yield u'' % (make_absolute(arg['url']), quote(arg['style'])) elif t == IMAGE: dims = '' if 'width' in arg: dims += ' width="%s"' % arg['width'] if 'height' in arg: dims += ' height="%s"' % arg['height'] yield u'' % (
                make_absolute(arg['url']), quote(arg['style']))
        elif t == PARAGRAPH:
            if self.pclass:
                yield u'<p class=' % self.pclass else: yield u'

    ' elif t == TABLE: yield u'\n' elif t == TABLE_ROW: yield u'' elif t == TABLE_CELL: argbit = '' if arg: if 'colspan' in arg: argbit += ' colspan="%s"' % arg['colspan'] yield u'' % argbit elif t == FOOTNOTE: yield u'' elif t == RIGHT: yield u'

    ' elif t == NOINDENT: yield u'

    ' elif t == SIZE: yield u'' % HTML_SIZES[arg] elif t == ERROR: yield u'' else: yield u'<%s>' % self.tag(t, arg) def end(self, t, arg=None): if t in self.LISTMAP: tag = self.LISTMAP[t] start = (arg * 2 - 2) * ' ' if arg > 1: end = '\n' else: end = '' yield u'%s%s' % (start, tag, end) elif t in (ORDERED_ITEM, UNORDERED_ITEM): yield '\n' elif t == BLOCKQUOTE_LINE: yield '\n' elif t == LINK: yield u'' elif t == IMAGE: yield u'">' elif t == PARAGRAPH: self.pclass = None yield u'

    ' elif t == TABLE: yield u'
    ' elif t == TABLE_ROW: yield u'\n' elif t == TABLE_CELL: yield u'' elif t == FOOTNOTE: yield u'' elif t in (RIGHT, NOINDENT): yield u'

    ' elif t in (ERROR, SIZE): yield u'' else: yield u'' % self.tag(t, arg) def entity(self, t, arg=None): if t == HRULE: yield u'
    ' elif t == LINEBREAK: yield u'
    ' elif t == ENV_BREAK: # arg is False for a fake break (a context we couldn't break # paragraphs) and True for a real break (we closed the paragraph) if arg: yield u'\n\n' else: assert False, 42 yield u'

    \n\n' elif t == NOINDENT: self.pclass = u'noindent' elif t == ERROR: for s in self.start(ERROR): yield s yield escape(arg) for s in self.end(ERROR): yield s elif t == REF: yield placeholder(arg) else: assert False, t