Add conditional replace_metadata feature.

This commit is contained in:
Jim Miller
2012-10-20 16:15:48 -05:00
parent 7d3b453ce8
commit cfbf491bd4
4 changed files with 31 additions and 12 deletions
+6 -2
View File
@@ -161,8 +161,10 @@ extratags: FanFiction
## for regexp details.
## Make sure to keep at least one space at the start of each line and
## to escape % to %%, if used.
## Two or three part lines. Two part effect everything.
## Two, three or five part lines. Two part effect everything.
## Three part effect only those key(s) lists.
## *Five* part lines. Effect only when trailing conditional key=>regexp matches
## metakey[,metakey]=>pattern=>replacement[&&metakey=>regexp]
#replace_metadata:
# genre,category=>Sci-Fi=>SF
# Puella Magi Madoka Magica.* => Madoka
@@ -170,7 +172,9 @@ extratags: FanFiction
# Crossover: (.*)=>\1
# title=>(.*)Great(.*)=>\1Moderate\2
# .*-Centered=>
# characters=>Sam W\.=>Sam Witwicky&&category=>Transformers
# characters=>Sam W\.=>Sam Winchester&&category=>Supernatural
## Some readers don't show horizontal rule (<hr />) tags correctly.
## This replaces them all with a centered '* * *'. (Note centering
## doesn't work on some devices either.)
+1 -1
View File
@@ -129,7 +129,7 @@ Some more longer description. "I suck at summaries!" "Better than it sounds!"
self.story.addToList('genre','Fantasy')
self.story.addToList('genre','Comedy')
self.story.addToList('genre','SF')
self.story.addToList('genre','Sci-Fi')
self.story.addToList('genre','Noir')
self.story.addToList('characters','Bob Smith')
+18 -7
View File
@@ -253,24 +253,35 @@ class Story(Configurable):
## Three part effect only those key(s) lists.
## pattern=>replacement
## metakey,metakey=>pattern=>replacement
## *Five* part lines. Effect only when trailing conditional key=>regexp matches
## metakey[,metakey]=>pattern=>replacement[&&metakey=>regexp]
def setReplace(self,replace):
for line in replace.splitlines():
if "&&" in line:
(line,conditional) = map( lambda x: x.strip(), line.split("&&") )
condparts = map( lambda x: x.strip(), conditional.split("=>") )
else:
condparts=[None,None]
if "=>" in line:
parts = map( lambda x: x.strip(), line.split("=>") )
if len(parts) > 2:
parts[0] = map( lambda x: x.strip(), parts[0].split(",") )
self.replacements.append(parts)
self.replacements.append(parts+condparts)
else:
self.replacements.append([None]+parts)
self.replacements.append([None]+parts+condparts)
def doReplacments(self,value,key):
for (keys,p,v) in self.replacements:
for (keys,regexp,replacement,condkey,condregexp) in self.replacements:
if (keys == None or key in keys) \
and isinstance(value,basestring) \
and re.search(p,value):
#pv=value
value = re.sub(p,v,value)
#print("change:%s => %s === %s => %s "%(p,v,pv,value))
and re.search(regexp,value):
doreplace=True
if condkey:
condval = self.getMetadata(condkey)
doreplace = condval != None and re.search(condregexp,condval)
if doreplace:
value = re.sub(regexp,replacement,value)
return value
def getMetadataRaw(self,key):
+6 -2
View File
@@ -131,8 +131,10 @@ extratags: FanFiction
## for regexp details.
## Make sure to keep at least one space at the start of each line and
## to escape % to %%, if used.
## Two or three part lines. Two part effect everything.
## Two, three or five part lines. Two part effect everything.
## Three part effect only those key(s) lists.
## *Five* part lines. Effect only when trailing conditional key=>regexp matches
## metakey[,metakey]=>pattern=>replacement[&&metakey=>regexp]
#replace_metadata:
# genre,category=>Sci-Fi=>SF
# Puella Magi Madoka Magica.* => Madoka
@@ -140,7 +142,9 @@ extratags: FanFiction
# Crossover: (.*)=>\1
# title=>(.*)Great(.*)=>\1Moderate\2
# .*-Centered=>
# characters=>Sam W\.=>Sam Witwicky&&category=>Transformers
# characters=>Sam W\.=>Sam Winchester&&category=>Supernatural
## Some readers don't show horizontal rule (<hr />) tags correctly.
## This replaces them all with a centered '* * *'. (Note centering
## doesn't work on some devices either.)