summaryrefslogtreecommitdiff
path: root/_exts/tsref.py
blob: 9ff5893e9aeff534d9e79d0004e2e1ded0cc3989 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
"""
  This file is part of GNU TALER.
  Copyright (C) 2014, 2015 GNUnet e.V. and INRIA
  TALER is free software; you can redistribute it and/or modify it under the
  terms of the GNU Lesser General Public License as published by the Free Software
  Foundation; either version 2.1, or (at your option) any later version.
  TALER is distributed in the hope that it will be useful, but WITHOUT ANY
  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
  You should have received a copy of the GNU Lesser General Public License along with
  TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>

  @author Florian Dold
"""

"""
This extension adds a new lexer "tsref" for TypeScript, which
allows reST-style links inside comments (`LinkName`_),
and semi-automatically adds links to the definition of types.

For type TYPE, a reference to tsref-type-TYPE is added.

Known bugs and limitations:
 - The way the extension works right now interferes wiht
   Sphinx's caching, the build directory should be cleared
   before every build.
"""


from pygments.util import get_bool_opt
from pygments.token import Name, Comment, Token, _TokenType
from pygments.filter import Filter
from sphinx.highlighting import PygmentsBridge
from sphinx.builders.html import StandaloneHTMLBuilder
from sphinx.pygments_styles import SphinxStyle
from pygments.formatters import HtmlFormatter
from docutils import nodes
from docutils.nodes import make_id
import re
import sys


_escape_html_table = {
    ord('&'): u'&amp;',
    ord('<'): u'&lt;',
    ord('>'): u'&gt;',
    ord('"'): u'&quot;',
    ord("'"): u'&#39;',
}


class LinkingHtmlFormatter(HtmlFormatter):
    def __init__(self, **kwargs):
        super(LinkingHtmlFormatter, self).__init__(**kwargs)
        self._builder = kwargs['_builder']

    def _fmt(self, value, tok):
        cls = self._get_css_class(tok)
        href = tok_getprop(tok, "href")
        caption = tok_getprop(tok, "caption")
        content = caption if caption is not None else value
        if href:
            value = '<a style="color:inherit;text-decoration:underline" href="%s">%s</a>' % (href, content)
        if cls is None or cls == "":
            return value
        return '<span class="%s">%s</span>' % (cls, value)

    def _format_lines(self, tokensource):
        """
        Just format the tokens, without any wrapping tags.
        Yield individual lines.
        """
        lsep = self.lineseparator
        escape_table = _escape_html_table

        line = ''
        for ttype, value in tokensource:
            link = get_annotation(ttype, "link")

            parts = value.translate(escape_table).split('\n')

            if len(parts) == 0:
                # empty token, usually should not happen
                pass
            elif len(parts) == 1:
                # no newline before or after token
                line += self._fmt(parts[0], ttype)
            else:
                line += self._fmt(parts[0], ttype)
                yield 1, line + lsep
                for part in parts[1:-1]:
                    yield 1, self._fmt(part, ttype) + lsep
                line = self._fmt(parts[-1], ttype)

        if line:
            yield 1, line + lsep


class MyPygmentsBridge(PygmentsBridge):
    def __init__(self, builder, trim_doctest_flags):
        self.dest = "html"
        self.trim_doctest_flags = trim_doctest_flags
        self.formatter_args = {'style': SphinxStyle, '_builder': builder}
        self.formatter = LinkingHtmlFormatter


class MyHtmlBuilder(StandaloneHTMLBuilder):
    name = "html-linked"
    def init_highlighter(self):
        if self.config.pygments_style is not None:
            style = self.config.pygments_style
        elif self.theme:
            style = self.theme.get_confstr('theme', 'pygments_style', 'none')
        else:
            style = 'sphinx'
        self.highlighter = MyPygmentsBridge(self, self.config.trim_doctest_flags)

    def write_doc(self, docname, doctree):
        self._current_docname = docname
        super(MyHtmlBuilder, self).write_doc(docname, doctree)

    def warn(self, msg):
        print("warning:", msg, file=sys.stderr)


def get_annotation(tok, key):
    if not hasattr(tok, "kv"):
        return None
    return tok.kv.get(key)


def copy_token(tok):
    new_tok = _TokenType(tok)
    # This part is very fragile against API changes ...
    new_tok.subtypes = set(tok.subtypes)
    new_tok.parent = tok.parent
    return new_tok


def tok_setprop(tok, key, value):
    tokid = id(tok)
    e = token_props.get(tokid)
    if e is None:
        e = token_props[tokid] = (tok, {})
    _, kv = e
    kv[key] = value


def tok_getprop(tok, key):
    tokid = id(tok)
    e = token_props.get(tokid)
    if e is None:
        return None
    _, kv = e
    return kv.get(key)


link_reg = re.compile(r"`([^`<]+)\s*(?:<([^>]+)>)?\s*`_")

# Map from token id to props.
# Properties can't be added to tokens
# since they derive from Python's tuple.
token_props = {}


class LinkFilter(Filter):
    def __init__(self, app, **options):
        self.app = app
        Filter.__init__(self, **options)

    def filter(self, lexer, stream):
        id_to_doc = self.app.env.domaindata.get("_tsref", {})
        for ttype, value in stream:
            if ttype in Token.Keyword.Type:
                defname = make_id('tsref-type-' + value);
                t = copy_token(ttype)
                if defname in id_to_doc:
                    if hasattr(self.app.builder, "_current_docname"):
                        current_docname = self.app.builder._current_docname
                    else:
                        current_docname = "(unknown-doc)"
                    docname = id_to_doc[defname]
                    uri = self.app.builder.get_relative_uri(current_docname, docname)
                    href = uri + "#" + defname
                    tok_setprop(t, "href", href)

                yield t, value
            elif ttype in Token.Comment:
                last = 0
                for m in re.finditer(link_reg, value):
                    pre = value[last:m.start()]
                    if pre:
                        yield ttype, pre
                    t = copy_token(ttype)
                    x1, x2 = m.groups()
                    if x2 is None:
                        caption = x1.strip()
                        id = make_id(x1)
                    else:
                        caption = x1.strip()
                        id = make_id(x2)
                    if id in id_to_doc:
                        docname = id_to_doc[id]
                        href = self.app.builder.get_target_uri(docname) + "#" + id
                        tok_setprop(t, "href", href)
                        tok_setprop(t, "caption", caption)
                    else:
                        self.app.builder.warn("unresolved link target in comment: " + id)
                    yield t, m.group(1)
                    last = m.end()
                post = value[last:]
                if post:
                    yield ttype, post
            else:
                yield ttype, value



def remember_targets(app, doctree):
    docname = app.env.docname
    id_to_doc = app.env.domaindata.get("_tsref", None)
    if id_to_doc is None:
        id_to_doc = app.env.domaindata["_tsref"] = {}
    for node in doctree.traverse():
        if not isinstance(node, nodes.Element):
            continue
        ids = node.get("ids")
        if ids:
            for id in ids:
                id_to_doc[id] = docname


def setup(app): 
    from sphinx.highlighting import lexers
    from pygments.token import Name
    from pygments.filters import NameHighlightFilter
    from tslex import BetterTypeScriptLexer
    lexer = BetterTypeScriptLexer()
    lexer.add_filter(LinkFilter(app))
    app.add_lexer('tsref', lexer)
    app.add_builder(MyHtmlBuilder)
    app.connect("doctree-read", remember_targets)