Example #1
0
def build_toc(docroot, basepath=None, block=None, i=0, depth=0, maxdepth=99):
    # If this is the "top" call, create a block to return
    block = block or {"type": "toc", "is_container": True}
    parents = docroot.get("parents")
    if not parents:
        return block

    # i is an index into the list of parents; it increases as we descend through
    # ancestors toward the current page

    # Get the "current" parent
    p = parents[i]
    # Copy the parent's attrs onto the block
    block.setdefault("attrs", {})
    block["attrs"].update(p["attrs"])

    # Copy the subtopics body into the block's body
    subtopics = p["subtopics"]
    if subtopics:
        block["body"] = copy.deepcopy(subtopics.get("body"))
    else:
        block["body"] = []

    if depth < maxdepth:
        # Let's get recursive all up in here
        for item in find_items(block["body"], "subtopics_item"):
            link = first_span_of_type(item.get("text"), "link")
            if not link:
                continue
            fullpath = link.get("fullpath")

            found = False
            if basepath and fullpath == basepath:
                item["is_here"] = True
                st = subblock_by_id(docroot, "subtopics")
                if st:
                    item["body"] = st.get("body", ())
            else:
                for j in xrange(i + 1, len(parents)):
                    if parents[j]["basepath"] == fullpath:
                        item["is_ancestor"] = True
                        build_toc(docroot, basepath, item, j, depth + 1,
                                  maxdepth)
                        found = True
                        break
            if found:
                break

    return block
Example #2
0
def random_name(length=5):
    charcount = len(random_chars)
    return "".join(random_chars[random.randint(0, charcount)]
                   for _ in xrange(length))