Coverage for src/ensae_teaching_cs/homeblog/python_exemple_py_to_html.py: 47%
74 statements
« prev ^ index » next coverage.py v7.1.0, created at 2023-04-28 06:23 +0200
« prev ^ index » next coverage.py v7.1.0, created at 2023-04-28 06:23 +0200
1# -*- coding: utf-8 -*-
2"""
3@file
4@brief Helper for HTML
5"""
7import os
8import os.path
9from pyquickhelper.loghelper import fLOG
10from .clean_python_script_before_exporting_outside import cleanFileFromtohtmlreplace
11from .py2html import file2HTML, makeBlock, readStyleFile
12from .py2html import __version__ as py2html__version__
15py_page = """
16<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
17<html>
18<head>
19<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1 (Latin-1)" >
20<title>%s</title>
21<style type="text/css">
22 h1 { color: green;
23 position: center;
24 }
25 .python_code { font-family: monospace;
26 font-size: 10pt;
27 }
28 .py_key {color: black;}
29 .py_num color: black;{}
30 .py_str { color: #00AA00;}
31 .py_op {color: black; }
32 .py_com { color: red;}
33 .py_res { color: #FF7700;}
34 .py_def { color: blue;}
35 .py_brk { color: black;}
36</style>
37</head>
38<body>
39<h1>Programme %s</h1>
40<hr>
41%s
42<hr>
43created avec py2html version:%s
44<p>
45</p>
46__TRACKER__
47</body>
48</html>"""
50trackerMarker = "__TRACKER__"
51### tohtmlreplace BEGIN ###
52trackerFooter = ""
53### tohtmlreplace ELSE ###
54trackerFooter = """
55"""
56### tohtmlreplace END ###
59def get_first_col(file):
60 """
61 function related to my teachings, it tries to associate a file
62 to a chapter of my book
64 @param file file name (python script)
65 @return a chapter
66 """
67 f = open(file, "r") # , encoding="utf8")
68 sall = f.readlines()
69 f.close()
70 s = "".join(sall)
71 if "thread" in s:
72 return "Chapitre 10 : thread"
73 if "Tkinter" in s:
74 return "Chapitre 9 : interface"
75 if "struct" in s:
76 return "Chapitre 8 : fichiers"
77 if "glob" in s:
78 return "Chapitre 8 : fichiers"
79 if "import Tix" in s:
80 return "Chapitre 9 : interface"
81 if "selection" in file:
82 return "Chapitre 9 : interface"
83 if "filelist" in file:
84 return "Chapitre 9 : interface"
85 if "class" in file:
86 return "Chapitre 5 : classes"
87 if "exemple" in file:
88 return "Chapitre 7 : modules"
89 if "setup" in file:
90 return "Chapitre 7 : modules"
91 if "PythonSample" in file:
92 return "Chapitre 7 : modules"
93 if "init" in file:
94 return "Chapitre 7 : modules"
95 return "-"
98def py_to_html_folder(folder, addTracking=True):
99 """
100 Converts all :epkg:`python` files from a folder into html files.
102 @param folder folder
103 @param addTracking add some code for the tracking,
104 @see fn py_to_html_file
105 @return list of processed files
106 """
107 res = []
108 li = os.listdir(folder)
109 for f in li:
110 fullf = folder + "/" + f
111 ext = os.path.splitext(fullf)[1]
112 if ext == ".py":
113 r = py_to_html_file(fullf, addTracking=addTracking)
114 res.append(r)
115 return res
118def py_to_html_file(file, writehtml="", addTracking=True, title=None):
119 """
120 Converts a :epkg:`python` script into a html file.
122 @param folder folder
123 @param writehtml filename
124 @param addTracking add some code for tracking
125 @return the processed file (same file but with extension .html)
126 """
127 tracking_code = trackerFooter if addTracking else ""
129 fLOG(f"[py_to_html_file] converting pyfile '{file}' in html.")
130 f = file
131 racine = os.path.splitext(file)[0]
133 try:
134 with open(file, "r", encoding="utf-8") as tf:
135 content = tf.read()
136 encoding = "utf-8"
137 except UnicodeDecodeError: # pragma: no cover
138 try:
139 with open(file, "r", encoding="latin-1") as tf:
140 content = tf.read()
141 encoding = "utf-8"
142 except UnicodeDecodeError:
143 with open(file, "r", encoding="utf-8", errors="utf-8") as tf:
144 content = tf.read()
145 encoding = "utf-8"
147 content = cleanFileFromtohtmlreplace(content)
149 appliedstyle = readStyleFile(None)
150 try:
151 data = file2HTML(content, "0", appliedstyle,
152 False, "1", encoding=encoding)
153 block = makeBlock(data)
154 page = py_page.replace(trackerMarker, tracking_code)
155 html = page % (title or f, title or f, block, py2html__version__)
156 except Exception as e: # pragma: no cover
157 raise RuntimeError(
158 f"Not a python file, running it again '{file}'.") from e
160 if len(writehtml) > 0:
161 outfile = writehtml
162 else:
163 outfile = racine + ".html"
165 with open(outfile, "w", encoding=encoding) as f:
166 f.write(html)
167 fLOG(f"[py_to_html_file] encoding='{encoding}' wrote '{outfile}'.")
169 return outfile