| 1 | # -*- coding: iso-8859-1 -*- |
|---|
| 2 | # |
|---|
| 3 | # Copyright (C) 2006 Christian Boos <cboos@neuf.fr> |
|---|
| 4 | # All rights reserved. |
|---|
| 5 | # |
|---|
| 6 | # This software is licensed as described in the file COPYING, which |
|---|
| 7 | # you should have received as part of this distribution. The terms |
|---|
| 8 | # are also available at http://trac.edgewall.com/license.html. |
|---|
| 9 | # |
|---|
| 10 | # Author: Christian Boos <cboos@neuf.fr> |
|---|
| 11 | |
|---|
| 12 | # See http://www.w3.org/TR/html401/sgml/entities.html for the official list |
|---|
| 13 | # and http://www.cookwood.com/html/extras/entities.html for an illustration. |
|---|
| 14 | |
|---|
| 15 | from trac.util.html import Markup |
|---|
| 16 | |
|---|
| 17 | from trac.core import implements, Component |
|---|
| 18 | from trac.wiki import IWikiSyntaxProvider, IWikiMacroProvider |
|---|
| 19 | |
|---|
| 20 | from tracwikiextras.util import render_table |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | ENTITIES = [ |
|---|
| 24 | "nbsp", "iexcl", "cent", "pound", "curren", "yen", "brvbar", "sect", |
|---|
| 25 | "uml", "copy", "ordf", "laquo", "not", "shy", "reg", "macr", "deg", |
|---|
| 26 | "plusmn", "sup2", "sup3", "acute", "micro", "para", "middot", "cedil", |
|---|
| 27 | "sup1", "ordm", "raquo", "frac14", "frac12", "frac34", "iquest", |
|---|
| 28 | "Agrave", "Aacute", "Acirc", "Atilde", "Auml", "Aring", "AElig", |
|---|
| 29 | "Ccedil", "Egrave", "Eacute", "Ecirc", "Euml", "Igrave", "Iacute", |
|---|
| 30 | "Icirc", "Iuml", "ETH", "Ntilde", "Ograve", "Oacute", "Ocirc", "Otilde", |
|---|
| 31 | "Ouml", "times", "Oslash", "Ugrave", "Uacute", "Ucirc", "Uuml", "Yacute", |
|---|
| 32 | "THORN", "szlig", "agrave", "aacute", "acirc", "atilde", "auml", "aring", |
|---|
| 33 | "aelig", "ccedil", "egrave", "eacute", "ecirc", "euml", "igrave", "iacute", |
|---|
| 34 | "icirc", "iuml", "eth", "ntilde", "ograve", "oacute", "ocirc", "otilde", |
|---|
| 35 | "ouml", "divide", "oslash", "ugrave", "uacute", "ucirc", "uuml", "yacute", |
|---|
| 36 | "thorn", "yuml", "fnof", "Alpha", "Beta", "Gamma", "Delta", "Epsilon", |
|---|
| 37 | "Zeta", "Eta", "Theta", "Iota", "Kappa", "Lambda", "Mu", "Nu", "Xi", |
|---|
| 38 | "Omicron", "Pi", "Rho", "Sigma", "Tau", "Upsilon", "Phi", "Chi", "Psi", |
|---|
| 39 | "Omega", "alpha", "beta", "gamma", "delta", "epsilon", "zeta", "eta", |
|---|
| 40 | "theta", "iota", "kappa", "lambda", "mu", "nu", "xi", "omicron", "pi", |
|---|
| 41 | "rho", "sigmaf", "sigma", "tau", "upsilon", "phi", "chi", "psi", "omega", |
|---|
| 42 | "thetasym", "upsih", "piv", "bull", "hellip", "prime", "Prime", "oline", |
|---|
| 43 | "frasl", "weierp", "image", "real", "trade", "alefsym", "larr", "uarr", |
|---|
| 44 | "rarr", "darr", "harr", "crarr", "lArr", "uArr", "rArr", "dArr", "hArr", |
|---|
| 45 | "forall", "part", "exist", "empty", "nabla", "isin", "notin", "ni", |
|---|
| 46 | "prod", "sum", "minus", "lowast", "radic", "prop", "infin", "ang", "and", |
|---|
| 47 | "or", "cap", "cup", "int", "there4", "sim", "cong", "asymp", "ne", |
|---|
| 48 | "equiv", "le", "ge", "sub", "sup", "nsub", "sube", "supe", "oplus", |
|---|
| 49 | "otimes", "perp", "sdot", "lceil", "rceil", "lfloor", "rfloor", "lang", |
|---|
| 50 | "rang", "loz", "spades", "clubs", "hearts", "diams", "quot", "amp", "lt", |
|---|
| 51 | "gt", "OElig", "oelig", "Scaron", "scaron", "Yuml", "circ", "tilde", |
|---|
| 52 | "ensp", "emsp", "thinsp", "zwnj", "zwj", "lrm", "rlm", "ndash", "mdash", |
|---|
| 53 | "lsquo", "rsquo", "sbquo", "ldquo", "rdquo", "bdquo", |
|---|
| 54 | "dagger", "Dagger", "permil", "lsaquo", "rsaquo", "euro" |
|---|
| 55 | ] |
|---|
| 56 | |
|---|
| 57 | |
|---|
| 58 | class Entities(Component): |
|---|
| 59 | """Use HTML entities in wiki text. |
|---|
| 60 | |
|---|
| 61 | The HTML entity `♥` in a wiki text, for example, renders a heart. |
|---|
| 62 | Use the `ShowEntities` macro to display a list of all entities. |
|---|
| 63 | |
|---|
| 64 | See http://www.w3.org/TR/html401/sgml/entities.html for the official list |
|---|
| 65 | and http://www.cookwood.com/html/extras/entities.html for an illustration. |
|---|
| 66 | """ |
|---|
| 67 | |
|---|
| 68 | implements(IWikiSyntaxProvider, IWikiMacroProvider) |
|---|
| 69 | |
|---|
| 70 | # IWikiSyntaxProvider methods |
|---|
| 71 | |
|---|
| 72 | def get_wiki_syntax(self): |
|---|
| 73 | yield (r"!?&#\d+;", self._format_entity) |
|---|
| 74 | yield (r"!?&(?:%s);" % '|'.join(ENTITIES), self._format_entity) |
|---|
| 75 | |
|---|
| 76 | def get_link_resolvers(self): |
|---|
| 77 | return [] |
|---|
| 78 | |
|---|
| 79 | #noinspection PyUnusedLocal |
|---|
| 80 | def _format_entity(self, formatter, match, fullmatch): |
|---|
| 81 | return Markup(match) |
|---|
| 82 | |
|---|
| 83 | # IWikiMacroProvider methods |
|---|
| 84 | |
|---|
| 85 | def get_macros(self): |
|---|
| 86 | yield 'ShowEntities' |
|---|
| 87 | |
|---|
| 88 | #noinspection PyUnusedLocal |
|---|
| 89 | def get_macro_description(self, name): |
|---|
| 90 | return ("Renders in a table the list of HTML entities. " |
|---|
| 91 | " Optional argument is the number of columns in the table " |
|---|
| 92 | "(defaults 3).") |
|---|
| 93 | |
|---|
| 94 | #noinspection PyUnusedLocal |
|---|
| 95 | def expand_macro(self, formatter, name, content, args=None): |
|---|
| 96 | return render_table(['&%s;' % e for e in ENTITIES], content, |
|---|
| 97 | lambda e: self._format_entity(formatter, e, None)) |
|---|
| 98 | |
|---|