Coverage for bookbuilderpy/resources/_generate_resources.py: 12%

104 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-07-17 23:15 +0000

1"""Generate the resources.""" 

2from os.path import dirname 

3from typing import Final 

4 

5from bookbuilderpy.git import Repo 

6from bookbuilderpy.logger import logger 

7from bookbuilderpy.path import Path 

8from bookbuilderpy.strings import enforce_non_empty_str 

9from bookbuilderpy.temp import TempDir 

10from bookbuilderpy.url import load_text_from_url 

11 

12 

13def load_html_github_template(dest: Path) -> Path: 

14 """ 

15 Load the HTML 5 template with GitHub look and feel. 

16 

17 :param dest: the destination directory 

18 :return: the full path of the template 

19 """ 

20 logger(f"now loading html github template to '{dest}'.") 

21 with TempDir.create() as temp: 

22 name = "GitHub.html5" 

23 repo = Repo.download("https://github.com/tajmone/pandoc-goodies/", 

24 temp) 

25 src_file = repo.path.resolve_inside(f"templates/html5/github/{name}") 

26 src_file.enforce_file() 

27 text = enforce_non_empty_str(src_file.read_all_str()) 

28 text = text.replace( 

29 "div.line-block{white-space:pre-line}", 

30 "div.line-block{line-height:0.85;white-space:pre-line}") 

31 

32 # remove the useless alert styles 

33 remove_start = ".Alert," 

34 idx_1 = text.find(remove_start) 

35 if idx_1 > 0: 

36 remove_end = ".Warning h6:" 

37 idx_2 = text.find(remove_end, idx_1) 

38 if idx_2 > idx_1: 

39 idx_3 = text.find("}", idx_2) 

40 if idx_3 > idx_2: 

41 text = text[:idx_1].strip() + text[(idx_3 + 1):].strip() 

42 

43 text = text.replace(',"Apple Color Emoji"', "") 

44 text = text.replace(',"Segoe UI Emoji"', "") 

45 text = text.replace(',"Segoe UI Symbol"', "") 

46 text = text.replace("div.line-block{line-height:0.85;", 

47 "div.line-block{") 

48 start1 = text.find(".markdown-body p,") 

49 if start1 > 0: 

50 start2 = text.find("{", start1) 

51 if start2 > start1: 

52 end1 = text.find("}", start1) 

53 if end1 > start2: 

54 replacer = "margin-top:0;" 

55 found = text.find(replacer, start2) 

56 if start2 < found < end1: 

57 text = (f"{text[:found]}margin-top:16px;" 

58 f"{text[found+len(replacer):]}") 

59 

60 dst_file = dest.resolve_inside(name) 

61 dst_file.write_all(text) 

62 Path.copy_file(repo.path.resolve_inside("LICENSE"), 

63 dest.resolve_inside(f"{name}_license")) 

64 if name != dst_file.relative_to(dest): 

65 raise ValueError(f"'{name}' should " 

66 f"be '{dst_file.relative_to(dest)}'.") 

67 logger(f"succeeded in loading html github template to '{dst_file}'.") 

68 return dst_file 

69 

70 

71def load_latex_eisvogel_template(dest: Path) -> Path: 

72 """ 

73 Load the LaTeX eisvogel template. 

74 

75 :param dest: the destination directory 

76 :return: the full path of the template 

77 """ 

78 logger(f"now loading latex eisvogel template to '{dest}'.") 

79 with TempDir.create() as temp: 

80 name = "eisvogel.tex" 

81 repo = Repo.download( 

82 "https://github.com/Wandmalfarbe/pandoc-latex-template", 

83 temp) 

84 src_file = repo.path.resolve_inside(f"{name}") 

85 src_file.enforce_file() 

86 text = enforce_non_empty_str(src_file.read_all_str()) 

87 text = text.replace("scrartcl", "scrbook") 

88 dst_file = dest.resolve_inside(name) 

89 dst_file.write_all(text) 

90 Path.copy_file(repo.path.resolve_inside("LICENSE"), 

91 dest.resolve_inside(f"{name}_license")) 

92 if name != dst_file.relative_to(dest): 

93 raise ValueError(f"'{name}' should " 

94 f"be '{dst_file.relative_to(dest)}'.") 

95 logger( 

96 f"succeeded in loading latex eisvogel template to '{dst_file}'.") 

97 return dst_file 

98 

99 

100def load_csl_template(dest: Path) -> list[Path]: 

101 """ 

102 Load the CSL templates template. 

103 

104 :param dest: the destination directory 

105 :return: the full path of the template 

106 """ 

107 logger(f"now csl template(s) to '{dest}'.") 

108 paths: Final[list[Path]] = [] 

109 

110 for name in ["association-for-computing-machinery"]: 

111 url = f"https://www.zotero.org/styles/{name}" 

112 urlname, text = load_text_from_url(url) 

113 usename = f"{name}.csl" 

114 if urlname != usename: 

115 raise ValueError(f"Name conflict: '{urlname}' vs. '{usename}'.") 

116 dst_file = dest.resolve_inside(usename) 

117 dst_file.write_all(text) 

118 if name != Path.split_prefix_suffix(dst_file.relative_to(dest))[0]: 

119 raise ValueError(f"'{name}' should " 

120 f"be '{dst_file.relative_to(dest)}'.") 

121 logger(f"finished loading '{url}' to file '{dst_file}'.") 

122 paths.append(dst_file) 

123 return paths 

124 

125 

126def load_mathjax(dest: Path) -> Path: 

127 """ 

128 Download a full the math jax installation for svg conversion. 

129 

130 For the HTML build, we want to convert all the equations to SVGs, 

131 because they will look the same regardless on which device the 

132 books are displayed. This also allows us to purge the javascripts 

133 after the conversion. 

134 

135 :param dest: the destination 

136 :return: the paths to the downloaded resources 

137 """ 

138 logger(f"now loading mathjax svg to '{dest}'.") 

139 name = "mathjax.js" 

140 url = "https://unpkg.com/mathjax@3/es5/tex-svg-full.js" 

141 _, data = load_text_from_url(url) 

142 data = enforce_non_empty_str(data.strip()) 

143 dst_file = dest.resolve_inside(name) 

144 dst_file.write_all(data) 

145 

146 name = f"{name}_license" 

147 url = "https://raw.githubusercontent.com/mathjax/MathJax/master/LICENSE" 

148 _, data = load_text_from_url(url) 

149 data = enforce_non_empty_str(data.strip()) 

150 dst_file = dest.resolve_inside(name) 

151 dst_file.write_all(data) 

152 

153 logger(f"finished loading mathjax svg to file '{dst_file}'.") 

154 return dst_file 

155 

156 

157if __name__ == "__main__": 

158 logger("begin loading resources.") 

159 current_dir = Path.directory(dirname(__file__)) 

160 logger(f"current dir is '{current_dir}'.") 

161 load_mathjax(current_dir) 

162 load_html_github_template(current_dir) 

163 load_csl_template(current_dir) 

164 load_latex_eisvogel_template(current_dir) 

165 logger("done loading resources.")