cli: Commands.Balance.composeAnchor: construct an anchor from Maybe base-url and query

Optionally add a missing trailing slash to the base URL.
This commit is contained in:
Henning Thielemann 2024-09-29 11:07:37 +02:00 committed by Simon Michael
parent 2a25bfdebc
commit e116b6af41
2 changed files with 25 additions and 12 deletions

View File

@ -598,6 +598,25 @@ registerQueryUrl query =
map quoteIfSpaced $ filter (not . T.null) query] map quoteIfSpaced $ filter (not . T.null) query]
} }
{- |
>>> composeAnchor Nothing ["date:2024"]
""
>>> composeAnchor (Just "") ["date:2024"]
"register?q=date:2024"
>>> composeAnchor (Just "/") ["date:2024"]
"/register?q=date:2024"
>>> composeAnchor (Just "foo") ["date:2024"]
"foo/register?q=date:2024"
>>> composeAnchor (Just "foo/") ["date:2024"]
"foo/register?q=date:2024"
-}
composeAnchor :: Maybe Text -> [Text] -> Text
composeAnchor Nothing _ = mempty
composeAnchor (Just baseUrl) query =
baseUrl <>
(if all (('/'==) . snd) $ T.unsnoc baseUrl then "" else "/") <>
registerQueryUrl query
-- cf. Web.Widget.Common -- cf. Web.Widget.Common
removeDates :: [Text] -> [Text] removeDates :: [Text] -> [Text]
removeDates = removeDates =
@ -612,8 +631,7 @@ headerDateSpanCell ::
headerDateSpanCell base query spn = headerDateSpanCell base query spn =
let prd = showDateSpan spn in let prd = showDateSpan spn in
(headerCell prd) { (headerCell prd) {
Ods.cellAnchor = Ods.cellAnchor = composeAnchor base $ replaceDate prd query
foldMap (<> registerQueryUrl (replaceDate prd query)) base
} }
simpleDateSpanCell :: DateSpan -> Ods.Cell Ods.NumLines Text simpleDateSpanCell :: DateSpan -> Ods.Cell Ods.NumLines Text
@ -626,9 +644,7 @@ dateSpanCell base query acct spn =
let prd = showDateSpan spn in let prd = showDateSpan spn in
(Ods.defaultCell prd) { (Ods.defaultCell prd) {
Ods.cellAnchor = Ods.cellAnchor =
foldMap composeAnchor base $ "inacct:"<>acct : replaceDate prd query
(<> registerQueryUrl ("inacct:"<>acct : replaceDate prd query))
base
} }
addTotalBorders :: [[Ods.Cell border text]] -> [[Ods.Cell Ods.NumLines text]] addTotalBorders :: [[Ods.Cell border text]] -> [[Ods.Cell Ods.NumLines text]]
@ -646,9 +662,7 @@ rawTableContent = map (map Ods.cellContent)
setAccountAnchor :: setAccountAnchor ::
Maybe Text -> [Text] -> Text -> Ods.Cell border text -> Ods.Cell border text Maybe Text -> [Text] -> Text -> Ods.Cell border text -> Ods.Cell border text
setAccountAnchor base query acct cell = setAccountAnchor base query acct cell =
cell cell {Ods.cellAnchor = composeAnchor base $ "inacct:"<>acct : query}
{Ods.cellAnchor =
foldMap (<> registerQueryUrl ("inacct:"<>acct : query)) base}
-- | Render a single-column balance report as FODS. -- | Render a single-column balance report as FODS.

View File

@ -393,10 +393,9 @@ E.g. if your `hledger-web` server is reachable
under the URL `http://localhost:5000/` under the URL `http://localhost:5000/`
then you might run the `balance` command then you might run the `balance` command
with the extra option `--base-url=http://localhost:5000/`. with the extra option `--base-url=http://localhost:5000/`.
The export function will not add any slash The export function will add a missing trailing slash
in order to support relative hyperreferences. if the base URL is non-empty.
Thus it is important that you add the trailing slash to the URL yourselves, However, `--base-url=""` can be used to produce relative URLs.
where needed.
### Multi-period balance report ### Multi-period balance report