Skip to content
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ bug report!
* `Aaron Swartz <http://www.aaronsw.com/>`_
* `Jakub Wilk <http://jwilk.net/>`_
* `Nestor Rodriguez <https://github.com/n3s7or>`_
* `Roman Zubov <https://github.com/romazu>`_
18 changes: 18 additions & 0 deletions feedparser/mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,14 @@ def pop(self, element, strip_whitespace=1):
else:
if element == "description":
element = "summary"

# Store the element in author/contributor dict when inside author
# context (for custom namespace elements)
author_context = self._maybe_get_author_context()
if author_context is not None:
author_context[element] = output
return output # Skip entry-level storage

old_value_depth = self.property_depth_map.setdefault(
self.entries[-1], {}
).get(element)
Expand Down Expand Up @@ -767,6 +775,16 @@ def _get_context(self):
context = self.feeddata
return context

def _maybe_get_author_context(self):
"""Get current author/contributor dict if inside one, else None."""
if self.inentry:
entry = self.entries[-1]
if self.inauthor and entry.get("authors"):
return entry["authors"][-1]
if self.incontributor and entry.get("contributors"):
return entry["contributors"][-1]
return None

def _save_author(self, key, value, prefix="author"):
context = self._get_context()
context.setdefault(prefix + "_detail", FeedParserDict())
Expand Down
6 changes: 4 additions & 2 deletions feedparser/namespaces/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,9 @@ def _start_author(self, attrs_d):
_start_managingeditor = _start_author

def _end_author(self):
self.pop("author")
# Exit author context before pop()
self.inauthor = 0
self.pop("author")
self._sync_author_detail()

_end_managingeditor = _end_author
Expand All @@ -160,8 +161,9 @@ def _start_contributor(self, attrs_d):
self.push("contributor", 0)

def _end_contributor(self):
self.pop("contributor")
# Exit contributor context before pop()
self.incontributor = 0
self.pop("contributor")

def _start_name(self, attrs_d):
self.push("name", 0)
Expand Down
13 changes: 13 additions & 0 deletions tests/wellformed/atom10/entry_author_custom_element.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!--
Description: custom namespace element inside author is stored in author dict
Expect: not bozo and entries[0]['authors'][0]['arxiv_affiliation'] == 'MIT'
-->
<feed xmlns="http://www.w3.org/2005/Atom"
xmlns:arxiv="http://arxiv.org/schemas/atom">
<entry>
<author>
<name>Example author</name>
<arxiv:affiliation>MIT</arxiv:affiliation>
</author>
</entry>
</feed>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!--
Description: custom namespace element inside author is NOT stored at entry level
Expect: not bozo and 'arxiv_affiliation' not in entries[0]
-->
<feed xmlns="http://www.w3.org/2005/Atom"
xmlns:arxiv="http://arxiv.org/schemas/atom">
<entry>
<author>
<name>Example author</name>
<arxiv:affiliation>MIT</arxiv:affiliation>
</author>
</entry>
</feed>
17 changes: 17 additions & 0 deletions tests/wellformed/atom10/entry_authors_custom_elements.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!--
Description: multiple authors each with custom namespace element
Expect: not bozo and entries[0]['authors'][0]['arxiv_affiliation'] == 'MIT' and entries[0]['authors'][1]['arxiv_affiliation'] == 'Stanford'
-->
<feed xmlns="http://www.w3.org/2005/Atom"
xmlns:arxiv="http://arxiv.org/schemas/atom">
<entry>
<author>
<name>Alice</name>
<arxiv:affiliation>MIT</arxiv:affiliation>
</author>
<author>
<name>Bob</name>
<arxiv:affiliation>Stanford</arxiv:affiliation>
</author>
</entry>
</feed>
13 changes: 13 additions & 0 deletions tests/wellformed/atom10/entry_contributor_custom_element.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!--
Description: custom namespace element inside contributor is stored in contributor dict
Expect: not bozo and entries[0]['contributors'][0]['custom_role'] == 'editor'
-->
<feed xmlns="http://www.w3.org/2005/Atom"
xmlns:custom="http://example.org/custom">
<entry>
<contributor>
<name>Bob Helper</name>
<custom:role>editor</custom:role>
</contributor>
</entry>
</feed>