Skip to content

Commit

Permalink
(feat) implement xml properties and html attributes for "transform" u…
Browse files Browse the repository at this point in the history
…sage
  • Loading branch information
alexeevnick committed Feb 13, 2019
1 parent 49618f5 commit 302f55a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
7 changes: 7 additions & 0 deletions mammoth/conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ def children():
return [html.force_write] + content

html_path = self._find_html_path_for_paragraph(paragraph)
if paragraph.html_attributes:
for el in html_path.elements:
el.tag.attributes = paragraph.html_attributes
return html_path.wrap(children)


Expand All @@ -124,6 +127,10 @@ def visit_run(self, run, context):
if run.is_bold:
paths.append(self._find_style_for_run_property("bold", default="strong"))
paths.append(self._find_html_path_for_run(run))
if run.html_attributes:
if isinstance(paths[0], html_paths.HtmlPath):
paths.insert(0, html_paths.element(["span"], fresh=False))
paths[0].tag.attributes.update(run.html_attributes)

for path in paths:
nodes = partial(path.wrap, nodes)
Expand Down
15 changes: 12 additions & 3 deletions mammoth/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class Paragraph(HasChildren):
numbering = cobble.field()
alignment = cobble.field()
indent = cobble.field()
xml_properties = cobble.field()
html_attributes = cobble.field()


@cobble.data
Expand Down Expand Up @@ -51,6 +53,8 @@ class Run(HasChildren):
is_small_caps = cobble.field()
vertical_alignment = cobble.field()
font = cobble.field()
xml_properties = cobble.field()
html_attributes = cobble.field()

@cobble.data
class Text(Element):
Expand Down Expand Up @@ -104,11 +108,12 @@ def document(children, notes=None, comments=None):
comments = []
return Document(children, notes, comments=comments)

def paragraph(children, style_id=None, style_name=None, numbering=None, alignment=None, indent=None):
def paragraph(children, style_id=None, style_name=None, numbering=None, alignment=None, indent=None, xml_properties=None, html_attributes=None):
if indent is None:
indent = paragraph_indent()

return Paragraph(children, style_id, style_name, numbering, alignment=alignment, indent=indent)
if html_attributes is None:
html_attributes = {}
return Paragraph(children, style_id, style_name, numbering, alignment=alignment, indent=indent, xml_properties=xml_properties, html_attributes=html_attributes)

def paragraph_indent(start=None, end=None, first_line=None, hanging=None):
return ParagraphIndent(start=start, end=end, first_line=first_line, hanging=hanging)
Expand All @@ -124,6 +129,8 @@ def run(
is_small_caps=None,
vertical_alignment=None,
font=None,
xml_properties=None,
html_attributes=None
):
if vertical_alignment is None:
vertical_alignment = VerticalAlignment.baseline
Expand All @@ -138,6 +145,8 @@ def run(
is_small_caps=bool(is_small_caps),
vertical_alignment=vertical_alignment,
font=font,
xml_properties=xml_properties,
html_attributes=html_attributes or {},
)

class VerticalAlignment(object):
Expand Down
2 changes: 2 additions & 0 deletions mammoth/docx/body_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ def add_complex_field_hyperlink(children):
is_small_caps=is_small_caps,
vertical_alignment=vertical_alignment,
font=font,
xml_properties=properties
))

def _read_run_style(properties):
Expand All @@ -132,6 +133,7 @@ def paragraph(element):
numbering=numbering,
alignment=alignment,
indent=indent,
xml_properties=properties,
)).append_extra()

def _read_paragraph_style(properties):
Expand Down

0 comments on commit 302f55a

Please sign in to comment.