sort output by year and month
This commit is contained in:
parent
42fd528b22
commit
dc0f73c846
13
convert.py
13
convert.py
@ -9,8 +9,11 @@ import re
|
|||||||
|
|
||||||
in_file = 'wpexport20241219.xml'
|
in_file = 'wpexport20241219.xml'
|
||||||
|
|
||||||
if not os.path.exists('output'):
|
def mkdirp(path):
|
||||||
os.makedirs('output')
|
if not os.path.exists(path):
|
||||||
|
os.makedirs(path)
|
||||||
|
|
||||||
|
mkdirp('output')
|
||||||
|
|
||||||
with open(in_file, 'r', encoding='utf-8') as f:
|
with open(in_file, 'r', encoding='utf-8') as f:
|
||||||
raw_content = f.read()
|
raw_content = f.read()
|
||||||
@ -43,13 +46,15 @@ for item in items:
|
|||||||
content = re.sub(r'<!--more-->', '<p class="more">:::more:::</p>', 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 = '\n'.join([s.strip() for s in content.splitlines() if s.strip()])
|
||||||
content = md(content).strip() + '\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'}\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)
|
title_slug = slugify(title)
|
||||||
if len(title_slug) < 1:
|
if len(title_slug) < 1:
|
||||||
title_slug = 'untitled'
|
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
|
filename_disambiguation = 1
|
||||||
|
|
||||||
while os.path.exists(filename_candidate):
|
while os.path.exists(filename_candidate):
|
||||||
|
16
main.py
16
main.py
@ -9,8 +9,11 @@ import re
|
|||||||
|
|
||||||
in_file = 'wpexport20241219.xml'
|
in_file = 'wpexport20241219.xml'
|
||||||
|
|
||||||
if not os.path.exists('output'):
|
def mkdirp(path):
|
||||||
os.makedirs('output')
|
if not os.path.exists(path):
|
||||||
|
os.makedirs(path)
|
||||||
|
|
||||||
|
mkdirp('output')
|
||||||
|
|
||||||
with open(in_file, 'r', encoding='utf-8') as f:
|
with open(in_file, 'r', encoding='utf-8') as f:
|
||||||
raw_content = f.read()
|
raw_content = f.read()
|
||||||
@ -42,14 +45,16 @@ for item in items:
|
|||||||
content = re.sub(r'<!-- \/?wp:.+?\s*-->', '', content)
|
content = re.sub(r'<!-- \/?wp:.+?\s*-->', '', content)
|
||||||
content = re.sub(r'<!--more-->', '<p class="more">:::more:::</p>', 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 = '\n'.join([s.strip() for s in content.splitlines() if s.strip()])
|
||||||
content = md(content).strip()
|
content = md(content).strip() + '\n'
|
||||||
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"
|
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)
|
title_slug = slugify(title)
|
||||||
if len(title_slug) < 1:
|
if len(title_slug) < 1:
|
||||||
title_slug = 'untitled'
|
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
|
filename_disambiguation = 1
|
||||||
|
|
||||||
while os.path.exists(filename_candidate):
|
while os.path.exists(filename_candidate):
|
||||||
@ -59,4 +64,3 @@ for item in items:
|
|||||||
with open(filename_candidate, 'w', encoding='utf-8') as f:
|
with open(filename_candidate, 'w', encoding='utf-8') as f:
|
||||||
f.write(frontmatter)
|
f.write(frontmatter)
|
||||||
f.write(content)
|
f.write(content)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user