| 1 |
|
| 2 |
try: |
| 3 |
from html import escape |
| 4 |
except ImportError: |
| 5 |
from cgi import escape |
| 6 |
|
| 7 |
|
| 8 |
def preformat(text): |
| 9 |
return escape(str(text), quote=False) |
| 10 |
|
| 11 |
|
| 12 |
class TagWrapper(str): |
| 13 |
tag = None |
| 14 |
attributes = None |
| 15 |
|
| 16 |
@classmethod |
| 17 |
def _wrap(cls, text): |
| 18 |
if cls.attributes: |
| 19 |
attrib = ' ' + ' '.join(['{}="{}"'.format(key, value) |
| 20 |
for key, value in cls.attributes.items()]) |
| 21 |
else: |
| 22 |
attrib = '' |
| 23 |
return '<{tag}{attrib}>{text}</{tag}>'.format(tag=cls.tag, |
| 24 |
attrib=attrib,text=text) |
| 25 |
|
| 26 |
def __new__(cls, text): |
| 27 |
return super(TagWrapper, cls).__new__(cls, cls._wrap(text)) |
| 28 |
|
| 29 |
|
| 30 |
class Italic(TagWrapper): |
| 31 |
tag = 'i' |
| 32 |
|
| 33 |
|
| 34 |
class Oblique(Italic): |
| 35 |
pass |
| 36 |
|
| 37 |
|
| 38 |
class Bold(TagWrapper): |
| 39 |
tag = 'b' |
| 40 |
|
| 41 |
|
| 42 |
class Light(TagWrapper): |
| 43 |
tag = 'l' |
| 44 |
|
| 45 |
|
| 46 |
class Underline(TagWrapper): |
| 47 |
tag = 'u' |
| 48 |
|
| 49 |
|
| 50 |
class Superscript(TagWrapper): |
| 51 |
tag = 'sup' |
| 52 |
|
| 53 |
|
| 54 |
class Subscript(TagWrapper): |
| 55 |
tag = 'sub' |
| 56 |
|
| 57 |
|
| 58 |
class SmallCaps(TagWrapper): |
| 59 |
tag = 'span' |
| 60 |
attributes = {'style': 'font-variant:small-caps;'} |