From d7b713f9582e2041d959911cf01f01e6512da728 Mon Sep 17 00:00:00 2001 From: Henning Thielemann Date: Thu, 17 Oct 2024 12:51:15 +0200 Subject: [PATCH] lib: Write.Ods.styleNames: common generation of ODS style names for both formatCell and cellConfig --- hledger-lib/Hledger/Write/Ods.hs | 55 +++++++++++++++----------------- 1 file changed, 26 insertions(+), 29 deletions(-) diff --git a/hledger-lib/Hledger/Write/Ods.hs b/hledger-lib/Hledger/Write/Ods.hs index d7b7b569b..9a8e803d5 100644 --- a/hledger-lib/Hledger/Write/Ods.hs +++ b/hledger-lib/Hledger/Write/Ods.hs @@ -269,23 +269,12 @@ cellConfig ((border, cstyle), dataStyle) = DataMixedAmount -> [alignParagraph "end"] _ -> [] ) - cstyleName = cellStyleName cstyle - bordName = borderName border style :: String style = - case dataStyle of - DataDate -> - printf - "style:name='%s-%s-date' style:data-style-name='iso-date'" - cstyleName bordName - DataAmount comm prec -> - let name = numberStyleName (comm, prec) in - printf - "style:name='%s-%s-%s' style:data-style-name='number-%s'" - cstyleName bordName name name - DataMixedAmount -> - printf "style:name='%s-%s-mixedamount'" cstyleName bordName - DataString -> printf "style:name='%s-%s'" cstyleName bordName + let (styleName,dataStyleName) = styleNames cstyle border dataStyle + in printf "style:name='%s'" styleName + ++ + foldMap (printf " style:data-style-name='%s'") dataStyleName in case moreStyles of [] -> @@ -301,20 +290,12 @@ cellConfig ((border, cstyle), dataStyle) = formatCell :: Cell Spr.NumLines Text -> [String] formatCell cell = let style, valueType :: String - style = tableStyle styleName - cstyleName = cellStyleName $ cellStyle cell - bordName = borderName $ cellBorder cell - styleName :: String - styleName = - case dataStyleFromType $ cellType cell of - DataDate -> printf "%s-%s-date" cstyleName bordName - DataAmount comm prec -> - let name = numberStyleName (comm, prec) in - printf "%s-%s-%s" cstyleName bordName name - DataMixedAmount -> - printf "%s-%s-mixedamount" cstyleName bordName - DataString -> printf "%s-%s" cstyleName bordName - tableStyle = printf " table:style-name='%s'" + style = + printf " table:style-name='%s'" $ fst $ + styleNames + (cellStyle cell) + (cellBorder cell) + (dataStyleFromType $ cellType cell) valueType = case cellType cell of @@ -354,6 +335,22 @@ formatCell cell = printf "" covered : [] +styleNames :: + Style -> Spr.Border Spr.NumLines -> DataStyle -> (String, Maybe String) +styleNames cstyle border dataStyle = + let cstyleName = cellStyleName cstyle in + let bordName = borderName border in + case dataStyle of + DataDate -> + (printf "%s-%s-date" cstyleName bordName, Just "iso-date") + DataAmount comm prec -> + let name = numberStyleName (comm, prec) in + (printf "%s-%s-%s" cstyleName bordName name, + Just $ printf "number-%s" name) + DataMixedAmount -> + (printf "%s-%s-mixedamount" cstyleName bordName, Nothing) + DataString -> (printf "%s-%s" cstyleName bordName, Nothing) + escape :: String -> String escape = concatMap $ \c ->