我正在Pelican(初学者)建立一个网站,我正在尝试对我的电子邮件地址进行编码。我的电子邮件地址在联系人页面上使用,您可以在该页面上单击图像以启动打开电子邮件(其中已经包含一些内容)。
我的contact.rst文件包括:
原始::html
<a href="mailto:petsy@petsy-fink.com?subject=Inquiry%20about%20a%20photo%20shooting&body=Name%3A%20%0AEmail%3A%20%0ACell%20phone%20number%3A%20%0AType%20of%20shooting%3A%20%0AEvent%20date%3A%0AEvent%20Time%20(from%2Funtil)%3A%20%0APhotographer%20required%20(from%2Funtil)%3A%20%0ALocation%20and%20Country%3A%20%0AReferral%20from%3A%20%0AMessage%3A%0A"><img src="theme/images/nav/contact_image_en_900W.jpg"></a>
它工作得很好,但没有保持编码。在页面源中,它显示了我的真实电子邮件地址。我错过了什么?非常感谢。
当pelican创建html文件时,它会重写所有内容,甚至是html源。这就是为什么你的电子邮件在没有十六进制编码的情况下被重写的原因。
你可以试试下面这个简单的插件。只需将__init.py__
和hide_links.py
放在插件目录中的文件夹hide_links
中,然后在pelicanconf.py
中添加hide_links
作为PLUGINS
值的最后一个条目。重要的是要把这个插件放在最后,以避免邮件地址被混淆后html文件被pelican重写。现在只需将您的邮件地址写入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 *
这种编码方式不会隐藏您的电子邮件地址,因为当浏览器读取电子邮件时,它会对其进行解码。
无论你如何编码你的电子邮件,都会有一种方法让人们解码并阅读。如果他们想找到它,他们可以。
如果你只是想对机器人隐藏它,你可以使用javascript .replace()
函数来切换一些字母。但是,如果用户禁用了javascript,他们将无法使用表单。