Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ticket 75 clear before import xml #76

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,8 @@ You can also import XML files into your InDesign® documents. The following rule
corresponding element into the idml document **and** its children.
- A node having the attribute ``simpleidml-setcontent="delete"`` will remove the corresponding
element into the idml document (Story and Spread elements).
- A node having the attribute ``simpleidml-setcontent="clear"`` will clear the text in the
element.
- A node having the attribute ``simpleidml-setcontent="remove-previous-br"`` will remove the new-line
characters before the element.
- You can mix several flags using a comma (i.e.: ``simpleidml-setcontent="delete,remove-previous-br"``)
Expand Down Expand Up @@ -457,6 +459,11 @@ installed in your PATH.
Revisions
=========

1.2.0
-----

- Add flag ``simpleidml-setcontent="clear"`` to import XML.

1.1.8
-----

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

setup(
name="SimpleIDML",
version="1.1.8a1",
version="1.1.8a3",
license='BSD Licence',
author='Stanislas Guerra',
author_email='[email protected]',
Expand Down
2 changes: 1 addition & 1 deletion src/simple_idml/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-

VERSION = "1.1.8a1"
VERSION = "1.1.8a3"
IdPkgNS = "http://ns.adobe.com/AdobeInDesign/idml/1.0/packaging"
BACKINGSTORY = "XML/BackingStory.xml"

Expand Down
7 changes: 5 additions & 2 deletions src/simple_idml/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,9 +444,12 @@ def remove_element(self, element_id, synchronize=False):
if synchronize:
self.synchronize()

def remove_children(self, element_id, synchronize=False):
def remove_children(self, element_id, keep_style=False, synchronize=False):
elt = self.get_element_by_id(element_id).element
for child in elt.iterchildren():

for i, child in enumerate(elt.iterchildren()):
if i == 0 and keep_style and child.tag in ['ParagraphStyleRange', 'CharacterStyleRange']:
continue
elt.remove(child)
if synchronize:
self.synchronize()
Expand Down
41 changes: 39 additions & 2 deletions src/simple_idml/idml.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,14 @@ def append_childs(source_node, destination_node):
if elt.get("XMLContent"):
xml_content_value = elt.get("XMLContent")
story_name = f"Stories/Story_{xml_content_value}.xml"
story = Story(self, name=story_name)
story = Story(self, name=story_name, working_copy_path=self.working_copy_path)
try:
new_source_node = story.get_element_by_id(elt.get("Self"))
# The story does not exists.
# The story does not exists (i.e. for an image).
except KeyError:
continue
except FileNotFoundError:
continue
else:
append_childs(new_source_node, new_destination_node)
else:
Expand Down Expand Up @@ -454,9 +456,44 @@ def _import_node(source_node, at=None, element_id=None, story=None, ignoreconten

_move_siblings_content(at, element_id)

self._clear_destination(source_node, at)
self.init_lazy_references()
_import_node(source_node, at)
return self

def _clear_destination(self, source_node, at):
""" Remove content marked for removal before importing XML. """

element_id = self.xml_structure.xpath(at)[0].get("Self")
items = dict(source_node.items())

content_flags = items.get(SETCONTENT_TAG, "").split(',')
if "clear" in content_flags:
story = self.get_story_object_by_xpath(at)
story.remove_children(element_id, keep_style=True, synchronize=True)
self._xml_structure.xpath(at)[0].clear()

source_node_children = source_node.getchildren()
if len(source_node_children):
source_node_children_tags = [n.tag for n in source_node_children]
destination_node = self.xml_structure.xpath(at)[0]
destination_node_children = destination_node.iterchildren()
destination_node_children_tags = [n.tag for n in destination_node.iterchildren()]

if destination_node_children_tags == source_node_children_tags:
for s, d in zip(source_node_children,
[self.xml_structure_tree.getpath(c) for c in destination_node_children]):
self._clear_destination(s, at=d)

# Step-by-step iteration.
else:
destination_node_child = next(destination_node_children, None)
for source_child in source_node_children:
if destination_node_child is not None and source_child.tag == destination_node_child.tag:
self._clear_destination(source_child,
at=self.xml_structure_tree.getpath(destination_node_child))
destination_node_child = next(destination_node_children, None)

@use_working_copy
def import_pdf(self, pdf_path, at, crop="CropContentVisibleLayers", page_number=1):
self.set_attributes(at, {'href': f'{pdf_path}'})
Expand Down
Binary file added tests/regressiontests/IDML/interview.idml
Binary file not shown.
22 changes: 22 additions & 0 deletions tests/regressiontests/XML/interview_en.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<dossier template="PR_206001.idml" reference="PR_206001_EN" standalone="1" support="PLF" parution="206" cahier="publi_redac">
<module>
<annonce simpleidml-setcontent="false">
<accroche simpleidml-setcontent="false">
<titre>Pyramid Cabinet</titre>
<sous_titre>Where people met goods</sous_titre>
</accroche>
<article simpleidml-setcontent="false">
<question simpleidml-setcontent="clear"><black>Guerra S.</black> Tell us about the history and added value of your agency</question>
<reponse simpleidml-setcontent="clear"><bold>Henri D.</bold> Our cabinet has been established by my great-great-grand father in the 90's.</reponse><br />
<question simpleidml-setcontent="clear">Which areas do you cover?</question>
<reponse simpleidml-setcontent="clear"><bold>H.D.</bold> South east of France.</reponse>
</article>
<article simpleidml-setcontent="false">
<question>Any tactical advantage over your pairs?</question>
<reponse simpleidml-setcontent="clear">Our dedication is top-tier.</reponse>
</article>
<exergue>« Please, contact us! »</exergue>
<nom>Henri Dupond</nom>
</annonce>
</module>
</dossier>
27 changes: 27 additions & 0 deletions tests/regressiontests/idml.py
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,33 @@ def test_import_xml_force_content3(self):
</Story>
</module>
</Root>
""")

def test_import_xml_clear_content(self):
"""Example of an article to be translated using an existing composition. """
shutil.copy2(os.path.join(IDMLFILES_DIR, "interview.idml"),
os.path.join(OUTPUT_DIR, "interview_origin.idml"))
with IDMLPackage(os.path.join(OUTPUT_DIR, "interview_origin.idml")) as idml_file,\
open(os.path.join(XML_DIR, "interview_en.xml"), "r") as xml_file:
with idml_file.import_xml(xml_file.read(), at="/Root/dossier") as f:
xml = f.export_xml()
self.assertXMLEqual(xml,
"""<Root>
<dossier template="PR_206001.idml" reference="PR_206001_EN" support="PLF" parution="206" cahier="publi_redac" standalone="1">
<module>
<annonce simpleidml-setcontent="false">
<accroche simpleidml-setcontent="false"><titre>Pyramid Cabinet</titre> <sous_titre>Where people met goods</sous_titre></accroche>
<article simpleidml-setcontent="false"><question simpleidml-setcontent="clear"><black>Guerra S.</black> Tell us about the history and added value of your agency</question><reponse simpleidml-setcontent="clear"><bold>Henri D.</bold> Our cabinet has been established by my great-great-grand father in the 90's.</reponse>&#8232;&#8232;<question simpleidml-setcontent="clear">Which areas do you cover?</question><reponse simpleidml-setcontent="clear"><bold>H.D.</bold> South east of France.</reponse></article>
<article simpleidml-setcontent="false">
<question>Any tactical advantage over your pairs?</question>
<reponse simpleidml-setcontent="clear">Our dedication is top-tier.</reponse>
</article>
<exergue>&#171; Please, contact us! &#187;</exergue>
<nom>Henri Dupond</nom>
</annonce>
</module>
</dossier>
</Root>
""")

def test_import_pdf(self):
Expand Down
2 changes: 2 additions & 0 deletions tests/runtests.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env python

import os
import sys
import unittest
Expand Down
Loading