2020-01-10 07:35:52 +08:00
|
|
|
# ===------------ doc_parser.py - Documentation Parsing Utility ------------===//
|
|
|
|
#
|
|
|
|
# Copyright 2019-2020 The IBM Research Authors.
|
|
|
|
#
|
|
|
|
# =============================================================================
|
|
|
|
#
|
|
|
|
# ===----------------------------------------------------------------------===//
|
|
|
|
|
2020-04-08 15:00:34 +08:00
|
|
|
import logging
|
|
|
|
|
2020-01-10 07:35:52 +08:00
|
|
|
from typing import List
|
|
|
|
from utils import *
|
|
|
|
|
|
|
|
from directive import Directive, generic_config_parser
|
|
|
|
|
2020-04-08 15:00:34 +08:00
|
|
|
logger = logging.getLogger('doc-check')
|
2020-01-10 07:35:52 +08:00
|
|
|
|
|
|
|
def parse_code_section_delimiter(ctx):
|
|
|
|
assert ctx.doc_file_ext() == ".md"
|
|
|
|
if not ctx.doc_file.next_non_empty_line().strip().startswith("```"):
|
|
|
|
raise ValueError("Did not parse a code section delimiter")
|
|
|
|
|
|
|
|
|
|
|
|
def try_parse_and_handle_directive(ctx):
|
|
|
|
from directive_impl import same_as_file
|
2020-04-08 15:00:34 +08:00
|
|
|
from directive_impl import file_same_as_stdout
|
|
|
|
|
|
|
|
try:
|
|
|
|
line = ctx.doc_file.next_non_empty_line()
|
|
|
|
except RuntimeError as e:
|
|
|
|
# Do not raise exception when next non-empty line
|
|
|
|
# does not exist. Instead, return failure.
|
|
|
|
if str(e) != "Enf of file.":
|
|
|
|
raise
|
|
|
|
return failure()
|
2020-01-10 07:35:52 +08:00
|
|
|
|
|
|
|
# Register all directives.
|
|
|
|
all_directives: List[Directive] = [
|
2020-04-08 15:00:34 +08:00
|
|
|
Directive(same_as_file.ext_to_patterns, [generic_config_parser, same_as_file.parse], same_as_file.handle),
|
|
|
|
Directive(file_same_as_stdout.ext_to_patterns, [generic_config_parser], file_same_as_stdout.handle),
|
2020-01-10 07:35:52 +08:00
|
|
|
]
|
|
|
|
|
|
|
|
for directive in all_directives:
|
|
|
|
directive_config = []
|
2020-04-08 15:00:34 +08:00
|
|
|
if succeeded(directive.try_parse_directive(line, ctx.doc_file_ext(), directive_config)):
|
2020-01-10 07:35:52 +08:00
|
|
|
directive.handle(directive_config.pop(), ctx)
|
|
|
|
return success(directive_config)
|
|
|
|
|
|
|
|
return failure()
|