def write_dl(self, rows, col_max=30, col_spacing=2): # noqa: D102 rows = list(rows) widths = measure_table(rows) if len(widths) != 2: raise TypeError("Expected two columns for definition list") first_col = min(widths[0], col_max) + col_spacing for first, second in iter_rows(rows, len(widths)): self.write(f"{'':>{self.current_indent}}{first}") if not second: self.write("\n") continue if term_len(first) <= first_col - col_spacing: self.write(" " * (first_col - term_len(first))) else: self.write("\n") self.write(" " * (first_col + self.current_indent)) text_width = max(self.width - first_col - 2, 10) wrapped_text = wrap_text(second, text_width, preserve_paragraphs=True) lines = wrapped_text.splitlines() if lines: self.write(f"{lines[0]}\n") for line in lines[1:]: self.write( f"{'':>{first_col + self.current_indent}}{line}\n") else: self.write("\n")
def write_dl(self, rows, col_max=30, col_spacing=2): rows = list(rows) widths = measure_table(rows) if len(widths) != 2: raise TypeError("Expected two columns for definition list") first_col = min(widths[0], col_max) + col_spacing for first, second in iter_rows(rows, len(widths)): self.write("%*s%s" % (self.current_indent, "", context.io.cyan(first))) if not second: self.write("\n") continue if term_len(first) <= first_col - col_spacing: self.write(" " * (first_col - term_len(first))) else: self.write("\n") self.write(" " * (first_col + self.current_indent)) text_width = max(self.width - first_col - 2, 10) lines = iter(wrap_text(second, text_width).splitlines()) if lines: self.write(next(lines) + "\n") for line in lines: self.write("%*s%s\n" % (first_col + self.current_indent, "", line)) else: self.write("\n")
def write_text(self, text): """Writes re-indented text into the buffer. This rewraps and preserves paragraphs. """ text_width = max(self.width - self.current_indent, 11) indent = ' ' * self.current_indent self.write( wrap_text(text, text_width, initial_indent=indent, subsequent_indent=indent, preserve_paragraphs=True)) self.write('\n')
def write_dl(self, rows, col_max=30, col_spacing=2, widths=None): """Writes a definition list into the buffer. This is how options and commands are usually formatted. :param rows: a list of two item tuples for the terms and values. :param col_max: the maximum width of the first column. :param col_spacing: the number of spaces between the first and second column. :param widths: optional pre-calculated line widths """ rows = list(rows) if widths is None: widths = measure_table(rows) if len(widths) != 2: raise TypeError('Expected two columns for definition list') first_col = min(widths[0], col_max) + col_spacing for first, second in iter_rows(rows, len(widths)): self.write('%*s%s' % (self.current_indent, '', first)) if not second: self.write('\n') continue if term_len(first) <= first_col - col_spacing: self.write(' ' * (first_col - term_len(first))) else: self.write('\n') self.write(' ' * (first_col + self.current_indent)) text_width = max(self.width - first_col - 2, 10) lines = iter(wrap_text(second, text_width).splitlines()) if lines: self.write(next(lines) + '\n') for line in lines: self.write('%*s%s\n' % ( first_col + self.current_indent, '', line, )) else: self.write('\n')
def write_dl(self, rows, col_max=30, col_spacing=2, widths=None): """Writes a definition list into the buffer. This is how options and commands are usually formatted. :param rows: a list of two item tuples for the terms and values. :param col_max: the maximum width of the first column. :param col_spacing: the number of spaces between the first and second column. :param widths: optional pre-calculated line widths """ rows = list(rows) if widths is None: widths = measure_table(rows) if len(widths) != 2: raise TypeError('Expected two columns for definition list') first_col = min(widths[0], col_max) + col_spacing for first, second in iter_rows(rows, len(widths)): self.write('%*s%s' % (self.current_indent, '', first)) if not second: self.write('\n') continue if term_len(first) <= first_col - col_spacing: self.write(' ' * (first_col - term_len(first))) else: self.write('\n') self.write(' ' * (first_col + self.current_indent)) text_width = max(self.width - first_col - 2, 10) lines = iter(wrap_text(second, text_width).splitlines()) if lines: self.write(next(lines) + '\n') for line in lines: self.write('%*s%s\n' % ( first_col + self.current_indent, '', line)) else: self.write('\n')
def gen_command(index, index2, cmd, target_path, parent_ctx): out = [] with click.Context(cmd, parent=parent_ctx, info_name=cmd.name) as ctx: category = parent_ctx.command.name if category == "cli": category = "shortcuts" meta = { "title": ctx.command_path, "short_title": cmd.name, "category": category, "path": "/" + category + "/" + cmd.name, } write_meta(meta, out) out.append(cmd.get_short_help_str()) out.append("") if cmd.deprecated: out.append("~~DEPRECATED~~") out.append("") out.append("### Usage") out.append("```bash") pieces = cmd.collect_usage_pieces(ctx) out.append(f"{ctx.command_path} " + " ".join(pieces)) out.append("```") out.append("") help, *examples = split_examples(cmd.help) help2 = click.unstyle(help) help3 = re.sub(r"([A-Z0-9\-]{3,60})", r"`\1`", help2) out.append(wrap_text(help3)) out.append("") for example in examples: out.append("### Examples") out.append("") out.append("```bash") example2 = click.unstyle(example) for line in example2.splitlines(): line = line.strip() if line.startswith("#"): out.append(line) else: if line: out.append("$ " + " ".join(shlex.split(line))) else: out.append("") out.append("```") out.append("") opts = [] w1 = w2 = 0 for param in cmd.get_params(ctx): rv = param.get_help_record(ctx) if rv is None: continue name, descr = rv # durty code for wrapping options with backticks l4 = [] l1 = re.split(" ?/ ?", name) for part in l1: l2 = re.split(" ?, ?", part) l4.append(", ".join(["`" + part2 + "`" for part2 in l2])) name2 = " / ".join(l4) descr2 = re.sub(r"(\[.+\])", r"_\1_", descr) w1 = max(w1, len(name2)) w2 = max(w2, len(descr2)) opts.append((name2, descr2)) name_title = "Name".ljust(w1) descr_title = "Description".ljust(w2) name_sep = "-" * w1 descr_sep = "-" * w2 out.append("### Options") out.append("") out.append(f"| {name_title} | {descr_title} |") out.append(f"| {name_sep} | {descr_sep} |") for name, descr in opts: name = name.ljust(w1) descr = descr.ljust(w2) out.append(f"| {name} | {descr} |") fname = target_path / f"{index:02d}_{index2:02d}__{cmd.name}.md" fname.write_text("\n".join(out))