Skip to content

Commit

Permalink
Move _glyph_defaults to Textile.glyph_definitions.
Browse files Browse the repository at this point in the history
Having them (back) in the Textile class allows changing them in a subclass,
for example to change quote marks to ones used in a local language.
Renaming to glyph_definitions, because the dictionary is used directly
rather than being a default.

Added a simple test case for sublassing Textile and changing a glyph.
  • Loading branch information
rczajka committed Sep 18, 2014
1 parent cbb5ae8 commit 023d762
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 30 deletions.
59 changes: 29 additions & 30 deletions textile/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,33 +56,6 @@ def _normalize_newlines(string):
return out


_glyph_defaults = {
'quote_single_open': '‘',
'quote_single_close': '’',
'quote_double_open': '“',
'quote_double_close': '”',
'apostrophe': '’',
'prime': '′',
'prime_double': '″',
'ellipsis': '…',
'ampersand': '&',
'emdash': '—',
'endash': '–',
'dimension': '×',
'trademark': '™',
'registered': '®',
'copyright': '©',
'half': '½',
'quarter': '¼',
'threequarters': '¾',
'degrees': '°',
'plusminus': '±',
'fn_ref_pattern': '<sup%(atts)s>%(marker)s</sup>',
'fn_foot_pattern': '<sup%(atts)s>%(marker)s</sup>',
'nl_ref_pattern': '<sup%(atts)s>%(marker)s</sup>',
}


class Textile(object):
halign_re_s = r'(?:\<(?!>)|(?<!<)\>|\<\>|\=|[()]+(?! ))'
valign_re_s = r'[\-^~]'
Expand Down Expand Up @@ -121,6 +94,32 @@ class Textile(object):

doctype_whitelist = ['xhtml', 'html5']

glyph_definitions = {
'quote_single_open': '&#8216;',
'quote_single_close': '&#8217;',
'quote_double_open': '&#8220;',
'quote_double_close': '&#8221;',
'apostrophe': '&#8217;',
'prime': '&#8242;',
'prime_double': '&#8243;',
'ellipsis': '&#8230;',
'ampersand': '&amp;',
'emdash': '&#8212;',
'endash': '&#8211;',
'dimension': '&#215;',
'trademark': '&#8482;',
'registered': '&#174;',
'copyright': '&#169;',
'half': '&#189;',
'quarter': '&#188;',
'threequarters': '&#190;',
'degrees': '&#176;',
'plusminus': '&#177;',
'fn_ref_pattern': '<sup%(atts)s>%(marker)s</sup>',
'fn_foot_pattern': '<sup%(atts)s>%(marker)s</sup>',
'nl_ref_pattern': '<sup%(atts)s>%(marker)s</sup>',
}

def __init__(self, restricted=False, lite=False, noimage=False,
auto_link=False, get_sizes=False, html_type='xhtml'):
"""Textile properties that are common to regular textile and
Expand Down Expand Up @@ -200,7 +199,7 @@ def __init__(self, restricted=False, lite=False, noimage=False,
self.glyph_search_initial[4] = re.compile(r'(\S)"(?=\s|%s|$)' %
self.pnct_re_s, re.U)

self.glyph_replace = [x % _glyph_defaults for x in (
self.glyph_replace = [x % self.glyph_definitions for x in (
r'\1%(apostrophe)s\2', # apostrophe's
r'\1%(apostrophe)s\2', # back in '88
r'\1%(quote_single_close)s', # single closing
Expand Down Expand Up @@ -904,9 +903,9 @@ def fBlock(self, tag, atts, ext, cite, content):

def formatFootnote(self, marker, atts='', anchor=True):
if anchor:
pattern = _glyph_defaults['fn_foot_pattern']
pattern = self.glyph_definitions['fn_foot_pattern']
else:
pattern = _glyph_defaults['fn_ref_pattern']
pattern = self.glyph_definitions['fn_ref_pattern']
return pattern % {'atts': atts, 'marker': marker}

def footnoteRef(self, text):
Expand Down
19 changes: 19 additions & 0 deletions textile/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -590,3 +590,22 @@ def testHTML5(self):
result = '\t<p>We use <abbr title="Cascading Style Sheets"><span class="caps">CSS</span></abbr>.</p>'
expect = textile.textile(test, html_type="html5")
eq_(result, expect)


class SubclassingTests():
"""Test Textile subclassing ability."""
def testChangeGlyphs(self):
class TextilePL(textile.Textile):
glyph_definitions = dict(textile.Textile.glyph_definitions,
quote_double_open = '&#8222;'
)

test = 'Test "quotes".'
expect = '\t<p>Test &#8222;quotes&#8221;'
result = TextilePL().parse(test)
eq_(expect, result)

# Base Textile is unchanged.
expect = '\t<p>Test &#8220;quotes&#8221;'
result = textile.textile(test)
eq_(expect, result)

0 comments on commit 023d762

Please sign in to comment.