git.iko.soy/typeable/pandoc
on master help sign in
Improve code style in lua and org modules
commit55d679e382954dd458acd6233609851748522d99 authorAlbert Krewinkel <albert@zeitkraut.de> dateSat 3 Jun 2017, 12:28 (9 years ago) parentd55f01c
12 files changed+75−83
src/Text/Pandoc/Lua.hs+9−9 src/Text/Pandoc/Lua/Compat.hs+2−2 src/Text/Pandoc/Lua/PandocModule.hs+12−14 src/Text/Pandoc/Lua/SharedInstances.hs+6−6 src/Text/Pandoc/Lua/StackInstances.hs+6−6 src/Text/Pandoc/Lua/Util.hs+2−4 src/Text/Pandoc/Readers/Org/Blocks.hs+1−1 src/Text/Pandoc/Readers/Org/DocumentTree.hs+3−3 src/Text/Pandoc/Readers/Org/Inlines.hs+6−7 src/Text/Pandoc/Readers/Org/Meta.hs+5−5 src/Text/Pandoc/Readers/Org/ParserState.hs+9−10 src/Text/Pandoc/Writers/Org.hs+14−16
src/Text/Pandoc/Lua.hs+9−9
@@ -15,8 +15,8 @@ You should have received a copy of the GNU General Public License
1515along with this program; if not, write to the Free Software
1616Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1717-}
18−{-# LANGUAGE FlexibleContexts #-}
19−{-# LANGUAGE FlexibleInstances #-}
18+{-# LANGUAGE FlexibleContexts #-}
19+{-# LANGUAGE FlexibleInstances #-}
2020{-# LANGUAGE MultiParamTypeClasses #-}
2121{- |
2222 Module : Text.Pandoc.Lua
@@ -30,12 +30,12 @@ Pandoc lua utils.
3030-}
3131module Text.Pandoc.Lua ( runLuaFilter, pushPandocModule ) where
3232
33−import Control.Monad ( (>=>), when )
34−import Control.Monad.Trans ( MonadIO(..) )
35−import Data.Map ( Map )
36−import Scripting.Lua ( LuaState, StackValue(..) )
33+import Control.Monad (unless, when, (>=>))
34+import Control.Monad.Trans (MonadIO (..))
35+import Data.Map (Map)
36+import Scripting.Lua (LuaState, StackValue (..))
3737import Text.Pandoc.Definition
38−import Text.Pandoc.Lua.PandocModule ( pushPandocModule )
38+import Text.Pandoc.Lua.PandocModule (pushPandocModule)
3939import Text.Pandoc.Lua.StackInstances ()
4040import Text.Pandoc.Walk
4141
@@ -80,7 +80,7 @@ pushGlobalFilter lua =
8080 *> Lua.rawseti lua (-2) 1
8181
8282runAll :: [LuaFilter] -> Pandoc -> IO Pandoc
83−runAll [] = return
83+runAll [] = return
8484runAll (x:xs) = walkMWithLuaFilter x >=> runAll xs
8585
8686walkMWithLuaFilter :: LuaFilter -> Pandoc -> IO Pandoc
@@ -225,7 +225,7 @@ instance StackValue LuaFilterFunction where
225225 push lua v = pushFilterFunction lua v
226226 peek lua i = do
227227 isFn <- Lua.isfunction lua i
228− when (not isFn) (error $ "Not a function at index " ++ (show i))
228+ unless isFn (error $ "Not a function at index " ++ (show i))
229229 Lua.pushvalue lua i
230230 push lua ("PANDOC_FILTER_FUNCTIONS"::String)
231231 Lua.rawget lua Lua.registryindex
src/Text/Pandoc/Lua/Compat.hs+2−2
@@ -28,13 +28,13 @@ Compatibility helpers for hslua
2828-}
2929module Text.Pandoc.Lua.Compat ( loadstring ) where
3030
31−import Scripting.Lua ( LuaState )
31+import Scripting.Lua (LuaState)
3232import qualified Scripting.Lua as Lua
3333
3434-- | Interpret string as lua code and load into the lua environment.
3535loadstring :: LuaState -> String -> String -> IO Int
3636#if MIN_VERSION_hslua(0,5,0)
37−loadstring lua script _ = Lua.loadstring lua script
37+loadstring lua script _ = Lua.loadstring lua script
3838#else
3939loadstring lua script cn = Lua.loadstring lua script cn
4040#endif
src/Text/Pandoc/Lua/PandocModule.hs+12−14
@@ -27,25 +27,23 @@ Pandoc module for lua.
2727-}
2828module Text.Pandoc.Lua.PandocModule ( pushPandocModule ) where
2929
30−import Data.ByteString.Char8 ( unpack )
31−import Data.Default ( Default(..) )
32−import Scripting.Lua ( LuaState, call, push, pushhsfunction, rawset)
33−import Text.Pandoc.Class hiding ( readDataFile )
34−import Text.Pandoc.Definition ( Pandoc )
35−import Text.Pandoc.Lua.Compat ( loadstring )
30+import Control.Monad (unless)
31+import Data.ByteString.Char8 (unpack)
32+import Data.Default (Default (..))
33+import Scripting.Lua (LuaState, call, push, pushhsfunction, rawset)
34+import Text.Pandoc.Class hiding (readDataFile)
35+import Text.Pandoc.Definition (Pandoc)
36+import Text.Pandoc.Lua.Compat (loadstring)
3637import Text.Pandoc.Lua.StackInstances ()
37−import Text.Pandoc.Readers ( Reader(..), getReader )
38−import Text.Pandoc.Shared ( readDataFile )
38+import Text.Pandoc.Readers (Reader (..), getReader)
39+import Text.Pandoc.Shared (readDataFile)
3940
4041-- | Push the "pandoc" on the lua stack.
4142pushPandocModule :: LuaState -> IO ()
4243pushPandocModule lua = do
4344 script <- pandocModuleScript
4445 status <- loadstring lua script "pandoc.lua"
45− if (status /= 0)
46− then return ()
47− else do
48− call lua 0 1
46+ unless (status /= 0) $ call lua 0 1
4947 push lua "__read"
5048 pushhsfunction lua read_doc
5149 rawset lua (-3)
@@ -57,13 +55,13 @@ pandocModuleScript = unpack <$> readDataFile Nothing "pandoc.lua"
5755read_doc :: String -> String -> IO (Either String Pandoc)
5856read_doc formatSpec content = do
5957 case getReader formatSpec of
60− Left s -> return $ Left s
58+ Left s -> return $ Left s
6159 Right reader ->
6260 case reader of
6361 StringReader r -> do
6462 res <- runIO $ r def content
6563 case res of
66− Left s -> return . Left $ show s
64+ Left s -> return . Left $ show s
6765 Right pd -> return $ Right pd
6866 _ -> return $ Left "Only string formats are supported at the moment."
6967
src/Text/Pandoc/Lua/SharedInstances.hs+6−6
@@ -16,9 +16,9 @@ You should have received a copy of the GNU General Public License
1616along with this program; if not, write to the Free Software
1717Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1818-}
19−{-# LANGUAGE CPP #-}
20−{-# LANGUAGE FlexibleInstances #-}
21−{-# LANGUAGE LambdaCase #-}
19+{-# LANGUAGE CPP #-}
20+{-# LANGUAGE FlexibleInstances #-}
21+{-# LANGUAGE LambdaCase #-}
2222#if !MIN_VERSION_base(4,8,0)
2323{-# LANGUAGE OverlappingInstances #-}
2424#endif
@@ -36,8 +36,8 @@ Shared StackValue instances for pandoc and generic types.
3636-}
3737module Text.Pandoc.Lua.SharedInstances () where
3838
39−import Scripting.Lua ( LTYPE(..), StackValue(..), newtable )
40−import Text.Pandoc.Lua.Util ( addRawInt, addValue, getRawInt, keyValuePairs )
39+import Scripting.Lua (LTYPE (..), StackValue (..), newtable)
40+import Text.Pandoc.Lua.Util (addRawInt, addValue, getRawInt, keyValuePairs)
4141
4242import qualified Data.Map as M
4343import qualified Text.Pandoc.UTF8 as UTF8
@@ -112,5 +112,5 @@ instance (StackValue a, StackValue b) => StackValue (Either a b) where
112112 peek lua idx = peek lua idx >>= \case
113113 Just left -> return . Just $ Left left
114114 Nothing -> fmap Right <$> peek lua idx
115− valuetype (Left x) = valuetype x
115+ valuetype (Left x) = valuetype x
116116 valuetype (Right x) = valuetype x
src/Text/Pandoc/Lua/StackInstances.hs+6−6
@@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software
1717Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1818-}
1919{-# LANGUAGE FlexibleInstances #-}
20−{-# LANGUAGE LambdaCase #-}
20+{-# LANGUAGE LambdaCase #-}
2121{-# OPTIONS_GHC -fno-warn-orphans #-}
2222{- |
2323 Module : Text.Pandoc.Lua.StackInstances
@@ -32,13 +32,13 @@ StackValue instances for pandoc types.
3232-}
3333module Text.Pandoc.Lua.StackInstances () where
3434
35−import Control.Applicative ( (<|>) )
36−import Scripting.Lua
37− ( LTYPE(..), LuaState, StackValue(..), ltype, newtable, objlen )
35+import Control.Applicative ((<|>))
36+import Scripting.Lua (LTYPE (..), LuaState, StackValue (..), ltype, newtable,
37+ objlen)
3838import Text.Pandoc.Definition
3939import Text.Pandoc.Lua.SharedInstances ()
40−import Text.Pandoc.Lua.Util ( addValue, getTable, pushViaConstructor )
41−import Text.Pandoc.Shared ( safeRead )
40+import Text.Pandoc.Lua.Util (addValue, getTable, pushViaConstructor)
41+import Text.Pandoc.Shared (safeRead)
4242
4343instance StackValue Pandoc where
4444 push lua (Pandoc meta blocks) = do
src/Text/Pandoc/Lua/Util.hs+2−4
@@ -42,10 +42,8 @@ module Text.Pandoc.Lua.Util
4242 , pushViaConstructor
4343 ) where
4444
45−import Scripting.Lua
46− ( LuaState, StackValue(..)
47− , call, getglobal2, gettable, next, pop, pushnil, rawgeti, rawseti, settable
48− )
45+import Scripting.Lua (LuaState, StackValue (..), call, getglobal2, gettable,
46+ next, pop, pushnil, rawgeti, rawseti, settable)
4947
5048-- | Adjust the stack index, assuming that @n@ new elements have been pushed on
5149-- the stack.
src/Text/Pandoc/Readers/Org/Blocks.hs+1−1
@@ -351,7 +351,7 @@ switchesAsAttributes = try $ do
351351 Just num -> ("startFrom", num):kv
352352 Nothing -> kv
353353 cls' = case pol of
354− SwitchPlus -> "continuedSourceBlock":cls
354+ SwitchPlus -> "continuedSourceBlock":cls
355355 SwitchMinus -> cls
356356 in ("numberLines":cls', kv')
357357 addToAttr _ x = x
src/Text/Pandoc/Readers/Org/DocumentTree.hs+3−3
@@ -35,14 +35,14 @@ module Text.Pandoc.Readers.Org.DocumentTree
3535import Control.Arrow ((***))
3636import Control.Monad (guard, void)
3737import Data.Char (toLower, toUpper)
38−import Data.List ( intersperse )
38+import Data.List (intersperse)
3939import Data.Monoid ((<>))
4040import Text.Pandoc.Builder (Blocks, Inlines)
4141import Text.Pandoc.Class (PandocMonad)
4242import Text.Pandoc.Definition
4343import Text.Pandoc.Readers.Org.BlockStarts
44−import Text.Pandoc.Readers.Org.Parsing
4544import Text.Pandoc.Readers.Org.ParserState
45+import Text.Pandoc.Readers.Org.Parsing
4646
4747import qualified Data.Map as Map
4848import qualified Text.Pandoc.Builder as B
@@ -78,7 +78,7 @@ documentTree blocks inline = do
7878 getTitle metamap =
7979 case Map.lookup "title" metamap of
8080 Just (MetaInlines inlns) -> inlns
81− _ -> []
81+ _ -> []
8282
8383newtype Tag = Tag { fromTag :: String }
8484 deriving (Show, Eq)
src/Text/Pandoc/Readers/Org/Inlines.hs+6−7
@@ -343,11 +343,10 @@ orgRefCiteKey =
343343 let citeKeySpecialChars = "-_:\\./," :: String
344344 isCiteKeySpecialChar c = c `elem` citeKeySpecialChars
345345 isCiteKeyChar c = isAlphaNum c || isCiteKeySpecialChar c
346−
347− in try $ many1Till (satisfy isCiteKeyChar)
348− $ try . lookAhead $ do
349− many . satisfy $ isCiteKeySpecialChar
350− satisfy $ not . isCiteKeyChar
346+ endOfCitation = try $ do
347+ many $ satisfy isCiteKeySpecialChar
348+ satisfy $ not . isCiteKeyChar
349+ in try $ satisfy isCiteKeyChar `many1Till` lookAhead endOfCitation
351350
352351
353352-- | Supported citation types. Only a small subset of org-ref types is
@@ -415,7 +414,7 @@ referencedNote = try $ do
415414 return $ do
416415 notes <- asksF orgStateNotes'
417416 case lookup ref notes of
418− Nothing -> return $ B.str $ "[" ++ ref ++ "]"
417+ Nothing -> return . B.str $ "[" ++ ref ++ "]"
419418 Just contents -> do
420419 st <- askF
421420 let contents' = runF contents st{ orgStateNotes' = [] }
@@ -439,7 +438,7 @@ explicitOrImageLink = try $ do
439438 src <- srcF
440439 case cleanLinkString title of
441440 Just imgSrc | isImageFilename imgSrc ->
442− pure $ B.link src "" $ B.image imgSrc mempty mempty
441+ pure . B.link src "" $ B.image imgSrc mempty mempty
443442 _ ->
444443 linkToInlinesF src =<< title'
445444
src/Text/Pandoc/Readers/Org/Meta.hs+5−5
@@ -75,9 +75,9 @@ declarationLine :: PandocMonad m => OrgParser m ()
7575declarationLine = try $ do
7676 key <- map toLower <$> metaKey
7777 (key', value) <- metaValue key
78− when (key' /= "results") $
79− updateState $ \st ->
80− st { orgStateMeta = B.setMeta key' <$> value <*> orgStateMeta st }
78+ let addMetaValue st =
79+ st { orgStateMeta = B.setMeta key' <$> value <*> orgStateMeta st }
80+ when (key' /= "results") $ updateState addMetaValue
8181
8282metaKey :: Monad m => OrgParser m String
8383metaKey = map toLower <$> many1 (noneOf ": \n\r")
@@ -236,8 +236,8 @@ macroDefinition = try $ do
236236 expansionPart = try $ many (notFollowedBy placeholder *> noneOf "\n\r")
237237
238238 alternate :: [a] -> [a] -> [a]
239− alternate [] ys = ys
240− alternate xs [] = xs
239+ alternate [] ys = ys
240+ alternate xs [] = xs
241241 alternate (x:xs) (y:ys) = x : y : alternate xs ys
242242
243243 reorder :: [Int] -> [String] -> [String]
src/Text/Pandoc/Readers/Org/ParserState.hs+9−10
@@ -1,5 +1,5 @@
1−{-# LANGUAGE FlexibleInstances #-}
2−{-# LANGUAGE MultiParamTypeClasses #-}
1+{-# LANGUAGE FlexibleInstances #-}
2+{-# LANGUAGE MultiParamTypeClasses #-}
33{-
44Copyright (C) 2014-2017 Albert Krewinkel <tarleb+pandoc@moltkeplatz.de>
55
@@ -60,15 +60,14 @@ import qualified Data.Set as Set
6060
6161import Text.Pandoc.Builder (Blocks, Inlines)
6262import Text.Pandoc.Definition (Meta (..), nullMeta)
63−import Text.Pandoc.Options (ReaderOptions (..))
6463import Text.Pandoc.Logging
65−import Text.Pandoc.Parsing (HasHeaderMap (..), HasIdentifierList (..),
66− HasLogMessages (..),
67− HasLastStrPosition (..), HasQuoteContext (..),
68− HasReaderOptions (..), HasIncludeFiles (..),
69− ParserContext (..),
70− QuoteContext (..), SourcePos, Future,
71− askF, asksF, returnF, runF, trimInlinesF)
64+import Text.Pandoc.Options (ReaderOptions (..))
65+import Text.Pandoc.Parsing (Future, HasHeaderMap (..), HasIdentifierList (..),
66+ HasIncludeFiles (..), HasLastStrPosition (..),
67+ HasLogMessages (..), HasQuoteContext (..),
68+ HasReaderOptions (..), ParserContext (..),
69+ QuoteContext (..), SourcePos, askF, asksF, returnF,
70+ runF, trimInlinesF)
7271
7372-- | This is used to delay evaluation until all relevant information has been
7473-- parsed and made available in the parser state.
src/Text/Pandoc/Writers/Org.hs+14−16
@@ -34,7 +34,7 @@ Conversion of 'Pandoc' documents to Emacs Org-Mode.
3434
3535Org-Mode: <http://orgmode.org>
3636-}
37−module Text.Pandoc.Writers.Org ( writeOrg) where
37+module Text.Pandoc.Writers.Org (writeOrg) where
3838import Control.Monad.State
3939import Data.Char (isAlphaNum, toLower)
4040import Data.List (intersect, intersperse, isPrefixOf, partition, transpose)
@@ -77,9 +77,9 @@ pandocToOrg (Pandoc meta blocks) = do
7777 body <- blockListToOrg blocks
7878 notes <- gets (reverse . stNotes) >>= notesToOrg
7979 hasMath <- gets stHasMath
80− let main = render colwidth $ foldl ($+$) empty $ [body, notes]
80+ let main = render colwidth . foldl ($+$) empty $ [body, notes]
8181 let context = defField "body" main
82− $ defField "math" hasMath
82+ . defField "math" hasMath
8383 $ metadata
8484 case writerTemplate opts of
8585 Nothing -> return main
@@ -88,8 +88,7 @@ pandocToOrg (Pandoc meta blocks) = do
8888-- | Return Org representation of notes.
8989notesToOrg :: PandocMonad m => [[Block]] -> Org m Doc
9090notesToOrg notes =
91− mapM (\(num, note) -> noteToOrg num note) (zip [1..] notes) >>=
92− return . vsep
91+ vsep <$> zipWithM noteToOrg [1..] notes
9392
9493-- | Return Org representation of a note.
9594noteToOrg :: PandocMonad m => Int -> [Block] -> Org m Doc
@@ -221,16 +220,16 @@ blockToOrg (Table caption' _ _ headers rows) = do
221220 -- FIXME: Org doesn't allow blocks with height more than 1.
222221 let hpipeBlocks blocks = hcat [beg, middle, end]
223222 where h = maximum (1 : map height blocks)
224− sep' = lblock 3 $ vcat (map text $ replicate h " | ")
225− beg = lblock 2 $ vcat (map text $ replicate h "| ")
226− end = lblock 2 $ vcat (map text $ replicate h " |")
223+ sep' = lblock 3 $ vcat (replicate h (text " | "))
224+ beg = lblock 2 $ vcat (replicate h (text "| "))
225+ end = lblock 2 $ vcat (replicate h (text " |"))
227226 middle = hcat $ intersperse sep' blocks
228227 let makeRow = hpipeBlocks . zipWith lblock widthsInChars
229228 let head' = makeRow headers'
230229 rows' <- mapM (\row -> do cols <- mapM blockListToOrg row
231230 return $ makeRow cols) rows
232231 let border ch = char '|' <> char ch <>
233− (hcat $ intersperse (char ch <> char '+' <> char ch) $
232+ (hcat . intersperse (char ch <> char '+' <> char ch) $
234233 map (\l -> text $ replicate l ch) widthsInChars) <>
235234 char ch <> char '|'
236235 let body = vcat rows'
@@ -251,8 +250,7 @@ blockToOrg (OrderedList (start, _, delim) items) = do
251250 let maxMarkerLength = maximum $ map length markers
252251 let markers' = map (\m -> let s = maxMarkerLength - length m
253252 in m ++ replicate s ' ') markers
254− contents <- mapM (\(item, num) -> orderedListItemToOrg item num) $
255− zip markers' items
253+ contents <- zipWithM orderedListItemToOrg markers' items
256254 -- ensure that sublists have preceding blank line
257255 return $ blankline $$ vcat contents $$ blankline
258256blockToOrg (DefinitionList items) = do
@@ -279,8 +277,8 @@ definitionListItemToOrg :: PandocMonad m
279277 => ([Inline], [[Block]]) -> Org m Doc
280278definitionListItemToOrg (label, defs) = do
281279 label' <- inlineListToOrg label
282− contents <- liftM vcat $ mapM blockListToOrg defs
283− return $ hang 2 "- " $ label' <> " :: " <> (contents <> cr)
280+ contents <- vcat <$> mapM blockListToOrg defs
281+ return . hang 2 "- " $ label' <> " :: " <> (contents <> cr)
284282
285283-- | Convert list of key/value pairs to Org :PROPERTIES: drawer.
286284propertiesDrawer :: Attr -> Doc
@@ -312,13 +310,13 @@ attrHtml (ident, classes, kvs) =
312310blockListToOrg :: PandocMonad m
313311 => [Block] -- ^ List of block elements
314312 -> Org m Doc
315−blockListToOrg blocks = mapM blockToOrg blocks >>= return . vcat
313+blockListToOrg blocks = vcat <$> mapM blockToOrg blocks
316314
317315-- | Convert list of Pandoc inline elements to Org.
318316inlineListToOrg :: PandocMonad m
319317 => [Inline]
320318 -> Org m Doc
321−inlineListToOrg lst = mapM inlineToOrg lst >>= return . hcat
319+inlineListToOrg lst = hcat <$> mapM inlineToOrg lst
322320
323321-- | Convert Pandoc inline element to Org.
324322inlineToOrg :: PandocMonad m => Inline -> Org m Doc
@@ -350,7 +348,7 @@ inlineToOrg (Quoted DoubleQuote lst) = do
350348 return $ "\"" <> contents <> "\""
351349inlineToOrg (Cite _ lst) = inlineListToOrg lst
352350inlineToOrg (Code _ str) = return $ "=" <> text str <> "="
353−inlineToOrg (Str str) = return $ text $ escapeString str
351+inlineToOrg (Str str) = return . text $ escapeString str
354352inlineToOrg (Math t str) = do
355353 modify $ \st -> st{ stHasMath = True }
356354 return $ if t == InlineMath