Adding replace_chapter_text feature.

This commit is contained in:
Jim Miller 2022-07-20 15:34:44 -05:00
parent ac5f94a6ac
commit e1d5a68a90
2 changed files with 51 additions and 3 deletions

View file

@ -460,6 +460,7 @@ def get_valid_keywords():
'rating_titles',
'remove_transparency',
'replace_br_with_p',
'replace_chapter_text',
'replace_hr',
'replace_xbr_with_hr',
'replace_metadata',
@ -902,7 +903,8 @@ class Configuration(ConfigParser):
clude_metadata_re = re.compile(r'(add_to_)?(in|ex)clude_metadata_(pre|post)$')
replace_metadata_re = re.compile(r'(add_to_)?replace_metadata$')
from .story import set_in_ex_clude, make_replacements
replace_chapter_text_re = re.compile(r'(add_to_)?replace_chapter_text$')
from .story import set_in_ex_clude, make_replacements, make_chapter_text_replacements
custom_columns_settings_re = re.compile(r'(add_to_)?custom_columns_settings$')
custom_columns_flags_re = re.compile(r'^[rna](_anthaver)?')
@ -941,6 +943,9 @@ class Configuration(ConfigParser):
if replace_metadata_re.match(keyword):
make_replacements(value)
if replace_chapter_text_re.match(keyword):
make_chapter_text_replacements(value)
if generate_cover_settings_re.match(keyword):
make_generate_cover_settings(value)

View file

@ -454,6 +454,32 @@ def make_replacements(replace):
# print("replace lines:%s"%len(retval))
return retval
def make_chapter_text_replacements(replace):
retval=[]
for repl_line in replace.splitlines():
line=repl_line
try:
(regexp,replacement)=(None,None)
if "=>" in line:
parts = line.split("=>")
(regexp,replacement)=parts
if regexp:
regexp = re_compile(regexp,line)
# A way to explicitly include spaces in the
# replacement string. The .ini parser eats any
# trailing spaces.
replacement=replacement\
.replace(SPACE_REPLACE,' ')
retval.append([repl_line,regexp,replacement])
except Exception as e:
logger.error("Problem with Chapter Text Replacement Line:%s"%repl_line)
raise exceptions.PersonalIniFailed(e,'replace_chapter_text unpacking failed',repl_line)
# raise
# print("replace lines:%s"%len(retval))
return retval
class StoryImage(dict):
pass
@ -523,6 +549,7 @@ class Story(Requestable):
self.metadata = {'version':'unknown'}
self.metadata['python_version']=sys.version
self.replacements = []
self.chapter_text_replacements = []
self.in_ex_cludes = {}
self.chapters = [] # chapters will be dict containing(url,title,html,etc)
self.chapter_first = None
@ -541,6 +568,7 @@ class Story(Requestable):
self.logfile=None # cheesy way to carry log file forward across update.
self.replacements_prepped = False
self.chapter_text_replacements_prepped = False
self.chapter_error_count = 0
@ -1243,11 +1271,26 @@ class Story(Requestable):
chapter['toctitle'] = toctempl.substitute(chapter)
# set after, otherwise changes origtitle and toctitle
chapter['title'] = chapter['chapter']
## XXX -- add chapter text replacement here?
## chapter['html'] is a soup or soup part?
## chapter['html'] is a string.
chapter['html'] = self.do_chapter_text_replacements(chapter['html'])
retval.append(chapter)
return retval
def do_chapter_text_replacements(self,data):
'''
'Undocumented' feature. This is a shotgun with a stirrup on
the end--you *will* shoot yourself in the foot a lot with it.
'''
# only compile chapter_text_replacements once.
if not self.chapter_text_replacements and self.getConfig('replace_chapter_text'):
self.chapter_text_replacements = make_chapter_text_replacements(self.getConfig('replace_chapter_text'))
logger.debug(self.chapter_text_replacements)
for replaceline in self.chapter_text_replacements:
(repl_line,regexp,replacement) = replaceline
if regexp.search(data):
data = regexp.sub(replacement,data)
return data
def get_filename_safe_metadata(self,pattern=None):
origvalues = self.getAllMetadata()
values={}