changeset 1149:be74ba54a06f

admin: Improvements to micropub endpoint's photo handling. - Add `.jpg` extension to photos that don't have any extension. - Add photos in the config section instead of the body, so that the layout can put them in separate `u-photo` tags.
author Ludovic Chabant <ludovic@chabant.com>
date Tue, 10 Jul 2018 21:03:58 -0700
parents 0aeb6f18d6a0
children 97b1b46cc156
files piecrust/admin/views/micropub.py
diffstat 1 files changed, 13 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/piecrust/admin/views/micropub.py	Tue Jul 10 21:01:45 2018 -0700
+++ b/piecrust/admin/views/micropub.py	Tue Jul 10 21:03:58 2018 -0700
@@ -98,6 +98,9 @@
     fn = re_unsafe_asset_char.sub('_', fn)
     fn = '%s_%s' % (uuid.uuid1().hex, fn)
     fn = fn.rstrip('_')
+    _, fnext = os.path.splitext(fn)
+    if not fnext:
+        fn = fn + '.jpg'
 
     photo_cache_dir = os.path.join(
         g.site.root_dir,
@@ -302,7 +305,7 @@
                 paths_to_commit.append(p_thumb_path)
 
                 p_thumb_no_ext = '%s_thumb' % p_fn_no_ext
-                photo_names.append((p_thumb_no_ext, p_fn_no_ext))
+                photo_names.append((p_fn_no_ext, p_thumb_no_ext))
             else:
                 photo_names.append((p_fn_no_ext, None))
 
@@ -354,9 +357,18 @@
     paths_to_commit.append(content_item.spec)
     with source.openItem(content_item, mode='w', encoding='utf8') as fp:
         fp.write('---\n')
+
         yaml.dump(post_config, fp,
                   default_flow_style=False,
                   allow_unicode=True)
+
+        if photo_names:
+            fp.write('photos:\n')
+            for pfull, pthumb in photo_names:
+                fp.write('- name: %s\n' % pfull)
+                if pthumb:
+                    fp.write('  thumb: %s\n' % pthumb)
+
         fp.write('---\n')
 
         if summary:
@@ -365,18 +377,6 @@
             fp.write('<!--break-->\n\n')
         fp.write(content)
 
-        if photo_names:
-            fp.write('\n\n')
-            for pthumb, pfull in photo_names:
-                if pfull:
-                    fp.write('<a href="{{assets["%s"]}}">'
-                             '<img src="{{assets["%s"]}}" alt="%s"/>'
-                             '</a>\n\n' %
-                             (pfull, pthumb, pthumb))
-                else:
-                    fp.write('<img src="{{assets["%s"]}}" alt="%s"/>\n\n' %
-                             (pthumb, pthumb))
-
         if os.supports_fd:
             import stat
             try: