~ubuntu-branches/debian/jessie/sqlalchemy/jessie

« back to all changes in this revision

Viewing changes to doc/build/builder/sqlformatter.py

  • Committer: Package Import Robot
  • Author(s): Piotr Ożarowski, Jakub Wilk, Piotr Ożarowski
  • Date: 2013-07-06 20:53:52 UTC
  • mfrom: (1.4.23) (16.1.17 experimental)
  • Revision ID: package-import@ubuntu.com-20130706205352-ryppl1eto3illd79
Tags: 0.8.2-1
[ Jakub Wilk ]
* Use canonical URIs for Vcs-* fields.

[ Piotr Ożarowski ]
* New upstream release
* Upload to unstable
* Build depend on python3-all instead of -dev, extensions are not built for
  Python 3.X 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from pygments.lexer import RegexLexer, bygroups, using
 
2
from pygments.token import Token
 
3
from pygments.filter import Filter
 
4
from pygments.filter import apply_filters
 
5
from pygments.lexers import PythonLexer, PythonConsoleLexer
 
6
from sphinx.highlighting import PygmentsBridge
 
7
from pygments.formatters import HtmlFormatter, LatexFormatter
 
8
 
 
9
import re
 
10
 
 
11
 
 
12
def _strip_trailing_whitespace(iter_):
 
13
    buf = list(iter_)
 
14
    if buf:
 
15
        buf[-1] = (buf[-1][0], buf[-1][1].rstrip())
 
16
    for t, v in buf:
 
17
        yield t, v
 
18
 
 
19
 
 
20
class StripDocTestFilter(Filter):
 
21
    def filter(self, lexer, stream):
 
22
        for ttype, value in stream:
 
23
            if ttype is Token.Comment and re.match(r'#\s*doctest:', value):
 
24
                continue
 
25
            yield ttype, value
 
26
 
 
27
class PyConWithSQLLexer(RegexLexer):
 
28
    name = 'PyCon+SQL'
 
29
    aliases = ['pycon+sql']
 
30
 
 
31
    flags = re.IGNORECASE | re.DOTALL
 
32
 
 
33
    tokens = {
 
34
            'root': [
 
35
                (r'{sql}', Token.Sql.Link, 'sqlpopup'),
 
36
                (r'{opensql}', Token.Sql.Open, 'opensqlpopup'),
 
37
                (r'.*?\n', using(PythonConsoleLexer))
 
38
            ],
 
39
            'sqlpopup': [
 
40
                (
 
41
                    r'(.*?\n)((?:PRAGMA|BEGIN|SELECT|INSERT|DELETE|ROLLBACK|'
 
42
                        'COMMIT|ALTER|UPDATE|CREATE|DROP|PRAGMA'
 
43
                        '|DESCRIBE).*?(?:{stop}\n?|$))',
 
44
                    bygroups(using(PythonConsoleLexer), Token.Sql.Popup),
 
45
                    "#pop"
 
46
                )
 
47
            ],
 
48
            'opensqlpopup': [
 
49
                (
 
50
                    r'.*?(?:{stop}\n*|$)',
 
51
                    Token.Sql,
 
52
                    "#pop"
 
53
                )
 
54
            ]
 
55
        }
 
56
 
 
57
 
 
58
class PythonWithSQLLexer(RegexLexer):
 
59
    name = 'Python+SQL'
 
60
    aliases = ['pycon+sql']
 
61
 
 
62
    flags = re.IGNORECASE | re.DOTALL
 
63
 
 
64
    tokens = {
 
65
            'root': [
 
66
                (r'{sql}', Token.Sql.Link, 'sqlpopup'),
 
67
                (r'{opensql}', Token.Sql.Open, 'opensqlpopup'),
 
68
                (r'.*?\n', using(PythonLexer))
 
69
            ],
 
70
            'sqlpopup': [
 
71
                (
 
72
                    r'(.*?\n)((?:PRAGMA|BEGIN|SELECT|INSERT|DELETE|ROLLBACK'
 
73
                        '|COMMIT|ALTER|UPDATE|CREATE|DROP'
 
74
                        '|PRAGMA|DESCRIBE).*?(?:{stop}\n?|$))',
 
75
                    bygroups(using(PythonLexer), Token.Sql.Popup),
 
76
                    "#pop"
 
77
                )
 
78
            ],
 
79
            'opensqlpopup': [
 
80
                (
 
81
                    r'.*?(?:{stop}\n*|$)',
 
82
                    Token.Sql,
 
83
                    "#pop"
 
84
                )
 
85
            ]
 
86
        }
 
87
 
 
88
class PopupSQLFormatter(HtmlFormatter):
 
89
    def _format_lines(self, tokensource):
 
90
        buf = []
 
91
        for ttype, value in apply_filters(tokensource, [StripDocTestFilter()]):
 
92
            if ttype in Token.Sql:
 
93
                for t, v in HtmlFormatter._format_lines(self, iter(buf)):
 
94
                    yield t, v
 
95
                buf = []
 
96
 
 
97
                if ttype is Token.Sql:
 
98
                    yield 1, "<div class='show_sql'>%s</div>" % \
 
99
                                        re.sub(r'(?:[{stop}|\n]*)$', '', value)
 
100
                elif ttype is Token.Sql.Link:
 
101
                    yield 1, "<a href='#' class='sql_link'>sql</a>"
 
102
                elif ttype is Token.Sql.Popup:
 
103
                    yield 1, "<div class='popup_sql'>%s</div>" % \
 
104
                                        re.sub(r'(?:[{stop}|\n]*)$', '', value)
 
105
            else:
 
106
                buf.append((ttype, value))
 
107
 
 
108
        for t, v in _strip_trailing_whitespace(
 
109
                        HtmlFormatter._format_lines(self, iter(buf))):
 
110
            yield t, v
 
111
 
 
112
class PopupLatexFormatter(LatexFormatter):
 
113
    def _filter_tokens(self, tokensource):
 
114
        for ttype, value in apply_filters(tokensource, [StripDocTestFilter()]):
 
115
            if ttype in Token.Sql:
 
116
                if ttype is not Token.Sql.Link and ttype is not Token.Sql.Open:
 
117
                    yield Token.Literal, re.sub(r'{stop}', '', value)
 
118
                else:
 
119
                    continue
 
120
            else:
 
121
                yield ttype, value
 
122
 
 
123
    def format(self, tokensource, outfile):
 
124
        LatexFormatter.format(self, self._filter_tokens(tokensource), outfile)
 
125
 
 
126
def setup(app):
 
127
    app.add_lexer('pycon+sql', PyConWithSQLLexer())
 
128
    app.add_lexer('python+sql', PythonWithSQLLexer())
 
129
 
 
130
    PygmentsBridge.html_formatter = PopupSQLFormatter
 
131
    PygmentsBridge.latex_formatter = PopupLatexFormatter
 
132