Uncomplete imple.

This commit is contained in:
Christopher Ramírez 2013-09-03 17:25:01 -06:00
parent 7e25b7634c
commit 99cb204ca0

View file

@ -42,6 +42,10 @@ from xml.dom.minidom import parseString
from jinja2 import Environment, Undefined from jinja2 import Environment, Undefined
# ---- Exceptions
class SecretaryError(Exception):
pass
class UndefinedSilently(Undefined): class UndefinedSilently(Undefined):
# Silently undefined, # Silently undefined,
# see http://stackoverflow.com/questions/6182498/jinja2-how-to-make-it-fail-silently-like-djangotemplate # see http://stackoverflow.com/questions/6182498/jinja2-how-to-make-it-fail-silently-like-djangotemplate
@ -233,6 +237,68 @@ class Render(object):
parent.removeChild(field) parent.removeChild(field)
def transfer_childs(from_node, to_node):
if from_node.hasChildNodes():
for child_node in from_node.childNodes:
new_child = to_node.appendChild(child_node)
if child_node.hasChildNodes():
transfer_childs(child_node, new_child)
# return to_node
def markdown_filter(markdown_text):
"""
Convert a markdown text into a ODT formated text
"""
try:
from copy import deepcopy
from markdown2 import markdown
except ImportError:
raise SecretaryError('Could not import markdown2 library. Install it using "pip install markdown2"')
html_text = markdown(markdown_text)
# Conver HTML tags to ODT tags
replacement_map = {
'p': {
'replace_with': 'text:p',
'attributes': {}
},
'strong': {
'replace_with': 'text:span',
'attributes': {}
}
}
xml_object = parseString( html_text )
# Replace HTML tags as specified in replacement_map
# Some tags may require extra attributes in ODT.
# Additional attributes are indicated in the 'attributes' property
for tag in replacement_map:
html_nodes = xml_object.getElementsByTagName(tag)
for html_node in html_nodes:
odt_node = xml_object.createElement(replacement_map[tag]['replace_with'])
# Transfer child nodes
if html_node.hasChildNodes():
for child_node in html_node.childNodes:
odt_node.appendChild(deepcopy(child_node))
html_node.parentNode.replaceChild(odt_node, html_node)
return xml_object.toxml()
def render_template(template, **kwargs): def render_template(template, **kwargs):
""" """