Skip to content

Commit c96ea79

Browse files
committed
fix: untrimed field[0] might miss the handler
1 parent f9fb0c7 commit c96ea79

File tree

1 file changed

+24
-17
lines changed

1 file changed

+24
-17
lines changed

article-manager.py

+24-17
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,24 @@
1919
# - images
2020
# - content hash
2121
# 2. mark the article if publish is true
22-
# 3. compare marked article hash with lockfile, update article if changed
22+
# 3. compare marked article hash with lockfile
2323
# in the lockfile:
2424
# - if id exist
2525
# - allow to publish, and content hash changed, update
2626
# - disallow to publish, delete
27-
# - if id do not exist, publish as new article
27+
# - if id does not exists
28+
# - allow to publish, release the new article
29+
# - disallow to publish, disregard
2830
# 4. copy article to .content
2931
# 5. copy all images to static-{{properties.title}} folder under ./content if has any
3032
# 6. next article
3133
#
3234
# Requirements:
3335
# 1. script should be able to dry run
34-
# 2. no 3th-part dependencies
36+
# 2. no 3th-party dependencies
3537
# 3. should not change article during the process
36-
# 4. Non declared "publish" equals false
38+
# 4. non declared "publish" equals false
39+
# 5. interrupting process should not cause state of lockfile loss
3740

3841

3942
FILE_EXTENSION = ".md"
@@ -76,27 +79,29 @@ class Action:
7679
# process individual file
7780
for fname in os.listdir(OBVAULT_ARTICELS):
7881
if not fname.strip().endswith(FILE_EXTENSION):
79-
print(f"'{fname}' skipped, not a markdown file")
82+
print(f"'{fname}' skipped: not a markdown file")
8083
continue
8184

8285
with open(os.path.join(OBVAULT_ARTICELS, fname), "r") as f:
8386
print(f"process '{fname}'...")
8487
if not f.readline().strip().startswith(PROPERTY_DELIMITER):
85-
print("file doesn't follow property format, skipped")
88+
print("file skipped: doesn't follow property format")
8689
continue
8790

8891
# retrieve properties
8992
meta = {"publish":False, "fname": fname}
9093
line = f.readline().strip()
9194
while not line.startswith(PROPERTY_DELIMITER):
9295
field = line.split(":", 1)
93-
h = RELEVANT_PROPERTY_HANDLER.get(field[0], None)
96+
key = field[0].strip()
97+
h = RELEVANT_PROPERTY_HANDLER.get(key, None)
9498
if h:
95-
meta[field[0]] = h(field[1])
99+
val = field[1].strip()
100+
meta[key] = h(val)
96101
line = f.readline().strip()
97102

98103
# retrieve images and content hash of the article
99-
if meta.get("publish"):
104+
if meta.get("publish") is True:
100105
# finds images of article and add to list
101106
while line := f.readline():
102107
res = REGEX.search(line.strip())
@@ -115,7 +120,7 @@ class Action:
115120
# decides action to article
116121
id = meta.get("id", None)
117122
hash = meta.get("hash", None)
118-
if meta["publish"]:
123+
if meta["publish"] is True:
119124
if id not in lck:
120125
meta.setdefault("action", Action.NEW)
121126
elif lck.get(id).get("hash", None) != hash:
@@ -128,6 +133,7 @@ class Action:
128133
ATCMETAARR.append(meta)
129134

130135
print("\n================================Dry Run Result================================\n")
136+
131137
print(f"Articles ready to publish: {Action.NEW}, remove: {Action.DELETE}, update: {Action.UPDATE}\n")
132138
i = 1
133139
for at in ATCMETAARR:
@@ -137,7 +143,11 @@ class Action:
137143
print(f'images:{at.get("images",{})}')
138144
print(f'\n')
139145
i+=1
140-
print("==============================================================================\n")
146+
147+
if len(ATCMETAARR) == 0:
148+
print("nothing to update")
149+
150+
print("\n==============================================================================\n")
141151

142152
def apply():
143153
lckfnametemp = lckfname + ".tmp"
@@ -187,10 +197,7 @@ def apply():
187197

188198

189199
if not DRYRUN:
190-
if len(ATCMETAARR) == 0:
191-
print("nothing to update")
200+
if "yes" == input("type yes if you want to apply the change:\n\n"):
201+
apply()
192202
else:
193-
if "yes" == input("type yes if you want to apply the change:\n\n"):
194-
apply()
195-
else:
196-
print("operation abort")
203+
print("operation abort")

0 commit comments

Comments
 (0)