Skip to content

Commit c924d2a

Browse files
author
Steve Canny
committed
oxml: add CT_P.new()
1 parent 63f32a4 commit c924d2a

File tree

5 files changed

+126
-0
lines changed

5 files changed

+126
-0
lines changed

docx/oxml/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,13 @@
1111
Initializes oxml sub-package, including registering custom element classes
1212
corresponding to Open XML elements.
1313
"""
14+
15+
from docx.oxml.base import register_custom_element_class
16+
from docx.oxml.text import CT_P
17+
18+
19+
# ===========================================================================
20+
# custom element class mappings
21+
# ===========================================================================
22+
23+
register_custom_element_class('w:p', CT_P)

docx/oxml/text.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# oxml/text.py
4+
#
5+
# Copyright (C) 2013 Steve Canny scanny@cisco.com
6+
#
7+
# This module is part of python-docx and is released under the MIT License:
8+
# http://www.opensource.org/licenses/mit-license.php
9+
10+
"""
11+
Custom element classes related to text, such as paragraph (CT_P) and runs
12+
(CT_R).
13+
"""
14+
15+
from docx.oxml.base import nsdecls, OxmlBaseElement, oxml_fromstring
16+
17+
18+
class CT_P(OxmlBaseElement):
19+
"""
20+
``<w:p>`` element, containing the properties and text for a paragraph.
21+
"""
22+
@staticmethod
23+
def new():
24+
"""
25+
Return a new ``<w:p>`` element.
26+
"""
27+
xml = '<w:p %s/>' % nsdecls('w')
28+
p = oxml_fromstring(xml)
29+
return p

tests/oxml/__init__.py

Whitespace-only changes.

tests/oxml/test_text.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# test_text.py
4+
#
5+
# Copyright (C) 2013 Steve Canny scanny@cisco.com
6+
#
7+
# This module is part of python-docx and is released under the MIT License:
8+
# http://www.opensource.org/licenses/mit-license.php
9+
10+
"""Test suite for the docx.oxml.text module."""
11+
12+
from docx.oxml.text import CT_P
13+
14+
from ..unitdata import a_p
15+
16+
17+
class DescribeCT_P(object):
18+
19+
def it_can_construct_a_new_p_element(self):
20+
p = CT_P.new()
21+
expected_xml = a_p().with_nsdecls().xml
22+
assert p.xml == expected_xml

tests/unitdata.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# unitdata.py
4+
#
5+
# Copyright (C) 2013 Steve Canny scanny@cisco.com
6+
#
7+
# This module is part of python-opc and is released under the MIT License:
8+
# http://www.opensource.org/licenses/mit-license.php
9+
10+
"""Test data builders for unit tests"""
11+
12+
from docx.oxml.base import nsdecls, oxml_fromstring
13+
14+
15+
class BaseBuilder(object):
16+
"""
17+
Provides common behavior for all data builders.
18+
"""
19+
nsdecls = ' %s' % nsdecls('w')
20+
21+
def __init__(self):
22+
"""Establish instance variables with default values"""
23+
self._empty = False
24+
self._indent = 0
25+
self._nsdecls = ''
26+
27+
@property
28+
def element(self):
29+
"""Return element based on XML generated by builder"""
30+
return oxml_fromstring(self.xml)
31+
32+
@property
33+
def is_empty(self):
34+
return True
35+
36+
def with_indent(self, indent):
37+
"""Add integer *indent* spaces at beginning of element XML"""
38+
self._indent = indent
39+
return self
40+
41+
def with_nsdecls(self):
42+
self._nsdecls = self.nsdecls
43+
return self
44+
45+
46+
class CT_PBuilder(BaseBuilder):
47+
"""
48+
Test data builder for a CT_P (<w:p>) XML element that appears within the
49+
body element of a document.xml file.
50+
"""
51+
def __init__(self):
52+
"""Establish instance variables with default values"""
53+
super(CT_PBuilder, self).__init__()
54+
55+
@property
56+
def xml(self):
57+
"""Return element XML based on attribute settings"""
58+
indent = ' ' * self._indent
59+
xml = '%s<w:p%s/>\n' % (indent, self._nsdecls)
60+
return xml
61+
62+
63+
def a_p():
64+
"""Return a CT_PBuilder instance"""
65+
return CT_PBuilder()

0 commit comments

Comments
 (0)