添加内存模式支持以在Linux上将PDF生成到内存中

Signed-off-by: szdytom <szdytom@qq.com>
This commit is contained in:
方而静 2025-07-11 13:40:30 +08:00
parent 3b7c51ab1f
commit a9089531d5
Signed by: szTom
GPG Key ID: 072D999D60C6473C

13
make.py
View File

@ -171,13 +171,20 @@ def prepare_typst():
print(f"Typst downloaded and extracted to {typst_bin_path}.") print(f"Typst downloaded and extracted to {typst_bin_path}.")
return True return True
def invoke_typst(mode="c"): def invoke_typst(mode="c", mem_mode=False):
"""调用 typst 命令""" """调用 typst 命令"""
if not typst_bin_path.exists(): if not typst_bin_path.exists():
print("Typst executable not found.") print("Typst executable not found.")
return False return False
command = [str(typst_bin_path), mode, "--font-path", str(fonts_dir), "--ignore-system-fonts", "main.typ"] command = [str(typst_bin_path), mode, "--font-path", str(fonts_dir), "--ignore-system-fonts", "main.typ"]
if mem_mode:
if platform.system().lower() == "linux":
command.extend(["/dev/shm/main.pdf"])
else:
print("Memory mode is only supported on Linux.")
return False
print("Executing command: ", " ".join(command)) print("Executing command: ", " ".join(command))
try: try:
os.execv(str(typst_bin_path), command) os.execv(str(typst_bin_path), command)
@ -189,6 +196,8 @@ def main():
parser = argparse.ArgumentParser(description="Run Typst with specified mode.") parser = argparse.ArgumentParser(description="Run Typst with specified mode.")
parser.add_argument('--mode', type=str, choices=['w', 'c'], default='c', parser.add_argument('--mode', type=str, choices=['w', 'c'], default='c',
help="Mode to run Typst: 'w' for watch mode, 'c' for compile mode.") help="Mode to run Typst: 'w' for watch mode, 'c' for compile mode.")
parser.add_argument('--mem', action='store_true',
help="Generate PDF to memory (/dev/shm) instead of file system. (Linux only)")
args = parser.parse_args() args = parser.parse_args()
if not prepare_fonts(): if not prepare_fonts():
@ -196,7 +205,7 @@ def main():
if not prepare_typst(): if not prepare_typst():
return return
invoke_typst(mode=args.mode) invoke_typst(mode=args.mode, mem_mode=args.mem)
if __name__ == "__main__": if __name__ == "__main__":
main() main()