Files
MDHelper/mdhelper/docx_inline.py
T

30 lines
1.4 KiB
Python

"""Private, temporary style markers shared by the three DOCX converters."""
PREFIX = 'MDHelperSource_'
LIST_PARAGRAPH_STYLE = 'MDHelperSourceListParagraph'
QUOTED_LIST_PARAGRAPH_STYLE = 'MDHelperSourceQuotedListParagraph'
INNER_QUOTE_STYLE = 'MDHelperSourceInnerQuote'
LIST_TARGETS = ('list_bullet', 'list_ordered', 'list_task')
SOURCE_LIST_STYLES = {base + '_' + target: (base, target)
for base in (LIST_PARAGRAPH_STYLE, QUOTED_LIST_PARAGRAPH_STYLE) for target in LIST_TARGETS}
SOURCE_PARAGRAPH_STYLES = (LIST_PARAGRAPH_STYLE, QUOTED_LIST_PARAGRAPH_STYLE, INNER_QUOTE_STYLE, *SOURCE_LIST_STYLES)
CONTINUATION = '{urn:mdhelper:source}continuation'
LIST_TARGET = '{urn:mdhelper:source}list-target'
CODES = {'strong': 'B', 'emphasis': 'I', 'strike': 'S', 'link': 'L', 'inline_code': 'C'}
def ordered_styles(order):
"""Each semantic type takes precedence at its innermost occurrence."""
return tuple(reversed(dict.fromkeys(reversed(tuple(order)))))
def source_style_name(order):
keys = ordered_styles(key for key in order if key in CODES)
return PREFIX + '_'.join(CODES[key] for key in keys) if keys else ''
def source_style_order(name):
if not name.startswith(PREFIX): return ()
reverse = {code: key for key, code in CODES.items()}
codes = name[len(PREFIX):].split('_')
return tuple(reverse[code] for code in codes) if all(code in reverse for code in codes) else ()