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 :: String
format = "csv" 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 :: FilePath -> Text -> Bool
detect f t detect f excerpt
| f /= "-" = takeExtension f == '.':format -- from a file: yes if the extension is .csv -- file name known: try this reader if it has any of these extensions
| otherwise = T.length (T.filter (==',') t) >= 2 -- from stdin: yes if there are two or more commas | 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. -- | Parse and post-process a "Journal" from CSV data, or give an error.
-- XXX currently ignores the string and reads from the file path -- 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 :: String
format = "journal" 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 :: FilePath -> Text -> Bool
detect f _t detect f _
| f /= "-" = takeExtension f `elem` ['.':format, ".j", ".hledger"] -- from a known file name: yes if the extension is .hledger or .journal or .j -- file name known: try this reader if it has any of these extensions
| otherwise = True -- from stdin: yes, always attempt to parse stdin as hledger journal data | f /= "-" = takeExtension f `elem` ['.':format, ".j", ".hledger", ".ledger", ".l"]
-- 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) -- 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 -- | Parse and post-process a "Journal" from hledger's journal file
-- format, or give an error. -- format, or give an error.

View File

@ -56,12 +56,13 @@ reader = Reader format detect parse
format :: String format :: String
format = "ledger" 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 :: FilePath -> Text -> Bool
detect f _t detect f _
| f /= "-" = takeExtension f `elem` ['.':format, ".l"] -- from a known file name: yes if the extension is .ledger or .l -- file name known: try this reader if it has any of these extensions
| otherwise = False -- from stdin: yes, always attempt to parse stdin as a ledger journal | f /= "-" = takeExtension f `elem` ['.':format, ".l"]
-- 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) -- 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 and post-process a "Journal" from ledger's journal format, or give an error.
parse :: Maybe FilePath -> Bool -> FilePath -> Text -> ExceptT String IO Journal parse :: Maybe FilePath -> Bool -> FilePath -> Text -> ExceptT String IO Journal

View File

@ -75,11 +75,13 @@ reader = Reader format detect parse
format :: String format :: String
format = "timeclock" 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 :: FilePath -> Text -> Bool
detect f t detect f excerpt
| f /= "-" = takeExtension f == '.':format -- from a known file name: yes if the extension is this format's name -- file name known: try this reader if it has any of these extensions
| otherwise = regexMatches "(^|\n)[io] " $ T.unpack t -- from stdin: yes if any line starts with "i " or "o " | 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 -- | Parse and post-process a "Journal" from timeclock.el's timeclock
-- 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

View File

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