MCPcopy Create free account
hub / github.com/Sigil-Ebook/Sigil / performOPFSourceUpdates

Function performOPFSourceUpdates

src/Resource_Files/python3lib/xmlprocessor.py:263–289  ·  view source on GitHub ↗
(data, newbkpath, oldbkpath, keylist, valuelist)

Source from the content-addressed store, hash-verified

261
262
263def performOPFSourceUpdates(data, newbkpath, oldbkpath, keylist, valuelist):
264 data = _remove_xml_header(data)
265 # lxml on a Mac does not seem to handle full unicode properly, so encode as utf-8
266 data = data.encode('utf-8')
267 # rebuild serialized lookup dictionary
268 updates = OrderedDict()
269 for i in range(0, len(keylist)):
270 updates[ keylist[i] ] = valuelist[i]
271 xmlbuilder = LXMLTreeBuilderForXML(parser=None, empty_element_tags=ebook_xml_empty_tags)
272 soup = BeautifulSoup(data, features=None, from_encoding="utf-8", builder=xmlbuilder)
273 for tag in soup.find_all(["link","item","reference","site"]):
274 if "href" in tag.attrs :
275 href = tag["href"]
276 if href.find(":") == -1 :
277 parts = href.split('#')
278 apath = urldecodepart(parts[0])
279 fragment = ""
280 if len(parts) > 1:
281 fragment = urldecodepart(parts[1])
282 oldtarget = buildBookPath(apath, startingDir(oldbkpath))
283 newtarget = updates.get(oldtarget, oldtarget)
284 attribute_value = urlencodepart(buildRelativePath(newbkpath, newtarget))
285 if fragment != "":
286 attribute_value = attribute_value + "#" + urlencodepart(fragment)
287 tag["href"] = attribute_value
288 newdata = soup.decodexml(indent_level=0, formatter='minimal', indent_chars=" ")
289 return newdata
290
291
292# Note xml_updates has paths relative to the OEBPS folder as base

Callers

nothing calls this directly

Calls 13

decodexmlMethod · 0.95
BeautifulSoupClass · 0.90
urldecodepartFunction · 0.90
buildBookPathFunction · 0.90
startingDirFunction · 0.90
urlencodepartFunction · 0.90
buildRelativePathFunction · 0.90
find_allMethod · 0.80
_remove_xml_headerFunction · 0.70
encodeMethod · 0.45
findMethod · 0.45

Tested by

no test coverage detected