コード例 #1
0
def createTokenHelper(key, parent, info, page, use_key_in_modal=False, optional=False, exact=False):
    match = PAGE_LINK_RE.search(info[key])
    bookmark = match.group('bookmark')[1:] if match.group('bookmark') else None
    filename = match.group('filename')

    # The link is local (i.e., [#foo]), the heading will be gathered on render because it
    # could be after the current position.
    if (filename is None) and (bookmark is not None or (match.group('bookmark') == '#')):
        return LocalLink(parent, bookmark=bookmark)

    elif filename is not None:
        return AutoLink(parent, page=filename, bookmark=bookmark, optional=optional, exact=exact)

    else:
        source = common.project_find(info[key])
        if len(source) > 1:
            options = mooseutils.levenshteinDistance(info[key], source, number=8)
            msg = "Multiple files match the supplied filename {}, did you mean:\n".format(info[key])
            for opt in options:
                msg += "    {}\n".format(opt)
            raise exceptions.MooseDocsException(msg)

        elif len(source) == 1:
            src_link = SourceLink(parent)
            src = str(source[0])
            content = common.fix_moose_header(common.read(os.path.join(MooseDocs.ROOT_DIR, src)))
            code = core.Code(None, language=common.get_language(src), content=content)
            local = src.replace(MooseDocs.ROOT_DIR, '')
            link = floats.create_modal_link(src_link, content=code, title=local)
            if use_key_in_modal:
                tokens.String(link, content=os.path.basename(info[key]))
            return link

    return None
コード例 #2
0
ファイル: appsyntax.py プロジェクト: jbadger95/moose
    def createTokenFromSyntax(self, parent, info, page, obj):
        parameters = dict()
        if isinstance(obj, moosesyntax.SyntaxNode):
            for action in obj.actions():
                parameters.update(action.parameters)
        elif obj.parameters:
            parameters.update(obj.parameters)

        param_name = self.settings['_param']
        if param_name not in parameters:
            results = mooseutils.levenshteinDistance(param_name,
                                                     parameters.keys(), 5)
            msg = "Unable to locate the parameter '{}/{}', did you mean:\n"
            for res in results:
                msg += '    {}/{}\n'.format(obj_syntax, res)
            raise exceptions.MooseDocsException(msg, param_name, obj_syntax)

        param_token = ParameterToken(None,
                                     parameter=parameters[param_name],
                                     syntax=obj.name,
                                     include_heading=False)
        modal.ModalLink(parent,
                        string='"{}"'.format(param_name),
                        title=param_name,
                        content=param_token)
        return parent
コード例 #3
0
ファイル: Translator.py プロジェクト: loganharbour/moose
    def findPage(self,
                 arg,
                 throw_on_zero=True,
                 exact=False,
                 warn_on_zero=False):
        """
        Locate a single Page object that has a local name ending with the supplied name.

        Inputs:
            see findPages
        """
        nodes = self.findPages(arg, exact)
        if len(nodes) == 0:
            if throw_on_zero or warn_on_zero:
                msg = "Unable to locate a page that ends with the name '{}'.".format(
                    arg)
                num = self.get('number_of_suggestions', 0)
                if num:
                    if self.__markdown_file_list is None:
                        self.__buildMarkdownFileCache()

                    dist = self.__levenshtein_cache.get(arg, None)
                    if dist is None:
                        dist = mooseutils.levenshteinDistance(
                            arg, self.__markdown_file_list, number=num)
                        self.__levenshtein_cache[arg] = dist
                    msg += " Did you mean one of the following:\n"
                    for d in dist:
                        msg += "     {}\n".format(d)

                if warn_on_zero:
                    LOG.warning(msg)
                    return None
                else:
                    raise exceptions.MooseDocsException(msg)
            else:
                return None

        elif len(nodes) > 1:
            # If multiple Directory objects are found and all have the same local path, this should
            # be fine as they are functionally no different from each other. In any other situation,
            # however, we need to raise an exception here.
            if all([isinstance(node, pages.Directory) for node in nodes]) \
               and [node.local for node in nodes].count(nodes[0].local) == len(nodes):
                return nodes[0]
            msg = "Multiple pages with a name that ends with '{}' were found:".format(
                arg)
            for node in nodes:
                msg += '\n  {}'.format(node.local)
            raise exceptions.MooseDocsException(msg)

        return nodes[0]
