lib: slightly better journal/time format detection

The Journal, Timelog and Timedot readers' detectors now check
each line in the sample data, not just the first one. I think
the sample data is only about 30 chars right now, but even so
this fixed a format detection issue I was seeing.
This commit is contained in:
Simon Michael 2016-02-19 23:02:10 -08:00
parent 70863ae40b
commit a9afd7bcbe
3 changed files with 9 additions and 9 deletions

View File

@ -88,9 +88,8 @@ format = "journal"
-- | Does the given file path and data look like it might be hledger's journal format ?
detect :: FilePath -> String -> Bool
detect f s
| f /= "-" = takeExtension f `elem` ['.':format, ".j"] -- from a file: yes if the extension is .journal or .j
-- from stdin: yes if we can see something that looks like a journal entry (digits in column 0 with the next line indented)
| otherwise = regexMatches "^[0-9]+.*\n[ \t]+" s
| f /= "-" = takeExtension f `elem` ['.':format, ".j"] -- from a known file name: yes if the extension is this format's name or .j
| otherwise = regexMatches "(^|\n)[0-9]+.*\n[ \t]+" s -- from stdin: yes if we can see something that looks like a journal entry (digits in column 0 with the next line indented)
-- | Parse and post-process a "Journal" from hledger's journal file
-- format, or give an error.
@ -275,6 +274,7 @@ includedirectivep = do
txt <- readFileOrError outerPos filepath
let inIncluded = show outerPos ++ " in included file " ++ show filename ++ ":\n"
r <- runParserT journalp outerState filepath txt
case r of
Right (ju, ctx) -> do
u <- combineJournalUpdates [ return $ journalAddFile (filepath,txt)

View File

@ -60,9 +60,9 @@ format = "timedot"
-- | Does the given file path and data look like it might contain this format ?
detect :: FilePath -> String -> Bool
detect f _s
| f /= "-" = takeExtension f == '.':format -- from a file: yes if the extension matches the format name
| otherwise = False -- from stdin: yes if...
detect f s
| f /= "-" = takeExtension f == '.':format -- from a file: yes if the extension matches the format name
| otherwise = regexMatches "(^|\n)[0-9]" s -- from stdin: yes if we can see a possible timedot day entry (digits in column 0)
-- | Parse and post-process a "Journal" from the timedot format, or give an error.
parse :: Maybe FilePath -> Bool -> FilePath -> String -> ExceptT String IO Journal

View File

@ -51,7 +51,7 @@ import Prelude ()
import Prelude.Compat
import Control.Monad (liftM)
import Control.Monad.Except (ExceptT)
import Data.List (isPrefixOf, foldl')
import Data.List (foldl')
import Data.Maybe (fromMaybe)
import Test.HUnit
import Text.Parsec hiding (parse)
@ -75,8 +75,8 @@ format = "timelog"
-- | Does the given file path and data look like it might be timeclock.el's timelog format ?
detect :: FilePath -> String -> Bool
detect f s
| f /= "-" = takeExtension f == '.':format -- from a file: yes if the extension is .timelog
| otherwise = "i " `isPrefixOf` s || "o " `isPrefixOf` s -- from stdin: yes if it starts with "i " or "o "
| f /= "-" = takeExtension f == '.':format -- from a known file name: yes if the extension is this format's name
| otherwise = regexMatches "(^|\n)[io] " s -- from stdin: yes if any line starts with "i " or "o "
-- | Parse and post-process a "Journal" from timeclock.el's timelog
-- format, saving the provided file path and the current time, or give an