Skip to content

Commit d608320

Browse files
tiberlas=
authored andcommitted
footnotes: clean up logick
1 parent b960fbc commit d608320

File tree

6 files changed

+8
-14
lines changed

6 files changed

+8
-14
lines changed

src/docx/document.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ def _calculate_next_footnote_reference_id(self, p):
208208
new_fr_id = 1
209209
# If paragraph already contains footnotes
210210
# append the new footnote and the end with the next reference id.
211-
if p.footnote_reference_ids is not None:
211+
if len(p.footnote_reference_ids) > 0:
212212
new_fr_id = p.footnote_reference_ids[-1] + 1
213213
# Read the paragraphs containing footnotes and find where the
214214
# new footnote will be. Keeping in mind that the footnotes are
@@ -224,7 +224,7 @@ def _calculate_next_footnote_reference_id(self, p):
224224
has_passed_containing_para = True
225225
continue
226226
# Skip paragraphs without footnotes (they don't impact new id).
227-
if self.paragraphs[p_i]._p.footnote_reference_ids is None:
227+
if len(self.paragraphs[p_i]._p.footnote_reference_ids) == 0:
228228
continue
229229
# These footnotes are after the new footnote, so we increment them.
230230
if not has_passed_containing_para:

src/docx/footnotes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def add_footnote(self, footnote_reference_id):
3535
"""
3636
elements = self._element # for easy access
3737
new_footnote = None
38-
if elements.get_by_id(footnote_reference_id):
38+
if elements.get_by_id(footnote_reference_id) is not None:
3939
# When adding a footnote it can be inserted
4040
# in front of some other footnotes, so
4141
# we need to sort footnotes by `footnote_reference_id`

src/docx/oxml/footnote.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def add_footnote(self, footnote_reference_id):
2424
return new_f
2525

2626
def get_by_id(self, id):
27-
found = self.xpath('w:footnote[@w:id="%s"]' % id)
27+
found = self.xpath(f'w:footnote[@w:id="{id}"]')
2828
if not found:
2929
return None
3030
return found[0]
@@ -56,6 +56,4 @@ def paragraphs(self):
5656
for child in self:
5757
if child.tag == qn('w:p'):
5858
paragraphs.append(child)
59-
if paragraphs == []:
60-
paragraphs = None
6159
return paragraphs

src/docx/oxml/section.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ def footnote_number_format(self):
248248
attribute is not present.
249249
"""
250250
fPr = self.footnotePr
251-
if fPr is None or fPr.numFmt:
251+
if fPr is None or fPr.numFmt is None:
252252
return None
253253
return fPr.numFmt.val
254254

@@ -266,7 +266,7 @@ def footnote_numbering_restart_location(self):
266266
attribute is not present.
267267
"""
268268
fPr = self.footnotePr
269-
if fPr is None or fPr.numRestart:
269+
if fPr is None or fPr.numRestart is None:
270270
return None
271271
return fPr.numRestart.val
272272

@@ -291,7 +291,7 @@ def footnote_numbering_start_value(self):
291291
attribute is not present.
292292
"""
293293
fPr = self.footnotePr
294-
if fPr is None or fPr.numStart:
294+
if fPr is None or fPr.numStart is None:
295295
return None
296296
return fPr.numStart.val
297297

src/docx/oxml/text/paragraph.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,6 @@ def footnote_reference_ids(self):
8181
new_footnote_ids = run.footnote_reference_ids
8282
if new_footnote_ids:
8383
footnote_ids.extend(new_footnote_ids)
84-
if footnote_ids == []:
85-
footnote_ids = None
8684
return footnote_ids
8785

8886
def set_sectPr(self, sectPr: CT_SectPr):

src/docx/text/paragraph.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,9 @@ def footnotes(self):
9595
Returns a list of |Footnote| instances that refers to the footnotes in this paragraph,
9696
or |None| if none footnote is defined.
9797
"""
98+
footnote_list = []
9899
reference_ids = self._p.footnote_reference_ids
99-
if reference_ids == None:
100-
return None
101100
footnotes = self._parent._parent.footnotes
102-
footnote_list = []
103101
for ref_id in reference_ids:
104102
footnote_list.append(footnotes[ref_id])
105103
return footnote_list

0 commit comments

Comments
 (0)