Back to index

moin  1.9.0~rc2
Public Member Functions | Static Public Attributes
MoinMoin.support.htmlmarkup.Markup Class Reference

List of all members.

Public Member Functions

def __new__
def __add__
def __mod__
def __mul__
def join
def stripentities
def striptags
def escape
def unescape
def plaintext
def sanitize

Static Public Attributes

tuple escape = classmethod(escape)

Detailed Description

Marks a string as being safe for inclusion in XML output without needing
to be escaped.

Strings are normally automatically escaped when added to the HDF.
`Markup`-strings are however an exception. Use with care.

(since Trac 0.9.3)

Definition at line 33 of file htmlmarkup.py.


Member Function Documentation

def MoinMoin.support.htmlmarkup.Markup.__add__ (   self,
  other 
)

Definition at line 47 of file htmlmarkup.py.

00047 
00048     def __add__(self, other):
00049         return Markup(unicode(self) + Markup.escape(other))

def MoinMoin.support.htmlmarkup.Markup.__mod__ (   self,
  args 
)

Definition at line 50 of file htmlmarkup.py.

00050 
00051     def __mod__(self, args):
00052         if not isinstance(args, (list, tuple)):
00053             args = [args]
00054         return Markup(unicode.__mod__(self,
00055                                       tuple([escape(arg) for arg in args])))

def MoinMoin.support.htmlmarkup.Markup.__mul__ (   self,
  num 
)

Definition at line 56 of file htmlmarkup.py.

00056 
00057     def __mul__(self, num):
00058         return Markup(unicode(self) * num)

def MoinMoin.support.htmlmarkup.Markup.__new__ (   self,
  text = '',
  args 
)

Definition at line 42 of file htmlmarkup.py.

00042 
00043     def __new__(self, text='', *args):
00044         if args:
00045             text %= tuple([escape(arg) for arg in args])
00046         return unicode.__new__(self, text)

def MoinMoin.support.htmlmarkup.Markup.escape (   cls,
  text,
  quotes = True 
)
Create a Markup instance from a string and escape special characters
it may contain (<, >, & and \").

If the `quotes` parameter is set to `False`, the \" character is left
as is. Escaping quotes is generally only required for strings that are
to be used in attribute values.

Definition at line 98 of file htmlmarkup.py.

00098 
00099     def escape(cls, text, quotes=True):
00100         """Create a Markup instance from a string and escape special characters
00101         it may contain (<, >, & and \").
00102         
00103         If the `quotes` parameter is set to `False`, the \" character is left
00104         as is. Escaping quotes is generally only required for strings that are
00105         to be used in attribute values.
00106         """
00107         if isinstance(text, (cls, Element)):
00108             return text
00109         text = unicode(text)
00110         if not text:
00111             return cls()
00112         text = text.replace('&', '&amp;') \
00113                    .replace('<', '&lt;') \
00114                    .replace('>', '&gt;')
00115         if quotes:
00116             text = text.replace('"', '&#34;')
        return cls(text)

Here is the caller graph for this function:

def MoinMoin.support.htmlmarkup.Markup.join (   self,
  seq 
)

Definition at line 59 of file htmlmarkup.py.

00059 
00060     def join(self, seq):
00061         return Markup(unicode(self).join([Markup.escape(item) for item in seq]))

def MoinMoin.support.htmlmarkup.Markup.plaintext (   self,
  keeplinebreaks = True 
)
Returns the text as a `unicode`with all entities and tags removed.

Definition at line 128 of file htmlmarkup.py.

00128 
00129     def plaintext(self, keeplinebreaks=True):
00130         """Returns the text as a `unicode`with all entities and tags removed."""
00131         text = unicode(self.striptags().stripentities())
00132         if not keeplinebreaks:
00133             text = text.replace('\n', ' ')
00134         return text

Here is the call graph for this function:

Parse the text as HTML and return a cleaned up XHTML representation.

This will remove any javascript code or other potentially dangerous
elements.

If the HTML cannot be parsed, an `HTMLParseError` will be raised by the
underlying `HTMLParser` module, which should be handled by the caller of
this function.

Definition at line 135 of file htmlmarkup.py.

00135 
00136     def sanitize(self):
00137         """Parse the text as HTML and return a cleaned up XHTML representation.
00138         
00139         This will remove any javascript code or other potentially dangerous
00140         elements.
00141         
00142         If the HTML cannot be parsed, an `HTMLParseError` will be raised by the
00143         underlying `HTMLParser` module, which should be handled by the caller of
00144         this function.
00145         """
00146         buf = StringIO()
00147         sanitizer = HTMLSanitizer(buf)
00148         sanitizer.feed(self.stripentities(keepxmlentities=True))
00149         return Markup(buf.getvalue())
00150 

Here is the call graph for this function:

Here is the caller graph for this function:

def MoinMoin.support.htmlmarkup.Markup.stripentities (   self,
  keepxmlentities = False 
)
Return a copy of the text with any character or numeric entities
replaced by the equivalent UTF-8 characters.

If the `keepxmlentities` parameter is provided and evaluates to `True`,
the core XML entities (&amp;, &apos;, &gt;, &lt; and &quot;).

(Since Trac 0.10)

Definition at line 62 of file htmlmarkup.py.

00062 
00063     def stripentities(self, keepxmlentities=False):
00064         """Return a copy of the text with any character or numeric entities
00065         replaced by the equivalent UTF-8 characters.
00066         
00067         If the `keepxmlentities` parameter is provided and evaluates to `True`,
00068         the core XML entities (&amp;, &apos;, &gt;, &lt; and &quot;).
00069         
00070         (Since Trac 0.10)
00071         """
00072         def _replace_entity(match):
00073             if match.group(1): # numeric entity
00074                 ref = match.group(1)
00075                 if ref.startswith('x'):
00076                     ref = int(ref[1:], 16)
00077                 else:
00078                     ref = int(ref, 10)
00079                 return unichr(ref)
00080             else: # character entity
00081                 ref = match.group(2)
00082                 if keepxmlentities and ref in ('amp', 'apos', 'gt', 'lt', 'quot'):
00083                     return '&%s;' % ref
00084                 try:
00085                     codepoint = htmlentitydefs.name2codepoint[ref]
00086                     return unichr(codepoint)
00087                 except KeyError:
00088                     if keepxmlentities:
00089                         return '&amp;%s;' % ref
00090                     else:
00091                         return ref
00092         return Markup(re.sub(r'&(?:#((?:\d+)|(?:[xX][0-9a-fA-F]+));?|(\w+);)',
00093                              _replace_entity, self))

Here is the caller graph for this function:

Return a copy of the text with all XML/HTML tags removed.

Definition at line 94 of file htmlmarkup.py.

00094 
00095     def striptags(self):
00096         """Return a copy of the text with all XML/HTML tags removed."""
00097         return Markup(re.sub(r'<[^>]*?>', '', self))

Here is the caller graph for this function:

Reverse-escapes &, <, > and \" and returns a `unicode` object.

Definition at line 119 of file htmlmarkup.py.

00119 
00120     def unescape(self):
00121         """Reverse-escapes &, <, > and \" and returns a `unicode` object."""
00122         if not self:
00123             return ''
00124         return unicode(self).replace('&#34;', '"') \
00125                             .replace('&gt;', '>') \
00126                             .replace('&lt;', '<') \
00127                             .replace('&amp;', '&')


Member Data Documentation

tuple MoinMoin.support.htmlmarkup.Markup.escape = classmethod(escape) [static]

Definition at line 117 of file htmlmarkup.py.


The documentation for this class was generated from the following file: