1
0
mirror of https://github.com/gryf/ebook-converter.git synced 2026-04-17 11:43:30 +02:00
Files
ebook-converter/ebook_converter/ebooks/lrf/pylrs/pylrfopt.py
2020-04-11 19:27:06 +02:00

45 lines
1.5 KiB
Python

from __future__ import absolute_import, division, print_function, unicode_literals
def _optimize(tagList, tagName, conversion):
# copy the tag of interest plus any text
newTagList = []
for tag in tagList:
if tag.name == tagName or tag.name == "rawtext":
newTagList.append(tag)
# now, eliminate any duplicates (leaving the last one)
for i, newTag in enumerate(newTagList[:-1]):
if newTag.name == tagName and newTagList[i+1].name == tagName:
tagList.remove(newTag)
# eliminate redundant settings to same value across text strings
newTagList = []
for tag in tagList:
if tag.name == tagName:
newTagList.append(tag)
for i, newTag in enumerate(newTagList[:-1]):
value = conversion(newTag.parameter)
nextValue = conversion(newTagList[i+1].parameter)
if value == nextValue:
tagList.remove(newTagList[i+1])
# eliminate any setting that don't have text after them
while len(tagList) > 0 and tagList[-1].name == tagName:
del tagList[-1]
def tagListOptimizer(tagList):
# this function eliminates redundant or unnecessary tags
# it scans a list of tags, looking for text settings that are
# changed before any text is output
# for example,
# fontsize=100, fontsize=200, text, fontsize=100, fontsize=200
# should be:
# fontsize=200 text
oldSize = len(tagList)
_optimize(tagList, "fontsize", int)
_optimize(tagList, "fontweight", int)
return oldSize - len(tagList)