Skip to content

Commit 6d12360

Browse files
author
Steve Canny
committed
oxml: convert CT_PPr to xmlchemy
1 parent a97f43d commit 6d12360

4 files changed

Lines changed: 64 additions & 141 deletions

File tree

docs/dev/analysis/schema/ct_ppr.rst

Lines changed: 41 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -74,19 +74,7 @@ Schema excerpt
7474

7575
::
7676

77-
<xsd:complexType name="CT_PPr">
78-
<xsd:complexContent>
79-
<xsd:extension base="CT_PPrBase">
80-
<xsd:sequence>
81-
<xsd:element name="rPr" type="CT_ParaRPr" minOccurs="0"/>
82-
<xsd:element name="sectPr" type="CT_SectPr" minOccurs="0"/>
83-
<xsd:element name="pPrChange" type="CT_PPrChange" minOccurs="0"/>
84-
</xsd:sequence>
85-
</xsd:extension>
86-
</xsd:complexContent>
87-
</xsd:complexType>
88-
89-
<xsd:complexType name="CT_PPrBase">
77+
<xsd:complexType name="CT_PPr"> <!-- denormalized -->
9078
<xsd:sequence>
9179
<xsd:element name="pStyle" type="CT_String" minOccurs="0"/>
9280
<xsd:element name="keepNext" type="CT_OnOff" minOccurs="0"/>
@@ -120,41 +108,44 @@ Schema excerpt
120108
<xsd:element name="textboxTightWrap" type="CT_TextboxTightWrap" minOccurs="0"/>
121109
<xsd:element name="outlineLvl" type="CT_DecimalNumber" minOccurs="0"/>
122110
<xsd:element name="divId" type="CT_DecimalNumber" minOccurs="0"/>
123-
<xsd:element name="cnfStyle" type="CT_Cnf" minOccurs="0" maxOccurs="1"/>
111+
<xsd:element name="cnfStyle" type="CT_Cnf" minOccurs="0"/>
112+
<xsd:element name="rPr" type="CT_ParaRPr" minOccurs="0"/>
113+
<xsd:element name="sectPr" type="CT_SectPr" minOccurs="0"/>
114+
<xsd:element name="pPrChange" type="CT_PPrChange" minOccurs="0"/>
124115
</xsd:sequence>
125116
</xsd:complexType>
126117

127118
<xsd:complexType name="CT_FramePr">
128-
<xsd:attribute name="dropCap" type="ST_DropCap" use="optional"/>
129-
<xsd:attribute name="lines" type="ST_DecimalNumber" use="optional"/>
130-
<xsd:attribute name="w" type="s:ST_TwipsMeasure" use="optional"/>
131-
<xsd:attribute name="h" type="s:ST_TwipsMeasure" use="optional"/>
132-
<xsd:attribute name="vSpace" type="s:ST_TwipsMeasure" use="optional"/>
133-
<xsd:attribute name="hSpace" type="s:ST_TwipsMeasure" use="optional"/>
134-
<xsd:attribute name="wrap" type="ST_Wrap" use="optional"/>
135-
<xsd:attribute name="hAnchor" type="ST_HAnchor" use="optional"/>
136-
<xsd:attribute name="vAnchor" type="ST_VAnchor" use="optional"/>
137-
<xsd:attribute name="x" type="ST_SignedTwipsMeasure" use="optional"/>
138-
<xsd:attribute name="xAlign" type="s:ST_XAlign" use="optional"/>
139-
<xsd:attribute name="y" type="ST_SignedTwipsMeasure" use="optional"/>
140-
<xsd:attribute name="yAlign" type="s:ST_YAlign" use="optional"/>
141-
<xsd:attribute name="hRule" type="ST_HeightRule" use="optional"/>
142-
<xsd:attribute name="anchorLock" type="s:ST_OnOff" use="optional"/>
119+
<xsd:attribute name="dropCap" type="ST_DropCap"/>
120+
<xsd:attribute name="lines" type="ST_DecimalNumber"/>
121+
<xsd:attribute name="w" type="s:ST_TwipsMeasure"/>
122+
<xsd:attribute name="h" type="s:ST_TwipsMeasure"/>
123+
<xsd:attribute name="vSpace" type="s:ST_TwipsMeasure"/>
124+
<xsd:attribute name="hSpace" type="s:ST_TwipsMeasure"/>
125+
<xsd:attribute name="wrap" type="ST_Wrap"/>
126+
<xsd:attribute name="hAnchor" type="ST_HAnchor"/>
127+
<xsd:attribute name="vAnchor" type="ST_VAnchor"/>
128+
<xsd:attribute name="x" type="ST_SignedTwipsMeasure"/>
129+
<xsd:attribute name="xAlign" type="s:ST_XAlign"/>
130+
<xsd:attribute name="y" type="ST_SignedTwipsMeasure"/>
131+
<xsd:attribute name="yAlign" type="s:ST_YAlign"/>
132+
<xsd:attribute name="hRule" type="ST_HeightRule"/>
133+
<xsd:attribute name="anchorLock" type="s:ST_OnOff"/>
143134
</xsd:complexType>
144135

