From e7df7fabeb9fbb1e58842e7cf4be81a76356949f Mon Sep 17 00:00:00 2001 From: Chris Rodgers Date: Wed, 14 Sep 2016 12:00:31 +0100 Subject: [PATCH] Adds exceptions to allow certain punctuation characters to behave as inline delimiters --- src/Markdig.Tests/Specs/EmphasisExtraSpecs.md | 11 ++++++ src/Markdig.Tests/Specs/Specs.cs | 35 +++++++++++++++---- src/Markdig/Helpers/CharHelper.cs | 9 +++-- 3 files changed, 47 insertions(+), 8 deletions(-) diff --git a/src/Markdig.Tests/Specs/EmphasisExtraSpecs.md b/src/Markdig.Tests/Specs/EmphasisExtraSpecs.md index 72f360d3..e1dada02 100644 --- a/src/Markdig.Tests/Specs/EmphasisExtraSpecs.md +++ b/src/Markdig.Tests/Specs/EmphasisExtraSpecs.md @@ -21,6 +21,17 @@ H~2~O is a liquid. 2^10^ is 1024 .

H2O is a liquid. 210 is 1024

```````````````````````````````` + +Certain punctuation characters are exempted from the rule forbidding them within inline delimiters + +```````````````````````````````` example +One quintillionth can be expressed as 10^-18^ + +Daggers^†^ and double-daggers^‡^ can be used to denote notes. +. +

One quintillionth can be expressed as 10-18

+

Daggers† and double-daggers‡ can be used to denote notes.

+```````````````````````````````` ## Inserted diff --git a/src/Markdig.Tests/Specs/Specs.cs b/src/Markdig.Tests/Specs/Specs.cs index 5251bd7c..800161c3 100644 --- a/src/Markdig.Tests/Specs/Specs.cs +++ b/src/Markdig.Tests/Specs/Specs.cs @@ -17357,6 +17357,29 @@ namespace Markdig.Tests Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 2, "Extensions Superscript and Subscript"); TestParser.TestSpec("H~2~O is a liquid. 2^10^ is 1024", "

H2O is a liquid. 210 is 1024

", "emphasisextras|advanced"); } + } + // Certain punctuation characters are exempted from the rule forbidding them within inline delimiters + [TestFixture] + public partial class TestExtensionsSuperscriptandSubscript + { + [Test] + public void Example003() + { + // Example 3 + // Section: Extensions Superscript and Subscript + // + // The following CommonMark: + // One quintillionth can be expressed as 10^-18^ + // + // Daggers^†^ and double-daggers^‡^ can be used to denote notes. + // + // Should be rendered as: + //

One quintillionth can be expressed as 10-18

+ //

Daggers† and double-daggers‡ can be used to denote notes.

+ + Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 3, "Extensions Superscript and Subscript"); + TestParser.TestSpec("One quintillionth can be expressed as 10^-18^\n\nDaggers^†^ and double-daggers^‡^ can be used to denote notes.", "

One quintillionth can be expressed as 10-18

\n

Daggers† and double-daggers‡ can be used to denote notes.

", "emphasisextras|advanced"); + } } // ## Inserted // @@ -17365,9 +17388,9 @@ namespace Markdig.Tests public partial class TestExtensionsInserted { [Test] - public void Example003() + public void Example004() { - // Example 3 + // Example 4 // Section: Extensions Inserted // // The following CommonMark: @@ -17376,7 +17399,7 @@ namespace Markdig.Tests // Should be rendered as: //

Inserted text

- Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 3, "Extensions Inserted"); + Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 4, "Extensions Inserted"); TestParser.TestSpec("++Inserted text++", "

Inserted text

", "emphasisextras|advanced"); } } @@ -17387,9 +17410,9 @@ namespace Markdig.Tests public partial class TestExtensionsMarked { [Test] - public void Example004() + public void Example005() { - // Example 4 + // Example 5 // Section: Extensions Marked // // The following CommonMark: @@ -17398,7 +17421,7 @@ namespace Markdig.Tests // Should be rendered as: //

Marked text

- Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 4, "Extensions Marked"); + Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 5, "Extensions Marked"); TestParser.TestSpec("==Marked text==", "

Marked text

", "emphasisextras|advanced"); } } diff --git a/src/Markdig/Helpers/CharHelper.cs b/src/Markdig/Helpers/CharHelper.cs index 1392b5fb..85748ab6 100644 --- a/src/Markdig/Helpers/CharHelper.cs +++ b/src/Markdig/Helpers/CharHelper.cs @@ -23,6 +23,8 @@ namespace Markdig.Helpers // We don't support LCDM private static IDictionary romanMap = new Dictionary { { 'I', 1 }, { 'V', 5 }, { 'X', 10 } }; + private static readonly char[] punctuationExceptions = { '−', '-', '†', '‡' }; + public static void CheckOpenCloseDelimiter(char pc, char c, bool enableWithinWord, out bool canOpen, out bool canClose) { // A left-flanking delimiter run is a delimiter run that is @@ -37,8 +39,11 @@ namespace Markdig.Helpers pc.CheckUnicodeCategory(out prevIsWhiteSpace, out prevIsPunctuation); c.CheckUnicodeCategory(out nextIsWhiteSpace, out nextIsPunctuation); + var prevIsExcepted = prevIsPunctuation && punctuationExceptions.Contains(pc); + var nextIsExcepted = nextIsPunctuation && punctuationExceptions.Contains(c); + canOpen = !nextIsWhiteSpace && - (!nextIsPunctuation || prevIsWhiteSpace || prevIsPunctuation); + ((!nextIsPunctuation || nextIsExcepted) || prevIsWhiteSpace || prevIsPunctuation); // A right-flanking delimiter run is a delimiter run that is @@ -47,7 +52,7 @@ namespace Markdig.Helpers // or a punctuation character. // For purposes of this definition, the beginning and the end of the line count as Unicode whitespace. canClose = !prevIsWhiteSpace && - (!prevIsPunctuation || nextIsWhiteSpace || nextIsPunctuation); + ((!prevIsPunctuation || prevIsExcepted) || nextIsWhiteSpace || nextIsPunctuation); if (!enableWithinWord) {