class ContentstateConverter: def __init__(self, features=None): self.features = features self.html_to_contentstate_handler = HtmlToContentStateHandler(features) exporter_config = { "block_map": { "unstyled": persist_key_for_block("p"), "atomic": render_children, "fallback": block_fallback, }, "style_map": { "FALLBACK": style_fallback, }, "entity_decorators": { "FALLBACK": entity_fallback, }, "composite_decorators": [ { "strategy": re.compile(r"\n"), "component": br, }, ], "engine": DOM.STRING, } for feature in self.features: rule = feature_registry.get_converter_rule("contentstate", feature) if rule is not None: feature_config = rule["to_database_format"] exporter_config["block_map"].update( { block_type: persist_key_for_block(config) for block_type, config in feature_config.get( "block_map", {} ).items() } ) exporter_config["style_map"].update(feature_config.get("style_map", {})) exporter_config["entity_decorators"].update( feature_config.get("entity_decorators", {}) ) self.exporter = HTMLExporter(exporter_config) def from_database_format(self, html): self.html_to_contentstate_handler.reset() self.html_to_contentstate_handler.feed(html) self.html_to_contentstate_handler.close() return self.html_to_contentstate_handler.contentstate.as_json( indent=4, separators=(",", ": ") ) def to_database_format(self, contentstate_json): return self.exporter.render(json.loads(contentstate_json))
class ContentstateConverter(): def __init__(self, features=None): self.features = features self.html_to_contentstate_handler = HtmlToContentStateHandler(features) exporter_config = { 'block_map': { 'unstyled': persist_key_for_block('p'), 'atomic': render_children, 'fallback': block_fallback, }, 'style_map': { 'FALLBACK': style_fallback, }, 'entity_decorators': { 'FALLBACK': entity_fallback, }, 'composite_decorators': [ { 'strategy': re.compile(r'\n'), 'component': br, }, ], 'engine': DOM.STRING, } for feature in self.features: rule = feature_registry.get_converter_rule('contentstate', feature) if rule is not None: feature_config = rule['to_database_format'] exporter_config['block_map'].update({ block_type: persist_key_for_block(config) for block_type, config in feature_config.get( 'block_map', {}).items() }) exporter_config['style_map'].update( feature_config.get('style_map', {})) exporter_config['entity_decorators'].update( feature_config.get('entity_decorators', {})) self.exporter = HTMLExporter(exporter_config) def from_database_format(self, html): self.html_to_contentstate_handler.reset() self.html_to_contentstate_handler.feed(html) self.html_to_contentstate_handler.close() return self.html_to_contentstate_handler.contentstate.as_json( indent=4, separators=(',', ': ')) def to_database_format(self, contentstate_json): return self.exporter.render(json.loads(contentstate_json))
class ContentstateConverter(): def __init__(self, features=None): self.features = features self.html_to_contentstate_handler = HtmlToContentStateHandler(features) exporter_config = { 'block_map': { 'unstyled': 'p', 'atomic': render_children, 'fallback': block_fallback, }, 'style_map': {}, 'entity_decorators': { 'FALLBACK': entity_fallback, }, 'composite_decorators': [ { 'strategy': re.compile(r'\n'), 'component': br, }, ], 'engine': DOM.STRING, } for feature in self.features: rule = feature_registry.get_converter_rule('contentstate', feature) if rule is not None: feature_config = rule['to_database_format'] exporter_config['block_map'].update( feature_config.get('block_map', {})) exporter_config['style_map'].update( feature_config.get('style_map', {})) exporter_config['entity_decorators'].update( feature_config.get('entity_decorators', {})) self.exporter = HTMLExporter(exporter_config) def from_database_format(self, html): self.html_to_contentstate_handler.reset() self.html_to_contentstate_handler.feed(html) if not self.html_to_contentstate_handler.contentstate.blocks: # Draftail does not accept an empty block list as valid, but does accept 'null' as meaning "no content" return 'null' return self.html_to_contentstate_handler.contentstate.as_json( indent=4, separators=(',', ': ')) def to_database_format(self, contentstate_json): return self.exporter.render(json.loads(contentstate_json))
class ContentstateConverter(): def __init__(self, features=None): self.features = features self.html_to_contentstate_handler = HtmlToContentStateHandler(features) exporter_config = { 'block_map': { 'unstyled': 'p', 'atomic': render_children, 'fallback': block_fallback, }, 'style_map': {}, 'entity_decorators': { 'FALLBACK': entity_fallback, }, 'composite_decorators': [ { 'strategy': re.compile(r'\n'), 'component': br, }, ], 'engine': DOM.STRING, } for feature in self.features: rule = feature_registry.get_converter_rule('contentstate', feature) if rule is not None: feature_config = rule['to_database_format'] exporter_config['block_map'].update(feature_config.get('block_map', {})) exporter_config['style_map'].update(feature_config.get('style_map', {})) exporter_config['entity_decorators'].update(feature_config.get('entity_decorators', {})) self.exporter = HTMLExporter(exporter_config) def from_database_format(self, html): self.html_to_contentstate_handler.reset() self.html_to_contentstate_handler.feed(html) if not self.html_to_contentstate_handler.contentstate.blocks: # Draftail does not accept an empty block list as valid, but does accept 'null' as meaning "no content" return 'null' return self.html_to_contentstate_handler.contentstate.as_json(indent=4, separators=(',', ': ')) def to_database_format(self, contentstate_json): return self.exporter.render(json.loads(contentstate_json))