diff --git a/extra/hledger-rewrite.hs b/extra/hledger-rewrite.hs new file mode 100755 index 000000000..479bb31c7 --- /dev/null +++ b/extra/hledger-rewrite.hs @@ -0,0 +1,30 @@ +#!/usr/bin/env runhaskell +{-| +hledger-rewrite PATTERNS --add-posting "ACCT AMTEXPR" ... + +Skeleton for a minimal generic rewriter of journal entries. +Reads the default journal and prints the entries, like print, +but adds the specified postings to any entries matching PATTERNS. + +|-} + +import Hledger.Data.Types (Journal(..)) +import Hledger +import Hledger.Cli + +main = do + putStrLn "(-f option not supported)" + opts <- getCliOpts (defCommandMode ["hledger-rewrite"]) + withJournalDo opts $ + \opts j@Journal{jtxns=ts} -> print' opts j{jtxns=map rewrite ts} + where + rewrite t = if matched t then t{tpostings=tpostings t ++ newps t} + else t + matched t = Acct "^income" `matchesTransaction` t + newps t = [generatePosting t "(Reserve)" (`divideMixedAmount` 10)] + generatePosting t acct amtfn = nullposting{paccount = accountNameWithoutPostingType acct + ,ptype = accountNamePostingType acct + ,pamount = amtfn amt + } + where + amt = pamount $ head $ filter (Acct "^income" `matchesPosting`) $ tpostings t diff --git a/hledger-lib/Hledger/Read.hs b/hledger-lib/Hledger/Read.hs index 7c87dbfdb..62bbfe652 100644 --- a/hledger-lib/Hledger/Read.hs +++ b/hledger-lib/Hledger/Read.hs @@ -18,6 +18,7 @@ module Hledger.Read ( requireJournalFileExists, ensureJournalFileExists, -- * Parsers used elsewhere + postingp, accountname, amountp, amountp', diff --git a/hledger-lib/Hledger/Read/JournalReader.hs b/hledger-lib/Hledger/Read/JournalReader.hs index d60ea9855..3412cb541 100644 --- a/hledger-lib/Hledger/Read/JournalReader.hs +++ b/hledger-lib/Hledger/Read/JournalReader.hs @@ -29,6 +29,7 @@ module Hledger.Read.JournalReader ( datetime, code, accountname, + postingp, amountp, amountp', mamountp',