If you benefit from web2py hope you feel encouraged to pay it forward by contributing back to society in whatever form you choose!

Paste this code below into file:  models/plugin_wiki_extra.py

 

class PluginWikiWidgets(PluginWikiWidgets):
     @staticmethod
    def encode_text(text='',mailto=''): 
        """
        Purpose: To foil most superficial harvesters
        Input: any text
        Output: the text encoded as either a decimal or hex entity,
                optionally with a mailto link
        widget inspired from python implementation of Markdown (MIT license)
        """
        def _xml_encode_text_char_at_random(ch):
            from random import random
            r = random()
            # Roughly 20% raw, 40% hex, 40% dec.
            # '@' and '_' are always encoded. 
            if r > 0.8 and ch not in "@_":
                return ch
            elif r < 0.4:
                # [1:] drops leading '0': 0x63 -> x63
                return '&#%s;' % hex(ord(ch))[1:]
            else:
                return '&#%s;' % ord(ch)
        def _encode_text(text,mailto):
            if mailto.lower()=='true':
                chars = [_xml_encode_text_char_at_random(ch)
                         for ch in "mailto:" + text]
                # make link and strip mailto: from the visible email address
                text = '<a href="%s">%s</a>' \
                       % (''.join(chars), 
                          ''.join(chars[7:]))
            else:
                chars = [_xml_encode_text_char_at_random(ch)
                         for ch in text]
                text = ''.join(chars)
            return text
        return _encode_text(text, mailto)

 

Now in your wiki page enter something like these following examples:

Example 1 - Text

``
name:encode_text
text:My private telephone number 01 234567
``:widget

 

Example 2 - Email Address

``
name:encode_text
text:foo@bar.com
mailto:true
``:widget
If you look at the HTML source produced, you will see something like the text below,  which isn't very nice for the average bot to parse::
 
M&#x79;&#x20;p&#x72;&#105;&#x76;&#97;&#x74;&#101; &#x74;&#101;&#108;&#x65;&#x70;&#x68;&#111;&#110;&#x65;&#32;&#x6e;&#117;&#x6d;&#x62;&#x65;&#x72;&#58;&#32;&#x30;&#49; &#x32;&#51;&#52;5&#54;&#55;
 
Note that apart from the text,  there is just one other optional parameter mailto:true  which makes an email address into a mailto: link. 
 
Hope you find it useful or interesting as a simple example of adding an extra plugin_wiki widget.

Related slices

Comments (0)


Hosting graciously provided by:
Python Anywhere