Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1# -*- coding: utf-8 -*- 

2""" 

3@file 

4@brief Helper for HTML 

5""" 

6 

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__ 

13 

14 

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__GOOGLETRACKER__ 

47</body> 

48</html>""" 

49 

50googleTrackerMarker = "__GOOGLETRACKER__" 

51### tohtmlreplace BEGIN ### 

52googleTrackerFooter = "" 

53### tohtmlreplace ELSE ### 

54googleTrackerFooter = """ 

55<script src="http://www.google-analytics.com/urchin.js" type="text/javascript"></script> 

56<script type="text/javascript">_uacct = "UA-2815364-1";urchinTracker();</script> 

57""" 

58### tohtmlreplace END ### 

59 

60 

61def get_first_col(file): 

62 """ 

63 function related to my teachings, it tries to associate a file 

64 to a chapter of my book 

65 

66 @param file file name (python script) 

67 @return a chapter 

68 """ 

69 f = open(file, "r") # , encoding="utf8") 

70 sall = f.readlines() 

71 f.close() 

72 s = "".join(sall) 

73 if "thread" in s: 

74 return "Chapitre 10 : thread" 

75 if "Tkinter" in s: 

76 return "Chapitre 9 : interface" 

77 if "struct" in s: 

78 return "Chapitre 8 : fichiers" 

79 if "glob" in s: 

80 return "Chapitre 8 : fichiers" 

81 if "import Tix" in s: 

82 return "Chapitre 9 : interface" 

83 if "selection" in file: 

84 return "Chapitre 9 : interface" 

85 if "filelist" in file: 

86 return "Chapitre 9 : interface" 

87 if "class" in file: 

88 return "Chapitre 5 : classes" 

89 if "exemple" in file: 

90 return "Chapitre 7 : modules" 

91 if "setup" in file: 

92 return "Chapitre 7 : modules" 

93 if "PythonSample" in file: 

94 return "Chapitre 7 : modules" 

95 if "init" in file: 

96 return "Chapitre 7 : modules" 

97 return "-" 

98 

99 

100def py_to_html_folder(folder, addGoogleTracking=True): 

101 """ 

102 Converts all :epkg:`python` files from a folder into html files. 

103 

104 @param folder folder 

105 @param addGoogleTracking add some code for the Google tracking (related to www.xavierdupre.fr), 

106 @see fn py_to_html_file 

107 @return list of processed files 

108 """ 

109 res = [] 

110 li = os.listdir(folder) 

111 for f in li: 

112 fullf = folder + "/" + f 

113 ext = os.path.splitext(fullf)[1] 

114 if ext == ".py": 

115 r = py_to_html_file(fullf, addGoogleTracking=addGoogleTracking) 

116 res.append(r) 

117 return res 

118 

119 

120def py_to_html_file(file, writehtml="", addGoogleTracking=True, title=None): 

121 """ 

122 Converts a :epkg:`python` script into a html file. 

123 

124 @param folder folder 

125 @param writehtml filename 

126 @param addGoogleTracking add some code for the Google tracking (related to www.xavierdupre.fr), it looks like: 

127 

128 :: 

129 

130 <script src="http://www.google-analytics.com/urchin.js" type="text/javascript"></script> 

131 <script type="text/javascript">_uacct = "UA-XXXXXXX-X";urchinTracker();</script> 

132 

133 @return the processed file (same file but with extension .html) 

134 """ 

135 googlet = googleTrackerFooter if addGoogleTracking else "" 

136 

137 fLOG("[py_to_html_file] converting pyfile '{}' in html.".format(file)) 

138 f = file 

139 racine = os.path.splitext(file)[0] 

140 

141 try: 

142 with open(file, "r", encoding="utf-8") as tf: 

143 content = tf.read() 

144 encoding = "utf-8" 

145 except UnicodeDecodeError: 

146 try: 

147 with open(file, "r", encoding="latin-1") as tf: 

148 content = tf.read() 

149 encoding = "utf-8" 

150 except UnicodeDecodeError: 

151 with open(file, "r", encoding="utf-8", errors="utf-8") as tf: 

152 content = tf.read() 

153 encoding = "utf-8" 

154 

155 content = cleanFileFromtohtmlreplace(content) 

156 

157 appliedstyle = readStyleFile(None) 

158 try: 

159 data = file2HTML(content, "0", appliedstyle, 

160 False, "1", encoding=encoding) 

161 block = makeBlock(data) 

162 page = py_page.replace(googleTrackerMarker, googlet) 

163 html = page % (title or f, title or f, block, py2html__version__) 

164 except Exception as e: 

165 raise Exception( 

166 "Not a python file, running it again '{0}'.".format( 

167 file)) from e 

168 

169 if len(writehtml) > 0: 

170 outfile = writehtml 

171 else: 

172 outfile = racine + ".html" 

173 

174 with open(outfile, "w", encoding=encoding) as f: 

175 f.write(html) 

176 fLOG("[py_to_html_file] encoding='{}' wrote '{}'.".format( 

177 encoding, outfile)) 

178 

179 return outfile