11"""A directive to generate an image tag with a Gravatar pic.
22"""
33
4- import os
5- import urllib .request
6- from hashlib import sha256
7-
8- from ablog .commands import find_confdir , read_conf
94from docutils import nodes
105from docutils .parsers .rst import Directive
116from jinja2 import BaseLoader , Environment
127from libgravatar import Gravatar
13- from PIL import Image , ImageDraw
148from sphinx .application import Sphinx
159from sphinx .util import logging
1610
1711logger = logging .getLogger (__name__ )
1812
1913GRAVATAR_TEMPLATE = """
2014<div class="{{ klass }}">
21- <img src=/ {{ url }}
15+ <img src={{ url }}
2216 {% if align %} align="{{ align }}"{% endif %}
2317 {% if klass %} class="{{ klass }}"{% endif %}
2418 {% if style %} style="{{ style }}"{% endif %}
2519 {% if width %} width="{{ width }}" height="{{ width }}"{% endif %}>
2620</div>
2721"""
28- FILENAME_EXT = ".png"
29-
30-
31- class GravatarError (Exception ):
32- pass
33-
34-
35- def _to_boolean (argument : str ) -> str :
36- # Weird behavior, but true~
37- return argument is None
3822
3923
4024class GravatarImage (Directive ):
@@ -46,80 +30,27 @@ class GravatarImage(Directive):
4630 "class" : lambda a : a .strip (),
4731 "style" : lambda a : a .strip (),
4832 "width" : lambda a : a .strip (),
49- "with-circle-clip" : _to_boolean ,
50- "with-grayscale" : _to_boolean ,
51- "static-subdir" : lambda a : a .strip (),
5233 }
5334
54- def _process_image (
55- self , filepath : str , with_circle_clip : bool , with_grayscale : bool
56- ):
57- original_image = Image .open (filepath )
58-
59- if with_grayscale :
60- # Convert the original image to grayscale
61- original_image = original_image .convert ("L" )
62-
63- if with_circle_clip :
64- # Create a mask image with a white circle on a black background
65- mask = Image .new ("L" , original_image .size , 0 )
66- draw = ImageDraw .Draw (mask )
67- width , height = original_image .size
68- draw .ellipse ((0 , 0 , width , height ), fill = 255 )
69-
70- # Convert the mask to use an alpha channel
71- result = original_image .copy ()
72- result .putalpha (mask )
73- else :
74- result = original_image
75- result .save (filepath )
76-
7735 def run (self ):
7836 email = self .content [0 ]
7937 align = self .options .get ("align" )
8038 klass = self .options .get ("class" )
8139 style = self .options .get ("style" )
8240 width = self .options .get ("width" )
83- with_circle_clip = self .options .get ("with-circle-clip" )
84- with_grayscale = self .options .get ("with-grayscale" )
85- static_subdir = self .options .get ("static-subdir" , "images/gravatar" )
86- confdir = find_confdir ()
87- conf = read_conf (confdir )
88- html_static_path = getattr (conf , "html_static_path" , [])
89-
90- if len (html_static_path ) == 0 :
91- raise GravatarError (
92- "html_static_path should have at least one path configured"
93- )
94-
95- # We choose the first path as the default path for the image
96- save_path = os .path .join (html_static_path [0 ], static_subdir )
9741
98- # Try to make the dir if it doesn't exist
99- os .makedirs (save_path , exist_ok = True )
100-
101- logger .info (f"Gravatar: Getting Gravatar image for email: { email } " )
42+ logger .info (f"Getting Gravatar image for email: { email } " )
10243 url = Gravatar (email ).get_image ()
10344 if width is not None :
10445 url = f"{ url } ?s={ width } "
105- logger .info (f"Gravatar: Requesting Gravatar image from URL: { url } " )
106-
107- filename = sha256 (url .encode ()).digest ().hex ()
108- filename = f"{ filename } { FILENAME_EXT } "
109- save_path = os .path .join (save_path , filename )
110- urllib .request .urlretrieve (url , save_path )
111- logger .info (f"Gravatar: Retrieving image into: { save_path } " )
112-
113- if with_circle_clip or with_grayscale :
114- self ._process_image (save_path , with_circle_clip , with_grayscale )
115- logger .info ("Gravatar: Applying image post-processing" )
46+ logger .info (f"Got image URL: { url } " )
11647
11748 template = Environment (
11849 loader = BaseLoader , trim_blocks = True , lstrip_blocks = True
11950 ).from_string (GRAVATAR_TEMPLATE )
12051
12152 out = template .render (
122- url = save_path ,
53+ url = url ,
12354 align = align ,
12455 klass = klass ,
12556 style = style ,
0 commit comments