def formatoption(block, width): desc = " ".join(map(str.strip, block["lines"])) colwidth = encoding.colwidth(block["optstr"]) usablewidth = width - 1 hanging = block["optstrwidth"] initindent = "%s%s " % (block["optstr"], " " * ((hanging - colwidth))) hangindent = " " * (encoding.colwidth(initindent) + 1) return " %s\n" % (util.wrap(desc, usablewidth, initindent=initindent, hangindent=hangindent))
def formatoption(block, width): desc = ' '.join(map(str.strip, block['lines'])) colwidth = encoding.colwidth(block['optstr']) usablewidth = width - 1 hanging = block['optstrwidth'] initindent = '%s%s ' % (block['optstr'], ' ' * ((hanging - colwidth))) hangindent = ' ' * (encoding.colwidth(initindent) + 1) return ' %s\n' % (util.wrap( desc, usablewidth, initindent=initindent, hangindent=hangindent))
def formatoption(block, width): desc = ' '.join(map(str.strip, block['lines'])) colwidth = encoding.colwidth(block['optstr']) usablewidth = width - 1 hanging = block['optstrwidth'] initindent = '%s%s ' % (block['optstr'], ' ' * ((hanging - colwidth))) hangindent = ' ' * (encoding.colwidth(initindent) + 1) return ' %s\n' % (util.wrap(desc, usablewidth, initindent=initindent, hangindent=hangindent))
def updateoptionlists(blocks): i = 0 while i < len(blocks): if blocks[i]['type'] != 'option': i += 1 continue optstrwidth = 0 j = i while j < len(blocks) and blocks[j]['type'] == 'option': m = _optionre.match(blocks[j]['lines'][0]) shortoption = m.group(2) group3 = m.group(3) longoption = group3[2:].strip() desc = m.group(6).strip() longoptionarg = m.group(5).strip() blocks[j]['lines'][0] = desc noshortop = '' if not shortoption: noshortop = ' ' opt = "%s%s" % (shortoption and "-%s " % shortoption or '', ("%s--%s %s") % (noshortop, longoption, longoptionarg)) opt = opt.rstrip() blocks[j]['optstr'] = opt optstrwidth = max(optstrwidth, encoding.colwidth(opt)) j += 1 for block in blocks[i:j]: block['optstrwidth'] = optstrwidth i = j + 1 return blocks
def maketable(data, indent=0, header=False): '''Generate an RST table for the given table data''' widths = [max(encoding.colwidth(e) for e in c) for c in zip(*data)] indent = ' ' * indent div = indent + ' '.join('=' * w for w in widths) + '\n' out = [div] for row in data: l = [] for w, v in zip(widths, row): pad = ' ' * (w - encoding.colwidth(v)) l.append(v + pad) out.append(indent + ' '.join(l) + "\n") if header and len(data) > 1: out.insert(2, div) out.append(div) return ''.join(out)
def maketable(data, indent=0, header=False): '''Generate an RST table for the given table data as a list of lines''' widths = [max(encoding.colwidth(e) for e in c) for c in zip(*data)] indent = ' ' * indent div = indent + ' '.join('=' * w for w in widths) + '\n' out = [div] for row in data: l = [] for w, v in zip(widths, row): if '\n' in v: # only remove line breaks and indentation, long lines are # handled by the next tool v = ' '.join(e.lstrip() for e in v.split('\n')) pad = ' ' * (w - encoding.colwidth(v)) l.append(v + pad) out.append(indent + ' '.join(l) + "\n") if header and len(data) > 1: out.insert(2, div) out.append(div) return out
def findsections(blocks): """Finds sections. The blocks must have a 'type' field, i.e., they should have been run through findliteralblocks first. """ for block in blocks: # Searching for a block that looks like this: # # +------------------------------+ # | Section title | # | ------------- | # +------------------------------+ if (block['type'] == 'paragraph' and len(block['lines']) == 2 and encoding.colwidth(block['lines'][0]) == len(block['lines'][1]) and _sectionre.match(block['lines'][1])): block['underline'] = block['lines'][1][0] block['type'] = 'section' del block['lines'][1] return blocks
def updateoptionlists(blocks): i = 0 while i < len(blocks): if blocks[i]["type"] != "option": i += 1 continue optstrwidth = 0 j = i while j < len(blocks) and blocks[j]["type"] == "option": m = _optionre.match(blocks[j]["lines"][0]) shortoption = m.group(2) group3 = m.group(3) longoption = group3[2:].strip() desc = m.group(6).strip() longoptionarg = m.group(5).strip() blocks[j]["lines"][0] = desc noshortop = "" if not shortoption: noshortop = " " opt = "%s%s" % ( shortoption and "-%s " % shortoption or "", ("%s--%s %s") % (noshortop, longoption, longoptionarg), ) opt = opt.rstrip() blocks[j]["optstr"] = opt optstrwidth = max(optstrwidth, encoding.colwidth(opt)) j += 1 for block in blocks[i:j]: block["optstrwidth"] = optstrwidth i = j + 1 return blocks
def formatblock(block, width): """Format a block according to width.""" if width <= 0: width = 78 indent = ' ' * block['indent'] if block['type'] == 'admonition': admonition = _admonitiontitles[block['admonitiontitle']] hang = len(block['lines'][-1]) - len(block['lines'][-1].lstrip()) defindent = indent + hang * ' ' text = ' '.join(map(str.strip, block['lines'])) return '%s\n%s' % ( indent + admonition, util.wrap( text, width=width, initindent=defindent, hangindent=defindent)) if block['type'] == 'margin': return '' if block['type'] == 'literal': indent += ' ' return indent + ('\n' + indent).join(block['lines']) if block['type'] == 'section': underline = encoding.colwidth(block['lines'][0]) * block['underline'] return "%s%s\n%s%s" % (indent, block['lines'][0], indent, underline) if block['type'] == 'definition': term = indent + block['lines'][0] hang = len(block['lines'][-1]) - len(block['lines'][-1].lstrip()) defindent = indent + hang * ' ' text = ' '.join(map(str.strip, block['lines'][1:])) return '%s\n%s' % ( term, util.wrap( text, width=width, initindent=defindent, hangindent=defindent)) subindent = indent if block['type'] == 'bullet': if block['lines'][0].startswith('| '): # Remove bullet for line blocks and add no extra # indention. block['lines'][0] = block['lines'][0][2:] else: m = _bulletre.match(block['lines'][0]) subindent = indent + m.end() * ' ' elif block['type'] == 'field': keywidth = block['keywidth'] key = block['key'] subindent = indent + _fieldwidth * ' ' if len(key) + 2 > _fieldwidth: # key too large, use full line width key = key.ljust(width) elif keywidth + 2 < _fieldwidth: # all keys are small, add only two spaces key = key.ljust(keywidth + 2) subindent = indent + (keywidth + 2) * ' ' else: # mixed sizes, use fieldwidth for this one key = key.ljust(_fieldwidth) block['lines'][0] = key + block['lines'][0] elif block['type'] == 'option': m = _optionre.match(block['lines'][0]) option, arg, rest = m.groups() subindent = indent + (len(option) + len(arg)) * ' ' text = ' '.join(map(str.strip, block['lines'])) return util.wrap(text, width=width, initindent=indent, hangindent=subindent)
def formatblock(block, width): """Format a block according to width.""" if width <= 0: width = 78 indent = ' ' * block['indent'] if block['type'] == 'admonition': admonition = _admonitiontitles[block['admonitiontitle']] if not block['lines']: return indent + admonition + '\n' hang = len(block['lines'][-1]) - len(block['lines'][-1].lstrip()) defindent = indent + hang * ' ' text = ' '.join(map(str.strip, block['lines'])) return '%s\n%s\n' % ( indent + admonition, util.wrap( text, width=width, initindent=defindent, hangindent=defindent)) if block['type'] == 'margin': return '\n' if block['type'] == 'literal': indent += ' ' return indent + ('\n' + indent).join(block['lines']) + '\n' if block['type'] == 'section': underline = encoding.colwidth(block['lines'][0]) * block['underline'] return "%s%s\n%s%s\n" % (indent, block['lines'][0], indent, underline) if block['type'] == 'table': table = block['table'] # compute column widths widths = [max([encoding.colwidth(e) for e in c]) for c in zip(*table)] text = '' span = sum(widths) + len(widths) - 1 indent = ' ' * block['indent'] hang = ' ' * (len(indent) + span - widths[-1]) for row in table: l = [] for w, v in zip(widths, row): pad = ' ' * (w - encoding.colwidth(v)) l.append(v + pad) l = ' '.join(l) l = util.wrap(l, width=width, initindent=indent, hangindent=hang) if not text and block['header']: text = l + '\n' + indent + '-' * (min(width, span)) + '\n' else: text += l + "\n" return text if block['type'] == 'definition': term = indent + block['lines'][0] hang = len(block['lines'][-1]) - len(block['lines'][-1].lstrip()) defindent = indent + hang * ' ' text = ' '.join(map(str.strip, block['lines'][1:])) return '%s\n%s\n' % ( term, util.wrap( text, width=width, initindent=defindent, hangindent=defindent)) subindent = indent if block['type'] == 'bullet': if block['lines'][0].startswith('| '): # Remove bullet for line blocks and add no extra # indention. block['lines'][0] = block['lines'][0][2:] else: m = _bulletre.match(block['lines'][0]) subindent = indent + m.end() * ' ' elif block['type'] == 'field': key = block['key'] subindent = indent + _fieldwidth * ' ' if len(key) + 2 > _fieldwidth: # key too large, use full line width key = key.ljust(width) else: # key fits within field width key = key.ljust(_fieldwidth) block['lines'][0] = key + block['lines'][0] elif block['type'] == 'option': return formatoption(block, width) text = ' '.join(map(str.strip, block['lines'])) return util.wrap( text, width=width, initindent=indent, hangindent=subindent) + '\n'
def formatblock(block, width): """Format a block according to width.""" if width <= 0: width = 78 indent = " " * block["indent"] if block["type"] == "admonition": admonition = _admonitiontitles[block["admonitiontitle"]] hang = len(block["lines"][-1]) - len(block["lines"][-1].lstrip()) defindent = indent + hang * " " text = " ".join(map(str.strip, block["lines"])) return "%s\n%s\n" % ( indent + admonition, util.wrap(text, width=width, initindent=defindent, hangindent=defindent), ) if block["type"] == "margin": return "\n" if block["type"] == "literal": indent += " " return indent + ("\n" + indent).join(block["lines"]) + "\n" if block["type"] == "section": underline = encoding.colwidth(block["lines"][0]) * block["underline"] return "%s%s\n%s%s\n" % (indent, block["lines"][0], indent, underline) if block["type"] == "table": table = block["table"] # compute column widths widths = [max([encoding.colwidth(e) for e in c]) for c in zip(*table)] text = "" span = sum(widths) + len(widths) - 1 indent = " " * block["indent"] hang = " " * (len(indent) + span - widths[-1]) for row in table: l = [] for w, v in zip(widths, row): pad = " " * (w - encoding.colwidth(v)) l.append(v + pad) l = " ".join(l) l = util.wrap(l, width=width, initindent=indent, hangindent=hang) if not text and block["header"]: text = l + "\n" + indent + "-" * (min(width, span)) + "\n" else: text += l + "\n" return text if block["type"] == "definition": term = indent + block["lines"][0] hang = len(block["lines"][-1]) - len(block["lines"][-1].lstrip()) defindent = indent + hang * " " text = " ".join(map(str.strip, block["lines"][1:])) return "%s\n%s\n" % (term, util.wrap(text, width=width, initindent=defindent, hangindent=defindent)) subindent = indent if block["type"] == "bullet": if block["lines"][0].startswith("| "): # Remove bullet for line blocks and add no extra # indention. block["lines"][0] = block["lines"][0][2:] else: m = _bulletre.match(block["lines"][0]) subindent = indent + m.end() * " " elif block["type"] == "field": keywidth = block["keywidth"] key = block["key"] subindent = indent + _fieldwidth * " " if len(key) + 2 > _fieldwidth: # key too large, use full line width key = key.ljust(width) elif keywidth + 2 < _fieldwidth: # all keys are small, add only two spaces key = key.ljust(keywidth + 2) subindent = indent + (keywidth + 2) * " " else: # mixed sizes, use fieldwidth for this one key = key.ljust(_fieldwidth) block["lines"][0] = key + block["lines"][0] elif block["type"] == "option": return formatoption(block, width) text = " ".join(map(str.strip, block["lines"])) return util.wrap(text, width=width, initindent=indent, hangindent=subindent) + "\n"
def subsubsubsection(s): return "%s\n%s\n\n" % (s, "." * encoding.colwidth(s))
def section(s): return "%s\n%s\n\n" % (s, "\"" * encoding.colwidth(s))
def formatblock(block, width): """Format a block according to width.""" if width <= 0: width = 78 indent = ' ' * block['indent'] if block['type'] == 'admonition': admonition = _admonitiontitles[block['admonitiontitle']] hang = len(block['lines'][-1]) - len(block['lines'][-1].lstrip()) defindent = indent + hang * ' ' text = ' '.join(map(str.strip, block['lines'])) return '%s\n%s\n' % (indent + admonition, util.wrap(text, width=width, initindent=defindent, hangindent=defindent)) if block['type'] == 'margin': return '\n' if block['type'] == 'literal': indent += ' ' return indent + ('\n' + indent).join(block['lines']) + '\n' if block['type'] == 'section': underline = encoding.colwidth(block['lines'][0]) * block['underline'] return "%s%s\n%s%s\n" % (indent, block['lines'][0],indent, underline) if block['type'] == 'table': table = block['table'] # compute column widths widths = [max([encoding.colwidth(e) for e in c]) for c in zip(*table)] text = '' span = sum(widths) + len(widths) - 1 indent = ' ' * block['indent'] hang = ' ' * (len(indent) + span - widths[-1]) for row in table: l = [] for w, v in zip(widths, row): pad = ' ' * (w - encoding.colwidth(v)) l.append(v + pad) l = ' '.join(l) l = util.wrap(l, width=width, initindent=indent, hangindent=hang) if not text and block['header']: text = l + '\n' + indent + '-' * (min(width, span)) + '\n' else: text += l + "\n" return text if block['type'] == 'definition': term = indent + block['lines'][0] hang = len(block['lines'][-1]) - len(block['lines'][-1].lstrip()) defindent = indent + hang * ' ' text = ' '.join(map(str.strip, block['lines'][1:])) return '%s\n%s\n' % (term, util.wrap(text, width=width, initindent=defindent, hangindent=defindent)) subindent = indent if block['type'] == 'bullet': if block['lines'][0].startswith('| '): # Remove bullet for line blocks and add no extra # indention. block['lines'][0] = block['lines'][0][2:] else: m = _bulletre.match(block['lines'][0]) subindent = indent + m.end() * ' ' elif block['type'] == 'field': key = block['key'] subindent = indent + _fieldwidth * ' ' if len(key) + 2 > _fieldwidth: # key too large, use full line width key = key.ljust(width) else: # key fits within field width key = key.ljust(_fieldwidth) block['lines'][0] = key + block['lines'][0] elif block['type'] == 'option': return formatoption(block, width) text = ' '.join(map(str.strip, block['lines'])) return util.wrap(text, width=width, initindent=indent, hangindent=subindent) + '\n'
def formatblock(block, width): """Format a block according to width.""" if width <= 0: width = 78 indent = ' ' * block['indent'] if block['type'] == 'admonition': admonition = _admonitiontitles[block['admonitiontitle']] hang = len(block['lines'][-1]) - len(block['lines'][-1].lstrip()) defindent = indent + hang * ' ' text = ' '.join(map(str.strip, block['lines'])) return '%s\n%s' % (indent + admonition, util.wrap(text, width=width, initindent=defindent, hangindent=defindent)) if block['type'] == 'margin': return '' if block['type'] == 'literal': indent += ' ' return indent + ('\n' + indent).join(block['lines']) if block['type'] == 'section': underline = encoding.colwidth(block['lines'][0]) * block['underline'] return "%s%s\n%s%s" % (indent, block['lines'][0],indent, underline) if block['type'] == 'definition': term = indent + block['lines'][0] hang = len(block['lines'][-1]) - len(block['lines'][-1].lstrip()) defindent = indent + hang * ' ' text = ' '.join(map(str.strip, block['lines'][1:])) return '%s\n%s' % (term, util.wrap(text, width=width, initindent=defindent, hangindent=defindent)) subindent = indent if block['type'] == 'bullet': if block['lines'][0].startswith('| '): # Remove bullet for line blocks and add no extra # indention. block['lines'][0] = block['lines'][0][2:] else: m = _bulletre.match(block['lines'][0]) subindent = indent + m.end() * ' ' elif block['type'] == 'field': keywidth = block['keywidth'] key = block['key'] subindent = indent + _fieldwidth * ' ' if len(key) + 2 > _fieldwidth: # key too large, use full line width key = key.ljust(width) elif keywidth + 2 < _fieldwidth: # all keys are small, add only two spaces key = key.ljust(keywidth + 2) subindent = indent + (keywidth + 2) * ' ' else: # mixed sizes, use fieldwidth for this one key = key.ljust(_fieldwidth) block['lines'][0] = key + block['lines'][0] elif block['type'] == 'option': return formatoption(block, width) text = ' '.join(map(str.strip, block['lines'])) return util.wrap(text, width=width, initindent=indent, hangindent=subindent)