sort output by year and month

This commit is contained in:
dousha 2024-12-28 11:22:51 +08:00
parent 42fd528b22
commit dc0f73c846
2 changed files with 19 additions and 10 deletions

View File

@ -9,8 +9,11 @@ import re
in_file = 'wpexport20241219.xml'
if not os.path.exists('output'):
os.makedirs('output')
def mkdirp(path):
if not os.path.exists(path):
os.makedirs(path)
mkdirp('output')
with open(in_file, 'r', encoding='utf-8') as f:
raw_content = f.read()
@ -43,13 +46,15 @@ for item in items:
content = re.sub(r'<!--more-->', '<p class="more">:::more:::</p>', content)
content = '\n'.join([s.strip() for s in content.splitlines() if s.strip()])
content = md(content).strip() + '\n'
frontmatter = f"---\nlayout: {post_type}\nid: {post_id}\ntitle: \"{title}\"\ncreator: \"{creator}\"\ndate: {date}\ncategories:\n{categories}\ntags:\n{tags}\ndraft: {'yes' if status == 'draft' else 'no'}\n---\n\n"
frontmatter = f"---\nlayout: {post_type}\nid: {post_id}\ntitle: \"{title}\"\ncreator: \"{creator}\"\ndate: {date}\ncategories:\n{categories}\ntags:\n{tags}\ndraft: {'yes' if status == 'draft' else 'no'}\npublished: {'yes' if status == 'publish' else 'no'}\n---\n\n"
title_slug = slugify(title)
if len(title_slug) < 1:
title_slug = 'untitled'
filename_candidate = "output/" + title_slug + ".md"
output_folder = 'output/' + str(date)[:4] + '/' + str(date)[5:7] + '/'
mkdirp(output_folder)
filename_candidate = output_folder + title_slug + ".md"
filename_disambiguation = 1
while os.path.exists(filename_candidate):

16
main.py
View File

@ -9,8 +9,11 @@ import re
in_file = 'wpexport20241219.xml'
if not os.path.exists('output'):
os.makedirs('output')
def mkdirp(path):
if not os.path.exists(path):
os.makedirs(path)
mkdirp('output')
with open(in_file, 'r', encoding='utf-8') as f:
raw_content = f.read()
@ -42,14 +45,16 @@ for item in items:
content = re.sub(r'<!-- \/?wp:.+?\s*-->', '', content)
content = re.sub(r'<!--more-->', '<p class="more">:::more:::</p>', content)
content = '\n'.join([s.strip() for s in content.splitlines() if s.strip()])
content = md(content).strip()
frontmatter = f"---\nlayout: {post_type}\ntitle: \"{title}\"\ncreator: \"{creator}\"\ndate: {date}\ncategories:\n{categories}\ntags:\n{tags}\ndraft: {'yes' if status == 'draft' else 'no'}\n---\n\n"
content = md(content).strip() + '\n'
frontmatter = f"---\nlayout: {post_type}\nid: {post_id}\ntitle: \"{title}\"\ncreator: \"{creator}\"\ndate: {date}\ncategories:\n{categories}\ntags:\n{tags}\ndraft: {'yes' if status == 'draft' else 'no'}\npublished: {'yes' if status == 'publish' else 'no'}\n---\n\n"
title_slug = slugify(title)
if len(title_slug) < 1:
title_slug = 'untitled'
filename_candidate = "output/" + title_slug + ".md"
output_folder = 'output/' + str(date)[:4] + '/' + str(date)[5:7] + '/'
mkdirp(output_folder)
filename_candidate = output_folder + title_slug + ".md"
filename_disambiguation = 1
while os.path.exists(filename_candidate):
@ -59,4 +64,3 @@ for item in items:
with open(filename_candidate, 'w', encoding='utf-8') as f:
f.write(frontmatter)
f.write(content)