lib, doc: extended "skip" in "if" body to "skip N"
This commit is contained in:
parent
f877a7789f
commit
f1ab107400
@ -218,10 +218,19 @@ printCSV records = unlined (printRecord `map` records)
|
|||||||
-- | Return the cleaned up and validated CSV data (can be empty), or an error.
|
-- | Return the cleaned up and validated CSV data (can be empty), or an error.
|
||||||
validateCsv :: CsvRules -> Int -> Either String CSV -> Either String [CsvRecord]
|
validateCsv :: CsvRules -> Int -> Either String CSV -> Either String [CsvRecord]
|
||||||
validateCsv _ _ (Left err) = Left err
|
validateCsv _ _ (Left err) = Left err
|
||||||
validateCsv rules numhdrlines (Right rs) = validate $ filter (not.shouldSkip) $ drop numhdrlines $ filternulls rs
|
validateCsv rules numhdrlines (Right rs) = validate $ applyConditionalSkips $ drop numhdrlines $ filternulls rs
|
||||||
where
|
where
|
||||||
filternulls = filter (/=[""])
|
filternulls = filter (/=[""])
|
||||||
shouldSkip r = isJust $ getEffectiveAssignment rules r "skip"
|
skipCount r =
|
||||||
|
case getEffectiveAssignment rules r "skip" of
|
||||||
|
Nothing -> Nothing
|
||||||
|
Just "" -> Just 1
|
||||||
|
Just x -> Just (read x)
|
||||||
|
applyConditionalSkips [] = []
|
||||||
|
applyConditionalSkips (r:rest) =
|
||||||
|
case skipCount r of
|
||||||
|
Nothing -> r:(applyConditionalSkips rest)
|
||||||
|
Just cnt -> applyConditionalSkips (drop (cnt-1) rest)
|
||||||
validate [] = Right []
|
validate [] = Right []
|
||||||
validate rs@(_first:_)
|
validate rs@(_first:_)
|
||||||
| isJust lessthan2 = let r = fromJust lessthan2 in
|
| isJust lessthan2 = let r = fromJust lessthan2 in
|
||||||
|
|||||||
@ -92,7 +92,7 @@ You'll need this whenever your CSV data contains header lines. Eg:
|
|||||||
<!-- XXX -->
|
<!-- XXX -->
|
||||||
<!-- hledger tries to skip initial CSV header lines automatically. -->
|
<!-- hledger tries to skip initial CSV header lines automatically. -->
|
||||||
<!-- If it guesses wrong, use this directive to skip exactly N lines. -->
|
<!-- If it guesses wrong, use this directive to skip exactly N lines. -->
|
||||||
This can also be used in a conditional block (without numeric argument) to ignore certain CSV records.
|
This can also be used in a conditional block to ignore certain CSV records.
|
||||||
```rules
|
```rules
|
||||||
# ignore the first CSV line
|
# ignore the first CSV line
|
||||||
skip 1
|
skip 1
|
||||||
@ -178,12 +178,12 @@ Note, interpolation strips any outer whitespace, so a CSV value like
|
|||||||
## conditional block
|
## conditional block
|
||||||
|
|
||||||
`if` *`PATTERN`*\
|
`if` *`PATTERN`*\
|
||||||
*`FIELDASSIGNMENTS or skip`*...
|
*`FIELDASSIGNMENTS or skip N`*...
|
||||||
|
|
||||||
`if`\
|
`if`\
|
||||||
*`PATTERN`*\
|
*`PATTERN`*\
|
||||||
*`PATTERN`*...\
|
*`PATTERN`*...\
|
||||||
*`FIELDASSIGNMENTS or skip`*...
|
*`FIELDASSIGNMENTS or skip N`*...
|
||||||
|
|
||||||
This applies one or more field assignments, only to those CSV records matched by one of the PATTERNs.
|
This applies one or more field assignments, only to those CSV records matched by one of the PATTERNs.
|
||||||
The patterns are case-insensitive regular expressions which match anywhere
|
The patterns are case-insensitive regular expressions which match anywhere
|
||||||
@ -192,7 +192,7 @@ specific field). When there are multiple patterns they can be written
|
|||||||
on separate lines, unindented.
|
on separate lines, unindented.
|
||||||
The field assignments are on separate lines indented by at least one space.
|
The field assignments are on separate lines indented by at least one space.
|
||||||
|
|
||||||
Instead of field assignments you can specify `skip` to skip the matching record.
|
Instead of field assignments you can specify `skip N` to skip the next N records (including the one that matchied).
|
||||||
|
|
||||||
Examples:
|
Examples:
|
||||||
```rules
|
```rules
|
||||||
|
|||||||
@ -354,7 +354,9 @@ $ ./hledger-csv | hledger balance -f - --no-total
|
|||||||
<
|
<
|
||||||
HEADER
|
HEADER
|
||||||
10/2009/09,Flubber Co,50
|
10/2009/09,Flubber Co,50
|
||||||
MIDDLE
|
MIDDLE SKIP THIS LINE
|
||||||
|
AND THIS
|
||||||
|
AND THIS ONE
|
||||||
10/2009/09,Flubber Co,50
|
10/2009/09,Flubber Co,50
|
||||||
*** END OF FILE ***
|
*** END OF FILE ***
|
||||||
RULES
|
RULES
|
||||||
@ -364,10 +366,12 @@ currency $
|
|||||||
account1 assets:myacct
|
account1 assets:myacct
|
||||||
|
|
||||||
if HEADER
|
if HEADER
|
||||||
MIDDLE
|
|
||||||
END OF FILE
|
END OF FILE
|
||||||
skip
|
skip
|
||||||
|
|
||||||
|
if MIDDLE
|
||||||
|
skip 3
|
||||||
|
|
||||||
$ ./hledger-csv
|
$ ./hledger-csv
|
||||||
2009/09/10 Flubber Co
|
2009/09/10 Flubber Co
|
||||||
assets:myacct $50
|
assets:myacct $50
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user