git.iko.soy/typeable/pandoc
on master help sign in
Org reader: put export setting parser into module
c1f6bd2Albert Krewinkel
pandoc.cabal+1
@@ -399,6 +399,7 @@ Library
399 Text.Pandoc.Readers.Odt.Arrows.Utils,
399 Text.Pandoc.Readers.Odt.Arrows.Utils,
400 Text.Pandoc.Readers.Org.BlockStarts,
400 Text.Pandoc.Readers.Org.BlockStarts,
401 Text.Pandoc.Readers.Org.Blocks,
401 Text.Pandoc.Readers.Org.Blocks,
402+ Text.Pandoc.Readers.Org.ExportSettings,
402 Text.Pandoc.Readers.Org.Inlines,
403 Text.Pandoc.Readers.Org.Inlines,
403 Text.Pandoc.Readers.Org.ParserState,
404 Text.Pandoc.Readers.Org.ParserState,
404 Text.Pandoc.Readers.Org.Parsing,
405 Text.Pandoc.Readers.Org.Parsing,
src/Text/Pandoc/Readers/Org/Blocks.hs+2−116
@@ -34,6 +34,7 @@ module Text.Pandoc.Readers.Org.Blocks
34 ) where
34 ) where
35
35
36import Text.Pandoc.Readers.Org.BlockStarts
36import Text.Pandoc.Readers.Org.BlockStarts
37+import Text.Pandoc.Readers.Org.ExportSettings ( exportSettings )
37import Text.Pandoc.Readers.Org.Inlines
38import Text.Pandoc.Readers.Org.Inlines
38import Text.Pandoc.Readers.Org.ParserState
39import Text.Pandoc.Readers.Org.ParserState
39import Text.Pandoc.Readers.Org.Parsing
40import Text.Pandoc.Readers.Org.Parsing
@@ -620,7 +621,7 @@ optionLine = try $ do
620 key <- metaKey
621 key <- metaKey
621 case key of
622 case key of
622 "link" -> parseLinkFormat >>= uncurry addLinkFormat
623 "link" -> parseLinkFormat >>= uncurry addLinkFormat
623− "options" -> () <$ sepBy spaces exportSetting
624+ "options" -> exportSettings
624 _ -> mzero
625 _ -> mzero
625
626
626addLinkFormat :: String
627addLinkFormat :: String
@@ -630,121 +631,6 @@ addLinkFormat key formatter = updateState $ \s ->
630 let fs = orgStateLinkFormatters s
631 let fs = orgStateLinkFormatters s
631 in s{ orgStateLinkFormatters = M.insert key formatter fs }
632 in s{ orgStateLinkFormatters = M.insert key formatter fs }
632
633
633−
634−--
635−-- Export Settings
636−--
637−
638−-- | Read and process org-mode specific export options.
639−exportSetting :: OrgParser ()
640−exportSetting = choice
641− [ booleanSetting "^" setExportSubSuperscripts
642− , booleanSetting "'" setExportSmartQuotes
643− , booleanSetting "*" setExportEmphasizedText
644− , booleanSetting "-" setExportSpecialStrings
645− , ignoredSetting ":"
646− , ignoredSetting "<"
647− , ignoredSetting "\\n"
648− , archivedTreeSetting "arch" setExportArchivedTrees
649− , ignoredSetting "author"
650− , ignoredSetting "c"
651− , ignoredSetting "creator"
652− , complementableListSetting "d" setExportDrawers
653− , ignoredSetting "date"
654− , ignoredSetting "e"
655− , ignoredSetting "email"
656− , ignoredSetting "f"
657− , ignoredSetting "H"
658− , ignoredSetting "inline"
659− , ignoredSetting "num"
660− , ignoredSetting "p"
661− , ignoredSetting "pri"
662− , ignoredSetting "prop"
663− , ignoredSetting "stat"
664− , ignoredSetting "tags"
665− , ignoredSetting "tasks"
666− , ignoredSetting "tex"
667− , ignoredSetting "timestamp"
668− , ignoredSetting "title"
669− , ignoredSetting "toc"
670− , ignoredSetting "todo"
671− , ignoredSetting "|"
672− ] <?> "export setting"
673−
674−booleanSetting :: String -> ExportSettingSetter Bool -> OrgParser ()
675−booleanSetting settingIdentifier setter = try $ do
676− string settingIdentifier
677− char ':'
678− value <- elispBoolean
679− updateState $ modifyExportSettings setter value
680−
681−-- | Read an elisp boolean. Only NIL is treated as false, non-NIL values are
682−-- interpreted as true.
683−elispBoolean :: OrgParser Bool
684−elispBoolean = try $ do
685− value <- many1 nonspaceChar
686− return $ case map toLower value of
687− "nil" -> False
688− "{}" -> False
689− "()" -> False
690− _ -> True
691−
692−archivedTreeSetting :: String
693− -> ExportSettingSetter ArchivedTreesOption
694− -> OrgParser ()
695−archivedTreeSetting settingIdentifier setter = try $ do
696− string settingIdentifier
697− char ':'
698− value <- archivedTreesHeadlineSetting <|> archivedTreesBoolean
699− updateState $ modifyExportSettings setter value
700− where
701− archivedTreesHeadlineSetting = try $ do
702− string "headline"
703− lookAhead (newline <|> spaceChar)
704− return ArchivedTreesHeadlineOnly
705−
706− archivedTreesBoolean = try $ do
707− exportBool <- elispBoolean
708− return $
709− if exportBool
710− then ArchivedTreesExport
711− else ArchivedTreesNoExport
712−
713−-- | A list or a complement list (i.e. a list starting with `not`).
714−complementableListSetting :: String
715− -> ExportSettingSetter (Either [String] [String])
716− -> OrgParser ()
717−complementableListSetting settingIdentifier setter = try $ do
718− _ <- string settingIdentifier <* char ':'
719− value <- choice [ Left <$> complementStringList
720− , Right <$> stringList
721− , (\b -> if b then Left [] else Right []) <$> elispBoolean
722− ]
723− updateState $ modifyExportSettings setter value
724− where
725− -- Read a plain list of strings.
726− stringList :: OrgParser [String]
727− stringList = try $
728− char '('
729− *> sepBy elispString spaces
730− <* char ')'
731−
732− -- Read an emacs lisp list specifying a complement set.
733− complementStringList :: OrgParser [String]
734− complementStringList = try $
735− string "(not "
736− *> sepBy elispString spaces
737− <* char ')'
738−
739− elispString :: OrgParser String
740− elispString = try $
741− char '"'
742− *> manyTill alphaNum (char '"')
743−
744−ignoredSetting :: String -> OrgParser ()
745−ignoredSetting s = try (() <$ string s <* char ':' <* many1 nonspaceChar)
746−
747−
748parseLinkFormat :: OrgParser ((String, String -> String))
634parseLinkFormat :: OrgParser ((String, String -> String))
749parseLinkFormat = try $ do
635parseLinkFormat = try $ do
750 linkType <- (:) <$> letter <*> many (alphaNum <|> oneOf "-_") <* skipSpaces
636 linkType <- (:) <$> letter <*> many (alphaNum <|> oneOf "-_") <* skipSpaces
src/Text/Pandoc/Readers/Org/ExportSettings.hsnew file+159
@@ -0,0 +1,159 @@
1+{-
2+Copyright (C) 2014-2016 Albert Krewinkel <tarleb+pandoc@moltkeplatz.de>
3+
4+This program is free software; you can redistribute it and/or modify
5+it under the terms of the GNU General Public License as published by
6+the Free Software Foundation; either version 2 of the License, or
7+(at your option) any later version.
8+
9+This program is distributed in the hope that it will be useful,
10+but WITHOUT ANY WARRANTY; without even the implied warranty of
11+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+GNU General Public License for more details.
13+
14+You should have received a copy of the GNU General Public License
15+along with this program; if not, write to the Free Software
16+Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17+-}
18+
19+{- |
20+ Module : Text.Pandoc.Readers.Org.Options
21+ Copyright : Copyright (C) 2016 Albert Krewinkel
22+ License : GNU GPL, version 2 or above
23+
24+ Maintainer : Albert Krewinkel <tarleb+pandoc@moltkeplatz.de>
25+
26+Parsers for Org-mode export options.
27+-}
28+module Text.Pandoc.Readers.Org.ExportSettings
29+ ( exportSettings
30+ ) where
31+
32+import Text.Pandoc.Readers.Org.ParserState
33+import Text.Pandoc.Readers.Org.Parsing
34+
35+import Control.Monad ( void )
36+import Data.Char ( toLower )
37+
38+-- | Read and handle space separated org-mode export settings.
39+exportSettings :: OrgParser ()
40+exportSettings = void $ sepBy spaces exportSetting
41+
42+-- | Setter function for export settings.
43+type ExportSettingSetter a = a -> ExportSettings -> ExportSettings
44+
45+-- | Read and process a single org-mode export option.
46+exportSetting :: OrgParser ()
47+exportSetting = choice
48+ [ booleanSetting "^" (\val es -> es { exportSubSuperscripts = val })
49+ , booleanSetting "'" (\val es -> es { exportSmartQuotes = val })
50+ , booleanSetting "*" (\val es -> es { exportEmphasizedText = val })
51+ , booleanSetting "-" (\val es -> es { exportSpecialStrings = val })
52+ , ignoredSetting ":"
53+ , ignoredSetting "<"
54+ , ignoredSetting "\\n"
55+ , archivedTreeSetting "arch" (\val es -> es { exportArchivedTrees = val })
56+ , ignoredSetting "author"
57+ , ignoredSetting "c"
58+ , ignoredSetting "creator"
59+ , complementableListSetting "d" (\val es -> es { exportDrawers = val })
60+ , ignoredSetting "date"
61+ , ignoredSetting "e"
62+ , ignoredSetting "email"
63+ , ignoredSetting "f"
64+ , ignoredSetting "H"
65+ , ignoredSetting "inline"
66+ , ignoredSetting "num"
67+ , ignoredSetting "p"
68+ , ignoredSetting "pri"
69+ , ignoredSetting "prop"
70+ , ignoredSetting "stat"
71+ , ignoredSetting "tags"
72+ , ignoredSetting "tasks"
73+ , ignoredSetting "tex"
74+ , ignoredSetting "timestamp"
75+ , ignoredSetting "title"
76+ , ignoredSetting "toc"
77+ , ignoredSetting "todo"
78+ , ignoredSetting "|"
79+ ] <?> "export setting"
80+
81+genericExportSetting :: OrgParser a
82+ -> String
83+ -> ExportSettingSetter a
84+ -> OrgParser ()
85+genericExportSetting optionParser settingIdentifier setter = try $ do
86+ _ <- string settingIdentifier *> char ':'
87+ value <- optionParser
88+ updateState $ modifyExportSettings value
89+ where
90+ modifyExportSettings val st =
91+ st { orgStateExportSettings = setter val . orgStateExportSettings $ st }
92+
93+-- | A boolean option, either nil (False) or non-nil (True).
94+booleanSetting :: String -> ExportSettingSetter Bool -> OrgParser ()
95+booleanSetting = genericExportSetting elispBoolean
96+
97+-- | Either the string "headline" or an elisp boolean and treated as an
98+-- @ArchivedTreesOption@.
99+archivedTreeSetting :: String
100+ -> ExportSettingSetter ArchivedTreesOption
101+ -> OrgParser ()
102+archivedTreeSetting =
103+ genericExportSetting $ archivedTreesHeadlineSetting <|> archivedTreesBoolean
104+ where
105+ archivedTreesHeadlineSetting = try $ do
106+ _ <- string "headline"
107+ lookAhead (newline <|> spaceChar)
108+ return ArchivedTreesHeadlineOnly
109+
110+ archivedTreesBoolean = try $ do
111+ exportBool <- elispBoolean
112+ return $
113+ if exportBool
114+ then ArchivedTreesExport
115+ else ArchivedTreesNoExport
116+
117+-- | A list or a complement list (i.e. a list starting with `not`).
118+complementableListSetting :: String
119+ -> ExportSettingSetter (Either [String] [String])
120+ -> OrgParser ()
121+complementableListSetting = genericExportSetting $ choice
122+ [ Left <$> complementStringList
123+ , Right <$> stringList
124+ , (\b -> if b then Left [] else Right []) <$> elispBoolean
125+ ]
126+ where
127+ -- Read a plain list of strings.
128+ stringList :: OrgParser [String]
129+ stringList = try $
130+ char '('
131+ *> sepBy elispString spaces
132+ <* char ')'
133+
134+ -- Read an emacs lisp list specifying a complement set.
135+ complementStringList :: OrgParser [String]
136+ complementStringList = try $
137+ string "(not "
138+ *> sepBy elispString spaces
139+ <* char ')'
140+
141+ elispString :: OrgParser String
142+ elispString = try $
143+ char '"'
144+ *> manyTill alphaNum (char '"')
145+
146+-- | Read but ignore the export setting.
147+ignoredSetting :: String -> OrgParser ()
148+ignoredSetting s = try (() <$ string s <* char ':' <* many1 nonspaceChar)
149+
150+-- | Read an elisp boolean. Only NIL is treated as false, non-NIL values are
151+-- interpreted as true.
152+elispBoolean :: OrgParser Bool
153+elispBoolean = try $ do
154+ value <- many1 nonspaceChar
155+ return $ case map toLower value of
156+ "nil" -> False
157+ "{}" -> False
158+ "()" -> False
159+ _ -> True
src/Text/Pandoc/Readers/Org/ParserState.hs+30−75
@@ -40,16 +40,8 @@ module Text.Pandoc.Readers.Org.ParserState
40 , trimInlinesF
40 , trimInlinesF
41 , runF
41 , runF
42 , returnF
42 , returnF
43− , ExportSettingSetter
44 , ExportSettings (..)
43 , ExportSettings (..)
45 , ArchivedTreesOption (..)
44 , ArchivedTreesOption (..)
46− , setExportArchivedTrees
47− , setExportDrawers
48− , setExportEmphasizedText
49− , setExportSmartQuotes
50− , setExportSpecialStrings
51− , setExportSubSuperscripts
52− , modifyExportSettings
53 , optionsToParserState
45 , optionsToParserState
54 ) where
46 ) where
55
47
@@ -80,26 +72,6 @@ type OrgNoteTable = [OrgNoteRecord]
80-- link-type, the corresponding function transforms the given link string.
72-- link-type, the corresponding function transforms the given link string.
81type OrgLinkFormatters = M.Map String (String -> String)
73type OrgLinkFormatters = M.Map String (String -> String)
82
74
83−-- | Options for the way archived trees are handled.
84−data ArchivedTreesOption =
85− ArchivedTreesExport -- ^ Export the complete tree
86− | ArchivedTreesNoExport -- ^ Exclude archived trees from exporting
87− | ArchivedTreesHeadlineOnly -- ^ Export only the headline, discard the contents
88−
89−-- | Export settings <http://orgmode.org/manual/Export-settings.html>
90−-- These settings can be changed via OPTIONS statements.
91−data ExportSettings = ExportSettings
92− { exportArchivedTrees :: ArchivedTreesOption -- ^ How to treat archived trees
93− , exportDrawers :: Either [String] [String]
94− -- ^ Specify drawer names which should be exported. @Left@ names are
95− -- explicitly excluded from the resulting output while @Right@ means that
96− -- only the listed drawer names should be included.
97− , exportEmphasizedText :: Bool -- ^ Parse emphasized text
98− , exportSmartQuotes :: Bool -- ^ Parse quotes smartly
99− , exportSpecialStrings :: Bool -- ^ Parse ellipses and dashes smartly
100− , exportSubSuperscripts :: Bool -- ^ TeX-like syntax for sub- and superscripts
101− }
102−
103-- | Org-mode parser state
75-- | Org-mode parser state
104data OrgParserState = OrgParserState
76data OrgParserState = OrgParserState
105 { orgStateAnchorIds :: [String]
77 { orgStateAnchorIds :: [String]
@@ -142,9 +114,6 @@ instance HasHeaderMap OrgParserState where
142 extractHeaderMap = orgStateHeaderMap
114 extractHeaderMap = orgStateHeaderMap
143 updateHeaderMap f s = s{ orgStateHeaderMap = f (orgStateHeaderMap s) }
115 updateHeaderMap f s = s{ orgStateHeaderMap = f (orgStateHeaderMap s) }
144
116
145−instance Default ExportSettings where
146− def = defaultExportSettings
147−
148instance Default OrgParserState where
117instance Default OrgParserState where
149 def = defaultOrgParserState
118 def = defaultOrgParserState
150
119
@@ -166,60 +135,46 @@ defaultOrgParserState = OrgParserState
166 , orgStateParserContext = NullState
135 , orgStateParserContext = NullState
167 }
136 }
168
137
169−defaultExportSettings :: ExportSettings
170−defaultExportSettings = ExportSettings
171− { exportArchivedTrees = ArchivedTreesHeadlineOnly
172− , exportDrawers = Left ["LOGBOOK"]
173− , exportEmphasizedText = True
174− , exportSmartQuotes = True
175− , exportSpecialStrings = True
176− , exportSubSuperscripts = True
177− }
178−
179optionsToParserState :: ReaderOptions -> OrgParserState
138optionsToParserState :: ReaderOptions -> OrgParserState
180optionsToParserState opts =
139optionsToParserState opts =
181 def { orgStateOptions = opts }
140 def { orgStateOptions = opts }
182
141
183−
184--
142--
185−-- Setter for exporting options
143+-- Export Settings
186--
144--
187
145
188−-- This whole section could be scraped if we were using lenses.
146+-- | Options for the way archived trees are handled.
189−
190−type ExportSettingSetter a = a -> ExportSettings -> ExportSettings
191−
192−-- | Set export options for archived trees.
193−setExportArchivedTrees :: ExportSettingSetter ArchivedTreesOption
147+data ArchivedTreesOption =
194−setExportArchivedTrees val es = es { exportArchivedTrees = val }
148+ ArchivedTreesExport -- ^ Export the complete tree
195−
149+ | ArchivedTreesNoExport -- ^ Exclude archived trees from exporting
196−-- | Set export options for drawers. See the @exportDrawers@ in ADT
150+ | ArchivedTreesHeadlineOnly -- ^ Export only the headline, discard the contents
197−-- @ExportSettings@ for details.
198−setExportDrawers :: ExportSettingSetter (Either [String] [String])
199−setExportDrawers val es = es { exportDrawers = val }
200−
201−-- | Set export options for emphasis parsing.
202−setExportEmphasizedText :: ExportSettingSetter Bool
203−setExportEmphasizedText val es = es { exportEmphasizedText = val }
204−
205−-- | Set export options for parsing of smart quotes.
206−setExportSmartQuotes :: ExportSettingSetter Bool
207−setExportSmartQuotes val es = es { exportSmartQuotes = val }
208
151
209−-- | Set export options for parsing of special strings (like em/en dashes or
152+-- | Export settings <http://orgmode.org/manual/Export-settings.html>
210−-- ellipses).
153+-- These settings can be changed via OPTIONS statements.
211−setExportSpecialStrings :: ExportSettingSetter Bool
154+data ExportSettings = ExportSettings
212−setExportSpecialStrings val es = es { exportSpecialStrings = val }
155+ { exportArchivedTrees :: ArchivedTreesOption -- ^ How to treat archived trees
156+ , exportDrawers :: Either [String] [String]
157+ -- ^ Specify drawer names which should be exported. @Left@ names are
158+ -- explicitly excluded from the resulting output while @Right@ means that
159+ -- only the listed drawer names should be included.
160+ , exportEmphasizedText :: Bool -- ^ Parse emphasized text
161+ , exportSmartQuotes :: Bool -- ^ Parse quotes smartly
162+ , exportSpecialStrings :: Bool -- ^ Parse ellipses and dashes smartly
163+ , exportSubSuperscripts :: Bool -- ^ TeX-like syntax for sub- and superscripts
164+ }
213
165
214−-- | Set export options for sub/superscript parsing. The short syntax will
166+instance Default ExportSettings where
215−-- not be parsed if this is set set to @False@.
167+ def = defaultExportSettings
216−setExportSubSuperscripts :: ExportSettingSetter Bool
217−setExportSubSuperscripts val es = es { exportSubSuperscripts = val }
218
168
219−-- | Modify a parser state
169+defaultExportSettings :: ExportSettings
220−modifyExportSettings :: ExportSettingSetter a -> a -> OrgParserState -> OrgParserState
170+defaultExportSettings = ExportSettings
221−modifyExportSettings setter val state =
171+ { exportArchivedTrees = ArchivedTreesHeadlineOnly
222− state { orgStateExportSettings = setter val . orgStateExportSettings $ state }
172+ , exportDrawers = Left ["LOGBOOK"]
173+ , exportEmphasizedText = True
174+ , exportSmartQuotes = True
175+ , exportSpecialStrings = True
176+ , exportSubSuperscripts = True
177+ }
223
178
224
179
225--
180--