Esempio n. 1
0
    def createToken(self, parent, info, page):
        """
        Build the tokens needed for displaying code listing.
        """

        filename = common.check_filenames(info['subcommand'])
        flt = floats.create_float(parent,
                                  self.extension,
                                  self.reader,
                                  page,
                                  self.settings,
                                  token_type=Listing)
        # Create code token
        lang = self.settings.get('language')
        content = self.extractContent(filename)
        lang = lang if lang else common.get_language(filename)

        code = core.Code(flt,
                         style="max-height:{};".format(
                             self.settings['max-height']),
                         content=content,
                         language=lang)
        if flt is parent:
            code.attributes.update(**self.attributes)

        if flt is not parent:
            code.name = 'ListingCode'

        # Add bottom modal
        link = self.settings['link']
        link = link if link is not None else self.extension['modal-link']
        if link:
            rel_filename = os.path.relpath(filename, MooseDocs.ROOT_DIR)

            # Get the complete file
            content = common.read(filename)
            settings = common.get_settings_as_dict(
                common.extractContentSettings())
            settings['strip-header'] = False
            content, _ = common.extractContent(content, settings)

            # Create modal for display the files a popup
            code = core.Code(None, language=lang, content=content)
            link = floats.create_modal_link(
                flt,
                url=unicode(rel_filename),
                content=code,
                title=unicode(filename),
                string=u'({})'.format(rel_filename))
            link.name = 'ListingLink'
            link['data-tooltip'] = unicode(rel_filename)

        return parent
Esempio n. 2
0
    def _createFieldError(self, parent, token, page, modal_flag):
        """Helper for creating error alert."""

        filename = page.local
        key = token['key']
        err = alert.AlertToken(None, brand=u'error')
        alert_title = alert.AlertTitle(err,
                                       brand=u'error',
                                       string=u'Missing Template Item: "{}"'.format(key))
        alert_content = alert.AlertContent(err, brand=u'error')
        token.copyToToken(alert_content)

        if modal_flag:
            modal_content = tokens.Token(None)
            core.Paragraph(modal_content,
                           string=u"The document must include the \"{0}\" template item, this can "\
                           u"be included by add adding the following to the markdown " \
                           u"file ({1}):".format(key, filename))

            core.Code(modal_content,
                      content=u"!template! item key={0}\nInclude text (in MooseDocs format) " \
                      u"regarding the \"{0}\" template item here.\n" \
                      u"!template-end!".format(key))

            link = floats.create_modal_link(alert_title,
                                            title=u'Missing Template Item "{}"'.format(key),
                                            content=modal_content)
            materialicon.Icon(link, icon=u'help_outline',
                              class_='small',
                              style='float:right;color:white;margin-bottom:5px;')

        self.renderer.render(parent, err, page)
Esempio n. 3
0
    def _addRequirement(self, parent, info, page, req):
        reqname = "{}:{}".format(req.path, req.name) if req.path != '.' else req.name
        item = SQARequirementMatrixItem(parent, label=req.label, reqname=reqname)
        self.reader.tokenize(item, req.text, page, MooseDocs.INLINE, info.line, report=False)
        for token in anytree.PreOrderIter(item):
            if token.name == 'ErrorToken':
                msg = common.report_error("Failed to tokenize SQA requirement.",
                                          req.filename,
                                          req.text_line,
                                          req.text,
                                          token['traceback'],
                                          'SQA TOKENIZE ERROR')
                LOG.critical(msg)

        p = core.Paragraph(item)
        tokens.String(p, content='Specification: ')

        content = common.read(req.filename)
        floats.create_modal_link(p,
                                 string=reqname,
                                 content=core.Code(None, language='text', content=content),
                                 title=str(req.filename))

        p = core.Paragraph(item)
        tokens.String(p, content='Documentation: ')
        filename = getattr(req, info['subcommand'])
        autolink.AutoLink(p, page=str(filename))
Esempio n. 4
0
 def createToken(self, parent, info, page):
     content = info['inline'] if 'inline' in info else info['block']
     content = re.sub(r'__(?P<package>[A-Z][A-Z_]+)__', self.subFunction, content,
                      flags=re.UNICODE)
     core.Code(parent, style="max-height:{};".format(self.settings['max-height']),
               language=self.settings['language'], content=content)
     return parent
