1 from __future__ import unicode_literals
7 'application/mathml+xml': 'librarian.embeds.mathml.MathML',
8 'application/x-latex': 'librarian.embeds.latex.LaTeX',
13 def transforms_to(cls, mime_types, downgrade=False):
15 for name, method in cls.__dict__.iteritems():
16 if hasattr(method, "embed_converts_to"):
17 conv_type, conv_downgrade = method.embed_converts_to
18 if downgrade == conv_downgrade and conv_type in mime_types:
19 matches.add(conv_type)
22 def transform_to(self, mime_type, downgrade=False):
23 for name, method in type(cls).__dict__.iteritems():
24 if hasattr(method, "embed_converts_to"):
25 conv_type, conv_downgrade = method.embed_converts_to
26 if downgrade == conv_downgrade and conv_type == mime_type:
30 class DataEmbed(Embed):
31 def __init__(self, data=None):
34 class TreeEmbed(Embed):
35 def __init__(self, tree=None):
36 if isinstance(tree, etree._Element):
37 tree = etree.ElementTree(tree)
40 def converts_to(mime_type, downgrade=False):
41 def decorator(method):
42 method.embed_converts_to = mime_type, downgrade
46 def downgrades_to(mime_type):
47 return converts_to(mime_type, True)
49 def create_embed(mime_type, tree=None, data=None):
50 embed = known_types.get(mime_type)
52 embed = DataEmbed if tree is None else TreeEmbed
54 mod_name, cls_name = embed.rsplit('.', 1)
55 mod = importlib.import_module(mod_name)
56 embed = getattr(mod, cls_name)
58 return embed(data if tree is None else tree)