diff options
| author | John MacFarlane <[email protected]> | 2022-10-24 10:44:18 -0700 |
|---|---|---|
| committer | John MacFarlane <[email protected]> | 2022-10-24 10:44:18 -0700 |
| commit | 9496ce818bfc22cb8e823a04de8f5791fd84c8dd (patch) | |
| tree | b280bd27a109a2dbfb865c59b2d72da095c972f3 | |
| parent | 79d6b45566c237b1caf6e9a2cb74861c071e84e8 (diff) | |
ODT writer: fix relative links.
Closes #3524.
| -rw-r--r-- | src/Text/Pandoc/Writers/ODT.hs | 19 |
1 files changed, 18 insertions, 1 deletions
diff --git a/src/Text/Pandoc/Writers/ODT.hs b/src/Text/Pandoc/Writers/ODT.hs index 302debe7f..064d48672 100644 --- a/src/Text/Pandoc/Writers/ODT.hs +++ b/src/Text/Pandoc/Writers/ODT.hs @@ -47,6 +47,7 @@ import Text.Pandoc.XML import Text.Pandoc.XML.Light import Text.TeXMath import qualified Text.XML.Light as XL +import Network.URI (parseRelativeReference, URI(uriPath)) newtype ODTState = ODTState { stEntries :: [Entry] } @@ -61,10 +62,26 @@ writeODT :: PandocMonad m writeODT opts doc = let initState = ODTState{ stEntries = [] } - doc' = ensureValidXmlIdentifiers doc + doc' = fixInternalLinks . ensureValidXmlIdentifiers $ doc in evalStateT (pandocToODT opts doc') initState +-- | ODT internal links are evaluated relative to an imaginary folder +-- structure that mirrors the zip structure. The result is that relative +-- links in the document need to start with `..`. See #3524. +fixInternalLinks :: Pandoc -> Pandoc +fixInternalLinks = walk go + where + go (Link attr ils (src,tit)) = + Link attr ils (fixRel src,tit) + go (Image attr ils (src,tit)) = + Image attr ils (fixRel src,tit) + go x = x + fixRel uri = + case parseRelativeReference (T.unpack uri) of + Nothing -> uri + Just u -> tshow $ u{ uriPath = "../" <> uriPath u } + -- | Produce an ODT file from a Pandoc document. pandocToODT :: PandocMonad m => WriterOptions -- ^ Writer options |
