| 1 |
{-# LANGUAGE LambdaCase #-} |
| 2 |
{-# LANGUAGE NamedFieldPuns #-} |
| 3 |
{-# LANGUAGE OverloadedStrings #-} |
| 4 |
{-# LANGUAGE QuasiQuotes #-} |
| 5 |
{-# LANGUAGE TemplateHaskell #-} |
| 6 |
|
| 7 |
module Hledger.Web.Widget.Common |
| 8 |
( accountQuery |
| 9 |
, accountOnlyQuery |
| 10 |
, balanceReportAsHtml |
| 11 |
, helplink |
| 12 |
, mixedAmountAsHtml |
| 13 |
, fromFormSuccess |
| 14 |
, writeJournalTextIfValidAndChanged |
| 15 |
, journalFile404 |
| 16 |
, transactionFragment |
| 17 |
, removeDates |
| 18 |
, removeInacct |
| 19 |
, replaceInacct |
| 20 |
) where |
| 21 |
|
| 22 |
import Control.Monad.Except (ExceptT, mapExceptT) |
| 23 |
import Data.Foldable (find, for_) |
| 24 |
import Data.List (elemIndex) |
| 25 |
import Data.Text (Text) |
| 26 |
import Data.Text qualified as T |
| 27 |
import System.FilePath (takeFileName) |
| 28 |
import Text.Blaze ((!), textValue) |
| 29 |
import Text.Blaze.Html5 qualified as H |
| 30 |
import Text.Blaze.Html5.Attributes qualified as A |
| 31 |
import Text.Blaze.Internal (preEscapedString) |
| 32 |
import Text.Hamlet (hamletFile) |
| 33 |
import Text.Printf (printf) |
| 34 |
import Yesod |
| 35 |
|
| 36 |
import Hledger |
| 37 |
import Hledger.Cli.Utils (writeFileWithBackupIfChanged) |
| 38 |
import Hledger.Web.Settings (manualurl) |
| 39 |
import Hledger.Query qualified as Query |
| 40 |
|
| 41 |
|
| 42 |
journalFile404 :: FilePath -> Journal -> HandlerFor m (FilePath, Text) |
| 43 |
journalFile404 f j = |
| 44 |
case find ((== f) . fst) (jfiles j) of |
| 45 |
Just (_, txt) -> pure (takeFileName f, txt) |
| 46 |
Nothing -> notFound |
| 47 |
|
| 48 |
fromFormSuccess :: Applicative m => m a -> FormResult a -> m a |
| 49 |
fromFormSuccess h FormMissing = h |
| 50 |
fromFormSuccess h (FormFailure _) = h |
| 51 |
fromFormSuccess _ (FormSuccess a) = pure a |
| 52 |
|
| 53 |
-- | A helper for postEditR/postUploadR: check that the given text |
| 54 |
-- parses as a Journal, and if so, write it to the given file, if the |
| 55 |
-- text has changed. Or, return any error message encountered. |
| 56 |
-- |
| 57 |
-- As a convenience for data received from web forms, which does not |
| 58 |
-- have normalised line endings, line endings will be normalised (to \n) |
| 59 |
-- before parsing. |
| 60 |
-- |
| 61 |
-- The file will be written (if changed) with the current system's native |
| 62 |
-- line endings (see writeFileWithBackupIfChanged). |
| 63 |
-- |
| 64 |
writeJournalTextIfValidAndChanged :: MonadHandler m => FilePath -> Text -> ExceptT String m () |
| 65 |
writeJournalTextIfValidAndChanged f t = mapExceptT liftIO $ do |
| 66 |
-- Ensure unix line endings, since both readJournal (cf |
| 67 |
-- formatdirectivep, #1194) writeFileWithBackupIfChanged require them. |
| 68 |
-- XXX klunky. Any equivalent of "hSetNewlineMode h universalNewlineMode" for form posts ? |
| 69 |
let t' = T.replace "\r" "" t |
| 70 |
j <- readJournal definputopts (Just f) =<< liftIO (textToHandle t') |
| 71 |
_ <- liftIO $ j `seq` writeFileWithBackupIfChanged f t' -- Only write backup if the journal didn't error |
| 72 |
return () |
| 73 |
|
| 74 |
-- | Link to a topic in the manual. |
| 75 |
helplink :: Text -> Text -> HtmlUrl r |
| 76 |
helplink topic label _ = H.a ! A.href u ! A.target "hledgerhelp" $ toHtml label |
| 77 |
where u = textValue $ manualurl <> if T.null topic then "" else T.cons '#' topic |
| 78 |
|
| 79 |
-- | Render a "BalanceReport" as html. |
| 80 |
balanceReportAsHtml :: Eq r => (r, r) -> r -> Bool -> Journal -> Text -> [QueryOpt] -> BalanceReport -> HtmlUrl r |
| 81 |
balanceReportAsHtml (journalR, registerR) here hideEmpty j qparam qopts (items, total) = |
| 82 |
$(hamletFile "templates/balance-report.hamlet") |
| 83 |
where |
| 84 |
l = ledgerFromJournal Any j |
| 85 |
indent a = preEscapedString $ concat $ replicate (2 + 2 * a) " " |
| 86 |
hasSubAccounts acct = maybe True (not . null . asubs) $ ledgerAccount l acct |
| 87 |
isInterestingAccount acct = maybe False isInteresting $ ledgerAccount l acct |
| 88 |
where isInteresting a = not (all (mixedAmountLooksZero . bdexcludingsubs) . pdperiods $ adata a) || any isInteresting (asubs a) |
| 89 |
matchesAcctSelector acct = Just True == ((`matchesAccount` acct) <$> inAccountQuery qopts) |
| 90 |
|
| 91 |
accountQuery :: AccountName -> Text |
| 92 |
accountQuery = ("inacct:" <>) . quoteIfSpaced |
| 93 |
|
| 94 |
accountOnlyQuery :: AccountName -> Text |
| 95 |
accountOnlyQuery = ("inacctonly:" <>) . quoteIfSpaced |
| 96 |
|
| 97 |
mixedAmountAsHtml :: MixedAmount -> HtmlUrl a |
| 98 |
mixedAmountAsHtml b _ = |
| 99 |
for_ (lines (showMixedAmountWith noCostFmt{displayZeroCommodity=True} b)) $ \t -> do |
| 100 |
H.span ! A.class_ c $ toHtml t |
| 101 |
H.br |
| 102 |
where |
| 103 |
c = case isNegativeMixedAmount b of |
| 104 |
Just True -> "negative amount" |
| 105 |
_ -> "positive amount" |
| 106 |
|
| 107 |
-- Make a slug to uniquely identify this transaction |
| 108 |
-- in hyperlinks (as far as possible). |
| 109 |
transactionFragment :: Journal -> Transaction -> String |
| 110 |
transactionFragment j Transaction{tindex, tsourcepos} = |
| 111 |
printf "transaction-%d-%d" tfileindex tindex |
| 112 |
where |
| 113 |
-- the numeric index of this txn's file within all the journal files, |
| 114 |
-- or 0 if this txn has no known file (eg a forecasted txn) |
| 115 |
tfileindex = maybe 0 (+1) $ elemIndex (sourceName $ fst tsourcepos) (journalFilePaths j) |
| 116 |
|
| 117 |
removeDates :: Text -> [Text] |
| 118 |
removeDates = |
| 119 |
map quoteIfSpaced . |
| 120 |
filter (\term -> |
| 121 |
not $ T.isPrefixOf "date:" term || T.isPrefixOf "date2:" term) . |
| 122 |
Query.words'' queryprefixes |
| 123 |
|
| 124 |
removeInacct :: Text -> [Text] |
| 125 |
removeInacct = |
| 126 |
map quoteIfSpaced . |
| 127 |
filter (\term -> |
| 128 |
not $ T.isPrefixOf "inacct:" term || T.isPrefixOf "inacctonly:" term) . |
| 129 |
Query.words'' queryprefixes |
| 130 |
|
| 131 |
replaceInacct :: Text -> Text -> Text |
| 132 |
replaceInacct q acct = T.unwords $ acct : removeInacct q |