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 ? -- | Does the given file path and data look like it might be hledger's journal format ?
detect :: FilePath -> String -> Bool detect :: FilePath -> String -> Bool
detect f s detect f s
| f /= "-" = takeExtension f `elem` ['.':format, ".j"] -- from a file: yes if the extension is .journal or .j | f /= "-" = takeExtension f `elem` ['.':format, ".j"] -- from a known file name: yes if the extension is this format's name 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 "(^|\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)
| otherwise = regexMatches "^[0-9]+.*\n[ \t]+" s
-- | Parse and post-process a "Journal" from hledger's journal file -- | Parse and post-process a "Journal" from hledger's journal file
-- format, or give an error. -- format, or give an error.
@ -275,6 +274,7 @@ includedirectivep = do
txt <- readFileOrError outerPos filepath txt <- readFileOrError outerPos filepath
let inIncluded = show outerPos ++ " in included file " ++ show filename ++ ":\n" let inIncluded = show outerPos ++ " in included file " ++ show filename ++ ":\n"
r <- runParserT journalp outerState filepath txt r <- runParserT journalp outerState filepath txt
case r of case r of
Right (ju, ctx) -> do Right (ju, ctx) -> do
u <- combineJournalUpdates [ return $ journalAddFile (filepath,txt) 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 ? -- | Does the given file path and data look like it might contain this format ?
detect :: FilePath -> String -> Bool detect :: FilePath -> String -> Bool
detect f _s detect f s
| f /= "-" = takeExtension f == '.':format -- from a file: yes if the extension matches the format name | f /= "-" = takeExtension f == '.':format -- from a file: yes if the extension matches the format name
| otherwise = False -- from stdin: yes if... | 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 and post-process a "Journal" from the timedot format, or give an error.
parse :: Maybe FilePath -> Bool -> FilePath -> String -> ExceptT String IO Journal parse :: Maybe FilePath -> Bool -> FilePath -> String -> ExceptT String IO Journal

View File

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