File: fityk_ext.py

package info (click to toggle)
fityk 1.3.1-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 3,784 kB
  • sloc: cpp: 34,396; ansic: 4,673; python: 971; makefile: 366; sh: 117; java: 31; ruby: 27; perl: 25; xml: 16
file content (53 lines) | stat: -rw-r--r-- 1,653 bytes parent folder | download | duplicates (2)
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

import re

from pygments.lexer import RegexLexer
from pygments.token import Text, Name, Comment, String, Generic
from sphinx import addnodes
from docutils import nodes

class FitykLexer(RegexLexer):
    name = 'fityklexer'

    tokens = {
            'root': [
                (r"'[^']*'", String.Single),
                (r'[#].*?$', Comment),
                (r'^=-> ', Generic.Prompt),
                (r"[^'#\n]+", Text),
                ],
            }

comment_re = re.compile(r'(\(\*.*?\*\))')

def doctree_read(app, doctree):
    env = app.builder.env
    for node in doctree.traverse(addnodes.productionlist):
        for production in node:
            if not isinstance(production, addnodes.production):
                continue
            if not isinstance(production[-1], nodes.Text):
                continue
            parts = comment_re.split(production.pop().astext())
            new_nodes = []
            for s in parts:
                if comment_re.match(s):
                    new_nodes.append(nodes.emphasis(s, s))
                elif s:
                    new_nodes.append(nodes.Text(s))
            production += new_nodes

def role_ftype(name, rawtext, text, lineno, inliner, options={}, content=[]):
    node = nodes.strong(text, text)
    assert text.count('(') in (0, 1)
    assert text.count('(') == text.count(')')
    assert ')' not in text or text.endswith(')')
    m = re.search(r'\((.*?)\)', text)
    node['ids'] = [m.group(1) if m else text]
    return [node], []

def setup(app):
    app.add_lexer('fityk', FitykLexer());
    app.connect('doctree-read', doctree_read)
    app.add_role('ftype', role_ftype)