Move posts to respective folder
|
Before Width: | Height: | Size: 9.4 MiB After Width: | Height: | Size: 9.4 MiB |
|
Before Width: | Height: | Size: 9.4 MiB After Width: | Height: | Size: 9.4 MiB |
|
Before Width: | Height: | Size: 8.7 MiB After Width: | Height: | Size: 8.7 MiB |
|
Before Width: | Height: | Size: 1.6 MiB After Width: | Height: | Size: 1.6 MiB |
|
Before Width: | Height: | Size: 1.6 MiB After Width: | Height: | Size: 1.6 MiB |
|
Before Width: | Height: | Size: 3.5 MiB After Width: | Height: | Size: 3.5 MiB |
|
Before Width: | Height: | Size: 2.6 MiB After Width: | Height: | Size: 2.6 MiB |
|
Before Width: | Height: | Size: 2.0 MiB After Width: | Height: | Size: 2.0 MiB |
|
Before Width: | Height: | Size: 4.9 MiB After Width: | Height: | Size: 4.9 MiB |
|
Before Width: | Height: | Size: 4.9 MiB After Width: | Height: | Size: 4.9 MiB |
|
Before Width: | Height: | Size: 4.8 MiB After Width: | Height: | Size: 4.8 MiB |
|
Before Width: | Height: | Size: 6.4 MiB After Width: | Height: | Size: 6.4 MiB |
|
Before Width: | Height: | Size: 6.4 MiB After Width: | Height: | Size: 6.4 MiB |
|
Before Width: | Height: | Size: 6.1 MiB After Width: | Height: | Size: 6.1 MiB |
|
Before Width: | Height: | Size: 4.9 MiB After Width: | Height: | Size: 4.9 MiB |
|
Before Width: | Height: | Size: 6.3 MiB After Width: | Height: | Size: 6.3 MiB |
|
Before Width: | Height: | Size: 6.1 MiB After Width: | Height: | Size: 6.1 MiB |
|
Before Width: | Height: | Size: 6.5 MiB After Width: | Height: | Size: 6.5 MiB |
|
Before Width: | Height: | Size: 3.7 MiB After Width: | Height: | Size: 3.7 MiB |
|
Before Width: | Height: | Size: 4.2 MiB After Width: | Height: | Size: 4.2 MiB |
|
Before Width: | Height: | Size: 4.1 MiB After Width: | Height: | Size: 4.1 MiB |
|
Before Width: | Height: | Size: 3.7 MiB After Width: | Height: | Size: 3.7 MiB |
|
Before Width: | Height: | Size: 39 KiB After Width: | Height: | Size: 39 KiB |
|
Before Width: | Height: | Size: 8.6 MiB After Width: | Height: | Size: 8.6 MiB |
|
Before Width: | Height: | Size: 8.6 MiB After Width: | Height: | Size: 8.6 MiB |
|
Before Width: | Height: | Size: 8.8 MiB After Width: | Height: | Size: 8.8 MiB |
|
Before Width: | Height: | Size: 6.9 MiB After Width: | Height: | Size: 6.9 MiB |
|
Before Width: | Height: | Size: 6.5 MiB After Width: | Height: | Size: 6.5 MiB |
|
Before Width: | Height: | Size: 6.7 MiB After Width: | Height: | Size: 6.7 MiB |
|
Before Width: | Height: | Size: 6.5 MiB After Width: | Height: | Size: 6.5 MiB |
|
Before Width: | Height: | Size: 5.7 MiB After Width: | Height: | Size: 5.7 MiB |
|
Before Width: | Height: | Size: 9.0 MiB After Width: | Height: | Size: 9.0 MiB |
|
Before Width: | Height: | Size: 7.2 MiB After Width: | Height: | Size: 7.2 MiB |
|
Before Width: | Height: | Size: 7.4 MiB After Width: | Height: | Size: 7.4 MiB |
|
Before Width: | Height: | Size: 8.2 MiB After Width: | Height: | Size: 8.2 MiB |
|
Before Width: | Height: | Size: 9.0 MiB After Width: | Height: | Size: 9.0 MiB |
|
Before Width: | Height: | Size: 7.9 MiB After Width: | Height: | Size: 7.9 MiB |
|
Before Width: | Height: | Size: 10 MiB After Width: | Height: | Size: 10 MiB |
|
Before Width: | Height: | Size: 9.3 MiB After Width: | Height: | Size: 9.3 MiB |
|
Before Width: | Height: | Size: 7.9 MiB After Width: | Height: | Size: 7.9 MiB |
|
Before Width: | Height: | Size: 9.2 MiB After Width: | Height: | Size: 9.2 MiB |
|
Before Width: | Height: | Size: 10 MiB After Width: | Height: | Size: 10 MiB |
|
Before Width: | Height: | Size: 9.6 MiB After Width: | Height: | Size: 9.6 MiB |
|
Before Width: | Height: | Size: 7.4 MiB After Width: | Height: | Size: 7.4 MiB |
|
Before Width: | Height: | Size: 7.4 MiB After Width: | Height: | Size: 7.4 MiB |
|
Before Width: | Height: | Size: 9.3 MiB After Width: | Height: | Size: 9.3 MiB |
|
Before Width: | Height: | Size: 10 MiB After Width: | Height: | Size: 10 MiB |
|
Before Width: | Height: | Size: 11 MiB After Width: | Height: | Size: 11 MiB |
|
Before Width: | Height: | Size: 8.9 MiB After Width: | Height: | Size: 8.9 MiB |
|
Before Width: | Height: | Size: 5.5 MiB After Width: | Height: | Size: 5.5 MiB |
|
Before Width: | Height: | Size: 5.5 MiB After Width: | Height: | Size: 5.5 MiB |
|
Before Width: | Height: | Size: 4.9 MiB After Width: | Height: | Size: 4.9 MiB |
|
Before Width: | Height: | Size: 7.4 MiB After Width: | Height: | Size: 7.4 MiB |
122
newpost.py
@@ -1,88 +1,92 @@
|
|||||||
#!python
|
#!python
|
||||||
|
|
||||||
|
import os
|
||||||
import argparse
|
import argparse
|
||||||
import os, sys
|
|
||||||
import datetime
|
import datetime
|
||||||
import traceback
|
import traceback
|
||||||
|
|
||||||
|
|
||||||
DEFAULT_FRONT_MATTER = {
|
DEFAULT_FRONT_MATTER = {
|
||||||
'slug': '',
|
'slug': '',
|
||||||
'title': '',
|
'title': '',
|
||||||
'description': '',
|
'description': '',
|
||||||
'toc': True,
|
'toc': True,
|
||||||
'authors': [],
|
'authors': [],
|
||||||
'tags': [],
|
'tags': [],
|
||||||
'categories': [],
|
'categories': [],
|
||||||
'series': [],
|
'series': [],
|
||||||
'date': '',
|
'date': '',
|
||||||
'lastmod': '',
|
'lastmod': '',
|
||||||
'featuredVideo': '',
|
'featuredVideo': '',
|
||||||
'featuredImage': '',
|
'featuredImage': '',
|
||||||
'draft': False,
|
'draft': False,
|
||||||
'enableComment': True
|
'enableComment': True
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def standard_time(time_str):
|
def standard_time(time_str):
|
||||||
dt = datetime.datetime.strptime(time_str, "%Y-%m-%d")
|
dt = datetime.datetime.strptime(time_str, "%Y-%m-%d")
|
||||||
return dt.isoformat()
|
return dt.isoformat()
|
||||||
|
|
||||||
|
|
||||||
|
def get_year(time_str):
|
||||||
|
dt = datetime.datetime.strptime(time_str, "%Y-%m-%d")
|
||||||
|
return dt.year
|
||||||
|
|
||||||
|
|
||||||
def create_post(filepath, args):
|
def create_post(filepath, args):
|
||||||
with open(filepath, 'w') as f:
|
with open(filepath, 'w') as f:
|
||||||
f.write('---\n');
|
f.write('---\n')
|
||||||
|
|
||||||
for key in DEFAULT_FRONT_MATTER.keys():
|
for key in DEFAULT_FRONT_MATTER.keys():
|
||||||
if key in ('title', 'slug'):
|
if key in ('title', 'slug'):
|
||||||
f.write(f'{key}: {args.posttitle}\n')
|
f.write(f'{key}: {args.posttitle}\n')
|
||||||
elif key in ('date', 'lastmod'):
|
elif key in ('date', 'lastmod'):
|
||||||
if args.postdate is not None:
|
if args.postdate is not None:
|
||||||
f.write(f'{key}: {standard_time(args.postdate)}\n')
|
f.write(f'{key}: {standard_time(args.postdate)}\n')
|
||||||
else:
|
else:
|
||||||
f.write(f'{key}: {datetime.datetime.now().isoformat()}\n')
|
f.write(f'{key}: {datetime.datetime.now().isoformat()}\n')
|
||||||
else:
|
else:
|
||||||
if type(DEFAULT_FRONT_MATTER[key]) is str:
|
if type(DEFAULT_FRONT_MATTER[key]) is str:
|
||||||
if DEFAULT_FRONT_MATTER[key] == '':
|
if DEFAULT_FRONT_MATTER[key] == '':
|
||||||
f.write(f'{key}: \n')
|
f.write(f'{key}: \n')
|
||||||
else:
|
else:
|
||||||
f.write(f'{key}: {DEFAULT_FRONT_MATTER[key]}\n')
|
f.write(f'{key}: {DEFAULT_FRONT_MATTER[key]}\n')
|
||||||
elif type(DEFAULT_FRONT_MATTER[key]) is bool:
|
elif type(DEFAULT_FRONT_MATTER[key]) is bool:
|
||||||
f.write(f'{key}: {"true" if DEFAULT_FRONT_MATTER[key] else "false"}\n')
|
f.write(f'{key}: {"true" if DEFAULT_FRONT_MATTER[key] else "false"}\n')
|
||||||
else:
|
else:
|
||||||
f.write(f'{key}: {DEFAULT_FRONT_MATTER[key]}\n')
|
f.write(f'{key}: {DEFAULT_FRONT_MATTER[key]}\n')
|
||||||
|
|
||||||
f.write('---\n\n');
|
f.write('---\n\n')
|
||||||
|
|
||||||
|
|
||||||
def mkdir(dirpath):
|
def mkdir(dirpath):
|
||||||
if os.path.isdir(dirpath) is False:
|
if os.path.isdir(dirpath) is False:
|
||||||
os.mkdir(dirpath)
|
os.makedirs(dirpath)
|
||||||
|
|
||||||
|
|
||||||
def main(args):
|
def main(args):
|
||||||
if args.posttitle is None:
|
if args.posttitle is None:
|
||||||
print("Need a title!")
|
print("Need a title!")
|
||||||
return
|
return
|
||||||
|
|
||||||
os.chdir('content/posts')
|
|
||||||
try:
|
|
||||||
mkdir(args.posttitle)
|
|
||||||
mkdir(os.path.join(args.posttitle, 'images'))
|
|
||||||
except:
|
|
||||||
print(traceback.format_exc())
|
|
||||||
print(f"Cannot create folder: {args.posttitle}")
|
|
||||||
|
|
||||||
os.chdir(args.posttitle)
|
|
||||||
create_post('index.md', args)
|
|
||||||
|
|
||||||
|
os.chdir('content/posts')
|
||||||
|
dir_name = os.path.join(str(get_year(args.postdate)), args.posttitle)
|
||||||
|
try:
|
||||||
|
mkdir(dir_name)
|
||||||
|
mkdir(os.path.join(dir_name, 'images'))
|
||||||
|
|
||||||
|
os.chdir(dir_name)
|
||||||
|
create_post('index.md', args)
|
||||||
|
except:
|
||||||
|
print(traceback.format_exc())
|
||||||
|
print(f"Cannot create folder: {dir_name}")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument("-t", "--posttitle", default=None, help="The title of post.")
|
parser.add_argument("-t", "--posttitle", default=None, help="The title of post.")
|
||||||
parser.add_argument("-d", "--postdate", default=None, help="The date of post.")
|
parser.add_argument("-d", "--postdate", default=datetime.datetime.now().strftime('%Y-%m-%d'), help="The date of post.")
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
main(args)
|
main(args)
|
||||||
|
|||||||