def prefix_references(self, prefix): """Update references inside various XML files found in an IDML package after a call to prefix().""" # <XMLElement Self="di2i3" MarkupTag="XMLTag/article" XMLContent="u102"/> # <[Spread|Page|...] Self="ub6" FlattenerOverride="Default" # <[TextFrame|...] Self="uca" ParentStory="u102" ...> # <CharacterStyleRange AppliedCharacterStyle="CharacterStyle/$ID/[No character style]" # PointSize="10" /> for elt in self.dom.iter(): if elt.tag in self.excluded_tags_for_prefix: continue for attr in self.prefixable_attrs: if elt.get(attr): elt.set(attr, "%s%s" % (prefix, elt.get(attr))) # <idPkg:Spread src="Spreads/Spread_ub6.xml"/> # <idPkg:Story src="Stories/Story_u139.xml"/> for elt in self.dom.xpath(".//idPkg:Spread | .//idPkg:Story", namespaces={'idPkg': IdPkgNS}): if elt.get("src"): elt.set("src", prefix_content_filename(elt.get("src"), prefix, "ref")) # <Document xmlns:idPkg="http://ns.adobe.com/AdobeInDesign/idml/1.0/packaging"... # StoryList="ue4 u102 u11b u139 u9c"...> elt = self.dom.xpath("/Document") if elt and elt[0].get("StoryList"): elt[0].set("StoryList", " ".join(["%s%s" % (prefix, s) for s in elt[0].get("StoryList").split(" ")]))
def prefix(self, prefix): """Change references and filename by inserting `prefix' everywhere. files in ZipFile cannot be renamed or moved so we make a copies of them. """ if not re.match("^\w+$", prefix): raise BaseException("Prefix must be alphanumeric.") # Change the references inside the file. for filename in self.namelist(): if ( os.path.basename(filename) in ["container.xml", "metadata.xml"] or os.path.splitext(filename)[1] != ".xml" ): continue idml_xml_file = get_idml_xml_file_by_name(self, filename, self.working_copy_path) idml_xml_file.prefix_references(prefix) idml_xml_file.synchronize() # Story and Spread XML files are "prefixed". for filename in self.contentfile_namelist(): new_basename = prefix_content_filename(os.path.basename(filename), prefix, "filename") # mv file in the new archive with the prefix. old_name = os.path.join(self.working_copy_path, filename) new_name = os.path.join(os.path.dirname(old_name), new_basename) os.rename(old_name, new_name) # Update designmap.xml. self.designmap.prefix(prefix) self.designmap.synchronize() return self
def prefix(self, prefix): """Change references and filename by inserting `prefix' everywhere. files in ZipFile cannot be renamed or moved so we make a copies of them. """ if not re.match("^\w+$", prefix): raise BaseException("Prefix must be alphanumeric.") # Change the references inside the file. for filename in self.namelist(): if (os.path.basename(filename) in ["container.xml", "metadata.xml"] or os.path.splitext(filename)[1] != ".xml"): continue idml_xml_file = get_idml_xml_file_by_name(self, filename, self.working_copy_path) idml_xml_file.prefix_references(prefix) idml_xml_file.synchronize() # Story and Spread XML files are "prefixed". for filename in self.contentfile_namelist(): new_basename = prefix_content_filename(os.path.basename(filename), prefix, "filename") # mv file in the new archive with the prefix. old_name = os.path.join(self.working_copy_path, filename) new_name = os.path.join(os.path.dirname(old_name), new_basename) os.rename(old_name, new_name) # Update designmap.xml. self.designmap.prefix(prefix) self.designmap.synchronize() return self
def prefix_references(self, prefix): """Update references inside various XML files found in an IDML package after a call to prefix().""" # <XMLElement Self="di2i3" MarkupTag="XMLTag/article" XMLContent="u102"/> # <[Spread|Page|...] Self="ub6" FlattenerOverride="Default" # <[TextFrame|...] Self="uca" ParentStory="u102" ...> # <CharacterStyleRange AppliedCharacterStyle="CharacterStyle/$ID/[No character style]" # PointSize="10" /> for elt in self.dom.iter(): if elt.tag in self.excluded_tags_for_prefix: continue for attr in self.prefixable_attrs: if elt.get(attr): elt.set(attr, "%s%s" % (prefix, elt.get(attr))) # <idPkg:Spread src="Spreads/Spread_ub6.xml"/> # <idPkg:Story src="Stories/Story_u139.xml"/> for elt in self.dom.xpath(".//idPkg:Spread | .//idPkg:Story", namespaces={'idPkg': IdPkgNS}): if elt.get("src"): elt.set("src", prefix_content_filename(elt.get("src"), prefix, "ref")) # <Document xmlns:idPkg="http://ns.adobe.com/AdobeInDesign/idml/1.0/packaging"... # StoryList="ue4 u102 u11b u139 u9c"...> elt = self.dom.xpath("/Document") if elt and elt[0].get("StoryList"): elt[0].set( "StoryList", " ".join([ "%s%s" % (prefix, s) for s in elt[0].get("StoryList").split(" ") ]))
def test_prefix_content_filename(self): from simple_idml.utils import prefix_content_filename # Prefix stories and spread filename references. src = "Stories/Story_u139.xml" result = prefix_content_filename(src, "MyPrefix", "ref") self.assertEqual(result, "Stories/Story_MyPrefixu139.xml") src = "Spreads/Spread_ub6.xml" result = prefix_content_filename(src, "MyPrefix", "ref") self.assertEqual(result, "Spreads/Spread_MyPrefixub6.xml") # Prefix filenames. src = "Story_u139.xml" result = prefix_content_filename(src, "MyPrefix", "filename") self.assertEqual(result, "Story_MyPrefixu139.xml") src = "Spread_ub6.xml" result = prefix_content_filename(src, "MyPrefix", "filename") self.assertEqual(result, "Spread_MyPrefixub6.xml")