Skip to content

Commit 89883ee

Browse files
committed
feat: import test code in PDF
1 parent 734111a commit 89883ee

File tree

4 files changed

+61
-39
lines changed

4 files changed

+61
-39
lines changed

config.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ cheatsheet_dir: cheatsheet
55
test_dir: test_tinplate
66
notebook_file: notebook
77
enable_test: false
8-
generate_test_in_notebook: false
8+
generate_test_in_notebook: true
99
notebook:
1010
chapters:
1111
util: 杂项

libs/latex_utils.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,4 @@ def latex_section(name: NameLaTeX, **kwargs) -> list[str]:
8282

8383
@withlog
8484
def latex_listing_code(path: PathLaTeX, code_style: str, **kwargs) -> list[str]:
85-
return [rf'Path: \verb|{path.get()}|', '\n\n', __latex_command_('inputminted', code_style, path.get())]
85+
return [rf'Path: \verb|{path.get()}|', '\n\n', __latex_command_('inputminted', code_style, path.get()), '\n']

manager.py

+59-37
Original file line numberDiff line numberDiff line change
@@ -65,63 +65,79 @@ def generate_notebook_contents(logger: logging.Logger):
6565
os.makedirs(CONTENTS_DIR)
6666
with open(CONTENTS_NB, 'w', encoding='utf8') as f:
6767
f.write('%-*- coding: utf-8 -*-\n')
68-
chapters = file_preprocess(CONFIG.get_chapter_key(),
69-
scandir_dir_merge(CONFIG.get_code_dir(), CONFIG.get_doc_dir()), logger)
68+
chapters = file_preprocess(CONFIG.get_chapter_key(), scandir_dir_merge(
69+
CONFIG.get_code_dir(), CONFIG.get_doc_dir()), logger)
7070
logger.info(rf"{len(chapters)} chapter(s) found")
7171
logger.debug('Which are:\n\t' + '\n\t'.join(chapters))
7272
logger.debug('Will include in listed order')
7373

7474
for chapter in chapters:
75-
f.writelines(latex_chapter(NameLaTeX(CONFIG.get_chapter_title(chapter))))
75+
f.writelines(latex_chapter(
76+
NameLaTeX(CONFIG.get_chapter_title(chapter))))
7677

77-
sections: list[Section] = CONFIG.get_sections_by_chapter(chapter)
78+
sections: list[Section] = CONFIG.get_sections_by_chapter(
79+
chapter)
7880
logger.info(f"{len(sections)} section(s) found in config")
7981

8082
logger.debug(f"Getting section from existing files")
8183
_sections_generated: list[Section] = []
82-
_partitioned_code_filename = load_from(os.path.join(CONFIG.get_code_dir(), chapter))
83-
_partitioned_doc_filename = load_from(os.path.join(CONFIG.get_doc_dir(), chapter))
84+
_partitioned_code_filename = load_from(
85+
os.path.join(CONFIG.get_code_dir(), chapter))
86+
_partitioned_doc_filename = load_from(
87+
os.path.join(CONFIG.get_doc_dir(), chapter))
8488
__dict: dict[str, str] = dict(_partitioned_code_filename)
8589
if CONFIG.enable_test():
86-
_partitioned_test_filename = load_from(os.path.join(CONFIG.get_test_dir(), chapter))
90+
_partitioned_test_filename = load_from(
91+
os.path.join(CONFIG.get_test_dir(), chapter))
8792
for k, v in _partitioned_test_filename:
8893
try:
89-
_sections_generated.append(Section(chapter, k, k, __dict.pop(k), v))
94+
_sections_generated.append(
95+
Section(chapter, k, k, __dict.pop(k), v))
9096
except KeyError as e:
91-
logger.error(f"Test file '{k}.{v}' found, while code file is not")
97+
logger.error(
98+
f"Test file '{k}.{v}' found, while code file is not")
9299
raise e
93100
__null_ext_name: str = f"{random.randint(0, 114514)}.null"
94101
for k, v in _partitioned_doc_filename:
95-
_sections_generated.append(Section(chapter, k, k, __dict.pop(k, __null_ext_name), __null_ext_name))
102+
_sections_generated.append(
103+
Section(chapter, k, k, __dict.pop(k, __null_ext_name), __null_ext_name))
96104
for k, v in __dict.items():
97-
_sections_generated.append(Section(chapter, k, k, v, __null_ext_name))
98-
logger.info(f"{len(_sections_generated)} section(s) generated from existing files")
105+
_sections_generated.append(
106+
Section(chapter, k, k, v, __null_ext_name))
107+
logger.info(
108+
f"{len(_sections_generated)} section(s) generated from existing files")
99109

