public
Created

  • Download Gist
gistfile1.hs
Haskell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
-- | Get the add form from template files reloaded at run-time.
getAddformRTR :: Handler HledgerWebApp RepHtml
getAddformRTR = do
(a, p, _, _, j, msg, here) <- getHandlerParameters
today <- liftIO getCurrentDay
let td = mktd{here=here, title="hledger add transaction", msg=msg, a=a, p=p, j=j, today=today}
descriptions = sort $ nub $ map tdescription $ jtxns j
acctnames = sort $ journalAccountNamesUsed j
postingData n = HDMap [
("acctlabel", hdstring acctlabel)
,("acctvar", hdstring acctvar)
,("acctnames", HDList $ map hdstring acctnames)
,("amtfield", HDHtml $ renderHamlet' amtfield)
,("accthelp", hdstring accthelp)
,("amthelp", hdstring amthelp)
] :: HamletData HledgerWebAppRoute
where
numbered = (++ show n)
acctvar = numbered "account"
amtvar = numbered "amount"
(acctlabel, accthelp, amtfield, amthelp)
| n == 1 = ("To account"
,"eg: expenses:food"
,[$hamlet|
%td!style=padding-left:1em;
Amount:
%td
%input.textinput!size=15!name=$amtvar$!value=""
|]
,"eg: 5, $6, €7.01"
)
| otherwise = ("From account"
,"eg: assets:bank:checking"
,nulltemplate
,""
)
pfields1 <- renderHamletFile "addformpostingfields.hamlet" (postingData 1)
pfields2 <- renderHamletFile "addformpostingfields.hamlet" (postingData 2)
addform <- renderHamletFile "addform.hamlet" (HDMap [
("date", hdstring "today")
,("desc", hdstring "")
,("descriptions", HDList $ map hdstring descriptions)
,("datehelp", hdstring "eg: 7/20, 2010/1/1")
,("deschelp", hdstring "eg: supermarket (optional)")
,("postingfields1", HDHtml pfields1)
,("postingfields2", HDHtml pfields2)
])
hamletToRepHtml $ pageLayout td $ htmlAsHamlet addform
 
hdstring = HDHtml . string
 
instance Failure HamletException (Handler HledgerWebApp)
where failure = error . show
 
renderHamletFile :: FilePath -> HamletData HledgerWebAppRoute -> Handler HledgerWebApp (Html ())
renderHamletFile hfile hdata = do
hrt <- readHamletFile hfile >>= parseHamletRT defaultHamletSettings
renderHamletRT hrt hdata show
 
readHamletFile :: FilePath -> Handler HledgerWebApp String
readHamletFile hfile = do
dir <- ((</> "templates") . appDir) `fmap` getYesod
liftIO $ readFile $ dir </> hfile
 
htmlAsHamlet :: Html () -> Hamlet HledgerWebAppRoute
htmlAsHamlet h = [$hamlet|$h$|]
 
parseHamletRT' :: Failure HamletException m => String -> m HamletRT
parseHamletRT' s = parseHamletRT defaultHamletSettings s
 
renderHamletRT' :: Failure HamletException m => HamletData HledgerWebAppRoute -> HamletRT -> m (Html ())
renderHamletRT' d h = renderHamletRT h d show
 
renderHamlet' :: Hamlet HledgerWebAppRoute -> Html ()
renderHamlet' h = h show

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.