Exemplo n.º 1
0
Arquivo: page.py Projeto: Ahrak/mkapi
 def split(self, source: str) -> Iterator[str]:
     cursor = 0
     callback = self.resolve_link_from_base
     for index, match in enumerate(MKAPI_PATTERN.finditer(source)):
         start, end = match.start(), match.end()
         if cursor < start:
             markdown = source[cursor:start].strip()
             if markdown:
                 yield self.resolve_link(markdown)
         heading, name = match.groups()
         level = len(heading)
         name, filters = utils.split_filters(name)
         if "code" in filters:
             code = get_code(name)
             self.nodes.append(code)
             markdown = code.get_markdown(level)
         else:
             node = get_node(name)
             inherit(node)
             postprocess.transform(node, filters)
             self.nodes.append(node)
             markdown = node.get_markdown(level, callback=callback)
         yield node_markdown(index, markdown, filters)
         cursor = end
     if cursor < len(source):
         markdown = source[cursor:].strip()
         if markdown:
             yield self.resolve_link(markdown)
Exemplo n.º 2
0
def collect(path: str, docs_dir: str, config_dir,
            global_filters) -> Tuple[list, list]:
    _, api_path, *paths, package_path = path.split("/")
    abs_api_path = os.path.join(docs_dir, api_path)
    if os.path.exists(abs_api_path):
        logger.error(
            f"[MkApi] {abs_api_path} exists: Delete manually for safety.")
        sys.exit(1)
    os.mkdir(abs_api_path)
    os.mkdir(os.path.join(abs_api_path, "source"))
    atexit.register(lambda path=abs_api_path: rmtree(path))

    root = os.path.join(config_dir, *paths)
    if root not in sys.path:
        sys.path.insert(0, root)

    package_path, filters = utils.split_filters(package_path)
    filters = utils.update_filters(global_filters, filters)

    nav = []
    abs_api_paths = []
    modules: Dict[str, str] = {}
    package = None

    def add_page(module: Module, package: Optional[str]):
        page_file = module.object.id + ".md"
        abs_path = os.path.join(abs_api_path, page_file)
        abs_api_paths.append(abs_path)
        create_page(abs_path, module, filters)
        page_name = module.object.id
        if package and "short_nav" in filters and page_name != package:
            page_name = page_name[len(package) + 1:]
        modules[page_name] = os.path.join(api_path, page_file)

        abs_path = os.path.join(abs_api_path, "source", page_file)
        create_source_page(abs_path, module, filters)

    module = get_module(package_path)
    for m in module:
        print(m)
        if m.object.kind == "package":
            if package and modules:
                nav.append({package: modules})
            package = m.object.id
            modules = {}
            if m.docstring or any(s.docstring for s in m.members):
                add_page(m, package)
        else:
            add_page(m, package)
    if package and modules:
        nav.append({package: modules})

    return nav, abs_api_paths
Exemplo n.º 3
0
 def split(self, source: str) -> Iterator[str]:
     cursor = 0
     callback = self.resolve_link_from_base
     index = 0
     for match in MKAPI_PATTERN.finditer(source):
         start, end = match.start(), match.end()
         if cursor < start:
             markdown = source[cursor:start].strip()
             if markdown:
                 yield self.resolve_link(markdown)
         cursor = end
         heading, name = match.groups()
         level = len(heading)
         name, filters = utils.split_filters(name)
         if not name:
             self.filters = filters
             continue
         filters = utils.update_filters(self.filters, filters)
         if "code" in filters:
             code = get_code(name)
             self.nodes.append(code)
             markdown = code.get_markdown(level)
         else:
             node = get_node(name)
             inherit(node)
             postprocess.transform(node, filters)
             self.nodes.append(node)
             markdown = node.get_markdown(level, callback=callback)
             if level:
                 self.headings.append((level, node.object.id))
         yield node_markdown(index, markdown, filters)
         index += 1
     if cursor < len(source):
         markdown = source[cursor:].strip()
         if markdown:
             yield self.resolve_link(markdown)