@@ -553,10 +553,12 @@ def transform_multiple_newlines(self, content):
553
553
else :
554
554
return content
555
555
556
- def transform_content (self , content , post_format ):
556
+ def transform_content (self , content , post_format , attachments ):
557
557
if post_format == 'wp' :
558
558
if self .transform_to_html :
559
559
additional_data = {}
560
+ if attachments is not None :
561
+ additional_data ['attachments' ] = attachments
560
562
try :
561
563
content = self .wordpress_page_compiler .compile_to_string (content , additional_data = additional_data )
562
564
except TypeError : # old versions of the plugin don't support the additional argument
@@ -651,7 +653,7 @@ def _create_metadata(self, status, excerpt, tags, categories, post_name=None):
651
653
tags_cats = tags + categories
652
654
return tags_cats , other_meta
653
655
654
- def import_item (self , item , wordpress_namespace , out_folder = None ):
656
+ def import_postpage_item (self , item , wordpress_namespace , out_folder = None , attachments = None ):
655
657
"""Takes an item from the feed and creates a post file."""
656
658
if out_folder is None :
657
659
out_folder = 'posts'
@@ -771,7 +773,7 @@ def import_item(self, item, wordpress_namespace, out_folder=None):
771
773
default_language = self .context ["DEFAULT_LANG" ]
772
774
for lang , content in content_translations .items ():
773
775
try :
774
- content , extension , rewrite_html = self .transform_content (content , post_format )
776
+ content , extension , rewrite_html = self .transform_content (content , post_format , attachments )
775
777
except :
776
778
LOGGER .error (('Cannot interpret post "{0}" (language {1}) with post ' +
777
779
'format {2}!' ).format (os .path .join (out_folder , slug ), lang , post_format ))
@@ -818,7 +820,7 @@ def import_item(self, item, wordpress_namespace, out_folder=None):
818
820
' no content.' ).format (title ))
819
821
return False
820
822
821
- def process_item (self , item ):
823
+ def _extract_item_info (self , item ):
822
824
# The namespace usually is something like:
823
825
# http://wordpress.org/export/1.2/
824
826
wordpress_namespace = item .nsmap ['wp' ]
@@ -828,6 +830,10 @@ def process_item(self, item):
828
830
item , '{{{0}}}post_id' .format (wordpress_namespace ), "0" ))
829
831
parent_id = get_text_tag (
830
832
item , '{{{0}}}post_parent' .format (wordpress_namespace ), None )
833
+ return wordpress_namespace , post_type , post_id , parent_id
834
+
835
+ def process_item_if_attachment (self , item ):
836
+ wordpress_namespace , post_type , post_id , parent_id = self ._extract_item_info (item )
831
837
832
838
if post_type == 'attachment' :
833
839
files = self .import_attachment (item , wordpress_namespace )
@@ -836,34 +842,42 @@ def process_item(self, item):
836
842
self .attachments [int (parent_id )][post_id ] = files
837
843
else :
838
844
LOGGER .warn ("Attachment #{0} ({1}) has no parent!" .format (post_id , files ))
839
- else :
845
+
846
+ def process_item_if_post_or_page (self , item ):
847
+ wordpress_namespace , post_type , post_id , parent_id = self ._extract_item_info (item )
848
+
849
+ if post_type != 'attachment' :
850
+ # Get attachments for post
851
+ attachments = self .attachments .pop (post_id , None )
852
+ # Import item
840
853
if post_type == 'post' :
841
- out_folder_slug = self .import_item (item , wordpress_namespace , 'posts' )
854
+ out_folder_slug = self .import_postpage_item (item , wordpress_namespace , 'posts' , attachments )
842
855
else :
843
- post_type = 'page'
844
- out_folder_slug = self .import_item (item , wordpress_namespace , 'stories' )
845
- # If post was exported, store data
846
- if out_folder_slug :
847
- self .posts_pages [post_id ] = (post_type , out_folder_slug [0 ], out_folder_slug [1 ])
856
+ out_folder_slug = self .import_postpage_item (item , wordpress_namespace , 'stories' , attachments )
857
+ # Process attachment data
858
+ if attachments is not None :
859
+ # If post was exported, store data
860
+ if out_folder_slug :
861
+ destination = os .path .join (self .output_folder , out_folder_slug [0 ],
862
+ out_folder_slug [1 ] + ".attachments.json" )
863
+ self .write_attachments_info (destination , attachments )
848
864
849
865
def write_attachments_info (self , path , attachments ):
850
866
with io .open (path , "wb" ) as file :
851
867
file .write (json .dumps (attachments ).encode ('utf-8' ))
852
868
853
869
def import_posts (self , channel ):
854
- self .posts_pages = {}
855
870
self .attachments = defaultdict (dict )
871
+ # First process attachments
872
+ for item in channel .findall ('item' ):
873
+ self .process_item_if_attachment (item )
874
+ # Next process posts
856
875
for item in channel .findall ('item' ):
857
- self .process_item (item )
876
+ self .process_item_if_post_or_page (item )
858
877
# Assign attachments to posts
859
878
for post_id in self .attachments :
860
- if post_id in self .posts_pages :
861
- destination = os .path .join (self .output_folder , self .posts_pages [post_id ][1 ],
862
- self .posts_pages [post_id ][2 ] + ".attachments.json" )
863
- self .write_attachments_info (destination , self .attachments [post_id ])
864
- else :
865
- LOGGER .warn (("Found attachments for post or page #{0}, but didn't find post or page. " +
866
- "(Attachments: {1})" ).format (post_id , [e [0 ] for _ , e in self .attachments [post_id ].items ()]))
879
+ LOGGER .warn (("Found attachments for post or page #{0}, but didn't find post or page. " +
880
+ "(Attachments: {1})" ).format (post_id , [e [0 ] for _ , e in self .attachments [post_id ].items ()]))
867
881
868
882
869
883
def get_text_tag (tag , name , default ):
0 commit comments