thorstenb / odpdown

Generate OpenDocument Presentation (odp) files from markdown
Other
107 stars 18 forks source link

Fix for concatenation, allows duplicate slides and images #41

Closed th0ma5w closed 7 years ago

th0ma5w commented 7 years ago

The diff below to the main script allows for several things:

Cons

This may not be ideal, but helped me with making presentations using multiple different input files and then reassembling them later, and then additionally with reuse of slides and images.

diff --git a/odpdown.py b/odpdown.py
index eec4ed9..e67f6cc 100755
--- a/odpdown.py
+++ b/odpdown.py
@@ -49,6 +49,9 @@ import re
 from urllib import urlopen
 from mimetypes import guess_type

+from uuid import uuid4
+hasher = lambda : uuid4().get_hex()
+
 from lpod import ODF_MANIFEST, ODF_STYLES
 from lpod.document import odf_get_document
 from lpod.frame import odf_create_text_frame, odf_create_image_frame, odf_frame
@@ -391,7 +394,6 @@ class ODFFormatter(Formatter):

 class ODFRenderer(mistune.Renderer):
     """Render mistune event stream as ODF"""
-    image_prefix = 'odpdown_image_'

     def __init__(self,
                  document,
@@ -410,12 +412,6 @@ class ODFRenderer(mistune.Renderer):
         self.formatter = ODFFormatter(style=highlight_style)
         self.document = document
         self.doc_manifest = document.get_part(ODF_MANIFEST)
-        # make sure nested odpdown calls don't end up writing
-        # similarly-named images
-        self.image_entry_id = len([path for path in
-                                   self.doc_manifest.get_paths()
-                                   if path.startswith(
-                                       ODFRenderer.image_prefix)])
         self.break_master = 'Default' if break_master is None else break_master
         self.breakheader_size = ((u'20cm', u'3cm') if breakheader_size is None
                                  else breakheader_size)
@@ -547,7 +543,7 @@ class ODFRenderer(mistune.Renderer):
         if level == 1:
             page = odf_create_draw_page(
                 'page1',
-                name=''.join(e.get_formatted_text() for e in text.get()),
+                name=hasher(),
                 master_page=self.break_master,
                 presentation_page_layout=u'AL3T19')
             page.append(
@@ -562,7 +558,7 @@ class ODFRenderer(mistune.Renderer):
         elif level == 2:
             page = odf_create_draw_page(
                 'page1',
-                name=''.join(e.get_formatted_text() for e in text.get()),
+                name=hasher(),
                 master_page=self.content_master,
                 presentation_page_layout=u'AL3T1')
             page.append(
@@ -679,8 +675,8 @@ class ODFRenderer(mistune.Renderer):
         # embed picture - TODO: optionally just link it
         media_type = guess_type(src)
         fragment_ext = urlparse.urlparse(src)[2].split('.')[-1]
-        fragment_name = 'Pictures/%s%d.%s' % (ODFRenderer.image_prefix,
-                                              self.image_entry_id,
+        self.image_entry_id = hasher()
+        fragment_name = 'Pictures/%s.%s' % (self.image_entry_id,
                                               fragment_ext)
         imagedata = urlopen(src).read()
         try:
@@ -735,7 +731,6 @@ class ODFRenderer(mistune.Renderer):
                                         media_type[0])
         self.document.set_part(fragment_name,
                                imagedata)
-        self.image_entry_id += 1
thorstenb commented 7 years ago

Ah nice! Can you send a pull request please, that significantly improves my workflow?