lib: clarify file format detectors

This commit is contained in:
Simon Michael 2016-11-16 13:25:33 -08:00
parent 6a36efb7ca
commit 3ddc9d7432
5 changed files with 33 additions and 22 deletions

View File

@ -69,11 +69,13 @@ reader = Reader format detect parse
format :: String
format = "csv"
-- | Does the given file path and data look like it might be CSV ?
-- | Does the given file path and data look like something this reader can handle ?
detect :: FilePath -> Text -> Bool
detect f t
| f /= "-" = takeExtension f == '.':format -- from a file: yes if the extension is .csv
| otherwise = T.length (T.filter (==',') t) >= 2 -- from stdin: yes if there are two or more commas
detect f excerpt
-- file name known: try this reader if it has any of these extensions
| f /= "-" = takeExtension f `elem` ['.':format]
-- file name unknown: try this reader if excerpt contains two or more commas
| otherwise = T.length (T.filter (==',') excerpt) >= 2
-- | Parse and post-process a "Journal" from CSV data, or give an error.
-- XXX currently ignores the string and reads from the file path

View File

@ -111,12 +111,16 @@ reader = Reader format detect parse
format :: String
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 something this reader can handle ?
detect :: FilePath -> Text -> Bool
detect f _t
| f /= "-" = takeExtension f `elem` ['.':format, ".j", ".hledger"] -- from a known file name: yes if the extension is .hledger or .journal or .j
| otherwise = True -- from stdin: yes, always attempt to parse stdin as hledger journal data
-- otherwise = regexMatches "(^|\n)[0-9]+.*\n[ \t]+" $ T.unpack t -- from stdin: yes if we can see something that looks like a journal entry (digits in column 0 with the next line indented)
detect f _
-- file name known: try this reader if it has any of these extensions
| f /= "-" = takeExtension f `elem` ['.':format, ".j", ".hledger", ".ledger", ".l"]
-- file name unknown: always try this reader
| otherwise = True
-- file name unknown: try this reader if we can see something like a journal entry
-- (digits in column 0 with the next line indented)
-- otherwise = regexMatches "(^|\n)[0-9]+.*\n[ \t]+" $ T.unpack excerpt
-- | Parse and post-process a "Journal" from hledger's journal file
-- format, or give an error.

View File

@ -56,12 +56,13 @@ reader = Reader format detect parse
format :: String
format = "ledger"
-- | Does the given file path and data look like it might be ledger's journal format ?
-- | Does the given file path and data look like something this reader can handle ?
detect :: FilePath -> Text -> Bool
detect f _t
| f /= "-" = takeExtension f `elem` ['.':format, ".l"] -- from a known file name: yes if the extension is .ledger or .l
| otherwise = False -- from stdin: yes, always attempt to parse stdin as a ledger journal
-- otherwise = regexMatches "(^|\n)[0-9]+.*\n[ \t]+" $ T.unpack t -- from stdin: yes if we can see something that looks like a journal entry (digits in column 0 with the next line indented)
detect f _
-- file name known: try this reader if it has any of these extensions
| f /= "-" = takeExtension f `elem` ['.':format, ".l"]
-- file name unknown: don't try this reader
| otherwise = False
-- | Parse and post-process a "Journal" from ledger's journal format, or give an error.
parse :: Maybe FilePath -> Bool -> FilePath -> Text -> ExceptT String IO Journal

View File

@ -75,11 +75,13 @@ reader = Reader format detect parse
format :: String
format = "timeclock"
-- | Does the given file path and data look like it might be timeclock.el's timeclock format ?
-- | Does the given file path and data look like something this reader can handle ?
detect :: FilePath -> Text -> Bool
detect f t
| f /= "-" = takeExtension f == '.':format -- from a known file name: yes if the extension is this format's name
| otherwise = regexMatches "(^|\n)[io] " $ T.unpack t -- from stdin: yes if any line starts with "i " or "o "
detect f excerpt
-- file name known: try this reader if it has any of these extensions
| f /= "-" = takeExtension f `elem` ['.':format]
-- file name unknown: try this reader if a line starts with "i " or "o " in excerpt
| otherwise = regexMatches "(^|\n)[io] " $ T.unpack excerpt
-- | Parse and post-process a "Journal" from timeclock.el's timeclock
-- format, saving the provided file path and the current time, or give an

View File

@ -61,11 +61,13 @@ reader = Reader format detect parse
format :: String
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 something this reader can handle ?
detect :: FilePath -> Text -> Bool
detect f t
| f /= "-" = takeExtension f == '.':format -- from a file: yes if the extension matches the format name
| otherwise = regexMatches "(^|\n)[0-9]" $ T.unpack t -- from stdin: yes if we can see a possible timedot day entry (digits in column 0)
detect f excerpt
-- file name known: try this reader if it has any of these extensions
| f /= "-" = takeExtension f `elem` ['.':format]
-- file name unknown: try this reader if a line starts with a number in excerpt
| otherwise = regexMatches "(^|\n)[0-9]" $ T.unpack excerpt
-- | Parse and post-process a "Journal" from the timedot format, or give an error.
parse :: Maybe FilePath -> Bool -> FilePath -> Text -> ExceptT String IO Journal