Hong-Phuc Bui
2025-01-16 2889de7f0c2d587a17fbd322af57c29e84238620
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
# Program zum Erstellen ein neu JS-Program
import sys
import os.path
 
"""
create a HTML and a JS file with the same name.
 
Require: Python3
 
Usage:
 
    new-program.py <program-name>
"""
 
 
program_name = sys.argv[1]
 
html_template = \
"""<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>{program}</title>    
    <link rel="stylesheet" href="dom-output.css" />
    <link rel="stylesheet" href="dfhi.css" />
</head>
<body>
<p class="description">(Passen Sie die Beschreibung des Programs hier an.)</p>
<label for="argv"><code>main</code>'s arguments:</label> <input type="text" id="argv" name="argv" />
<button name="run" id="run" >Rerun</button>
 
 
<pre id="text-output"></pre>
<script type="module" src="{program}.js"></script>
 
</body>
</html>
"""
 
js_template = \
"""/**
 * {program}.js
 *
 */
 
import {{terminal}} from "./dfhi.js";
 
window.main = function(...argv) {{
    terminal.print("{program}");
}};
"""
 
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 + " already exists")
    else:
        with open(file_name,"w") as f:
            f.write(code)
 
 
write_program(program_name + ".html", html_template, program_name)
write_program(program_name + ".js", js_template, program_name)