Esempio n. 5
0
    def _addRequirement(self, parent, info, page, req):
        item = SQARequirementMatrixItem(parent, requirement=req)
        self.reader.tokenize(item,
                             req.text,
                             page,
                             MooseDocs.INLINE,
                             info.line,
                             report=False)
        for token in anytree.PreOrderIter(item):
            if token.name == 'ErrorToken':
                msg = common.report_error(
                    "Failed to tokenize SQA requirement.", req.filename,
                    req.text_line, req.text, token['traceback'],
                    u'SQA TOKENIZE ERROR')
                LOG.critical(msg)

        p = core.Paragraph(item)
        tokens.String(p, content=u'Specification: ')

        with codecs.open(req.filename, encoding='utf-8') as fid:
            content = fid.read()
            floats.create_modal_link(p,
                                     string=u"{}:{}".format(
                                         req.path, req.name),
                                     content=core.Code(None,
                                                       language=u'text',
                                                       content=content),
                                     title=unicode(req.filename))

        p = core.Paragraph(item)
        tokens.String(p, content=u'Details: ')
        filename = u'{}/{}.md'.format(req.path, req.name)
        autolink.AutoLink(p, page=unicode(filename))
Esempio n. 6
0
def createTokenHelper(key, parent, info, page, use_key_in_modal=False):
    match = PAGE_LINK_RE.search(info[key])
    bookmark = match.group('bookmark')[1:] if match.group('bookmark') else u''
    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 != u''):
        return LocalLink(parent, bookmark=bookmark)

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

    else:
        source = common.project_find(info[key])
        if len(source) == 1:
            src = unicode(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(parent, content=code, title=local)
            if use_key_in_modal:
                tokens.String(link, content=os.path.basename(info[key]))
            return link

    return None
Esempio n. 7
0
 def createToken(self, parent, info, page):
     flt = floats.create_float(parent, self.extension, self.reader, page,
                               self.settings)
     content = info['inline'] if 'inline' in info else info['block']
     core.Code(flt,
               style="max-height:{};".format(self.settings['max-height']),
               language=self.settings['language'],
               content=content)
     return parent
Esempio n. 8
0
    def _addRequirement(self, parent, info, page, req, requirements):
        reqname = "{}:{}".format(req.path, req.name) if req.path != '.' else req.name
        item = SQARequirementMatrixItem(parent, label=req.label, reqname=reqname,
                                        satisfied=req.satisfied)
        text = SQARequirementText(item)

        self.reader.tokenize(text, req.text, page, MooseDocs.INLINE, info.line, report=False)
        for token in anytree.PreOrderIter(item):
            if token.name == 'ErrorToken':
                msg = common.report_error("Failed to tokenize SQA requirement.",
                                          req.filename,
                                          req.text_line,
                                          req.text,
                                          token['traceback'],
                                          'SQA TOKENIZE ERROR')
                LOG.critical(msg)

        if req.details:
            details = SQARequirementDetails(item)
            for detail in req.details:
                ditem = SQARequirementDetailItem(details)
                text = SQARequirementText(ditem)
                self.reader.tokenize(text, detail.text, page, MooseDocs.INLINE, info.line, \
                                     report=False)

        if self.settings['link']:
            if self.settings['link-spec']:
                p = SQARequirementSpecification(item, spec_path=req.path, spec_name=req.name)

                hit_root = mooseutils.hit_load(req.filename)
                h = hit_root.find(req.name)
                content = h.render()

                floats.create_modal_link(p,
                                         title=reqname, string=reqname,
                                         content=core.Code(None, language='text', content=content))

            if self.settings['link-design'] and req.design:
                p = SQARequirementDesign(item, filename=req.filename, design=req.design,
                                         line=req.design_line)

            if self.settings['link-issues'] and req.issues:
                p = SQARequirementIssues(item, filename=req.filename, issues=req.issues,
                                         line=req.issues_line)

            if self.settings.get('link-prerequisites', False) and req.prerequisites:
                labels = []
                for prereq in req.prerequisites:
                    for other in requirements:
                        if (other.name == prereq) and (other.path == req.path):
                            labels.append((other.path, other.name, other.label))
                        for detail in other.details:
                            if (detail.name == prereq) and (detail.path == req.path):
                                labels.append((other.path, other.name, other.label))

                SQARequirementPrequisites(item, specs=labels)
Esempio n. 9
0
    def _addRequirement(self, parent, info, page, req, requirements):
        item = SQARequirementMatrixItem(parent,
                                        label=unicode(req.label),
                                        satisfied=req.satisfied,
                                        id_=req.path)
        self.reader.tokenize(item, req.text, page)

        if self.settings['link']:
            if self.settings['link-spec']:
                p = core.Paragraph(item)
                tokens.String(p, content=u'Specification: ')

                with codecs.open(req.filename, encoding='utf-8') as fid:
                    content = fid.read()

                floats.create_modal_link(
                    p,
                    url=u"#",
                    title=tokens.String(None, content=unicode(req.filename)),
                    content=core.Code(None, language=u'text', code=content),
                    string=u"{}:{}".format(req.path, req.name),
                    tooltip=False)

            if self.settings['link-design'] and req.design:
                p = core.Paragraph(item)
                tokens.String(p, content=u'Design: ')
                for design in req.design:
                    autolink.AutoLink(p, page=unicode(design))

            if self.settings['link-issues'] and req.issues:
                p = core.Paragraph(item)
                tokens.String(p, content=u'Issues: ')
                for issue in req.issues:
                    if issue.startswith('#'):
                        url = u"https://github.com/idaholab/moose/issues/{}".format(
                            issue[1:])
                    else:
                        url = u"https://github.com/idaholab/moose/commit/{}".format(
                            issue[1:])
                    core.Link(p, url=url, string=unicode(issue))
                    core.Space(p)

            if self.settings['link-prerequisites'] and req.prerequisites:
                labels = []
                for prereq in req.prerequisites:
                    for other in requirements:
                        if other.name == prereq:
                            labels.append(other.label)

                p = core.Paragraph(item)
                tokens.String(p,
                              content=u'Prerequisites: {}'.format(
                                  ' '.join(labels)))
Esempio n. 10
0
    def _addRequirement(self, parent, info, page, req, requirements):

        item = SQARequirementMatrixItem(parent, requirement=req)
        text = SQARequirementText(item)

        self.reader.tokenize(text,
                             req.text,
                             page,
                             MooseDocs.INLINE,
                             info.line,
                             report=False)
        for token in anytree.PreOrderIter(item):
            if token.name == 'ErrorToken':
                msg = common.report_error(
                    "Failed to tokenize SQA requirement.", req.filename,
                    req.text_line, req.text, token['traceback'],
                    u'SQA TOKENIZE ERROR')
                LOG.critical(msg)

        if self.settings['link']:
            if self.settings['link-spec']:
                p = SQARequirementSpecification(item,
                                                spec_path=req.path,
                                                spec_name=req.name)

                with codecs.open(req.filename, encoding='utf-8') as fid:
                    content = fid.read()
                floats.create_modal_link(p,
                                         title=req.filename,
                                         content=core.Code(None,
                                                           language=u'text',
                                                           content=content),
                                         string=u"{}:{}".format(
                                             req.path, req.name))

            if self.settings['link-design'] and req.design:
                p = SQARequirementDesign(item, design=req.design)

            if self.settings['link-issues'] and req.issues:
                p = SQARequirementIssues(item, issues=req.issues)

            if self.settings['link-prerequisites'] and req.prerequisites:
                labels = []
                for prereq in req.prerequisites:
                    for other in requirements:
                        if (other.name == prereq) and (other.path == req.path):
                            labels.append(
                                (other.path, other.name, other.label))

                SQARequirementPrequisites(item, specs=labels)
Esempio n. 11
0
    def createToken(self, parent, info, page):
        master = floats.create_float(parent,
                                     self.extension,
                                     self.reader,
                                     page,
                                     self.settings,
                                     token_type=ExampleFloat)
        data = info['block'] if 'block' in info else info['inline']
        code = core.Code(master, content=data)

        if master is parent:
            code.attributes.update(**self.attributes)

        return master
Esempio n. 12
0
    def createMaterialize(self, parent, token, page):

        key = token.key
        func = lambda n: isinstance(n, SQADocumentItem) and (n.key == key)
        replacement = anytree.search.find(token.root, filter_=func, maxlevel=2)

        if replacement:

            self.renderer.render(parent, replacement, page)

            # Remove item so it doesn't render again
            replacement.parent = None
            for child in replacement:
                child.parent = None


#
        else:
            filename = page.local
            err = alert.AlertToken(None, brand=u'error')
            alert_title = alert.AlertTitle(
                err,
                brand=u'error',
                string=u'Missing Template Item "{}"'.format(key))
            alert_content = alert.AlertContent(err, brand=u'error')

            modal_content = tokens.Token(None)
            core.Paragraph(modal_content,
                           string=u"The document must include the \"{0}\" template item, this can "\
                           u"be included by add adding the following to the markdown " \
                           u"file ({1}):".format(key, filename))

            core.Code(modal_content,
                      code=u"!sqa! item key={0}\nInclude text (in MooseDocs format) " \
                      u"regarding the \"{0}\" template item here.\n" \
                      u"!sqa-end!".format(key))

            link = floats.create_modal_link(
                alert_title,
                title=u'Missing Template Item "{}"'.format(key),
                content=modal_content)
            materialicon.IconToken(link,
                                   icon=u'help_outline',
                                   class_=u'material-icons moose-help')

            for child in token.children:
                child.parent = alert_content

            self.renderer.render(parent, err, page)
Esempio n. 13
0
    def testMaterializeErrors(self):

        ast = modal.ModalLink(None, content=core.Code(None, content='1+2=3'))
        with self.assertLogs(level=logging.ERROR) as cm:
            res = self.render(ast, renderer=base.MaterializeRenderer())
        self.assertEqual(len(cm.output), 1)
        self.assertIn("The \'ModalLink\' token requires children", cm.output[0])

        ast = modal.ModalLink(None, string='test', content=1980)
        with self.assertLogs(level=logging.ERROR) as cm:
            res = self.render(ast, renderer=base.MaterializeRenderer())
        self.assertEqual(len(cm.output), 1)
        self.assertIn("The \'ModalLink\' token 'content' attribute must be a string or Token", cm.output[0])

        ast = modal.ModalLink(None, string='test')
        with self.assertLogs(level=logging.ERROR) as cm:
            res = self.render(ast, renderer=base.MaterializeRenderer())
        self.assertEqual(len(cm.output), 1)
        self.assertIn("The \'ModalLink\' token 'content' attribute must be a string or Token", cm.output[0])
Esempio n. 14
0
    def createTokenFromSyntax(self, parent, info, page, obj):

        item = self.extension.database.get(obj.name, None)
        attr = getattr(item, self.SUBCOMMAND, None)
        if item and attr:
            self.createHeading(parent, page)
            ul = core.UnorderedList(parent, class_='moose-list-{}'.format(self.SUBCOMMAND))
            for filename in attr:
                filename = unicode(filename)
                li = core.ListItem(ul)
                lang = common.get_language(filename)
                content = common.fix_moose_header(common.read(os.path.join(MooseDocs.ROOT_DIR,
                                                                           filename)))
                code = core.Code(None, language=lang, code=content)
                floats.create_modal_link(li,
                                         url=filename,
                                         content=code,
                                         title=filename,
                                         string=filename)
        return parent
Esempio n. 15
0
    def createToken(self, parent, info, page):
        flt = floats.create_float(parent,
                                  self.extension,
                                  self.reader,
                                  page,
                                  self.settings,
                                  token_type=Listing)
        content = info['inline'] if 'inline' in info else info['block']
        code = core.Code(flt,
                         style="max-height:{};".format(
                             self.settings['max-height']),
                         language=self.settings['language'],
                         content=content)

        if flt is parent:
            code.attributes.update(**self.attributes)

        if flt is not parent:
            code.name = 'ListingCode'  #TODO: Find a better way

        return parent
Esempio n. 16
0
    def _addRequirement(self, parent, info, page, req):
        item = SQARequirementMatrixItem(parent,
                                        label=unicode(req.label),
                                        satisfied=req.satisfied,
                                        id_=req.path)
        self.reader.tokenize(item, req.text, page, line=info.line)

        p = core.Paragraph(item)
        tokens.String(p, content=u'Specification: ')

        with codecs.open(req.filename, encoding='utf-8') as fid:
            content = fid.read()
            floats.create_modal_link(
                p,
                tooltip=False,
                url=u"#",
                string=u"{}:{}".format(req.path, req.name),
                title=tokens.String(None, content=unicode(req.filename)),
                content=core.Code(None, language=u'text', code=content))

        p = core.Paragraph(item)
        tokens.String(p, content=u'Details: ')
        filename = u'{}/{}.md'.format(req.path, req.name)
        autolink.AutoLink(p, page=unicode(filename))
Esempio n. 17
0
    def testMaterialize(self):

        # Content as Token
        ast = modal.ModalLink(None, string='test', content=core.Code(None, content='1+2=3'))
        res = self.render(ast, renderer=base.MaterializeRenderer())
        self.assertSize(res, 2)

        uid = res(1)['id']
        self.assertHTMLTag(res(0), 'a', size=1, href='#{}'.format(uid), string='test')
        self.assertHTMLTag(res(1), 'div', size=1, class_='moose-modal modal', id_=uid)
        self.assertHTMLTag(res(1,0), 'div', size=1, class_='modal-content')
        self.assertHTMLTag(res(1,0,0), 'pre', size=1, class_='moose-pre')
        self.assertHTMLTag(res(1,0,0,0), 'code', size=1, class_='language-text', string='1+2=3')

        # Content as String
        ast = modal.ModalLink(None, string='test', content='1+2=3')
        res = self.render(ast, renderer=base.MaterializeRenderer())
        self.assertSize(res, 2)

        uid = res(1)['id']
        self.assertHTMLTag(res(0), 'a', size=1, href='#{}'.format(uid), string='test')
        self.assertHTMLTag(res(1), 'div', size=1, class_='moose-modal modal', id_=uid)
        self.assertHTMLTag(res(1,0), 'div', size=1, class_='modal-content')
        self.assertHTMLTag(res(1,0,0), 'p', size=1, string='1+2=3')

        # title
        ast = modal.ModalLink(None, string='test', content='1+2=3', title='math')
        res = self.render(ast, renderer=base.MaterializeRenderer())
        self.assertSize(res, 2)

        uid = res(1)['id']
        self.assertHTMLTag(res(0), 'a', size=1, href='#{}'.format(uid), string='test')
        self.assertHTMLTag(res(1), 'div', size=1, class_='moose-modal modal', id_=uid)
        self.assertHTMLTag(res(1,0), 'div', size=2, class_='modal-content')
        self.assertHTMLTag(res(1,0,0), 'h4', size=1, string='math')
        self.assertHTMLTag(res(1,0,1), 'p', size=1, string='1+2=3')
Esempio n. 18
0
 def createToken(self, parent, info, page):
     master = floats.create_float(parent, self.extension, self.reader, page,
                                  self.settings, **self.attributes)
     data = info['block'] if 'block' in info else info['inline']
     core.Code(master, content=data)
     return master
Esempio n. 19
0
    def _addRequirement(self, parent, info, page, req, requirements):
        item = SQARequirementMatrixItem(parent,
                                        label=unicode(req.label),
                                        satisfied=req.satisfied,
                                        id_=req.path)

        self.reader.tokenize(item,
                             req.text,
                             page,
                             MooseDocs.INLINE,
                             info.line,
                             report=False)
        for token in anytree.PreOrderIter(item):
            if token.name == 'ErrorToken':
                msg = common.report_error(
                    "Failed to tokenize SQA requirement.", req.filename,
                    req.text_line, req.text, token['traceback'],
                    u'SQA TOKENIZE ERROR')
                LOG.critical(msg)

        if self.settings['link']:
            if self.settings['link-spec']:
                p = core.Paragraph(item)
                tokens.String(p, content=u'Specification: ')

                with codecs.open(req.filename, encoding='utf-8') as fid:
                    content = fid.read()

                floats.create_modal_link(p,
                                         title=req.filename,
                                         content=core.Code(None,
                                                           language=u'text',
                                                           content=content),
                                         string=u"{}:{}".format(
                                             req.path, req.name))

            if self.settings['link-design'] and req.design:
                p = core.Paragraph(item)
                tokens.String(p, content=u'Design: ')
                for design in req.design:
                    autolink.AutoLink(p, page=unicode(design))

            if self.settings['link-issues'] and req.issues:
                p = core.Paragraph(item)
                tokens.String(p, content=u'Issue(s): ')
                for issue in req.issues:
                    if issue.startswith('#'):
                        url = u"https://github.com/idaholab/moose/issues/{}".format(
                            issue[1:])
                    else:
                        url = u"https://github.com/idaholab/moose/commit/{}".format(
                            issue[1:])
                    core.Link(p, url=url, string=unicode(issue))
                    core.Space(p)

            if self.settings['link-prerequisites'] and req.prerequisites:
                labels = []
                for prereq in req.prerequisites:
                    for other in requirements:
                        if other.name == prereq:
                            labels.append(other.label)

                p = core.Paragraph(item)
                tokens.String(p,
                              content=u'Prerequisites: {}'.format(
                                  ' '.join(labels)))