File: engine.py

package info (click to toggle)
elpi 2.0.7-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 50,296 kB
  • sloc: ml: 18,791; makefile: 229; python: 95; sh: 7
file content (113 lines) | stat: -rw-r--r-- 3,529 bytes parent folder | download | duplicates (5)
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
#!/usr/bin/env python3

import os, sys, in_place, pathlib, subprocess, re

def check(input, expression):
    pattern = re.compile(expression, re.IGNORECASE)
    return pattern.match(input)

def exec(path, base_path):
    file = base_path / path
    print('  - Executing elpi on', file.as_posix().rstrip())
    elpi = subprocess.Popen(['dune', 'exec', 'elpi', '--', '-test', file.as_posix()[:-1]], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
    output, errors = elpi.communicate()
    elpi.wait()
    return output, errors

def process(source, base_path):
    atext = '   :assert:'
    stext = '.. elpi::'
    rtext = '.. literalinclude::'

    file_ = open(source, 'r')
    lines = file_.readlines()

    with in_place.InPlace(source) as file:

        index = 0
        
        for line in file:

            path = ''

            output = ''
            errors = ''
            matchr = ''

            if line.startswith(atext):
                index += 1
                #file.write('')
                continue

            if line.startswith(stext):
                path = line[10:]

                output, errors = exec(path, base_path)
                
                if index < len(lines)-1:
                    next = lines[index+1]
                    
                    if next.startswith(atext):
                        
                        expression = next[12:].rstrip()
                        
                        if check(output, expression) is None:
                            output = ''
                            errors = ''
                            matchr = 'Injection failure: result did not pass regexp check (' + expression + ')'

            if line.startswith(stext):
                block = '**' + path + ':' + '**' + '\n' + '\n'
                block += line.replace(stext, rtext)
                block += '   :linenos:' + '\n'
                block += '   :language: elpi' + '\n'
                file.write(block)
            else:
                file.write(line)

            if len(output) > 0:
                block  = '\n'
                block += '.. code-block:: console' + '\n'
                block += '\n   '
                block += output.replace('\n', '\n   ')
                block += '\n'
                file.write(block)
                
            if len(errors) > 0:
                block  = '\n'
                block += '.. code-block:: console' + '\n'
                block += '\n   '
                block += errors.replace('\n', '\n   ')
                block += '\n'
                file.write(block)

            if len(matchr) > 0:
                block  = '\n'
                block += '.. raw:: html' + '\n'
                block += '\n   '
                block += '<div class="highlight-console notranslate"><div class="highlight" style="background-color: rgb(248, 148, 148);"><pre>'
                block += matchr
                block += '</pre></div></div>'
                block += '\n'
                file.write(block)
            
            index += 1

def find(path):
    files = sorted(path.glob('**/**/*.rst'))
    return files;


print('Elpi documentation - Engine started.')

base_path = os.path.basename(sys.argv[0])
base_path = pathlib.Path(base_path).parent.parent.resolve()
path = base_path / 'docs' / 'source'

print('- Base path.\n', path.as_posix())

for file in find(path):
    print('- Processing file:', file.as_posix())
    process(file.as_posix(), path)

print('Elpi documentation - Engine finished.\n')