summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorfancsali <fancsali@users.noreply.github.com>2020-04-16 12:32:19 +0100
committerGitHub <noreply@github.com>2020-04-16 13:32:19 +0200
commitf610801ee8c67df5ed7e3223fe54a2578347b88e (patch)
treeeb60747691bfe3ea2b3a88ebfd9812d688e5b1f7
parentcb3bb4ad6966e7c384275da7d52ac7d67852183a (diff)
downloadpelican-f610801ee8c67df5ed7e3223fe54a2578347b88e.tar.gz
Add Typogrify/SmartyPants dashes config parameter (#2615)
-rw-r--r--docs/settings.rst11
-rw-r--r--pelican/readers.py8
-rw-r--r--pelican/settings.py1
-rw-r--r--pelican/tests/content/article_with_typogrify_dashes.md3
-rw-r--r--pelican/tests/content/article_with_typogrify_dashes.rst4
-rw-r--r--pelican/tests/test_generators.py6
-rw-r--r--pelican/tests/test_readers.py68
7 files changed, 101 insertions, 0 deletions
diff --git a/docs/settings.rst b/docs/settings.rst
index af8d627e..48344076 100644
--- a/docs/settings.rst
+++ b/docs/settings.rst
@@ -279,6 +279,17 @@ Basic settings
``pre`` and ``code`` tags. This requires that Typogrify version 2.0.4 or
later is installed
+.. data:: TYPOGRIFY_DASHES = 'default'
+
+ This setting controls how Typogrify sets up the Smartypants filter to
+ interpret multiple dash/hyphen/minus characters. A single ASCII dash
+ character (``-``) is always rendered as a hyphen. The ``default`` setting
+ does not handle en-dashes and converts double-hyphens into em-dashes. The
+ ``oldschool`` setting renders both en-dashes and em-dashes when it sees two
+ (``--``) and three (``---``) hyphen characters, respectively. The
+ ``oldschool_inverted`` setting turns two hyphens into an em-dash and three
+ hyphens into an en-dash.
+
.. data:: SUMMARY_MAX_LENGTH = 50
When creating a short summary of an article, this will be the default length
diff --git a/pelican/readers.py b/pelican/readers.py
index 6d9923f2..fe53558d 100644
--- a/pelican/readers.py
+++ b/pelican/readers.py
@@ -585,6 +585,14 @@ class Readers(FileStampDataCacher):
from typogrify.filters import typogrify
import smartypants
+ typogrify_dashes = self.settings['TYPOGRIFY_DASHES']
+ if typogrify_dashes == 'oldschool':
+ smartypants.Attr.default = smartypants.Attr.set2
+ elif typogrify_dashes == 'oldschool_inverted':
+ smartypants.Attr.default = smartypants.Attr.set3
+ else:
+ smartypants.Attr.default = smartypants.Attr.set1
+
# Tell `smartypants` to also replace &quot; HTML entities with
# smart quotes. This is necessary because Docutils has already
# replaced double quotes with said entities by the time we run
diff --git a/pelican/settings.py b/pelican/settings.py
index c3b29ca5..77aea059 100644
--- a/pelican/settings.py
+++ b/pelican/settings.py
@@ -138,6 +138,7 @@ DEFAULT_CONFIG = {
'ARTICLE_PERMALINK_STRUCTURE': '',
'TYPOGRIFY': False,
'TYPOGRIFY_IGNORE_TAGS': [],
+ 'TYPOGRIFY_DASHES': 'default',
'SUMMARY_END_MARKER': '…',
'SUMMARY_MAX_LENGTH': 50,
'PLUGIN_PATHS': [],
diff --git a/pelican/tests/content/article_with_typogrify_dashes.md b/pelican/tests/content/article_with_typogrify_dashes.md
new file mode 100644
index 00000000..2914b2df
--- /dev/null
+++ b/pelican/tests/content/article_with_typogrify_dashes.md
@@ -0,0 +1,3 @@
+Title: One -, two --, three --- dashes!
+
+One: -; Two: --; Three: ---
diff --git a/pelican/tests/content/article_with_typogrify_dashes.rst b/pelican/tests/content/article_with_typogrify_dashes.rst
new file mode 100644
index 00000000..90064c5d
--- /dev/null
+++ b/pelican/tests/content/article_with_typogrify_dashes.rst
@@ -0,0 +1,4 @@
+One -, two --, three --- dashes!
+################################
+
+One: -; Two: --; Three: ---
diff --git a/pelican/tests/test_generators.py b/pelican/tests/test_generators.py
index fdc1b937..d559cec9 100644
--- a/pelican/tests/test_generators.py
+++ b/pelican/tests/test_generators.py
@@ -248,6 +248,10 @@ class TestArticlesGenerator(unittest.TestCase):
['Article with template', 'published', 'Default', 'custom'],
['Metadata tags as list!', 'published', 'Default', 'article'],
['Rst with filename metadata', 'published', 'yeah', 'article'],
+ ['One -, two --, three --- dashes!', 'published', 'Default',
+ 'article'],
+ ['One -, two --, three --- dashes!', 'published', 'Default',
+ 'article'],
['Test Markdown extensions', 'published', 'Default', 'article'],
['Test markdown File', 'published', 'test', 'article'],
['Test md File', 'published', 'test', 'article'],
@@ -562,6 +566,8 @@ class TestArticlesGenerator(unittest.TestCase):
'Article with markdown containing footnotes',
'Article with template',
'Metadata tags as list!',
+ 'One -, two --, three --- dashes!',
+ 'One -, two --, three --- dashes!',
'Rst with filename metadata',
'Test Markdown extensions',
'Test markdown File',
diff --git a/pelican/tests/test_readers.py b/pelican/tests/test_readers.py
index 70e6da77..07590012 100644
--- a/pelican/tests/test_readers.py
+++ b/pelican/tests/test_readers.py
@@ -443,6 +443,40 @@ class RstReaderTest(ReaderTest):
with self.assertRaisesRegex(Exception, "underline too short"):
self.read_file(path='../parse_error/parse_error.rst')
+ def test_typogrify_dashes_config(self):
+ # Test default config
+ page = self.read_file(
+ path='article_with_typogrify_dashes.rst',
+ TYPOGRIFY=True,
+ TYPOGRIFY_DASHES='default')
+ expected = "<p>One: -; Two: &#8212;; Three:&nbsp;&#8212;-</p>\n"
+ expected_title = "One -, two &#8212;, three &#8212;-&nbsp;dashes!"
+
+ self.assertEqual(page.content, expected)
+ self.assertEqual(page.title, expected_title)
+
+ # Test 'oldschool' variant
+ page = self.read_file(
+ path='article_with_typogrify_dashes.rst',
+ TYPOGRIFY=True,
+ TYPOGRIFY_DASHES='oldschool')
+ expected = "<p>One: -; Two: &#8211;; Three:&nbsp;&#8212;</p>\n"
+ expected_title = "One -, two &#8211;, three &#8212;&nbsp;dashes!"
+
+ self.assertEqual(page.content, expected)
+ self.assertEqual(page.title, expected_title)
+
+ # Test 'oldschool_inverted' variant
+ page = self.read_file(
+ path='article_with_typogrify_dashes.rst',
+ TYPOGRIFY=True,
+ TYPOGRIFY_DASHES='oldschool_inverted')
+ expected = "<p>One: -; Two: &#8212;; Three:&nbsp;&#8211;</p>\n"
+ expected_title = "One -, two &#8212;, three &#8211;&nbsp;dashes!"
+
+ self.assertEqual(page.content, expected)
+ self.assertEqual(page.title, expected_title)
+
@unittest.skipUnless(readers.Markdown, "markdown isn't installed")
class MdReaderTest(ReaderTest):
@@ -674,6 +708,40 @@ class MdReaderTest(ReaderTest):
self.assertEqual(metadata, {})
self.assertEqual(content, '')
+ def test_typogrify_dashes_config(self):
+ # Test default config
+ page = self.read_file(
+ path='article_with_typogrify_dashes.md',
+ TYPOGRIFY=True,
+ TYPOGRIFY_DASHES='default')
+ expected = "<p>One: -; Two: &#8212;; Three:&nbsp;&#8212;-</p>"
+ expected_title = "One -, two &#8212;, three &#8212;-&nbsp;dashes!"
+
+ self.assertEqual(page.content, expected)
+ self.assertEqual(page.title, expected_title)
+
+ # Test 'oldschool' variant
+ page = self.read_file(
+ path='article_with_typogrify_dashes.md',
+ TYPOGRIFY=True,
+ TYPOGRIFY_DASHES='oldschool')
+ expected = "<p>One: -; Two: &#8211;; Three:&nbsp;&#8212;</p>"
+ expected_title = "One -, two &#8211;, three &#8212;&nbsp;dashes!"
+
+ self.assertEqual(page.content, expected)
+ self.assertEqual(page.title, expected_title)
+
+ # Test 'oldschool_inverted' variant
+ page = self.read_file(
+ path='article_with_typogrify_dashes.md',
+ TYPOGRIFY=True,
+ TYPOGRIFY_DASHES='oldschool_inverted')
+ expected = "<p>One: -; Two: &#8212;; Three:&nbsp;&#8211;</p>"
+ expected_title = "One -, two &#8212;, three &#8211;&nbsp;dashes!"
+
+ self.assertEqual(page.content, expected)
+ self.assertEqual(page.title, expected_title)
+
class HTMLReaderTest(ReaderTest):
def test_article_with_comments(self):