mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Add support for conditional text to template mini-language
This commit is contained in:
commit
63db48962c
@ -17,7 +17,6 @@ from calibre.library.field_metadata import FieldMetadata
|
|||||||
from calibre.utils.date import isoformat, format_date
|
from calibre.utils.date import isoformat, format_date
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
NULL_VALUES = {
|
NULL_VALUES = {
|
||||||
'user_metadata': {},
|
'user_metadata': {},
|
||||||
'cover_data' : (None, None),
|
'cover_data' : (None, None),
|
||||||
@ -38,10 +37,17 @@ class SafeFormat(string.Formatter):
|
|||||||
Provides a format function that substitutes '' for any missing value
|
Provides a format function that substitutes '' for any missing value
|
||||||
'''
|
'''
|
||||||
def get_value(self, key, args, mi):
|
def get_value(self, key, args, mi):
|
||||||
ign, v = mi.format_field(key, series_with_index=False)
|
from calibre.library.save_to_disk import explode_string_template_value
|
||||||
if v is None:
|
try:
|
||||||
return ''
|
prefix, key, suffix = explode_string_template_value(key)
|
||||||
return v
|
ign, v = mi.format_field(key, series_with_index=False)
|
||||||
|
if v is None:
|
||||||
|
return ''
|
||||||
|
if v is '':
|
||||||
|
return ''
|
||||||
|
return prefix + v + suffix
|
||||||
|
except:
|
||||||
|
return key
|
||||||
|
|
||||||
composite_formatter = SafeFormat()
|
composite_formatter = SafeFormat()
|
||||||
compress_spaces = re.compile(r'\s+')
|
compress_spaces = re.compile(r'\s+')
|
||||||
@ -50,6 +56,7 @@ def format_composite(x, mi):
|
|||||||
try:
|
try:
|
||||||
ans = composite_formatter.vformat(x, [], mi).strip()
|
ans = composite_formatter.vformat(x, [], mi).strip()
|
||||||
except:
|
except:
|
||||||
|
traceback.print_exc()
|
||||||
ans = x
|
ans = x
|
||||||
return compress_spaces.sub(' ', ans)
|
return compress_spaces.sub(' ', ans)
|
||||||
|
|
||||||
|
@ -122,15 +122,6 @@ class SafeFormat(string.Formatter):
|
|||||||
v = ','.join(v)
|
v = ','.join(v)
|
||||||
return v
|
return v
|
||||||
|
|
||||||
composite_formatter = SafeFormat()
|
|
||||||
|
|
||||||
def format_composite(x, mi):
|
|
||||||
try:
|
|
||||||
ans = composite_formatter.vformat(x, [], mi).strip()
|
|
||||||
except:
|
|
||||||
ans = x
|
|
||||||
return ans
|
|
||||||
|
|
||||||
class MetadataBulkDialog(QDialog, Ui_MetadataBulkDialog):
|
class MetadataBulkDialog(QDialog, Ui_MetadataBulkDialog):
|
||||||
|
|
||||||
s_r_functions = { '' : lambda x: x,
|
s_r_functions = { '' : lambda x: x,
|
||||||
|
@ -101,15 +101,30 @@ def preprocess_template(template):
|
|||||||
template = template.decode(preferred_encoding, 'replace')
|
template = template.decode(preferred_encoding, 'replace')
|
||||||
return template
|
return template
|
||||||
|
|
||||||
|
template_value_re = re.compile(r'^([^\|]*(?=\|))(?:\|?)([^\|]*)(?:\|?)((?<=\|).*?)$')
|
||||||
|
|
||||||
|
def explode_string_template_value(key):
|
||||||
|
try:
|
||||||
|
matches = template_value_re.match(key)
|
||||||
|
if matches.lastindex != 3:
|
||||||
|
return key
|
||||||
|
return matches.groups()
|
||||||
|
except:
|
||||||
|
return '', key, ''
|
||||||
|
|
||||||
class SafeFormat(string.Formatter):
|
class SafeFormat(string.Formatter):
|
||||||
'''
|
'''
|
||||||
Provides a format function that substitutes '' for any missing value
|
Provides a format function that substitutes '' for any missing value
|
||||||
'''
|
'''
|
||||||
def get_value(self, key, args, kwargs):
|
def get_value(self, key, args, kwargs):
|
||||||
try:
|
try:
|
||||||
return kwargs[key]
|
prefix, key, suffix = explode_string_template_value(key)
|
||||||
|
if kwargs[key]:
|
||||||
|
return '%s%s%s'%(prefix, kwargs[key], suffix)
|
||||||
|
return ''
|
||||||
except:
|
except:
|
||||||
return ''
|
return ''
|
||||||
|
|
||||||
safe_formatter = SafeFormat()
|
safe_formatter = SafeFormat()
|
||||||
|
|
||||||
def safe_format(x, format_args):
|
def safe_format(x, format_args):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user