imp: lib: Write.Html: use Lucid to generate HTML

This commit is contained in:
Henning Thielemann 2024-08-10 12:04:13 +02:00
parent 48723c930c
commit f306df6d61
4 changed files with 22 additions and 39 deletions

View File

@ -1,3 +1,4 @@
{-# LANGUAGE OverloadedStrings #-}
{- |
Export spreadsheet table data as HTML table.
@ -9,50 +10,29 @@ module Hledger.Write.Html (
import Hledger.Write.Spreadsheet (Type(..), Style(..), Emphasis(..), Cell(..))
import qualified Data.Text.Lazy as TL
import qualified Data.Text as T
import Text.Printf (printf)
import qualified Lucid.Base as LucidBase
import qualified Lucid
import Data.Foldable (for_)
printHtml :: [[Cell]] -> TL.Text
printHtml :: [[Cell]] -> Lucid.Html ()
printHtml table =
TL.unlines $ map (TL.fromStrict . T.pack) $
"<table>" :
(table >>= \row ->
"<tr>" :
(row >>= formatCell) ++
"</tr>" :
[]) ++
"</table>" :
[]
Lucid.table_ $ for_ table $ \row ->
Lucid.tr_ $ for_ row $ \cell ->
formatCell cell
formatCell :: Cell -> [String]
formatCell :: Cell -> Lucid.Html ()
formatCell cell =
(let str = escape $ T.unpack $ cellContent cell in
case cellStyle cell of
Head -> printf "<th>%s</th>" str
let str = Lucid.toHtml $ cellContent cell in
case cellStyle cell of
Head -> Lucid.th_ str
Body emph ->
let align =
case cellType cell of
TypeString -> ""
_ -> " align=right"
(emphOpen, emphClose) =
TypeString -> []
_ -> [LucidBase.makeAttribute "align" "right"]
withEmph =
case emph of
Item -> ("", "")
Total -> ("<b>", "</b>")
in printf "<td%s>%s%s%s</td>" align emphOpen str emphClose) :
[]
escape :: String -> String
escape =
concatMap $ \c ->
case c of
'\n' -> "<br>"
'&' -> "&amp;"
'<' -> "&lt;"
'>' -> "&gt;"
'"' -> "&quot;"
'\'' -> "&apos;"
_ -> [c]
Item -> id
Total -> Lucid.b_
in Lucid.td_ align $ withEmph str

View File

@ -138,6 +138,7 @@ library
, file-embed >=0.0.10
, filepath
, hashtables >=1.2.3.1
, lucid
, megaparsec >=7.0.0 && <9.7
, microlens >=0.4
, microlens-th >=0.4

View File

@ -61,6 +61,7 @@ dependencies:
- file-embed >=0.0.10
- filepath
- hashtables >=1.2.3.1
- lucid
- megaparsec >=7.0.0 && <9.7
- microlens >=0.4
- microlens-th >=0.4

View File

@ -403,7 +403,8 @@ balance opts@CliOpts{reportspec_=rspec} j = case balancecalc_ of
"txt" -> \ropts1 -> TB.toLazyText . balanceReportAsText ropts1
"csv" -> \ropts1 -> printCSV . balanceReportAsCsv ropts1
"tsv" -> \ropts1 -> printTSV . balanceReportAsCsv ropts1
"html" -> \ropts1 -> printHtml . balanceReportAsSpreadsheet ropts1
"html" -> \ropts1 -> (<>"\n") . L.renderText .
printHtml . balanceReportAsSpreadsheet ropts1
"json" -> const $ (<>"\n") . toJsonText
"fods" -> \ropts1 -> printFods IO.localeEncoding . Map.singleton "Hledger" . (,) (Just 1, Nothing) . balanceReportAsSpreadsheet ropts1
_ -> error' $ unsupportedOutputFormatError fmt -- PARTIAL: