Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit d36ced3

Browse files
committedJul 12, 2015
Also supplying attachment info to WordPress page compiler when directly converting to HTML.
1 parent a2bf0a2 commit d36ced3

File tree

1 file changed

+34
-20
lines changed

1 file changed

+34
-20
lines changed
 

‎nikola/plugins/command/import_wordpress.py

+34-20
Original file line numberDiff line numberDiff line change
@@ -553,10 +553,12 @@ def transform_multiple_newlines(self, content):
553553
else:
554554
return content
555555

556-
def transform_content(self, content, post_format):
556+
def transform_content(self, content, post_format, attachments):
557557
if post_format == 'wp':
558558
if self.transform_to_html:
559559
additional_data = {}
560+
if attachments is not None:
561+
additional_data['attachments'] = attachments
560562
try:
561563
content = self.wordpress_page_compiler.compile_to_string(content, additional_data=additional_data)
562564
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):
651653
tags_cats = tags + categories
652654
return tags_cats, other_meta
653655

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):
655657
"""Takes an item from the feed and creates a post file."""
656658
if out_folder is None:
657659
out_folder = 'posts'
@@ -771,7 +773,7 @@ def import_item(self, item, wordpress_namespace, out_folder=None):
771773
default_language = self.context["DEFAULT_LANG"]
772774
for lang, content in content_translations.items():
773775
try:
774-
content, extension, rewrite_html = self.transform_content(content, post_format)
776+
content, extension, rewrite_html = self.transform_content(content, post_format, attachments)
775777
except:
776778
LOGGER.error(('Cannot interpret post "{0}" (language {1}) with post ' +
777779
'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):
818820
' no content.').format(title))
819821
return False
820822

821-
def process_item(self, item):
823+
def _extract_item_info(self, item):
822824
# The namespace usually is something like:
823825
# http://wordpress.org/export/1.2/
824826
wordpress_namespace = item.nsmap['wp']
@@ -828,6 +830,10 @@ def process_item(self, item):
828830
item, '{{{0}}}post_id'.format(wordpress_namespace), "0"))
829831
parent_id = get_text_tag(
830832
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)
831837

832838
if post_type == 'attachment':
833839
files = self.import_attachment(item, wordpress_namespace)
@@ -836,34 +842,42 @@ def process_item(self, item):
836842
self.attachments[int(parent_id)][post_id] = files
837843
else:
838844
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
840853
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)
842855
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)
848864

849865
def write_attachments_info(self, path, attachments):
850866
with io.open(path, "wb") as file:
851867
file.write(json.dumps(attachments).encode('utf-8'))
852868

853869
def import_posts(self, channel):
854-
self.posts_pages = {}
855870
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
856875
for item in channel.findall('item'):
857-
self.process_item(item)
876+
self.process_item_if_post_or_page(item)
858877
# Assign attachments to posts
859878
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()]))
867881

868882

869883
def get_text_tag(tag, name, default):

0 commit comments

Comments
 (0)
Please sign in to comment.