Exemplo n.º 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)
Exemplo n.º 2
0
 def _get_method_names(self, block):
     methodnames = set()
     section = functions.subblock_by_id(block, "methods")
     if section:
         for methblock in functions.find_items(section, "methods_item"):
             text = functions.string(methblock.get("text"))
             name = text.split("(")[0]
             methodnames.add(name)
     return methodnames
Exemplo n.º 3
0
    def apply(self, block, context):
        basepath = paths.basepath(context.get("path"))

        # Find the subtopics section
        subtopics = functions.subblock_by_id(block, "subtopics")
        if not subtopics:
            return

        attrs = subtopics.get("attrs", {})
        maxdepth = int(attrs.get("maxdepth", "0"))
        if not maxdepth:
            return

        topics = functions.find_items(subtopics, "subtopics_item")
        for item in topics:
            self._apply(context, item, basepath, 1, maxdepth)
Exemplo n.º 4
0
def find_parm(root, parmid, label):
    """
    Tries to find a parameter in a help document, based on the parameter ID and
    its label.
    """

    from bookish.util import dump_tree

    section = functions.subblock_by_id(root, "parameters")
    if section:
        parameters = functions.find_items(section, "parameters_item")
        for parmblock in parameters:
            if functions.block_id(parmblock) == parmid:
                return parmblock
            elif functions.string(parmblock.get("text")).strip() == label:
                return parmblock
Exemplo n.º 5
0
    def root_block(self, block):
        attrs = block.get("attrs", {})
        self.pagetype = pagetype = attrs.get("type")
        body = block.get("body", ())

        if pagetype == "hscript":
            # Command help has the title at column 0 and all other text
            # indented. We'll indent everything here, and then outdent the
            # title
            with self.push(indent=4):
                self.render(body)

        elif pagetype == "expression" or pagetype == "vex":
            self.emit(u"{")
            self.render_body(body)
            self.emit(u"}")

        elif pagetype == "hompackage":
            self.emit(u'%define MODULE_DOCSTRING /**/ "', top=1)
            with self.push(replacements=repl_quotes):
                self.render(body)
            self.emit(u'" %enddef', bottom=1)

        elif pagetype in (u"homclass", u"homfunction", u"hommodule"):
            cppname = attrs["cppname"]
            self.emit(u'%%feature("docstring") %s "' % cppname)
            nonsect = list(subb for subb in body
                           if subb.get("role") != "section")
            with self.push(replacements=repl_quotes):
                self.render(nonsect)
            self.emit(u'";')

            for subblock in body:
                sid = subblock.get("id")
                if sid in (u"methods", u"functions"):
                    itemtype = sid + "_item"
                    for itemblock in functions.find_items(subblock, itemtype):
                        self._homfn(itemblock)

        elif pagetype == u"hom_module":
            self.emit(u'%define MODULE_DOCSTRING /**/ "')
            with self.push(replacements=repl_quotes):
                self.render(body)
            self.emit(u'" %enddef')

        else:
            self.render(body)
Exemplo n.º 6
0
    def _superclasses(self, pages, methodnames, context, block, history=None):
        # Recursively loads the doc pointed to by the block's "superclass"
        # attribute and yields a (path, rootblock) pair for each superclass

        history = history or set()
        attrs = block.get("attrs", {})
        superclass = attrs.get("superclass")
        if superclass:
            path = "/hom/" + superclass.replace(".", "/")
            spath = pages.source_path(path)

            if pages.exists(spath):
                if spath in history:
                    raise Exception("Circular superclass structure")
                else:
                    history.add(spath)

                doc = pages.json(spath, conditional=context.get("conditional"),
                                 postprocess=False)

                titleblock = functions.first_subblock_of_type(doc, "title")
                if titleblock:
                    title = functions.string(titleblock.get("text"))
                else:
                    title = superclass

                # Find the method items on the superclass
                section = functions.subblock_by_id(doc, "methods")
                methods = []
                if section:
                    for methblock in functions.find_items(doc, "methods_item"):
                        text = methblock.get("text")
                        name = functions.string(text).split("(")[0]
                        attrs = methblock.get("attrs", {})
                        body = methblock.get("body", [])

                        # If this name is in the set of seen methods, it's
                        # overridden, so we should skip it
                        if name in methodnames:
                            continue
                        methodnames.add(name)

                        # Copy information about the method into a dict
                        summary = functions.first_subblock_string(methblock)
                        methdict = {
                            "name": name,
                            "text": text,
                            "summary": summary,
                            "more": len(body) > 1,
                        }
                        if "status" in attrs:
                            methdict["status"] = attrs["status"]
                        methods.append(methdict)

                yield {
                    "path": path,
                    "title": title,
                    "methods": methods
                }
                for x in self._superclasses(pages, methodnames, context, doc,
                                            history):
                    yield x