Add a slug argument to the new-photo-post script
This commit is contained in:
parent
2501cac13a
commit
29c402c3ec
1 changed files with 30 additions and 2 deletions
|
@ -8,6 +8,7 @@ New script.
|
||||||
import argparse
|
import argparse
|
||||||
import datetime
|
import datetime
|
||||||
import os.path
|
import os.path
|
||||||
|
import re
|
||||||
import shutil
|
import shutil
|
||||||
import subprocess
|
import subprocess
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
|
@ -15,10 +16,14 @@ from PIL.ExifTags import TAGS
|
||||||
|
|
||||||
PHOTOS_CONTENT_DIR = 'content/photos'
|
PHOTOS_CONTENT_DIR = 'content/photos'
|
||||||
|
|
||||||
|
def slugify(s):
|
||||||
|
return re.sub(r'\s+', '-', s.strip().lower())
|
||||||
|
|
||||||
def parse_args(argv, *a, **kw):
|
def parse_args(argv, *a, **kw):
|
||||||
parser = argparse.ArgumentParser(*a, **kw)
|
parser = argparse.ArgumentParser(*a, **kw)
|
||||||
parser.add_argument('-n', '--dry-run', action='store_true')
|
parser.add_argument('-n', '--dry-run', action='store_true')
|
||||||
parser.add_argument('-t', '--title', type=str)
|
parser.add_argument('-t', '--title')
|
||||||
|
parser.add_argument('-s', '--slug')
|
||||||
parser.add_argument('photos', nargs='+')
|
parser.add_argument('photos', nargs='+')
|
||||||
args = parser.parse_args(argv)
|
args = parser.parse_args(argv)
|
||||||
return args
|
return args
|
||||||
|
@ -47,7 +52,12 @@ def main(argv):
|
||||||
year = earliest_exif_date.year
|
year = earliest_exif_date.year
|
||||||
month = earliest_exif_date.month
|
month = earliest_exif_date.month
|
||||||
|
|
||||||
name = args.title if args.title else os.path.splitext(os.path.basename(photo))[0]
|
if args.slug:
|
||||||
|
name = args.slug
|
||||||
|
elif args.title:
|
||||||
|
name = slugify(args.title)
|
||||||
|
else:
|
||||||
|
name = os.path.splitext(os.path.basename(photo))[0]
|
||||||
|
|
||||||
post_path_year = os.path.join(PHOTOS_CONTENT_DIR, f'{year:04}', name)
|
post_path_year = os.path.join(PHOTOS_CONTENT_DIR, f'{year:04}', name)
|
||||||
post_path_year_month = os.path.join(PHOTOS_CONTENT_DIR, f'{year:04}', f'{month:02}', name)
|
post_path_year_month = os.path.join(PHOTOS_CONTENT_DIR, f'{year:04}', f'{month:02}', name)
|
||||||
|
@ -74,6 +84,24 @@ def main(argv):
|
||||||
print(f'Failed to create new Hugo post', file=sys.stderr)
|
print(f'Failed to create new Hugo post', file=sys.stderr)
|
||||||
return -1
|
return -1
|
||||||
|
|
||||||
|
if args.title:
|
||||||
|
# The hugo command can't set a title for a post so I need to do it myself.
|
||||||
|
index_file_path = os.path.join(post_path, 'index.md')
|
||||||
|
with open(index_file_path) as index_file:
|
||||||
|
index_file_contents = index_file.readlines()
|
||||||
|
|
||||||
|
for i in range(len(index_file_contents)):
|
||||||
|
line = index_file_contents[i]
|
||||||
|
|
||||||
|
if not line.startswith('title:'):
|
||||||
|
continue
|
||||||
|
|
||||||
|
updated_line = f'title: "{args.title}"\n'
|
||||||
|
index_file_contents[i] = updated_line
|
||||||
|
|
||||||
|
with open(index_file_path, 'w') as index_file:
|
||||||
|
index_file.writelines(index_file_contents)
|
||||||
|
|
||||||
for photo in args.photos:
|
for photo in args.photos:
|
||||||
print(f'Copy {photo} -> {post_path}')
|
print(f'Copy {photo} -> {post_path}')
|
||||||
try:
|
try:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue