git.iko.soy/typeable/pandoc
on master help sign in
Class: Add stResourcePath to CommonState, getResourcePath, setResourcePath.

To be used in implementing `\graphicspath` in LaTeX, and possibly in things like PDF production via context. Use resource path in fetchItem. Issue an info message if we get a resource from somewhere other than ".". Added UsingResourceFrom to log message.

commit1c84855aab5ddda3872e5b31d9a1bcbc14de7dca authorJohn MacFarlane <jgm@berkeley.edu> dateFri 24 Feb 2017, 13:48 (10 years ago) parent6936747
2 files changed+40−8
src/Text/Pandoc/Class.hs+32−8 src/Text/Pandoc/Logging.hs+8
src/Text/Pandoc/Class.hs+32−8
@@ -1,5 +1,6 @@
11{-# LANGUAGE DeriveFunctor, DeriveDataTypeable, TypeSynonymInstances,
2−FlexibleInstances, GeneralizedNewtypeDeriving, FlexibleContexts #-}
2+FlexibleInstances, GeneralizedNewtypeDeriving, FlexibleContexts,
3+StandaloneDeriving #-}
34
45{-
56Copyright (C) 2016 Jesse Rosenthal <jrosenthal@jhu.edu>
@@ -50,6 +51,8 @@ module Text.Pandoc.Class ( PandocMonad(..)
5051 , fetchItem
5152 , getInputFiles
5253 , getOutputFile
54+ , setResourcePath
55+ , getResourcePath
5356 , PandocIO(..)
5457 , PandocPure(..)
5558 , FileTree(..)
@@ -88,7 +91,7 @@ import qualified Data.ByteString as B
8891import qualified Data.ByteString.Lazy as BL
8992import qualified System.Environment as IO (lookupEnv)
9093import System.FilePath.Glob (match, compile)
91−import System.FilePath ((</>), takeExtension, dropExtension)
94+import System.FilePath ((</>), takeExtension, dropExtension, isRelative)
9295import qualified System.FilePath.Glob as IO (glob)
9396import qualified System.Directory as IO (getModificationTime)
9497import Control.Monad as M (fail)
@@ -165,6 +168,12 @@ getInputFiles = getsCommonState stInputFiles
165168getOutputFile :: PandocMonad m => m (Maybe FilePath)
166169getOutputFile = getsCommonState stOutputFile
167170
171+setResourcePath :: PandocMonad m => [FilePath] -> m ()
172+setResourcePath ps = modifyCommonState $ \st -> st{stResourcePath = ps}
173+
174+getResourcePath :: PandocMonad m => m [FilePath]
175+getResourcePath = getsCommonState stResourcePath
176+
168177getPOSIXTime :: (PandocMonad m) => m POSIXTime
169178getPOSIXTime = utcTimeToPOSIXSeconds <$> getCurrentTime
170179
@@ -183,11 +192,12 @@ readFileFromDirs (d:ds) f = catchError
183192
184193--
185194
186−data CommonState = CommonState { stLog :: [LogMessage]
187− , stMediaBag :: MediaBag
188− , stInputFiles :: Maybe [FilePath]
189− , stOutputFile :: Maybe FilePath
190− , stVerbosity :: Verbosity
195+data CommonState = CommonState { stLog :: [LogMessage]
196+ , stMediaBag :: MediaBag
197+ , stInputFiles :: Maybe [FilePath]
198+ , stOutputFile :: Maybe FilePath
199+ , stResourcePath :: [FilePath]
200+ , stVerbosity :: Verbosity
191201 }
192202
193203instance Default CommonState where
@@ -195,6 +205,7 @@ instance Default CommonState where
195205 , stMediaBag = mempty
196206 , stInputFiles = Nothing
197207 , stOutputFile = Nothing
208+ , stResourcePath = ["."]
198209 , stVerbosity = WARNING
199210 }
200211
@@ -289,7 +300,10 @@ downloadOrRead sourceURL s = do
289300 readLocalFile $ dropWhile (=='/') (uriPath u')
290301 _ -> readLocalFile fp -- get from local file system
291302 where readLocalFile f = do
292− cont <- readFileStrict f
303+ resourcePath <- getResourcePath
304+ cont <- if isRelative f
305+ then withPaths resourcePath readFileStrict f
306+ else readFileStrict f
293307 return (cont, mime)
294308 httpcolon = URI{ uriScheme = "http:",
295309 uriAuthority = Nothing,
@@ -306,6 +320,16 @@ downloadOrRead sourceURL s = do
306320 convertSlash '\\' = '/'
307321 convertSlash x = x
308322
323+withPaths :: PandocMonad m => [FilePath] -> (FilePath -> m a) -> FilePath -> m a
324+withPaths [] _ fp = throwError $ PandocIOError fp
325+ (userError "file not found in resource path")
326+withPaths (p:ps) action fp =
327+ catchError (do res <- action (p </> fp)
328+ when (p /= ".") $
329+ report $ UsingResourceFrom fp p
330+ return res)
331+ (\_ -> withPaths ps action fp)
332+
309333data PureState = PureState { stStdGen :: StdGen
310334 , stWord8Store :: [Word8] -- should be
311335 -- inifinite,
src/Text/Pandoc/Logging.hs+8
@@ -74,6 +74,7 @@ data LogMessage =
7474 | CouldNotConvertTeXMath String String
7575 | CouldNotParseCSS String
7676 | Fetching String
77+ | UsingResourceFrom FilePath FilePath
7778 deriving (Show, Eq, Data, Ord, Typeable, Generic)
7879
7980instance ToJSON LogMessage where
@@ -163,6 +164,10 @@ instance ToJSON LogMessage where
163164 Fetching fp ->
164165 ["type" .= String "CouldNotParseCSS",
165166 "path" .= Text.pack fp]
167+ UsingResourceFrom resource dir ->
168+ ["type" .= String "UsingResourceFrom",
169+ "resource" .= Text.pack resource,
170+ "path" .= Text.pack dir]
166171
167172showPos :: SourcePos -> String
168173showPos pos = sn ++ "line " ++
@@ -220,6 +225,8 @@ showLogMessage msg =
220225 "Could not parse CSS" ++ if null m then "" else (':':'\n':m)
221226 Fetching fp ->
222227 "Fetching " ++ fp ++ "..."
228+ UsingResourceFrom fp dir ->
229+ "Using " ++ fp ++ " from " ++ dir
223230
224231messageVerbosity:: LogMessage -> Verbosity
225232messageVerbosity msg =
@@ -242,3 +249,4 @@ messageVerbosity msg =
242249 CouldNotConvertTeXMath{} -> WARNING
243250 CouldNotParseCSS{} -> WARNING
244251 Fetching{} -> INFO
252+ UsingResourceFrom{} -> INFO