-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupdate.py
128 lines (96 loc) · 3.85 KB
/
update.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import sys
import toml
import re
from datetime import datetime
####################
# Modify TOML
####################
# Check if a new version number was provided as a command-line argument
if len(sys.argv) < 2:
print("Usage: python script.py <new_version>")
sys.exit(1)
new_version = sys.argv[1] # Get the new version number from the command-line arguments
# Define the TOML content
toml_content = """
[package]
name = "ssrn-scribe"
version = "0.2.5"
entrypoint = "paper_template.typ"
authors = ["jxpeng98"]
repository = "https://github.com/jxpeng98/Typst-Paper-Template"
license = "MIT"
description = "Personal working paper template for general doc and SSRN paper."
categories = ["paper"]
[template]
path = "template"
entrypoint = "main.typ"
thumbnail = "thumbnail.png"
"""
# Parse the TOML content
parsed_toml = toml.loads(toml_content)
# Update the version
parsed_toml["package"]["version"] = new_version
# Define a file path to save the updated TOML content
file_path = "typst.toml"
# Write the updated TOML content to a file
with open(file_path, "w") as file:
toml.dump(parsed_toml, file)
print(f"Updated TOML file saved to {file_path}")
####################
# Update .typ file
####################
new_year = datetime.now().year
new_date = datetime.now().strftime("%Y-%m-%d")
# Define the path to your .typ file
typ_file_path = 'paper_template.typ'
# Define the content to prepend, ensure no leading newline before the first comment line
new_content_to_prepend = f"""///////////////////////////////
// This Typst template is for working paper draft.
// It is based on the general SSRN paper.
// Copyright (c) {new_year}
// Author: Jiaxin Peng
// License: MIT
// Version: {new_version}
// Date: {new_date}
// Email: [email protected]
///////////////////////////////
""" # Ensuring only the intended newlines are included
# Read the existing content of the file
with open(typ_file_path, 'r', encoding='utf-8') as file:
original_content = file.read()
# Pattern to match the existing header, assuming it's always at the start of the file
header_pattern = re.compile(r'^\/\/+[\s\S]*?\/\/+\n\n', re.MULTILINE)
# Check if the existing header is present and replace it
if header_pattern.search(original_content):
updated_content = header_pattern.sub(new_content_to_prepend, original_content, count=1)
else:
# If no header is found, prepend new header without adding an extra newline at the beginning
updated_content = new_content_to_prepend.rstrip('\n') + original_content
# Write the updated content back to the file
with open(typ_file_path, 'w', encoding='utf-8') as file:
file.write(updated_content)
print("Template File has been updated with the new version and date.")
# update the main.typ file
# define the path to main.typ file
main_typ_file_path = 'template/main.typ'
# define the content to prepend
new_content_to_prepend = f"""#import "@preview/ssrn-scribe:{new_version}": *
""" # Ensuring only the intended newlines are included
# Read the existing content of the file
with open(main_typ_file_path, 'r', encoding='utf-8') as file:
original_content = file.read()
# Pattern to match the existing header, assuming it's always at the start of the file
header_pattern = re.compile(
r'^#import\s+"@preview/ssrn-scribe:([^"]+)"\s*:\s*\*\s*\n',
re.MULTILINE
)
# Check if the existing header is present and replace it
if header_pattern.search(original_content):
updated_content = header_pattern.sub(new_content_to_prepend, original_content, count=1)
else:
# If no header is found, prepend new header without adding an extra newline at the beginning
updated_content = new_content_to_prepend.rstrip('\n') + original_content
# Write the updated content back to the file
with open(main_typ_file_path, 'w', encoding='utf-8') as file:
file.write(updated_content)
print("Main.typ file has been updated with the new version.")