From f56792927fbcebfd25593c488476fee69d078b48 Mon Sep 17 00:00:00 2001 From: Albert Krewinkel Date: Fri, 3 Jun 2016 11:10:18 +0200 Subject: Org reader: support special strings export option Parsing of special strings (like '...' as ellipsis or '--' as en dash) can be toggled using the `-` option. --- src/Text/Pandoc/Readers/Org/Blocks.hs | 2 +- src/Text/Pandoc/Readers/Org/Inlines.hs | 12 ++++++++---- src/Text/Pandoc/Readers/Org/ParserState.hs | 20 ++++++++++++++------ src/Text/Pandoc/Readers/Org/Parsing.hs | 14 +++++++++++++- 4 files changed, 36 insertions(+), 12 deletions(-) diff --git a/src/Text/Pandoc/Readers/Org/Blocks.hs b/src/Text/Pandoc/Readers/Org/Blocks.hs index 896f1da1a..75e564f2f 100644 --- a/src/Text/Pandoc/Readers/Org/Blocks.hs +++ b/src/Text/Pandoc/Readers/Org/Blocks.hs @@ -502,7 +502,7 @@ exportSetting = choice [ booleanSetting "^" setExportSubSuperscripts , booleanSetting "'" setExportSmartQuotes , booleanSetting "*" setExportEmphasizedText - , ignoredSetting "-" + , booleanSetting "-" setExportSpecialStrings , ignoredSetting ":" , ignoredSetting "<" , ignoredSetting "\\n" diff --git a/src/Text/Pandoc/Readers/Org/Inlines.hs b/src/Text/Pandoc/Readers/Org/Inlines.hs index fdefd2a3f..001aeb569 100644 --- a/src/Text/Pandoc/Readers/Org/Inlines.hs +++ b/src/Text/Pandoc/Readers/Org/Inlines.hs @@ -728,8 +728,12 @@ smart = do doubleQuoted <|> singleQuoted <|> choice (map (return <$>) [orgApostrophe, orgDash, orgEllipses]) where - orgDash = dash <* updatePositions '-' - orgEllipses = ellipses <* updatePositions '.' + orgDash = do + guard =<< getExportSetting exportSpecialStrings + dash <* updatePositions '-' + orgEllipses = do + guard =<< getExportSetting exportSpecialStrings + ellipses <* updatePositions '.' orgApostrophe = (char '\'' <|> char '\8217') <* updateLastPreCharPos <* updateLastForbiddenCharPos @@ -737,7 +741,7 @@ smart = do singleQuoted :: OrgParser (F Inlines) singleQuoted = try $ do - guard . exportSmartQuotes . orgStateExportSettings =<< getState + guard =<< getExportSetting exportSmartQuotes singleQuoteStart updatePositions '\'' withQuoteContext InSingleQuote $ @@ -749,7 +753,7 @@ singleQuoted = try $ do -- in the same paragraph. doubleQuoted :: OrgParser (F Inlines) doubleQuoted = try $ do - guard . exportSmartQuotes . orgStateExportSettings =<< getState + guard =<< getExportSetting exportSmartQuotes doubleQuoteStart updatePositions '"' contents <- mconcat <$> many (try $ notFollowedBy doubleQuoteEnd >> inline) diff --git a/src/Text/Pandoc/Readers/Org/ParserState.hs b/src/Text/Pandoc/Readers/Org/ParserState.hs index 2fa5d920b..0c58183f9 100644 --- a/src/Text/Pandoc/Readers/Org/ParserState.hs +++ b/src/Text/Pandoc/Readers/Org/ParserState.hs @@ -45,6 +45,7 @@ module Text.Pandoc.Readers.Org.ParserState , setExportDrawers , setExportEmphasizedText , setExportSmartQuotes + , setExportSpecialStrings , setExportSubSuperscripts , modifyExportSettings , optionsToParserState @@ -84,8 +85,9 @@ data ExportSettings = ExportSettings -- ^ Specify drawer names which should be exported. @Left@ names are -- explicitly excluded from the resulting output while @Right@ means that -- only the listed drawer names should be included. - , exportEmphasizedText :: Bool -- ^ Parse emphasized text. - , exportSmartQuotes :: Bool -- ^ Parse quotes, ellipses, apostrophs smartly + , exportEmphasizedText :: Bool -- ^ Parse emphasized text + , exportSmartQuotes :: Bool -- ^ Parse quotes smartly + , exportSpecialStrings :: Bool -- ^ Parse ellipses and dashes smartly , exportSubSuperscripts :: Bool -- ^ TeX-like syntax for sub- and superscripts } @@ -160,6 +162,7 @@ defaultExportSettings = ExportSettings { exportDrawers = Left ["LOGBOOK"] , exportEmphasizedText = True , exportSmartQuotes = True + , exportSpecialStrings = True , exportSubSuperscripts = True } @@ -182,15 +185,20 @@ setExportDrawers val es = es { exportDrawers = val } setExportEmphasizedText :: ExportSettingSetter Bool setExportEmphasizedText val es = es { exportEmphasizedText = val } +-- | Set export options for parsing of smart quotes. +setExportSmartQuotes :: ExportSettingSetter Bool +setExportSmartQuotes val es = es { exportSmartQuotes = val } + +-- | Set export options for parsing of special strings (like em/en dashes or +-- ellipses). +setExportSpecialStrings :: ExportSettingSetter Bool +setExportSpecialStrings val es = es { exportSpecialStrings = val } + -- | Set export options for sub/superscript parsing. The short syntax will -- not be parsed if this is set set to @False@. setExportSubSuperscripts :: ExportSettingSetter Bool setExportSubSuperscripts val es = es { exportSubSuperscripts = val } --- | Set export options for parsing of smart quotes. -setExportSmartQuotes :: ExportSettingSetter Bool -setExportSmartQuotes val es = es { exportSmartQuotes = val } - -- | Modify a parser state modifyExportSettings :: ExportSettingSetter a -> a -> OrgParserState -> OrgParserState modifyExportSettings setter val state = diff --git a/src/Text/Pandoc/Readers/Org/Parsing.hs b/src/Text/Pandoc/Readers/Org/Parsing.hs index 0b6b876d8..8cf0c696c 100644 --- a/src/Text/Pandoc/Readers/Org/Parsing.hs +++ b/src/Text/Pandoc/Readers/Org/Parsing.hs @@ -37,6 +37,7 @@ module Text.Pandoc.Readers.Org.Parsing , skipSpaces1 , inList , withContext + , getExportSetting , updateLastForbiddenCharPos , updateLastPreCharPos , orgArgKey @@ -174,9 +175,13 @@ withContext context parser = do return result -- --- Parser state update functions +-- Parser state functions -- +-- | Get an export setting. +getExportSetting :: (ExportSettings -> a) -> OrgParser a +getExportSetting s = s . orgStateExportSettings <$> getState + -- | Set the current position as the last position at which a forbidden char -- was found (i.e. a character which is not allowed at the inner border of -- markup). @@ -190,13 +195,20 @@ updateLastPreCharPos :: OrgParser () updateLastPreCharPos = getPosition >>= \p -> updateState $ \s -> s{ orgStateLastPreCharPos = Just p} +-- +-- Org key-value parsing +-- + +-- | Read the key of a plist style key-value list. orgArgKey :: OrgParser String orgArgKey = try $ skipSpaces *> char ':' *> many1 orgArgWordChar +-- | Read the value of a plist style key-value list. orgArgWord :: OrgParser String orgArgWord = many1 orgArgWordChar +-- | Chars treated as part of a word in plists. orgArgWordChar :: OrgParser Char orgArgWordChar = alphaNum <|> oneOf "-_" -- cgit v1.2.3