Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

html filter and usage of defined styles #35

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 48 additions & 16 deletions secretary.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import sys
import logging
import zipfile
import copy
from os import path
from mimetypes import guess_type, guess_extension
from uuid import uuid4
Expand Down Expand Up @@ -139,6 +140,7 @@ def __init__(self, environment=None, **kwargs):
# Register filters
self.environment.filters['pad'] = pad_string
self.environment.filters['markdown'] = self.markdown_filter
self.environment.filters['html'] = self.html_filter
self.environment.filters['image'] = self.image_filter

self.media_path = kwargs.pop('media_path', '')
Expand Down Expand Up @@ -351,8 +353,8 @@ def _prepare_document_tags(self, document):
is_block = self._is_block_tag(content)
scale_to = tag.getAttribute('text:description').strip().lower()

if content.lower().find('|markdown') > 0:
# Take whole paragraph when handling a markdown field
if content.lower().find('|markdown') > 0 or content.lower().find('|html') > 0:
# Take whole paragraph when handling a markdown or html field
scale_to = 'text:p'

if scale_to:
Expand Down Expand Up @@ -716,26 +718,56 @@ def markdown_filter(self, markdown_text):
if not isinstance(markdown_text, basestring):
return ''

from xml.dom import Node
from markdown_map import transform_map

try:
from markdown2 import markdown
except ImportError:
raise SecretaryError('Could not import markdown2 library. Install it using "pip install markdown2"')

return self.html_filter(markdown(markdown_text))

def html_filter(self, html_text):
"""
Convert an html text into a ODT formated text
"""

if not isinstance(html_text, basestring):
return ''

from xml.dom import Node

try:
transform_map = self.transform_map
except AttributeError:
from markdown_map import transform_map
self.transform_map = copy.deepcopy (transform_map)
for definition in self.transform_map.values():
style_name = definition.get('style_attributes', {}).get('style-name')
if style_name is not None:
pass
for style in self.styles.getElementsByTagName('style:style'):
if style.hasAttribute('style:family') and style.getAttribute('style:family') == 'paragraph':
if not style.hasAttribute('style:parent-style-name'):
self.transform_map['p']['style_attributes']['style-name'] = style.getAttribute('style:name')
for autostyle in self.styles.getElementsByTagName('office:automatic-styles'):
for liststyle in autostyle.getElementsByTagName('text:list-style'):
if liststyle.getElementsByTagName('text:list-level-style-bullet'):
definition = self.transform_map['ul']
else:
definition = self.transform_map['ol']
definition['style_attributes'] = definition.get('style_attributes', {})
definition['style_attributes']['style-name'] = liststyle.getAttribute('style:name')

styles_cache = {} # cache styles searching
html_text = markdown(markdown_text)
xml_object = parseString('<html>%s</html>' % html_text.encode('ascii', 'xmlcharrefreplace'))

# Transform HTML tags as specified in transform_map
# Some tags may require extra attributes in ODT.
# Additional attributes are indicated in the 'attributes' property

for tag in transform_map:
for tag in self.transform_map:
html_nodes = xml_object.getElementsByTagName(tag)
for html_node in html_nodes:
odt_node = xml_object.createElement(transform_map[tag]['replace_with'])
odt_node = xml_object.createElement(self.transform_map[tag]['replace_with'])

# Transfer child nodes
if html_node.hasChildNodes():
Expand All @@ -755,13 +787,13 @@ def markdown_filter(self, markdown_text):
container.appendChild(child_node.cloneNode(True))

# Add style-attributes defined in transform_map
if 'style_attributes' in transform_map[tag]:
for k, v in transform_map[tag]['style_attributes'].items():
if 'style_attributes' in self.transform_map[tag]:
for k, v in self.transform_map[tag]['style_attributes'].items():
odt_node.setAttribute('text:%s' % k, v)

# Add defined attributes
if 'attributes' in transform_map[tag]:
for k, v in transform_map[tag]['attributes'].items():
if 'attributes' in self.transform_map[tag]:
for k, v in self.transform_map[tag]['attributes'].items():
odt_node.setAttribute(k, v)

# copy original href attribute in <a> tag
Expand All @@ -771,16 +803,16 @@ def markdown_filter(self, markdown_text):
html_node.getAttribute('href'))

# Does the node need to create an style?
if 'style' in transform_map[tag]:
name = transform_map[tag]['style']['name']
if 'style' in self.transform_map[tag]:
name = self.transform_map[tag]['style']['name']
if not name in styles_cache:
style_node = self.get_style_by_name(name)

if style_node is None:
# Create and cache the style node
style_node = self.insert_style_in_content(
name, transform_map[tag]['style'].get('attributes', None),
**transform_map[tag]['style']['properties'])
name, self.transform_map[tag]['style'].get('attributes', None),
**self.transform_map[tag]['style']['properties'])
styles_cache[name] = style_node

html_node.parentNode.replaceChild(odt_node, html_node)
Expand Down