From dcd8f11a7d24abb7a4f69a8ab1f9df31ad67b9e3 Mon Sep 17 00:00:00 2001 From: goodger Date: Fri, 22 Apr 2005 23:57:18 +0000 Subject: Added "cloak_email_addresses" setting & support; updated test & docs. Thanks to Barry Warsaw & Ned Batchelder for the idea and initial patch. git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk@3243 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/HISTORY.txt | 1 + docutils/THANKS.txt | 1 + docutils/docs/user/config.txt | 13 ++++++++++ docutils/docutils/writers/html4css1.py | 32 +++++++++++++++++++++++-- docutils/test/functional/expected/pep_html.html | 29 +++++++++++++++++----- docutils/test/functional/input/pep_html.txt | 11 ++++++++- docutils/test/functional/tests/pep_html.py | 1 + 7 files changed, 79 insertions(+), 9 deletions(-) diff --git a/docutils/HISTORY.txt b/docutils/HISTORY.txt index 95af46972..7218ac5bc 100644 --- a/docutils/HISTORY.txt +++ b/docutils/HISTORY.txt @@ -135,6 +135,7 @@ Changes Since 0.3.7 support. - Added support for table stub columns. - Added support for ``align`` attribute on ``figure`` elements. + - Added the ``cloak_email_addresses`` setting & support. * docutils/writers/latex2e.py: diff --git a/docutils/THANKS.txt b/docutils/THANKS.txt index 91b90f259..26b6e9114 100644 --- a/docutils/THANKS.txt +++ b/docutils/THANKS.txt @@ -17,6 +17,7 @@ donations, tasty treats, and related projects: * Aahz * David Abrahams * David Ascher +* Ned Batchelder * Heiko Baumann * Eric Bellot * Ian Bicking diff --git a/docutils/docs/user/config.txt b/docutils/docs/user/config.txt index 7e93626a1..ebff78289 100644 --- a/docutils/docs/user/config.txt +++ b/docutils/docs/user/config.txt @@ -527,6 +527,19 @@ attribution __ `attribution [latex2e writer]`_ +_`cloak_email_addresses` + Scramble email addresses to confuse harvesters. In the visible + text of an email address, the "@" will be replaced by "at", and + all periods (".") will be replaced by "dot", with spaces added. + In the reference URI, the address will be replaced by %-escapes. + For example, "abc@example.org" will be output as:: + + + abc at example dot org + + Default: don't cloak (None). Option: ``--cloak-email``. + _`compact_lists` Remove extra vertical whitespace between items of bullet lists and enumerated lists, when list items are "simple" (i.e., all items diff --git a/docutils/docutils/writers/html4css1.py b/docutils/docutils/writers/html4css1.py index cdc8bf583..cdb3f7f88 100644 --- a/docutils/docutils/writers/html4css1.py +++ b/docutils/docutils/writers/html4css1.py @@ -101,7 +101,12 @@ class Writer(writers.Writer): ('Omit the XML declaration. Use with caution.', ['--no-xml-declaration'], {'dest': 'xml_declaration', 'default': 1, 'action': 'store_false', - 'validator': frontend.validate_boolean}),)) + 'validator': frontend.validate_boolean}), + ('Scramble email addresses to confuse harvesters. ' + 'For example, "abc@example.org" will become ' + '``abc at example dot org``.', + ['--cloak-email-addresses'], + {'action': 'store_true', 'validator': frontend.validate_boolean}),)) relative_path_settings = ('stylesheet_path',) @@ -240,6 +245,7 @@ class HTMLTranslator(nodes.NodeVisitor): self.header = [] self.footer = [] self.in_document_title = 0 + self.in_mailto = 0 def astext(self): return ''.join(self.head_prefix + self.head @@ -259,6 +265,20 @@ class HTMLTranslator(nodes.NodeVisitor): text = text.replace(u'\u00a0', " ") return text + def cloak_mailto(self, uri): + """Try to hide a mailto: URL from harvesters.""" + addr = uri.split(':', 1)[1] + if '?' in addr: + addr, query = addr.split('?', 1) + query = '?' + query + else: + query = '' + escaped = ['%%%02X' % ord(c) for c in addr] + return 'mailto:%s%s' % (''.join(escaped), query) + + def cloak_email(self, addr): + return addr.replace('@', ' at ').replace('.', ' dot ') + def attval(self, text, whitespace=re.compile('[\n\r\t\v\f]')): """Cleanse, HTML encode, and return attribute value text.""" @@ -315,7 +335,10 @@ class HTMLTranslator(nodes.NodeVisitor): node[-1]['classes'].append('last') def visit_Text(self, node): - self.body.append(self.encode(node.astext())) + text = node.astext() + if self.in_mailto and self.settings.cloak_email_addresses: + text = self.cloak_email(text) + self.body.append(self.encode(text)) def depart_Text(self, node): pass @@ -1086,6 +1109,10 @@ class HTMLTranslator(nodes.NodeVisitor): self.context.append('\n') if node.has_key('refuri'): href = node['refuri'] + if ( self.settings.cloak_email_addresses + and href.startswith('mailto:')): + href = self.cloak_mailto(href) + self.in_mailto = 1 else: assert node.has_key('refid'), \ 'References must have "refuri" or "refid" attribute.' @@ -1096,6 +1123,7 @@ class HTMLTranslator(nodes.NodeVisitor): def depart_reference(self, node): self.body.append('') self.body.append(self.context.pop()) + self.in_mailto = 0 def visit_revision(self, node): self.visit_docinfo_item(node, 'revision', meta=None) diff --git a/docutils/test/functional/expected/pep_html.html b/docutils/test/functional/expected/pep_html.html index 6e990e61a..00d7d1d2b 100644 --- a/docutils/test/functional/expected/pep_html.html +++ b/docutils/test/functional/expected/pep_html.html @@ -38,7 +38,7 @@ to templates. DO NOT USE THIS HTML FILE AS YOUR TEMPLATE! Author:John Doe <john at example.org> -Discussions-To:<devnull at example.org> +Discussions-To:<devnull at example.org> Status:Draft @@ -56,18 +56,35 @@ to templates. DO NOT USE THIS HTML FILE AS YOUR TEMPLATE!