145136
<xsd:complexType name="CT_Ind">
146-
<xsd:attribute name="start" type="ST_SignedTwipsMeasure" use="optional"/>
147-
<xsd:attribute name="startChars" type="ST_DecimalNumber" use="optional"/>
148-
<xsd:attribute name="end" type="ST_SignedTwipsMeasure" use="optional"/>
149-
<xsd:attribute name="endChars" type="ST_DecimalNumber" use="optional"/>
150-
<xsd:attribute name="left" type="ST_SignedTwipsMeasure" use="optional"/>
151-
<xsd:attribute name="leftChars" type="ST_DecimalNumber" use="optional"/>
152-
<xsd:attribute name="right" type="ST_SignedTwipsMeasure" use="optional"/>
153-
<xsd:attribute name="rightChars" type="ST_DecimalNumber" use="optional"/>
154-
<xsd:attribute name="hanging" type="s:ST_TwipsMeasure" use="optional"/>
155-
<xsd:attribute name="hangingChars" type="ST_DecimalNumber" use="optional"/>
156-
<xsd:attribute name="firstLine" type="s:ST_TwipsMeasure" use="optional"/>
157-
<xsd:attribute name="firstLineChars" type="ST_DecimalNumber" use="optional"/>
137+
<xsd:attribute name="start" type="ST_SignedTwipsMeasure"/>
138+
<xsd:attribute name="startChars" type="ST_DecimalNumber"/>
139+
<xsd:attribute name="end" type="ST_SignedTwipsMeasure"/>
140+
<xsd:attribute name="endChars" type="ST_DecimalNumber"/>
141+
<xsd:attribute name="left" type="ST_SignedTwipsMeasure"/>
142+
<xsd:attribute name="leftChars" type="ST_DecimalNumber"/>
143+
<xsd:attribute name="right" type="ST_SignedTwipsMeasure"/>
144+
<xsd:attribute name="rightChars" type="ST_DecimalNumber"/>
145+
<xsd:attribute name="hanging" type="s:ST_TwipsMeasure"/>
146+
<xsd:attribute name="hangingChars" type="ST_DecimalNumber"/>
147+
<xsd:attribute name="firstLine" type="s:ST_TwipsMeasure"/>
148+
<xsd:attribute name="firstLineChars" type="ST_DecimalNumber"/>
158149
</xsd:complexType>
159150

160151
<xsd:complexType name="CT_Jc">
@@ -166,14 +157,14 @@ Schema excerpt
166157
</xsd:complexType>
167158

