Пример #1
0
    def get_tokens_unprocessed(self, text):
        style = self.options.get('litstyle')
        if style is None:
            style = (text.lstrip()[0:1] in '%\\') and 'latex' or 'bird'

        code = ''
        insertions = []
        if style == 'bird':
            # bird-style
            for match in line_re.finditer(text):
                line = match.group()
                m = self.bird_re.match(line)
                if m:
                    insertions.append(
                        (len(code), [(0, Comment.Special, m.group(1))]))
                    code += m.group(2)
                else:
                    insertions.append((len(code), [(0, Text, line)]))
        else:
            # latex-style
            from pygments.lexers.markup import TexLexer
            lxlexer = TexLexer(**self.options)
            codelines = 0
            latex = ''
            for match in line_re.finditer(text):
                line = match.group()
                if codelines:
                    if line.lstrip().startswith('\\end{code}'):
                        codelines = 0
                        latex += line
                    else:
                        code += line
                elif line.lstrip().startswith('\\begin{code}'):
                    codelines = 1
                    latex += line
                    insertions.append(
                        (len(code),
                         list(lxlexer.get_tokens_unprocessed(latex))))
                    latex = ''
                else:
                    latex += line
            insertions.append(
                (len(code), list(lxlexer.get_tokens_unprocessed(latex))))
        for item in do_insertions(insertions,
                                  self.baselexer.get_tokens_unprocessed(code)):
            yield item
Пример #2
0
    def get_tokens_unprocessed(self, text):
        style = self.options.get('litstyle')
        if style is None:
            style = (text.lstrip()[0:1] in '%\\') and 'latex' or 'bird'

        code = ''
        insertions = []
        if style == 'bird':
            # bird-style
            for match in line_re.finditer(text):
                line = match.group()
                m = self.bird_re.match(line)
                if m:
                    insertions.append((len(code),
                                       [(0, Comment.Special, m.group(1))]))
                    code += m.group(2)
                else:
                    insertions.append((len(code), [(0, Text, line)]))
        else:
            # latex-style
            from pygments.lexers.markup import TexLexer
            lxlexer = TexLexer(**self.options)
            codelines = 0
            latex = ''
            for match in line_re.finditer(text):
                line = match.group()
                if codelines:
                    if line.lstrip().startswith('\\end{code}'):
                        codelines = 0
                        latex += line
                    else:
                        code += line
                elif line.lstrip().startswith('\\begin{code}'):
                    codelines = 1
                    latex += line
                    insertions.append((len(code),
                                       list(lxlexer.get_tokens_unprocessed(latex))))
                    latex = ''
                else:
                    latex += line
            insertions.append((len(code),
                               list(lxlexer.get_tokens_unprocessed(latex))))
        for item in do_insertions(insertions, self.baselexer.get_tokens_unprocessed(code)):
            yield item
Пример #3
0
    def retrieve(self,
                 request: Request,
                 key: str,
                 format: Optional[str] = None) -> Response:
        """
        Retrieve a node.

        Performs content negotiation and authorization based on the `Accept` header.

        - For `application/json` returns 403 if the request user is not authorized
        to read the project.

        - For `text/html` (and others) returns a HTML rendering of the
        of the node without authorization checking; this is to allow scrapers such
        as Google Docs link preview generator to be able to fetch basic information
        on the node. For private projects node URLs should be kept private to avoid
        unauthorized access.
        """
        node = get_object_or_404(Node, key=key)
        if format == "json" or request.accepted_renderer.format == "json":
            # Require the user is authenticated and has VIEW permissions for the project
            if not request.user.is_authenticated:
                raise NotAuthenticated
            if node.project:
                try:
                    get_projects(request.user).get(id=node.project.id)
                except Project.DoesNotExist:
                    raise PermissionDenied

            serializer = NodeSerializer(node, context={"request": request})
            return Response(serializer.data)
        else:
            # Return a HTML representation of the node
            node_type = schema_node_type(node.json)
            if node_type in ("CodeChunk", "CodeExpression"):
                lang = node.json.get("programmingLanguage", "")
                try:
                    lexer = pygments.lexers.get_lexer_by_name(lang)
                except pygments.util.ClassNotFound:
                    lexer = TextLexer()
                code = node.json.get("text", "")
            elif node_type in ("MathBlock", "MathFragment"):
                lexer = TexLexer()
                code = node.json.get("text")
            else:
                lexer = JsonLexer()
                code = json.dumps(node.json, indent=2)

            formatter = HtmlFormatter(cssclass="source", style="colorful")
            css = formatter.get_style_defs(".source")
            html = pygments.highlight(code, lexer, formatter)

            app_name, app_url = node.get_app()

            return Response(
                dict(
                    meta=node.get_meta(),
                    node=node,
                    css=css,
                    html=html,
                    app_name=app_name,
                    app_url=app_url,
                ),
                template_name="projects/nodes/retrieve.html",
            )