Skip to content

Commit

Permalink
smarty: add support for angled quotes
Browse files Browse the repository at this point in the history
  • Loading branch information
mitya57 committed Jun 19, 2014
1 parent 51857fd commit 2d47fce
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 12 deletions.
24 changes: 13 additions & 11 deletions docs/extensions/smarty.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ their HTML entity equivalents.

ASCII symbol | Replacements | HTML Entities
------------ | --------------- | -------------------
' | ‘ ’ | `‘` `’`
" | “ ” | `“` `”`
\... | … | `…`
\-- | – | `–`
-\-- | — | `—`
`'` | ‘ ’ | `‘` `’`
`"` | “ ” | `“` `”`
`<< >>` | &laquo; &raquo; | `&laquo;` `&raquo;`
`...` | &hellip; | `&hellip;`
`--` | &ndash; | `&ndash;`
`---` | &mdash; | `&mdash;`

!!! note
This extension reimplements the Python [SmartyPants]
Expand All @@ -42,13 +43,14 @@ as the name of the extension.
See the [Library Reference](../reference.html#extensions) for information about
configuring extensions.

The following options are provided to configure the output (all three are set to `True` by default):
The following options are provided to configure the output:

Option | Description
------ | -----------
`smart_dashes` | whether to convert dashes
`smart_quotes` | whether to convert quotes
`smart_ellipses` | whether to convert ellipses
Option | Default value | Description
------ | ------------- | -----------
`smart_dashes` | enabled | whether to convert dashes
`smart_quotes` | enabled | whether to convert straight quotes
`smart_angled_quotes` | disabled | whether to convert angled quotes
`smart_ellipses` | enabled | whether to convert ellipses

Further reading
---------------
Expand Down
11 changes: 11 additions & 0 deletions markdown/extensions/smarty.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ class SmartyExtension(Extension):
def __init__(self, configs):
self.config = {
'smart_quotes': [True, 'Educate quotes'],
'smart_angled_quotes': [False, 'Educate angled quotes'],
'smart_dashes': [True, 'Educate dashes'],
'smart_ellipses': [True, 'Educate ellipses']
}
Expand All @@ -162,6 +163,14 @@ def educateEllipses(self, md):
ellipsesPattern = SubstituteTextPattern(r'(?<!\.)\.{3}(?!\.)', ('&hellip;',), md)
self.inlinePatterns.add('smarty-ellipses', ellipsesPattern, '_begin')

def educateAngledQuotes(self, md):
leftAngledQuotePattern = SubstituteTextPattern(r'\<\<', ('&laquo;',), md)
rightAngledQuotePattern = SubstituteTextPattern(r'\>\>', ('&raquo;',), md)
self.inlinePatterns.add('smarty-left-angle-quotes',
leftAngledQuotePattern, '_begin')
self.inlinePatterns.add('smarty-right-angle-quotes',
rightAngledQuotePattern, '>smarty-left-angle-quotes')

def educateQuotes(self, md):
patterns = (
(singleQuoteStartRe, (rsquo,)),
Expand All @@ -186,6 +195,8 @@ def extendMarkdown(self, md, md_globals):
self.educateEllipses(md)
if configs['smart_quotes']:
self.educateQuotes(md)
if configs['smart_angled_quotes']:
self.educateAngledQuotes(md)
if configs['smart_dashes']:
self.educateDashes(md)
inlineProcessor = InlineProcessor(md)
Expand Down
2 changes: 2 additions & 0 deletions tests/extensions/smarty.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
em-dashes (&mdash;) and ellipes (&hellip;)<br />
&ldquo;<a href="http://example.com">Link</a>&rdquo; &mdash; she said.</p>
<p>&ldquo;Ellipsis within quotes&hellip;&rdquo;</p>
<p>Кавычки-&laquo;ёлочки&raquo;<br />
Anführungszeichen-&raquo;Chevrons&laquo;</p>
<hr />
<p>Escaped -- ndash<br />
'Escaped' "quotes"<br />
Expand Down
3 changes: 3 additions & 0 deletions tests/extensions/smarty.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ em-dashes (---) and ellipes (...)

"Ellipsis within quotes..."

Кавычки-<<ёлочки>>
Anführungszeichen->>Chevrons<<

--- -- ---

Escaped \-- ndash
Expand Down
2 changes: 1 addition & 1 deletion tests/extensions/test.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ extensions=nl2br,attr_list
extensions=admonition

[smarty]
extensions=smarty
extensions=smarty(smart_angled_quotes=1)

0 comments on commit 2d47fce

Please sign in to comment.