コード例 #4
0
    def createMaterialize(self, parent, token, page):
        fullpath = token['src']
        text = '({})'.format(os.path.relpath(token['src'], MooseDocs.ROOT_DIR))
        string =  text if not token.children else None
        a = html.Tag(parent, 'span', string=string, class_='moose-source-filename tooltipped')

        # This should remain a Extension option, so it can be disable universally
        if self.extension['show_source']:

            # Create the <div> for the modal content
            uid = uuid.uuid4()
            modal_div = html.Tag(None, 'div', class_='moose-modal modal', id_=str(uid))
            modal_content = html.Tag(modal_div, 'div', class_="modal-content")
            self.extension.addModal(page.uid, modal_div)

            # Add the title and update the span to be the <a> trigger
            html.Tag(modal_content, 'h4', string=token['title'] or text)
            a.name = 'a'
            a['href'] = '#{}'.format(uid)
            a.addClass('modal-trigger')

            footer = html.Tag(modal_div, 'div', class_='modal-footer')
            html.Tag(footer, 'a', class_='modal-close btn-flat', string='Close')

            source = common.project_find(fullpath)
            if len(source) > 1:
                options = mooseutils.levenshteinDistance(fullpath, source, number=8)
                msg = "Multiple files match the supplied filename {}:\n".format(fullpath)
                for opt in options:
                    msg += "    {}\n".format(opt)
                msg = report_error(msg, page.source,
                                   token.info.line if token.info else None,
                                   token.info[0] if token.info else token.text())
                raise exceptions.MooseDocsException(msg)

            elif len(source) == 0:
                msg = "Unable to locate file that matches the supplied filename {}\n".format(fullpath)
                msg = report_error(msg, page.source,
                                   token.info.line if token.info else None,
                                   token.info[0] if token.info else token.text())
                raise exceptions.MooseDocsException(msg)

            content = common.fix_moose_header(common.read(source[0]))
            language = token['language'] or common.get_language(source[0])
            code = core.Code(None, language=language, content=content)
            self.renderer.render(modal_content, code, page)

        return a
コード例 #5
0
    def findPage(self,
                 arg,
                 throw_on_zero=True,
                 exact=False,
                 warn_on_zero=False):
        """
        Locate a single Page object that has a local name ending with the supplied name.

        Inputs:
            see findPages
        """
        nodes = self.findPages(arg, exact)
        if len(nodes) == 0:
            if throw_on_zero or warn_on_zero:
                msg = "Unable to locate a page that ends with the name '{}'.".format(
                    arg)
                num = self.get('number_of_suggestions', 0)
                if num:
                    if self.__markdown_file_list is None:
                        self.__buildMarkdownFileCache()

                    dist = self.__levenshtein_cache.get(arg, None)
                    if dist is None:
                        dist = mooseutils.levenshteinDistance(
                            arg, self.__markdown_file_list, number=num)
                        self.__levenshtein_cache[arg] = dist
                    msg += " Did you mean one of the following:\n"
                    for d in dist:
                        msg += "     {}\n".format(d)

                if warn_on_zero:
                    LOG.warning(msg)
                    return None
                else:
                    raise exceptions.MooseDocsException(msg)
            else:
                return None

        elif len(nodes) > 1:
            msg = "Multiple pages with a name that ends with '{}' were found:".format(
                arg)
            for node in nodes:
                msg += '\n  {}'.format(node.local)
            raise exceptions.MooseDocsException(msg)
        return nodes[0]
コード例 #6
0
    def createToken(self, parent, info, page):

        obj_syntax, param_name = info[MarkdownReader.INLINE].rsplit('/', 1)

        obj = self.extension.find(obj_syntax)
        parameters = dict()
        if isinstance(obj, moosesyntax.SyntaxNode):
            for action in obj.actions():
                parameters.update(action.parameters)
        elif obj.parameters:
            parameters.update(obj.parameters)

        if param_name not in parameters:
            results = mooseutils.levenshteinDistance(param_name, parameters.keys(), 5)
            msg = "Unable to locate the parameter '{}/{}', did you mean:\n"
            for res in results:
                msg += '    {}/{}\n'.format(obj_syntax, res)
            raise exceptions.MooseDocsException(msg, param_name, obj_syntax)

        ParameterToken(parent, parameter=parameters[param_name],
                       string='"{}"'.format(param_name))
        return parent