168159
<xsd:complexType name="CT_Spacing">
169-
<xsd:attribute name="before" type="s:ST_TwipsMeasure" use="optional"/>
170-
<xsd:attribute name="beforeLines" type="ST_DecimalNumber" use="optional"/>
171-
<xsd:attribute name="beforeAutospacing" type="s:ST_OnOff" use="optional"/>
172-
<xsd:attribute name="after" type="s:ST_TwipsMeasure" use="optional"/>
173-
<xsd:attribute name="afterLines" type="ST_DecimalNumber" use="optional"/>
174-
<xsd:attribute name="afterAutospacing" type="s:ST_OnOff" use="optional"/>
175-
<xsd:attribute name="line" type="ST_SignedTwipsMeasure" use="optional"/>
176-
<xsd:attribute name="lineRule" type="ST_LineSpacingRule" use="optional"/>
160+
<xsd:attribute name="before" type="s:ST_TwipsMeasure"/>
161+
<xsd:attribute name="beforeLines" type="ST_DecimalNumber"/>
162+
<xsd:attribute name="beforeAutospacing" type="s:ST_OnOff"/>
163+
<xsd:attribute name="after" type="s:ST_TwipsMeasure"/>
164+
<xsd:attribute name="afterLines" type="ST_DecimalNumber"/>
165+
<xsd:attribute name="afterAutospacing" type="s:ST_OnOff"/>
166+
<xsd:attribute name="line" type="ST_SignedTwipsMeasure"/>
167+
<xsd:attribute name="lineRule" type="ST_LineSpacingRule"/>
177168
</xsd:complexType>
178169

179170
<xsd:complexType name="CT_String">
@@ -182,7 +173,7 @@ Schema excerpt
182173

183174
<xsd:complexType name="CT_Tabs">
184175
<xsd:sequence>
185-
<xsd:element name="tab" type="CT_TabStop" minOccurs="1" maxOccurs="unbounded"/>
176+
<xsd:element name="tab" type="CT_TabStop" maxOccurs="unbounded"/>
186177
</xsd:sequence>
187178
</xsd:complexType>
188179

docx/oxml/shared.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,6 @@ def new(cls, nsptagname, val):
5454
elm.val = val
5555
return elm
5656

57-
@classmethod
58-
def new_pStyle(cls, val):
59-
"""
60-
Return a new ``<w:pStyle>`` element with ``val`` attribute set to
61-
*val*.
62-
"""
63-
pStyle = OxmlElement('w:pStyle')
64-
pStyle.val = val
65-
return pStyle
66-
6757
@classmethod
6858
def new_rStyle(cls, val):
6959
"""

docx/oxml/text.py

Lines changed: 22 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
(CT_R).
66
"""
77

