diff options
Diffstat (limited to 'catapult/devil/devil/utils/markdown.py')
-rwxr-xr-x | catapult/devil/devil/utils/markdown.py | 66 |
1 files changed, 32 insertions, 34 deletions
diff --git a/catapult/devil/devil/utils/markdown.py b/catapult/devil/devil/utils/markdown.py index ba666643..12d2eb3c 100755 --- a/catapult/devil/devil/utils/markdown.py +++ b/catapult/devil/devil/utils/markdown.py @@ -19,8 +19,8 @@ _CODE_BLOCK_FORMAT = '''```{language} ``` ''' -_DEVIL_ROOT = os.path.abspath(os.path.join( - os.path.dirname(__file__), '..', '..')) +_DEVIL_ROOT = os.path.abspath( + os.path.join(os.path.dirname(__file__), '..', '..')) def md_bold(raw_text): @@ -31,14 +31,15 @@ def md_bold(raw_text): def md_code(raw_text, language): """Returns a markdown-formatted code block in the given language.""" return _CODE_BLOCK_FORMAT.format( - language=language or '', - code=md_escape(raw_text, characters='`')) + language=language or '', code=md_escape(raw_text, characters='`')) def md_escape(raw_text, characters='*_'): """Escapes * and _.""" + def escape_char(m): return '\\%s' % m.group(0) + pattern = '[%s]' % re.escape(characters) return re.sub(pattern, escape_char, raw_text) @@ -46,8 +47,8 @@ def md_escape(raw_text, characters='*_'): def md_heading(raw_text, level): """Returns markdown-formatted heading.""" adjusted_level = min(max(level, 0), 6) - return '%s%s%s' % ( - '#' * adjusted_level, ' ' if adjusted_level > 0 else '', raw_text) + return '%s%s%s' % ('#' * adjusted_level, ' ' if adjusted_level > 0 else '', + raw_text) def md_inline_code(raw_text): @@ -62,9 +63,8 @@ def md_italic(raw_text): def md_link(link_text, link_target): """returns a markdown-formatted link.""" - return '[%s](%s)' % ( - md_escape(link_text, characters=']'), - md_escape(link_target, characters=')')) + return '[%s](%s)' % (md_escape(link_text, characters=']'), + md_escape(link_target, characters=')')) class MarkdownHelpFormatter(argparse.HelpFormatter): @@ -112,8 +112,10 @@ class MarkdownHelpFormatter(argparse.HelpFormatter): class MarkdownHelpAction(argparse.Action): - def __init__(self, option_strings, - dest=argparse.SUPPRESS, default=argparse.SUPPRESS, + def __init__(self, + option_strings, + dest=argparse.SUPPRESS, + default=argparse.SUPPRESS, **kwargs): super(MarkdownHelpAction, self).__init__( option_strings=option_strings, @@ -137,8 +139,10 @@ def add_md_help_argument(parser): Args: parser: The ArgumentParser to which --md-help should be added. """ - parser.add_argument('--md-help', action=MarkdownHelpAction, - help='print Markdown-formatted help text and exit.') + parser.add_argument( + '--md-help', + action=MarkdownHelpAction, + help='print Markdown-formatted help text and exit.') def load_module_from_path(module_path): @@ -172,24 +176,25 @@ def load_module_from_path(module_path): return module -def md_module(module_obj, module_path=None, module_link=None): - """Write markdown documentation for a class. +def md_module(module_obj, module_link=None): + """Write markdown documentation for a module. Documents public classes and functions. Args: - class_obj: a types.TypeType object for the class that should be - documented. + module_obj: a module object that should be documented. Returns: A list of markdown-formatted lines. """ + def should_doc(name): return (not isinstance(module_obj.__dict__[name], types.ModuleType) and not name.startswith('_')) - stuff_to_doc = sorted( - obj for name, obj in module_obj.__dict__.iteritems() - if should_doc(name)) + stuff_to_doc = [ + obj for name, obj in sorted(module_obj.__dict__.iteritems()) + if should_doc(name) + ] classes_to_doc = [] functions_to_doc = [] @@ -200,12 +205,6 @@ def md_module(module_obj, module_path=None, module_link=None): elif isinstance(s, types.FunctionType): functions_to_doc.append(s) - command = ['devil/utils/markdown.py'] - if module_link: - command.extend(['--module-link', module_link]) - if module_path: - command.append(os.path.relpath(module_path, _DEVIL_ROOT)) - heading_text = module_obj.__name__ if module_link: heading_text = md_link(heading_text, module_link) @@ -213,8 +212,8 @@ def md_module(module_obj, module_path=None, module_link=None): content = [ md_heading(heading_text, level=1), '', - md_italic('This page was autogenerated by %s' - % md_inline_code(' '.join(command))), + md_italic('This page was autogenerated. ' + 'Run `devil/bin/generate_md_docs` to update'), '', ] @@ -248,9 +247,10 @@ def md_class(class_obj): return (isinstance(obj, types.FunctionType) and (name.startswith('__') or not name.startswith('_'))) - methods_to_doc = sorted( - obj for name, obj in class_obj.__dict__.iteritems() - if should_doc(name, obj)) + methods_to_doc = [ + obj for name, obj in sorted(class_obj.__dict__.iteritems()) + if should_doc(name, obj) + ] for m in methods_to_doc: content.extend(md_function(m, class_obj=class_obj)) @@ -313,10 +313,8 @@ def main(raw_args): args = parser.parse_args(raw_args) return md_module( - load_module_from_path(args.module_path), - module_link=args.module_link) + load_module_from_path(args.module_path), module_link=args.module_link) if __name__ == '__main__': sys.exit(main(sys.argv[1:])) - |