虽然 pelican 创建 html 文件,但它会重写所有内容,甚至是 html 源代码。这就是您的电子邮件在没有十六进制编码的情况下被重写的原因。
您可以尝试以下简单的插件。只需将__init.py__
and放在插件目录内hide_links.py
的文件夹中,然后将其添加为值中的最后一个条目。重要的是把这个插件放在最后,以避免邮件地址被混淆后 pelican 重写 html 文件。现在只需将您的邮件地址写入没有十六进制编码。hide_links
pelicanconf.py
hide_links
PLUGINS
contact.rst
hide_links.py
:
"""
Hide Links
----------
adds the possibility to obfuscate email addresses with a random mixture of
ordinal and hexadecimal chars
"""
from pelican import signals
from bs4 import BeautifulSoup
import random
import re
def obfuscate_string(value):
''' Obfuscate mail addresses
see http://www.web2pyslices.com/slice/show/1528/obfuscation-of-text-and-email-addresses
'''
result = ''
for char in value:
r = random.random()
# Roughly 20% raw, 40% hex, 40% dec.
# '@' and '_' are always encoded.
if r > 0.8 and char not in "@_":
result += char
elif r < 0.4:
result += '&#%s;' % hex(ord(char))[1:]
else:
result += '&#%s;' % str(ord(char))
return result
def hide_links(htmlDoc, context='localcontext'):
with open(htmlDoc,'r') as filehandle:
soup = BeautifulSoup(filehandle.read())
for link in soup.find_all('a', href=re.compile('mailto')):
link['href'] = obfuscate_string(link['href'])
link.contents[0].replaceWith(obfuscate_string(link.contents[0]))
title = link.get('title')
if title:
link['title'] = obfuscate_string(title)
html = soup.decode(formatter=None)
html = html.encode('utf-8')
with open(htmlDoc, "wb") as file:
file.write(html)
def register():
signals.content_written.connect(hide_links)
__init.py__
:
from .hide_links import *