Contents

-

Abstract

-

Just a test.

+

Abstract

+

This is just a test [1]. See the PEP repository [2] for the real +thing.

+
+

References and Footnotes

+ + + + + +
[1]PEP editors: peps at python dot org
+ + + + + +
[2]http://www.python.org/peps/
+
diff --git a/docutils/test/functional/input/pep_html.txt b/docutils/test/functional/input/pep_html.txt index 5cca3726e..483077131 100644 --- a/docutils/test/functional/input/pep_html.txt +++ b/docutils/test/functional/input/pep_html.txt @@ -14,10 +14,19 @@ Post-History: 13-Jun-2001 Abstract ======== -Just a test. +This is just a test [#]_. See the `PEP repository`_ for the real +thing. + +.. _PEP repository: http://www.python.org/peps/ Copyright ========= This document has been placed in the public domain. + + +References and Footnotes +======================== + +.. [#] PEP editors: peps@python.org diff --git a/docutils/test/functional/tests/pep_html.py b/docutils/test/functional/tests/pep_html.py index 47e8b52d3..efb067ac4 100644 --- a/docutils/test/functional/tests/pep_html.py +++ b/docutils/test/functional/tests/pep_html.py @@ -14,3 +14,4 @@ settings_overrides['template'] = "../tools/pep-html-template" settings_overrides['python_home'] = "http://www.python.org" settings_overrides['pep_home'] = "http://www.python.org/peps" settings_overrides['no_random'] = 1 +settings_overrides['cloak_email_addresses'] = 1 -- cgit v1.2.1