1+ import xml .etree .ElementTree as etree
12from urllib .parse import urlparse
23
4+ import markdown .treeprocessors
35from django .conf import settings
46from django .core .cache import caches
57from django .db import models
1416from docutils .core import publish_parts
1517from docutils .nodes import document
1618from docutils .writers .html4css1 import HTMLTranslator , Writer
17-
18- from markdown import markdown , Markdown
19+ from markdown import Markdown , markdown
1920from markdown .extensions .toc import TocExtension , slugify as _md_title_slugify
20- import markdown .treeprocessors
21- import xml .etree .ElementTree as etree
2221
2322BLOG_DOCUTILS_SETTINGS = {
2423 "doctitle_xform" : False ,
@@ -42,19 +41,25 @@ def published(self):
4241 def active (self ):
4342 return self .filter (is_active = True )
4443
45- _IMG_LAZY_ATTRIBUTES = {"loading" :"lazy" }
44+
45+ _IMG_LAZY_ATTRIBUTES = {"loading" : "lazy" }
46+
4647
4748class LazyImageHTMLTranslator (HTMLTranslator ):
4849 """Alter the img tags to include the lazy attribute."""
49- def __init__ (self , document : document , img_attributes : dict [str ,str ]| None = None ) -> None :
50+
51+ def __init__ (
52+ self , document : document , img_attributes : dict [str , str ] | None = None
53+ ) -> None :
5054 super ().__init__ (document )
51- self ._img_attributes = img_attributes or _IMG_LAZY_ATTRIBUTES
52-
53- def emptytag (self , node , tagname , suffix = ' \n ' , ** attributes ):
55+ self ._img_attributes = img_attributes or _IMG_LAZY_ATTRIBUTES
56+
57+ def emptytag (self , node , tagname , suffix = " \n " , ** attributes ):
5458 """Construct and return an XML-compatible empty tag."""
55- if tagname == "img" :
59+ if tagname == "img" :
5660 attributes .update (self ._img_attributes )
57- return super ().emptytag (node ,tagname ,suffix ,** attributes )
61+ return super ().emptytag (node , tagname , suffix , ** attributes )
62+
5863
5964class LazyImageTreeprocessor (markdown .treeprocessors .Treeprocessor ):
6065 """
@@ -63,13 +68,16 @@ class LazyImageTreeprocessor(markdown.treeprocessors.Treeprocessor):
6368 This processor will add loading=lazy attribute on img tags
6469
6570 """
66- def __init__ (self , img_attributes : dict [str ,str ]| None = None , md : Markdown | None = None ) -> None :
71+
72+ def __init__ (
73+ self , img_attributes : dict [str , str ] | None = None , md : Markdown | None = None
74+ ) -> None :
6775 super ().__init__ (md )
68- self ._img_attributes = img_attributes or _IMG_LAZY_ATTRIBUTES
76+ self ._img_attributes = img_attributes or _IMG_LAZY_ATTRIBUTES
6977
7078 def run (self , root : etree .Element ) -> etree .Element | None :
7179 """Alter img tags with the supplemental attributes."""
72- for img_elem in root .iter (' img' ):
80+ for img_elem in root .iter (" img" ):
7381 img_elem .attrib .update (self ._img_attributes )
7482
7583
@@ -86,8 +94,8 @@ def to_html(cls, fmt, source):
8694 if not fmt or fmt == cls .HTML :
8795 return source
8896 if fmt == cls .REST :
89- writer = Writer ()
90- writer .translator_class = LazyImageHTMLTranslator
97+ writer = Writer ()
98+ writer .translator_class = LazyImageHTMLTranslator
9199
92100 return publish_parts (
93101 source = source ,
@@ -102,7 +110,7 @@ def to_html(cls, fmt, source):
102110 TocExtension (baselevel = 3 , slugify = _md_slugify ),
103111 ],
104112 )
105- md .treeprocessors .register (LazyImageTreeprocessor (),"lazyimage" ,0.3 )
113+ md .treeprocessors .register (LazyImageTreeprocessor (), "lazyimage" , 0.3 )
106114 return md .convert (source )
107115 raise ValueError (f"Unsupported format { fmt } " )
108116
0 commit comments