git.iko.soy/typeable/pandoc
on master help sign in
Added readerAbbreviations to ParserState.

Markdown reader now consults this to determine what is an abbreviation. Eventually it will be possible to specify a custom list (see #256).

commit95f2726ee74a7770bf0eb5fe5629c493addbe298 authorJohn MacFarlane <jgm@berkeley.edu> dateSun 5 Mar 2017, 10:24 (10 years ago) parent7fc6919
2 files changed+27−25
src/Text/Pandoc/Options.hs+11 src/Text/Pandoc/Readers/Markdown.hs+16−25
src/Text/Pandoc/Options.hs+11
@@ -47,6 +47,7 @@ module Text.Pandoc.Options ( module Text.Pandoc.Extensions
4747 ) where
4848import Data.Data (Data)
4949import Data.Default
50+import qualified Data.Set as Set
5051import Data.Typeable (Typeable)
5152import GHC.Generics (Generic)
5253import Text.Pandoc.Extensions
@@ -60,6 +61,7 @@ data ReaderOptions = ReaderOptions{
6061 , readerApplyMacros :: Bool -- ^ Apply macros to TeX math
6162 , readerIndentedCodeClasses :: [String] -- ^ Default classes for
6263 -- indented code blocks
64+ , readerAbbreviations :: Set.Set String -- ^ Strings to treat as abbreviations
6365 , readerDefaultImageExtension :: String -- ^ Default extension for images
6466 , readerTrackChanges :: TrackChanges
6567} deriving (Show, Read, Data, Typeable, Generic)
@@ -72,10 +74,19 @@ instance Default ReaderOptions
7274 , readerTabStop = 4
7375 , readerApplyMacros = True
7476 , readerIndentedCodeClasses = []
77+ , readerAbbreviations = defaultAbbrevs
7578 , readerDefaultImageExtension = ""
7679 , readerTrackChanges = AcceptChanges
7780 }
7881
82+defaultAbbrevs :: Set.Set String
83+defaultAbbrevs = Set.fromList
84+ [ "Mr.", "Mrs.", "Ms.", "Capt.", "Dr.", "Prof.",
85+ "Gen.", "Gov.", "e.g.", "i.e.", "Sgt.", "St.",
86+ "vol.", "vs.", "Sen.", "Rep.", "Pres.", "Hon.",
87+ "Rev.", "Ph.D.", "M.D.", "M.A.", "p.", "pp.",
88+ "ch.", "sec.", "cf.", "cp."]
89+
7990--
8091-- Writer options
8192--
src/Text/Pandoc/Readers/Markdown.hs+16−25
@@ -42,6 +42,7 @@ import Data.Maybe
4242import Data.Monoid ((<>))
4343import Data.Ord (comparing)
4444import Data.Scientific (base10Exponent, coefficient)
45+import qualified Data.Set as Set
4546import Data.Text (Text)
4647import qualified Data.Text as T
4748import qualified Data.Vector as V
@@ -1688,32 +1689,22 @@ nonEndline = satisfy (/='\n')
16881689
16891690str :: PandocMonad m => MarkdownParser m (F Inlines)
16901691str = do
1691− result <- many1 alphaNum
1692+ result <- many1 (alphaNum <|> try (char '.' <* notFollowedBy (char '.')))
16921693 updateLastStrPos
1693− let spacesToNbr = map (\c -> if c == ' ' then '\160' else c)
1694− isSmart <- extensionEnabled Ext_smart <$> getOption readerExtensions
1695− if isSmart
1696− then case likelyAbbrev result of
1697− [] -> return $ return $ B.str result
1698− xs -> choice (map (\x ->
1699− try (string x >> oneOf " \n" >>
1700− lookAhead alphaNum >>
1701− return (return $ B.str
1702− $ result ++ spacesToNbr x ++ "\160"))) xs)
1703− <|> (return $ return $ B.str result)
1704− else return $ return $ B.str result
1705−
1706−-- | if the string matches the beginning of an abbreviation (before
1707−-- the first period, return strings that would finish the abbreviation.
1708−likelyAbbrev :: String -> [String]
1709−likelyAbbrev x =
1710− let abbrevs = [ "Mr.", "Mrs.", "Ms.", "Capt.", "Dr.", "Prof.",
1711− "Gen.", "Gov.", "e.g.", "i.e.", "Sgt.", "St.",
1712− "vol.", "vs.", "Sen.", "Rep.", "Pres.", "Hon.",
1713− "Rev.", "Ph.D.", "M.D.", "M.A.", "p.", "pp.",
1714− "ch.", "sec.", "cf.", "cp."]
1715− abbrPairs = map (break (=='.')) abbrevs
1716− in map snd $ filter (\(y,_) -> y == x) abbrPairs
1694+ (do guardEnabled Ext_smart
1695+ abbrevs <- getOption readerAbbreviations
1696+ if not (null result) && last result == '.' && result `Set.member` abbrevs
1697+ then try (do ils <- whitespace <|> endline
1698+ lookAhead alphaNum
1699+ return $ do
1700+ ils' <- ils
1701+ if ils' == B.space
1702+ then return (B.str result <> B.str "\160")
1703+ else -- linebreak or softbreak
1704+ return (ils' <> B.str result <> B.str "\160"))
1705+ <|> return (return (B.str result))
1706+ else return (return (B.str result)))
1707+ <|> return (return (B.str result))
17171708
17181709-- an endline character that can be treated as a space, not a structural break
17191710endline :: PandocMonad m => MarkdownParser m (F Inlines)