def li(self, elem, theme, width): """List element; this element is a bit of a special case.""" block, indent = [], ansi.length(theme.margin) inline = u('') # TODO: the `br` handling here is kind of a hack, must rework for child in self.children(elem): if child.tag in self.BLOCK: if child.tag in ['br']: inline += u('\n') if len(inline) > 0: block += wrap_text([inline], self.justify, width) inline = u('') if child.tag not in ['ol', 'ul', 'br']: block += [''] if child.tag not in ['br']: block += self.call(child, width - indent) elif child.tag in self.INLINE: inline += self.call(child) if len(inline) > 0: block += wrap_text([inline], self.justify, width) if (len(block) > 0) and block[0].strip() == '': del block[0] return [theme.margin + line.lstrip('\n') for line in block]
def ul(self, elem, theme, width): """Unordered list, has complex margins. Contains <li> only.""" block, indent = [], ansi.length(theme.margin.head) for child in self.children(elem, {u('li')}): block += [u('')] + add_margin(self.call(child, width - indent), theme.margin.head, theme.margin.tail(indent)) return block
def just(line, width): words = line.split(' ') done = {t: t == len(words) - 1 for t in range(len(words))} if width - ansi.length(line) > 14: return line while ansi.length(' '.join(words)) < width: best = -1 for t, word in enumerate(words): if not done[t]: if (best == -1) or (len(words[t]) < len(words[best])): best = t if best == -1: done = {t: t == len(words) - 1 for t in range(len(words))} else: words[best] += ' ' done[best] = True return ' '.join(words)
def blockquote(self, elem, theme, width): """Blockquote, can contain everything (equivalent to div).""" block, indent = [], ansi.length(theme.margin) for child in self.children(elem, self.BLOCK): if child.tag not in [u('ol'), u('ul'), u('br')]: block += [u('')] block += [line for line in self.call(child, width - indent)] if len(block) > 0: del block[0] return [theme.margin + line for line in block]
def p(self, elem, theme, width): """Paragraph, which can only contain other inline elements.""" block, indent = [], ansi.length(theme.margin) for child in self.children(elem, self.INLINE | {'br'}): if child.tag == 'br': block += self.call(child, width) elif child.tag in self.INLINE: if len(block) > 0: block[-1] += self.call(child) else: block = [self.call(child)] text = [line.lstrip('\n') for line in block] wrapped = wrap_text(text, self.justify, width) return [theme.margin + line for line in wrapped]
def div(self, elem, theme, width): """Div block element, used only as root element in markdown.""" block, indent = [], ansi.length(theme.margin) for child in self.children(elem, self.BLOCK): if child.tag not in [u('ul'), u('ol'), u('br')] and len(block) != 0: block += [''] block += [line for line in self.call(child, width - indent)] if len(self.links) > 0: block += [''] for t, link in enumerate(self.links): block += [theme.links.format(index=t + 1, href=link)] return [theme.margin + line for line in block]
def div(self, elem, theme, width): """Div block element, used only as root element in markdown.""" block, indent = [], ansi.length(theme.margin) for child in self.children(elem, self.BLOCK): if child.tag not in [u('ul'), u('ol'), u('br') ] and len(block) != 0: block += [''] block += [line for line in self.call(child, width - indent)] if len(self.links) > 0: block += [''] for t, link in enumerate(self.links): block += [theme.links.format(index=t + 1, href=link)] return [theme.margin + line for line in block]
def hr(self, elem, theme, width): """Horizontal rule that should occupy all available width.""" offset = ansi.length(theme.head) + ansi.length(theme.last) return [theme.head + theme.rule * (width - offset) + theme.last]