lib: use glob matching if filename in 'include' directive is a glob pattern

This commit is contained in:
Joseph Weston 2018-07-23 11:39:01 +02:00 committed by Simon Michael
parent 96f7a03487
commit 0ade323d2a
2 changed files with 10 additions and 5 deletions

View File

@ -94,6 +94,7 @@ import Text.Megaparsec.Char
import Text.Megaparsec.Custom
import Text.Printf
import System.FilePath
import System.FilePath.Glob hiding (match)
import Hledger.Data
import Hledger.Read.Common
@ -189,15 +190,18 @@ includedirectivep :: MonadIO m => JournalParser m ()
includedirectivep = do
string "include"
lift (skipSome spacenonewline)
filename <- T.unpack <$> takeWhileP Nothing (/= '\n') -- don't consume newline yet
fileglob <- T.unpack <$> takeWhileP Nothing (/= '\n') -- don't consume newline yet
parentpos <- getPosition
curdir <- lift $ expandPath (takeDirectory $ sourceName parentpos) ""
`orRethrowIOError` (show parentpos ++ " locating " ++ filename)
-- </> correctly handles case when 'filename' is absolute
let filepaths = [curdir </> filename]
-- read child inputs
`orRethrowIOError` (show parentpos ++ " locating " ++ fileglob)
filepaths <- if isLiteral (compile fileglob)
-- </> and globDir1 correctly handle case when 'fileglob' is absolute
then pure [curdir </> fileglob]
else liftIO $ globDir1 (compile fileglob) curdir
forM_ filepaths $ parseChild parentpos
void newline

View File

@ -72,6 +72,7 @@ dependencies:
- utf8-string >=0.3.5
- HUnit
- extra
- Glob >= 0.9
# for ledger-parse:
#- parsers >=0.5
#- system-filepath