mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
DRY
This commit is contained in:
parent
b07752e389
commit
0b98f34f84
@ -37,43 +37,36 @@ scanner = re.Scanner([
|
|||||||
|
|
||||||
null = object()
|
null = object()
|
||||||
|
|
||||||
def parse_hyperlink(raw, log):
|
def parser(name, field_map, default_field_name):
|
||||||
ans = {}
|
|
||||||
last_option = None
|
|
||||||
raw = raw.replace('\\\\', '\x01').replace('\\"', '\x02')
|
|
||||||
for token, token_type in scanner.scan(raw)[0]:
|
|
||||||
token = token.replace('\x01', '\\').replace('\x02', '"')
|
|
||||||
if token_type is FLAG:
|
|
||||||
last_option = {'l':'anchor', 'm':'image-map', 'n':'target', 'o':'title', 't':'target'}.get(token[1], null)
|
|
||||||
if last_option is not None:
|
|
||||||
ans[last_option] = None
|
|
||||||
elif token_type is WORD:
|
|
||||||
if last_option is None:
|
|
||||||
ans['url'] = token
|
|
||||||
else:
|
|
||||||
ans[last_option] = token
|
|
||||||
last_option = None
|
|
||||||
ans.pop(null, None)
|
|
||||||
return ans
|
|
||||||
|
|
||||||
def parse_xe(raw, log):
|
def parse(raw, log=None):
|
||||||
ans = {}
|
ans = {}
|
||||||
last_option = None
|
last_option = None
|
||||||
raw = raw.replace('\\\\', '\x01').replace('\\"', '\x02')
|
raw = raw.replace('\\\\', '\x01').replace('\\"', '\x02')
|
||||||
for token, token_type in scanner.scan(raw)[0]:
|
for token, token_type in scanner.scan(raw)[0]:
|
||||||
token = token.replace('\x01', '\\').replace('\x02', '"')
|
token = token.replace('\x01', '\\').replace('\x02', '"')
|
||||||
if token_type is FLAG:
|
if token_type is FLAG:
|
||||||
last_option = {'b':'bold', 'i':'italic', 'f':'entry_type', 'r':'page_range_bookmark', 't':'page_number_text', 'y':'yomi'}.get(token[1], null)
|
last_option = field_map.get(token[1], null)
|
||||||
if last_option is not None:
|
if last_option is not None:
|
||||||
ans[last_option] = None
|
ans[last_option] = None
|
||||||
elif token_type is WORD:
|
elif token_type is WORD:
|
||||||
if last_option is None:
|
if last_option is None:
|
||||||
ans['text'] = token
|
ans[default_field_name] = token
|
||||||
else:
|
else:
|
||||||
ans[last_option] = token
|
ans[last_option] = token
|
||||||
last_option = None
|
last_option = None
|
||||||
ans.pop(null, None)
|
ans.pop(null, None)
|
||||||
return ans
|
return ans
|
||||||
|
|
||||||
|
parse.__name__ = str('parse_' + name)
|
||||||
|
|
||||||
|
return parse
|
||||||
|
|
||||||
|
parse_hyperlink = parser('hyperlink',
|
||||||
|
{'l':'anchor', 'm':'image-map', 'n':'target', 'o':'title', 't':'target'}, 'url')
|
||||||
|
|
||||||
|
parse_xe = parser('xe',
|
||||||
|
{'b':'bold', 'i':'italic', 'f':'entry_type', 'r':'page_range_bookmark', 't':'page_number_text', 'y':'yomi'}, 'text')
|
||||||
|
|
||||||
class Fields(object):
|
class Fields(object):
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user