Skip to content

Commit 4563ddf

Browse files
committed
going back to the previous gravatar directive
1 parent 149c36c commit 4563ddf

File tree

4 files changed

+6
-78
lines changed

4 files changed

+6
-78
lines changed

blog/members/hellhound.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ lanaguage: Español
1212
```{gravatar} jean.pierre@chauvel.org
1313
---
1414
width: 200
15-
style: "margin: 0 auto; display: block;"
16-
with-circle-clip: true
17-
with-grayscale: true
15+
style: "margin: 0 auto; display: block; border-radius: 200px;"
1816
---
1917
```
2018

ext/gravatar.py

Lines changed: 4 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,24 @@
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
94
from docutils import nodes
105
from docutils.parsers.rst import Directive
116
from jinja2 import BaseLoader, Environment
127
from libgravatar import Gravatar
13-
from PIL import Image, ImageDraw
148
from sphinx.application import Sphinx
159
from sphinx.util import logging
1610

1711
logger = logging.getLogger(__name__)
1812

1913
GRAVATAR_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

4024
class 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,

poetry.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ sphinx-togglebutton = "^0.3.2"
1919
sphinx-sitemap = "^2.5.1"
2020
sphinx-tabs = "^3.4.5"
2121
libgravatar = "^1.0.4"
22-
pillow = "^10.3.0"
2322
sphinxext-opengraph = "^0.9.1"
2423
matplotlib = "^3.8.4"
2524

0 commit comments

Comments
 (0)