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,30 +15,39 @@ 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):
with open(md_file_path) as md_file: for md_file_path in md_file_paths:
md_content = md_file.read() with open(md_file_path) as md_file:
html = markdown(md_content) md_content = md_file.read()
doc_root = html5lib.parse(html) html = markdown(md_content)
for img in doc_root.iter('{http://www.w3.org/1999/xhtml}img'): doc_root = html5lib.parse(html)
img_url = img.attrib['src'] for img in doc_root.iter('{http://www.w3.org/1999/xhtml}img'):
if img_url.startswith('http'): img_url = img.attrib['src']
continue if img_url.startswith('http'):
gen_img(os.path.join('content', img_url)) continue
# Adding also images from "Image:" pelican metadata entries yield os.path.join('content', img_url)
for line in md_content.splitlines()[:6]: # Adding also images from "Image:" pelican metadata entries
if not line.startswith('Image: '): for line in md_content.splitlines()[:6]:
continue if not line.startswith('Image: '):
gen_img(os.path.join('content', line.replace('Image: ', '').strip())) continue
yield os.path.join('content', line.replace('Image: ', '').strip())
if not sys.argv[1:]: if __name__ == '__main__':
print('Checking that pelican plugin image_process.scale works OK on those imgs') if len(sys.argv) > 1:
from PIL import Image if len(sys.argv) > 2 and sys.argv[1] == '--list':
sys.path.append('../pelican-plugins/image_process/') for img_path in enumerate_imgs(sys.argv[2:]):
from image_process import scale print(img_path)
for ext, content in sorted(SMALLEST.items()): else:
print('- Testing {} img'.format(ext)) for img_path in enumerate_imgs(sys.argv[1:]):
small_img_filename = 'img.{}'.format(ext) gen_img(img_path)
with open(small_img_filename, 'wb') as small_img: else:
small_img.write(content) print('Checking that pelican plugin image_process.scale works OK on those imgs')
scale(Image.open(small_img_filename), '300', '300', False, False) # raise an exception if img.getbbox() returns None from PIL import Image
sys.path.append('../pelican-plugins/image_process/')
from image_process import scale
for ext, content in sorted(SMALLEST.items()):
print('- Testing {} img'.format(ext))
small_img_filename = 'img.{}'.format(ext)
with open(small_img_filename, 'wb') as small_img:
small_img.write(content)
scale(Image.open(small_img_filename), '300', '300', False, False) # raise an exception if img.getbbox() returns None