From fd8587fbdf7c30c0a199e26949aa185cf1ba242d Mon Sep 17 00:00:00 2001 From: tim Date: Wed, 4 Feb 2009 21:27:20 +0000 Subject: [PATCH] Handle !includes relative to the including file --- Ledger/Parse.hs | 14 +++++++++----- hledger.cabal | 5 +++-- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/Ledger/Parse.hs b/Ledger/Parse.hs index 6ea1a6d87..71729e977 100644 --- a/Ledger/Parse.hs +++ b/Ledger/Parse.hs @@ -26,6 +26,7 @@ import Ledger.Entry import Ledger.Commodity import Ledger.TimeLog import Ledger.RawLedger +import System.FilePath(takeDirectory,combine) -- utils @@ -115,7 +116,7 @@ ledgerInclude = do many1 spacenonewline outerState <- getState outerPos <- getPosition let inIncluded = show outerPos ++ " in included file " ++ show filename ++ ":\n" - return $ do contents <- expandPath filename >>= readFileE outerPos + return $ do contents <- expandPath outerPos filename >>= readFileE outerPos case runParser ledgerFile outerState filename contents of Right l -> l `catchError` (\err -> throwError $ inIncluded ++ err) Left perr -> throwError $ inIncluded ++ show perr @@ -124,10 +125,13 @@ ledgerInclude = do many1 spacenonewline currentPos = show outerPos whileReading = " reading " ++ show filename ++ ":\n" -expandPath :: (MonadIO m) => FilePath -> m FilePath -expandPath inname | "~/" `isPrefixOf` inname = do homedir <- liftIO getHomeDirectory - return $ homedir ++ drop 1 inname - | otherwise = return inname +expandPath :: (MonadIO m) => SourcePos -> FilePath -> m FilePath +expandPath pos fp = liftM mkRelative (expandHome fp) + where + mkRelative = combine (takeDirectory (sourceName pos)) + expandHome inname | "~/" `isPrefixOf` inname = do homedir <- liftIO getHomeDirectory + return $ homedir ++ drop 1 inname + | otherwise = return inname ledgerAccountBegin :: GenParser Char LedgerFileCtx (ErrorT String IO (RawLedger -> RawLedger)) ledgerAccountBegin = do many1 spacenonewline diff --git a/hledger.cabal b/hledger.cabal index 70365c757..41838fd70 100644 --- a/hledger.cabal +++ b/hledger.cabal @@ -41,7 +41,8 @@ Executable hledger time, HUnit, mtl, - bytestring + bytestring, + filepath Other-Modules: BalanceCommand Options PrintCommand @@ -81,7 +82,7 @@ Executable hledger Library Build-Depends: base, containers, haskell98, directory, parsec, regex-compat, - old-locale, time, HUnit + old-locale, time, HUnit, filepath Exposed-modules:Ledger Ledger.Account Ledger.AccountName