Example #1
0
    def _apply(context, item, basepath, depth, maxdepth):
        pages = context.pages

        # Find the first link in the item and use its path
        text = item.get("text")
        link = functions.first_span_of_type(text, "link")
        if not link:
            return
        ref = link.get("value")
        if not ref or ":" in ref:
            return

        fullpath = pages.full_path(basepath, ref)
        if not pages.exists(fullpath):
            return

        # Load the referenced page
        json = pages.json(fullpath, context)
        # Find the subtopics section
        subtopics = functions.subblock_by_id(json, "subtopics")
        if not subtopics:
            return

        # Copy the subtopics onto this topic's body
        if "body" in subtopics:
            body = copy.deepcopy(subtopics["body"])
            # Collapse certain block types
            body = functions.collapse(body, ("col_group", "col"))
            item["body"] = body

        if depth < maxdepth:
            # Recurse on the loaded subtopics
            topics = functions.find_items(subtopics, "subtopics_item")
            for subitem in topics:
                Toc._apply(context, subitem, fullpath, depth + 1, maxdepth)
Example #2
0
    def _parent_info(json, path):
        # Find the subtopics section
        subtopics = functions.subblock_by_id(json, "subtopics")
        if subtopics:
            stbody = subtopics.get("body")
            if stbody:
                # Remove
                body = functions.collapse(stbody, ("col_group", "col"))
                subtopics["body"] = body

        return {
            "path": path,
            "basepath": paths.basepath(path),
            "title": json.get("title", ()),
            "summary": json.get("summary", ()),
            "attrs": json.get("attrs", {}),
            "subtopics": subtopics,
        }