git.iko.soy/typeable/pandoc
on master help sign in
Org reader: add support for sub/superscript export options

Org-mode allows to specify export settings via `#+OPTIONS` lines. Disabling simple sub- and superscripts is one of these export options, this options is now supported.

commit76143de97ed64130e982507b43ca380c2bb25ca9 authorAlbert Krewinkel <albert@zeitkraut.de> dateWed 11 May 2016, 18:27 (10 years ago) parent7a0729e
3 files changed+63−3
src/Text/Pandoc/Readers/Org.hs+25−3 src/Text/Pandoc/Readers/Org/ParserState.hs+32 tests/Tests/Readers/Org.hs+6
src/Text/Pandoc/Readers/Org.hs+25−3
@@ -121,6 +121,25 @@ addToNotesTable note = do
121121 oldnotes <- orgStateNotes' <$> getState
122122 updateState $ \s -> s{ orgStateNotes' = note:oldnotes }
123123
124+--
125+-- Export Settings
126+--
127+exportSetting :: OrgParser ()
128+exportSetting = choice
129+ [ booleanSetting "^" setExportSubSuperscripts
130+ ] <?> "export setting"
131+
132+booleanSetting :: String -> ExportSettingSetter Bool -> OrgParser ()
133+booleanSetting str setter = try $ do
134+ string str
135+ char ':'
136+ value <- many nonspaceChar
137+ let boolValue = case value of
138+ "nil" -> False
139+ "{}" -> False
140+ _ -> True
141+ updateState $ modifyExportSettings setter boolValue
142+
124143--
125144-- Parser
126145--
@@ -590,8 +609,9 @@ optionLine :: OrgParser ()
590609optionLine = try $ do
591610 key <- metaKey
592611 case key of
593− "link" -> parseLinkFormat >>= uncurry addLinkFormat
594− _ -> mzero
612+ "link" -> parseLinkFormat >>= uncurry addLinkFormat
613+ "options" -> () <$ sepBy spaces exportSetting
614+ _ -> mzero
595615
596616parseLinkFormat :: OrgParser ((String, String -> String))
597617parseLinkFormat = try $ do
@@ -1460,7 +1480,9 @@ subOrSuperExpr = try $
14601480 where enclosing (left, right) s = left : s ++ [right]
14611481
14621482simpleSubOrSuperString :: OrgParser String
1463−simpleSubOrSuperString = try $
1483+simpleSubOrSuperString = try $ do
1484+ state <- getState
1485+ guard . exportSubSuperscripts . orgStateExportSettings $ state
14641486 choice [ string "*"
14651487 , mappend <$> option [] ((:[]) <$> oneOf "+-")
14661488 <*> many1 alphaNum
src/Text/Pandoc/Readers/Org/ParserState.hs+32
@@ -38,6 +38,10 @@ module Text.Pandoc.Readers.Org.ParserState
3838 , trimInlinesF
3939 , runF
4040 , returnF
41+ , ExportSettingSetter
42+ , exportSubSuperscripts
43+ , setExportSubSuperscripts
44+ , modifyExportSettings
4145 ) where
4246
4347import Control.Monad (liftM, liftM2)
@@ -70,6 +74,12 @@ type OrgBlockAttributes = M.Map String String
7074-- link-type, the corresponding function transforms the given link string.
7175type OrgLinkFormatters = M.Map String (String -> String)
7276
77+-- | Export settings <http://orgmode.org/manual/Export-settings.html>
78+-- These settings can be changed via OPTIONS statements.
79+data ExportSettings = ExportSettings
80+ { exportSubSuperscripts :: Bool -- ^ TeX-like syntax for sub- and superscripts
81+ }
82+
7383-- | Org-mode parser state
7484data OrgParserState = OrgParserState
7585 { orgStateOptions :: ReaderOptions
@@ -77,6 +87,7 @@ data OrgParserState = OrgParserState
7787 , orgStateBlockAttributes :: OrgBlockAttributes
7888 , orgStateEmphasisCharStack :: [Char]
7989 , orgStateEmphasisNewlines :: Maybe Int
90+ , orgStateExportSettings :: ExportSettings
8091 , orgStateLastForbiddenCharPos :: Maybe SourcePos
8192 , orgStateLastPreCharPos :: Maybe SourcePos
8293 , orgStateLastStrPos :: Maybe SourcePos
@@ -119,6 +130,8 @@ instance HasHeaderMap OrgParserState where
119130 extractHeaderMap = orgStateHeaderMap
120131 updateHeaderMap f s = s{ orgStateHeaderMap = f (orgStateHeaderMap s) }
121132
133+instance Default ExportSettings where
134+ def = defaultExportSettings
122135
123136instance Default OrgParserState where
124137 def = defaultOrgParserState
@@ -130,6 +143,7 @@ defaultOrgParserState = OrgParserState
130143 , orgStateBlockAttributes = M.empty
131144 , orgStateEmphasisCharStack = []
132145 , orgStateEmphasisNewlines = Nothing
146+ , orgStateExportSettings = def
133147 , orgStateLastForbiddenCharPos = Nothing
134148 , orgStateLastPreCharPos = Nothing
135149 , orgStateLastStrPos = Nothing
@@ -142,6 +156,24 @@ defaultOrgParserState = OrgParserState
142156 , orgStateHeaderMap = M.empty
143157 }
144158
159+defaultExportSettings :: ExportSettings
160+defaultExportSettings = ExportSettings
161+ { exportSubSuperscripts = True
162+ }
163+
164+
165+--
166+-- Setter for exporting options
167+--
168+type ExportSettingSetter a = a -> ExportSettings -> ExportSettings
169+
170+setExportSubSuperscripts :: ExportSettingSetter Bool
171+setExportSubSuperscripts val es = es { exportSubSuperscripts = val }
172+
173+-- | Modify a parser state
174+modifyExportSettings :: ExportSettingSetter a -> a -> OrgParserState -> OrgParserState
175+modifyExportSettings setter val state =
176+ state { orgStateExportSettings = setter val . orgStateExportSettings $ state }
145177
146178--
147179-- Parser state reader
tests/Tests/Readers/Org.hs+6
@@ -469,6 +469,12 @@ tests =
469469 , "[[expl:foo][bar]]"
470470 ] =?>
471471 (para (link "http://example.com/foo" "" "bar"))
472+
473+ , "Export option: Disable simple sub/superscript syntax" =:
474+ unlines [ "#+OPTIONS: ^:nil"
475+ , "a^b"
476+ ] =?>
477+ para "a^b"
472478 ]
473479
474480 , testGroup "Basic Blocks" $