8-
from . import parse_xml, OxmlElement
8+
from . import OxmlElement
99
from ..enum.text import WD_UNDERLINE
10-
from .ns import nsdecls, qn
10+
from .ns import qn
1111
from .shared import CT_String
1212
from .simpletypes import ST_BrClear, ST_BrType
1313
from .xmlchemy import (
@@ -59,52 +59,24 @@ class CT_PPr(BaseOxmlElement):
5959
"""
6060
``<w:pPr>`` element, containing the properties for a paragraph.
6161
"""
62-
def get_or_add_numPr(self):
63-
"""
64-
Return the numPr child element, newly added if not present.
65-
"""
66-
numPr = self.numPr
67-
if numPr is None:
68-
numPr = self._add_numPr()
69-
return numPr
62+
__child_sequence__ = (
63+
'w:pStyle', 'w:keepNext', 'w:keepLines', 'w:pageBreakBefore',
64+
'w:framePr', 'w:widowControl', 'w:numPr', 'w:suppressLineNumbers',
65+
'w:pBdr', 'w:shd', 'w:tabs', 'w:suppressAutoHyphens', 'w:kinsoku',
66+
'w:wordWrap', 'w:overflowPunct', 'w:topLinePunct', 'w:autoSpaceDE',
67+
'w:autoSpaceDN', 'w:bidi', 'w:adjustRightInd', 'w:snapToGrid',
68+
'w:spacing', 'w:ind', 'w:contextualSpacing', 'w:mirrorIndents',
69+
'w:suppressOverlap', 'w:jc', 'w:textDirection', 'w:textAlignment',
70+
'w:textboxTightWrap', 'w:outlineLvl', 'w:divId', 'w:cnfStyle',
71+
'w:rPr', 'w:sectPr', 'w:pPrChange'
72+
)
73+
pStyle = ZeroOrOne('w:pStyle')
74+
numPr = ZeroOrOne('w:numPr', successors=__child_sequence__[7:])
7075

71-
def get_or_add_pStyle(self):
72-
"""
73-
Return the pStyle child element, newly added if not present.
74-
"""
75-
pStyle = self.pStyle
76-
if pStyle is None:
77-
pStyle = self._add_pStyle()
76+
def _insert_pStyle(self, pStyle):
77+
self.insert(0, pStyle)
7878
return pStyle
7979

80-
@staticmethod
81-
def new():
82-
"""
83-
Return a new ``<w:pPr>`` element.
84-
"""
85-
xml = '<w:pPr %s/>' % nsdecls('w')
86-
pPr = parse_xml(xml)
87-
return pPr
88-
89-
@property
90-
def numPr(self):
91-
"""
92-
``<w:numPr>`` child element or None if not present.
93-
"""
94-
return self.find(qn('w:numPr'))
95-
96-
@property
97-
def pStyle(self):
98-
"""
99-
``<w:pStyle>`` child element or None if not present.
100-
"""
101-
return self.find(qn('w:pStyle'))
102-
103-
def remove_pStyle(self):
104-
pStyle = self.pStyle
105-
if pStyle is not None:
106-
self.remove(pStyle)
107-
10880
@property
10981
def style(self):
11082
"""
@@ -114,7 +86,7 @@ def style(self):
11486
pStyle = self.pStyle
11587
if pStyle is None:
11688
return None
117-
return pStyle.get(qn('w:val'))
89+
return pStyle.val
11890

11991
@style.setter
12092
def style(self, style):
@@ -124,35 +96,10 @@ def style(self, style):
12496
element if present.
12597
"""
12698
if style is None:
127-
self.remove_pStyle()
128-
elif self.pStyle is None:
129-
self._add_pStyle(style)
130-
else:
131-
self.pStyle.val = style
132-
133-
def _add_numPr(self):
134-
numPr = OxmlElement('w:numPr')
135-
return self._insert_numPr(numPr)
136-
137-
def _add_pStyle(self, style):
138-
pStyle = CT_String.new_pStyle(style)
139-
return self._insert_pStyle(pStyle)
140-
141-
def _insert_numPr(self, numPr):
142-
return self.insert_element_before(
143-
numPr, 'w:suppressLineNumbers', 'w:pBdr', 'w:shd', 'w:tabs',
144-
'w:suppressAutoHyphens', 'w:kinsoku', 'w:wordWrap',
145-
'w:overflowPunct', 'w:topLinePunct', 'w:autoSpaceDE',
146-
'w:autoSpaceDN', 'w:bidi', 'w:adjustRightInd', 'w:snapToGrid',
147-
'w:spacing', 'w:ind', 'w:contextualSpacing', 'w:mirrorIndents',
148-
'w:suppressOverlap', 'w:jc', 'w:textDirection',
149-
'w:textAlignment', 'w:textboxTightWrap', 'w:outlineLvl',
150-
'w:divId', 'w:cnfStyle', 'w:rPr', 'w:sectPr', 'w:pPrChange'
151-
)
152-
153-
def _insert_pStyle(self, pStyle):
154-
self.insert(0, pStyle)
155-
return pStyle
99+
self._remove_pStyle()
100+
return
101+
pStyle = self.get_or_add_pStyle()
102+
pStyle.val = style
156103

157104

158105
class CT_R(BaseOxmlElement):

tests/oxml/test_text.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
Test suite for the docx.oxml.text module.
55
"""
66

7-
from docx.oxml.text import CT_PPr, CT_R, CT_Text
7+
from docx.oxml.text import CT_R, CT_Text
88

99
from .unitdata.text import a_p, a_pPr, a_pStyle, a_t, an_r
1010

@@ -53,11 +53,6 @@ def it_can_set_its_paragraph_style(self):
5353

5454
class DescribeCT_PPr(object):
5555

56-
def it_can_construct_a_new_pPr_element(self):
57-
pPr = CT_PPr.new()
58-
expected_xml = a_pPr().with_nsdecls().xml()
59-
assert pPr.xml == expected_xml
60-
6156
def it_knows_the_paragraph_style(self):
6257
cases = (
6358
(a_pPr(), None),

0 commit comments

Comments
 (0)