11import warnings
22
33from defusedxml import minidom
4+ from xml .dom import Node
45from xml .dom .minidom import Document , Element
56from typing import BinaryIO , Any , Union
67from bs4 import BeautifulSoup
2728 ".xml" ,
2829]
2930
31+ XHTML_NAMESPACE = "http://www.w3.org/1999/xhtml"
32+
3033
3134class RssConverter (DocumentConverter ):
3235 """Convert RSS / Atom type to markdown"""
@@ -119,9 +122,9 @@ def _parse_atom_type(
119122 md_text += f"{ subtitle } \n "
120123 for entry in entries :
121124 entry_title = self ._get_data_by_tag_name (entry , "title" )
122- entry_summary = self ._get_data_by_tag_name (entry , "summary" )
125+ entry_summary = self ._get_atom_content (entry , "summary" )
123126 entry_updated = self ._get_data_by_tag_name (entry , "updated" )
124- entry_content = self ._get_data_by_tag_name (entry , "content" )
127+ entry_content = self ._get_atom_content (entry , "content" )
125128
126129 if entry_title :
127130 md_text += f"\n ## { entry_title } \n "
@@ -137,6 +140,37 @@ def _parse_atom_type(
137140 title = title ,
138141 )
139142
143+ def _get_atom_content (self , entry : Element , tag_name : str ) -> Union [str , None ]:
144+ nodes = entry .getElementsByTagName (tag_name )
145+ if not nodes :
146+ return None
147+
148+ node = nodes [0 ]
149+ if node .getAttribute ("type" ).lower () != "xhtml" :
150+ return self ._get_data_by_tag_name (entry , tag_name )
151+
152+ return "" .join (
153+ self ._localize_xhtml_names (child .cloneNode (True )).toxml ()
154+ for child in node .childNodes
155+ if child .nodeType == Node .ELEMENT_NODE
156+ )
157+
158+ def _localize_xhtml_names (self , node : Node ) -> Node :
159+ """Rewrite prefixed XHTML element names to their local HTML names.
160+
161+ Atom permits XHTML content to be namespace-prefixed (e.g. ``x:strong``).
162+ The downstream HTML converter dispatches on HTML tag names, so the
163+ prefix has to be dropped or the element is treated as an unknown tag
164+ and its formatting is lost.
165+ """
166+ if node .nodeType == Node .ELEMENT_NODE :
167+ if node .prefix and node .namespaceURI == XHTML_NAMESPACE :
168+ node .tagName = node .nodeName = node .localName
169+ node .prefix = None
170+ for child in node .childNodes :
171+ self ._localize_xhtml_names (child )
172+ return node
173+
140174 def _parse_rss_type (
141175 self , doc : Document , * , strict : bool = False
142176 ) -> DocumentConverterResult :
0 commit comments