Code clean-up in gen_imgs_from_mds.py

This commit is contained in:
Lucas Cimon 2018-01-31 08:42:57 +01:00
parent 97ab145521
commit f6e80ee0c0
No known key found for this signature in database
GPG Key ID: 08DA831E717571EE

View File

@ -15,7 +15,8 @@ def gen_img(img_url):
with open(img_url, 'wb') as img_file: with open(img_url, 'wb') as img_file:
img_file.write(SMALLEST[ext]) img_file.write(SMALLEST[ext])
for md_file_path in sys.argv[1:]: def enumerate_imgs(md_file_paths):
for md_file_path in md_file_paths:
with open(md_file_path) as md_file: with open(md_file_path) as md_file:
md_content = md_file.read() md_content = md_file.read()
html = markdown(md_content) html = markdown(md_content)
@ -24,14 +25,22 @@ for md_file_path in sys.argv[1:]:
img_url = img.attrib['src'] img_url = img.attrib['src']
if img_url.startswith('http'): if img_url.startswith('http'):
continue continue
gen_img(os.path.join('content', img_url)) yield os.path.join('content', img_url)
# Adding also images from "Image:" pelican metadata entries # Adding also images from "Image:" pelican metadata entries
for line in md_content.splitlines()[:6]: for line in md_content.splitlines()[:6]:
if not line.startswith('Image: '): if not line.startswith('Image: '):
continue continue
gen_img(os.path.join('content', line.replace('Image: ', '').strip())) yield os.path.join('content', line.replace('Image: ', '').strip())
if not sys.argv[1:]: if __name__ == '__main__':
if len(sys.argv) > 1:
if len(sys.argv) > 2 and sys.argv[1] == '--list':
for img_path in enumerate_imgs(sys.argv[2:]):
print(img_path)
else:
for img_path in enumerate_imgs(sys.argv[1:]):
gen_img(img_path)
else:
print('Checking that pelican plugin image_process.scale works OK on those imgs') print('Checking that pelican plugin image_process.scale works OK on those imgs')
from PIL import Image from PIL import Image
sys.path.append('../pelican-plugins/image_process/') sys.path.append('../pelican-plugins/image_process/')