100-
sections = unique(sections + _sections_generated, lambda x: x.name)
110+
sections = unique(
111+
sections + _sections_generated, lambda x: x.name)
101112
logger.info(f"{len(sections)} section(s) in total")
102113

103114
for section in sections:
104115
f.writelines(latex_section(NameLaTeX(section.title)))
105-
code_filepath, doc_filepath, test_filepath = section.get_filenames(CONFIG.get_code_dir(),
106-
CONFIG.get_doc_dir(),
107-
CONFIG.get_test_dir())
108-
execute_if_file_exist(doc_filepath, lambda x: f.writelines(latex_input(PathLaTeX(x))))
109-
execute_if_file_exist(code_filepath, lambda x: f.writelines(
110-
latex_listing_code(PathLaTeX(x), CONFIG.get_code_style(section.code_ext))))
116+
code_filepath, doc_filepath, test_filepath = section.get_filenames(
117+
CONFIG.get_code_dir(), CONFIG.get_doc_dir(), CONFIG.get_test_dir())
118+
f.writelines(latex_input(PathLaTeX(doc_filepath)))
119+
f.writelines(latex_listing_code(
120+
PathLaTeX(code_filepath), CONFIG.get_code_style(section.code_ext)))
111121
if CONFIG.generate_test_in_notebook():
112-
execute_if_file_exist(test_filepath, lambda x: f.writelines(
113-
latex_listing_code(PathLaTeX(x), CONFIG.get_code_style(section.test_ext))))
122+
if not os.path.getsize(test_filepath):
123+
continue
124+
f.writelines(latex_listing_code(
125+
PathLaTeX(test_filepath), CONFIG.get_code_style(section.test_ext)))
114126

115127
@withlog
116128
def load_from(dir_name: str, **kwargs) -> list[tuple[str, str]]:
117-
filename_in_dir: list[str] = scandir_file_merge(CONFIG.get_all_code_ext_names(), dir_name)
129+
filename_in_dir: list[str] = scandir_file_merge(
130+
CONFIG.get_all_code_ext_names(), dir_name)
118131
kwargs.get('logger').info(rf"{len(filename_in_dir)} file(s) found")
119132
if len(filename_in_dir):
120-
kwargs.get('logger').debug('Which are:\n\t' + '\n\t'.join(filename_in_dir))
133+
kwargs.get('logger').debug(
134+
'Which are:\n\t' + '\n\t'.join(filename_in_dir))
121135

122-
_partitioned_filename: list[tuple[str, str]] = [parse_filename(filename) for filename in filename_in_dir]
136+
_partitioned_filename: list[tuple[str, str]] = [
137+
parse_filename(filename) for filename in filename_in_dir]
123138
if len(set([k for k, v in _partitioned_filename])) != len(_partitioned_filename):
124-
kwargs.get('logger').error(f'File with duplicate name found in dir: {dir_name}')
139+
kwargs.get('logger').error(
140+
f'File with duplicate name found in dir: {dir_name}')
125141
kwargs.get('logger').info(
126142
'Each filenames in the same folder should not be the same after removing the ext names')
127143
raise FileExistsError()
@@ -143,18 +159,18 @@ def generate_cheatsheet_contents(logger: logging.Logger):
143159
f.write('%-*- coding: utf-8 -*-\n')
144160
files: list[str] = file_preprocess([f'{f}.tex' for f in CONFIG.get_cheatsheets()],
145161
scandir_file_merge(['tex'], CONFIG.get_cheatsheet_dir()), logger)
146-
files = [os.path.join(CONFIG.get_cheatsheet_dir(), item) for item in files]
162+
files = [os.path.join(CONFIG.get_cheatsheet_dir(), item)
163+
for item in files]
147164
logger.info(rf"{len(files)} file(s) found:")
148165
logger.debug('Which are:\n\t' + '\n\t'.join(files))
149166
logger.debug('Will include in listed order')
150167

151168
f.writelines(latex_chapter(NameLaTeX('Cheatsheet')))
152169
for file in files:
153-
f.writelines(
154-
latex_section(NameLaTeX(CONFIG.get_cheatsheet_name(file.removeprefix(CONFIG.get_cheatsheet_dir())
155-
.removeprefix('/')
156-
.removeprefix('\\')
157-
.removesuffix('.tex')))))
170+
f.writelines(latex_section(NameLaTeX(CONFIG.get_cheatsheet_name(file.removeprefix(CONFIG.get_cheatsheet_dir())
171+
.removeprefix('/')
172+
.removeprefix('\\')
173+
.removesuffix('.tex')))))
158174
f.writelines(latex_input(PathLaTeX(file)))
159175

160176
generate_cheatsheet_contents()
@@ -173,7 +189,8 @@ def compile_all(_no_fmt: bool, _no_gen: bool, _no_clean: bool, **kwargs):
173189
for procedure in LATEX_COMPILE_COMMAND_GROUP:
174190
now_proc: list[str] = procedure(CONFIG.get_notebook_file())
175191
cnt += 1
176-
kwargs.get('logger').info(f'Step {cnt} / {len(LATEX_COMPILE_COMMAND_GROUP)}' + '\n' + '-' * 80)
192+
kwargs.get('logger').info(
193+
f'Step {cnt} / {len(LATEX_COMPILE_COMMAND_GROUP)}' + '\n' + '-' * 80)
177194
subprocess.run(now_proc, encoding='utf8', check=True)
178195

179196
kwargs.get('logger').info('Finished')
@@ -203,7 +220,8 @@ def reformat_all_codes(_code_type: str, **kwargs):
203220
CONFIG.get_ext_names_by_code_style(_code_type))
204221
kwargs.get('logger').info(f"{len(filepaths)} file(s) found")
205222
for filepath in filepaths:
206-
cmd: list[str] = CONFIG.get_formatting_command(_code_type, filepath)
223+
cmd: list[str] = CONFIG.get_formatting_command(
224+
_code_type, filepath)
207225
subprocess.run(cmd, encoding='utf8', check=True)
208226

209227
kwargs.get('logger').info('finished')
@@ -226,20 +244,24 @@ def _new_note(chapter_name: str, file_name: str, section_title: str, code_ext_na
226244
@withlog
227245
def add_new_note(_chapter_name: str, _file_name: str, _section_title: str,
228246
_code_ext_name: str, _test_ext_name: str, **kwargs):
229-
section: Section = Section(_chapter_name, _file_name, _section_title, _code_ext_name, _test_ext_name)
247+
section: Section = Section(
248+
_chapter_name, _file_name, _section_title, _code_ext_name, _test_ext_name)
230249

231-
section.open(CONFIG.get_code_dir(), CONFIG.get_doc_dir(), CONFIG.get_test_dir(), 'x')
250+
section.open(CONFIG.get_code_dir(), CONFIG.get_doc_dir(),
251+
CONFIG.get_test_dir(), 'x')
232252
kwargs.get('logger').info('Created')
233253

234-
_code, _doc, _test = section.get_filenames(CONFIG.get_code_dir(), CONFIG.get_doc_dir(), CONFIG.get_test_dir())
254+
_code, _doc, _test = section.get_filenames(
255+
CONFIG.get_code_dir(), CONFIG.get_doc_dir(), CONFIG.get_test_dir())
235256
kwargs.get('logger').info(f"Code: {os.path.join(os.curdir, _code)}")
236257
kwargs.get('logger').info(f"Doc: {os.path.join(os.curdir, _doc)}")
237258
kwargs.get('logger').info(f"Test: {os.path.join(os.curdir, _test)}")
238259

239260
CONFIG.append_section(section)
240261
kwargs.get('logger').info('Config updated')
241262

242-
add_new_note(chapter_name, file_name, section_title, code_ext_name, test_ext_name)
263+
add_new_note(chapter_name, file_name, section_title,
264+
code_ext_name, test_ext_name)
243265

244266

245267
# TODO

src/test_tinplate/fast/u32tostrhex.cpp

Whitespace-only changes.

0 commit comments

Comments
 (0)