-
Notifications
You must be signed in to change notification settings - Fork 1
/
Build.py
186 lines (143 loc) · 4.18 KB
/
Build.py
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
import quickjs
from jsmin import jsmin
from pathlib import Path
TS1 = """
sequenceDiagram
title: Sequence Diagram Example
autonumber
participant [<actor> User]
User->>Pintora: Draw me a sequence diagram(with DSL)
activate Pintora
Pintora->>Pintora: Parse DSL, draw diagram
alt DSL is correct
Pintora->>User: Return the drawn diagram
else DSL is incorrect
Pintora->>User: Return error message
end
deactivate Pintora
@note left of Pintora
Different output formats according to render targets
1. In browser side. output SVG or Canvas
2. In Node.js side. output PNG file
@end_note
"""
TS2 = """
mindmap
@param layoutDirection TB
@param {
l1NodeBgColor #2B7A5D
l1NodeTextColor #fff
l2NodeBgColor #26946C
l2NodeTextColor #fff
nodeBgColor #67B599
textColor #fff
}
+ UML Diagrams
++ Behavior Diagrams
+++ Sequence Diagram
+++ State Diagram
+++ Activity Diagram
++ Structural Diagrams
+++ Class Diagram
+++ Component Diagram
"""
TS3= """
componentDiagram
title: Component Diagram Example
package "@pintora/core" {
() GraphicsIR
() IRenderer
() IDiagram
[Diagram Registry] as registry
}
package "@pintora/diagrams" {
[...Multiple Diagrams...] as diagrams
[diagrams]
[diagrams] --> IDiagram : implements
}
package "@pintora/renderer" {
() "render()" as renderFn
[SVGRender]
[CanvasRender]
[SVGRender] --> IRenderer : implements
[CanvasRender] --> IRenderer : implements
IRenderer ..> GraphicsIR : accepts
}
package "@pintora/standalone" {
[standalone]
}
[IDiagram] --> GraphicsIR : generate
[standalone] --> registry : register all of @pintora/diagrams
[@pintora/standalone] --> [@pintora/diagrams] : import
[standalone] --> renderFn : call with GraphicsIR
"""
qj = quickjs.Context()
file = []
def qjeval(instr):
qj.eval(instr)
file.append(instr)
pintora = Path('runtime.esm.js')
encoding = Path('encoding.js')
encodingIdx = Path('encoding-indexes.js')
qjeval('''
class ConsoleStub {
constructor() {
this.logHistory = [];
this.errorHistory = [];
this.warnHistory = [];
}
log(...args) {
const message = args.join(' ');
this.logHistory.push(message);
}
error(...args) {
const message = args.join(' ');
this.errorHistory.push(message);
}
warn(...args) {
const message = args.join(' ');
this.warnHistory.push(message);
}
}
var console = new ConsoleStub();
''')
import re
qjeval(encodingIdx.read_text(encoding="UTF-8"))
qjeval(encoding.read_text(encoding="UTF-8"))
QJSFIXED = re.sub(r"export\s*\{.*\}","//EXPORTS arn't SUPPORTED",
pintora.read_text(encoding="UTF-8")
.replace("import.meta.url",'""'),
flags = re.MULTILINE|re.DOTALL)
# print("\n".join(QJSFIXED.split("\n")[63152:63158]))
qjeval(QJSFIXED)
qjeval("""
var document = new Document()
var csrc = document.createElement("div")
csrc.dataset=[];
var rslt = document.createElement("svg")
csrc.dataset['renderer']
function PintoraRender(InputString) {
console = new ConsoleStub()
csrc.innerText=InputString
pintoraStandalone.renderContentOf(csrc,{resultContainer:rslt})
if (rslt.innerHTML===""){
errorString = '\\n ' + String(console.warnHistory.slice(-1))
throw new Error(errorString);
}
rslt.firstChild.setAttribute("xmlns","http://www.w3.org/2000/svg")
return rslt.innerHTML;
}
""")
Path("package/pintora.js").write_text(jsmin("\n".join(file)),encoding="UTF-8")
Render=qj.eval("PintoraRender")
print(Render(TS2))
print(qj.eval(""))
# print(Render(TS3))
# print(qj.eval("pintoraStandalone.renderTo(randStr,{container:rslt,config:null})"))
# #look at logs and errors:
# print(qj.eval("console.logHistory.join()"))
# print(qj.eval("console.warnHistory.join()"))
# print(qj.eval("console.errorHistory.join()"))
# # get output
# print(qj.eval("rslt.innerHTML"))
# Path("output.svg").write_text(qj.eval("rslt.innerHTML"),encoding="UTF-8")