Skip to content

Commit

Permalink
without global mimetype override
Browse files Browse the repository at this point in the history
  • Loading branch information
bmispelon committed May 8, 2024
1 parent 6b599de commit 855b5f2
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions django/utils/feedgenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,26 +58,32 @@ def get_tag_uri(url, date):
return "tag:%s%s:%s/%s" % (bits.hostname, d, bits.path, bits.fragment)


def _guess_stylesheet_mimetype(url):
"""
Return the given stylesheet's mimetype tuple, using a slightly custom
version of Python's mimetypes.guess_type().
"""
mimetypedb = mimetypes.MimeTypes()

# The official mimetype for XSLT files is technically `application/xslt+xml`
# but as of 2024 almost no browser supports that (they all expect text/xsl).
# On top of that, windows seems to assume that the type for xsl is text/xml.
mimetypedb.readfp(StringIO("text/xsl\txsl\ntext/xsl\txslt"))

return mimetypedb.guess_type(url)


NOT_PROVIDED = object()


class Stylesheet:
"""An RSS stylesheet"""

MIMETYPE_OVERRIDES = {
# The official mimetype for XSLT files is technically `application/xslt+xml`
# but as of 2024 almost no browser support that.
"application/xslt+xml": "text/xsl",
}

def __init__(self, url, mimetype=NOT_PROVIDED, media="screen"):
self.url = iri_to_uri(url)

if mimetype is NOT_PROVIDED:
mimetypes.add_type("application/xslt+xml", ".xsl")
mimetypes.add_type("application/xslt+xml", ".xslt")
mimetype, _ = mimetypes.guess_type(self.url)
mimetype = self.MIMETYPE_OVERRIDES.get(mimetype, mimetype)
mimetype, _ = _guess_stylesheet_mimetype(self.url)

self.mimetype = mimetype
self.media = media
Expand Down

0 comments on commit 855b5f2

Please sign in to comment.