Hong-Phuc Bui
2024-10-16 f8613c9ce2bd4b74b11727d2eae204f49151bcba
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
# Program zum Erstellen ein neu JS-Program
import sys
import os.path
 
 
 
html_template = \
"""<html lang="en">
<head>
<meta charset="UTF-8">
<title>{program}</title>
<link rel="stylesheet" href="lib/dom-output.css" />
<link rel="stylesheet" href="lib/dfhi.css" />
<link rel="stylesheet" href="lib/dfhi-plot.css"/>
</head>
<body>
 
<div>
    <p>
        (Something about your program here)
    </p>
Instruction to user
<input type="text" id="argv" name="argv" />
<button name="run" id="run" >Plot Points</button>
</div>
 
<div class="row">
    <div id="plotter-container" class="column">
        <div class="jxgbox" id="plotter"></div>
        <div>
            <button id="export">Export to SVG</button>
        </div>
    </div>
    <div id="text-output-container" class="column">
        <pre id="text-output"></pre>
    </div>
</div>
 
 
<script type="module" src="{program}.js"></script>
 
</body>
</html>
"""
 
js_template = \
"""/*
* {program}.js
*
*/
 
import "./lib/dfhi.js";
import * as JXG from "jsxgraph" ;
import {{exportBoard}} from "./lib/dfhi-plot";
 
let board = undefined;
function initBoard () {{
    return JXG.JSXGraph.initBoard('plotter', {{
        boundingbox: [-2, 9, 3, -1],
        axis: true
    }});
}}
 
window.init = function() {{
    terminal.printl(`Program {program} initializes ... `);
    board = initBoard();
    document.getElementById("export")
            .addEventListener("click", (event) => exportBoard(board));
}};
 
 
window.main = function(...argv) {{
    terminal.printl(`program runs with arguments ${{argv}}`);
    if (board) {{
        JXG.JSXGraph.freeBoard(board);
        board = undefined;
    }}
    board = initBoard();
}};
"""
 
def write_program(file_name, template, program_name):
    code = template.format(program=program_name)
    print("Write " + code + " to file " + file_name)
    if os.path.exists(file_name):
        raise ValueError("File " + file_name + " exist")
    else:
        with open(file_name,"w") as f:
            f.write(code)
 
 
if __name__ == "__main__":
    programm_name = sys.argv[1]
    write_program("src/" + programm_name + ".html", html_template, programm_name)
    write_program("src/" + programm_name + ".js", js_template, programm_name)