示例#1
0
    def _hook_template(self, parser, space, title):
        """Handles Template:Template name, formatting the content using given
        args"""
        params = title.split('|')
        short_title = params.pop(0)
        template_title = 'Template:' + short_title

        message = _('The template "%s" does not exist or has no approved '
                    'revision.') % short_title
        template = get_object_fallback(Document, template_title,
                                       locale=self.locale, is_template=True)

        if not template or not template.current_revision:
            return message

        if template.id in parser.inclusions:
            return RECURSION_MESSAGE % template_title
        else:
            parser.inclusions.append(template.id)
        c = template.current_revision.content.rstrip()
        # Note: this completely ignores the allowed attributes passed to the
        # WikiParser.parse() method and defaults to ALLOWED_ATTRIBUTES.
        parsed = parser.parse(c, show_toc=False, attributes=ALLOWED_ATTRIBUTES,
                              locale=self.locale)
        parser.inclusions.pop()

        # Special case for inline templates
        if '\n' not in c:
            parsed = parsed.replace('<p>', '')
            parsed = parsed.replace('</p>', '')
        # Do some string formatting to replace parameters
        return _format_template_content(parsed, _build_template_params(params))
示例#2
0
    def _hook_include(self, parser, space, title):
        """Returns the document's parsed content."""
        message = _('The document "%s" does not exist.') % title
        include = get_object_fallback(Document, title, locale=self.locale)
        if not include or not include.current_revision:
            return message

        if include.id in parser.inclusions:
            return RECURSION_MESSAGE % title
        else:
            parser.inclusions.append(include.id)
        ret = parser.parse(include.current_revision.content, show_toc=False,
                           locale=self.locale)
        parser.inclusions.pop()

        return ret
示例#3
0
文件: parser.py 项目: swznd/kuma
    def _hook_include(self, parser, space, title):
        """Returns the document's parsed content."""
        from wiki.models import Document
        message = _('The document "%s" does not exist.') % title
        t = get_object_fallback(Document, title, locale=self.locale)
        if not t or not t.current_revision:
            return message

        if t.id in parser.inclusions:
            return RECURSION_MESSAGE % title
        else:
            parser.inclusions.append(t.id)
        ret = parser.parse(t.current_revision.content,
                           show_toc=False,
                           locale=self.locale)
        parser.inclusions.pop()
        return ret
示例#4
0
文件: parser.py 项目: swznd/kuma
    def _hook_template(self, parser, space, title):
        """Handles Template:Template name, formatting the content using given
        args"""
        from wiki.models import Document
        from wiki.models import ALLOWED_ATTRIBUTES
        params = title.split('|')
        short_title = params.pop(0)
        template_title = 'Template:' + short_title

        message = _('The template "%s" does not exist or has no approved '
                    'revision.') % short_title
        t = get_object_fallback(Document,
                                template_title,
                                locale=self.locale,
                                is_template=True)

        if not t or not t.current_revision:
            return message

        if t.id in parser.inclusions:
            return RECURSION_MESSAGE % template_title
        else:
            parser.inclusions.append(t.id)
        c = t.current_revision.content.rstrip()
        # Note: this completely ignores the allowed attributes passed to the
        # WikiParser.parse() method and defaults to ALLOWED_ATTRIBUTES.
        parsed = parser.parse(c,
                              show_toc=False,
                              attributes=ALLOWED_ATTRIBUTES,
                              locale=self.locale)
        parser.inclusions.pop()

        # Special case for inline templates
        if '\n' not in c:
            parsed = parsed.replace('<p>', '')
            parsed = parsed.replace('</p>', '')
        # Do some string formatting to replace parameters
        return _format_template_content(parsed, _build_template_params(params))