html2pdf: inject base tag wih current working directory (#4443)

fixes #4413
This commit is contained in:
Mauro Bieg 2018-03-12 02:21:15 +01:00 committed by John MacFarlane
parent a0da1e2723
commit 15f700d8ed

View file

@ -56,6 +56,8 @@ import System.IO.Error (IOError, isDoesNotExistError)
#else
import System.IO.Error (isDoesNotExistError)
#endif
import Text.HTML.TagSoup
import Text.HTML.TagSoup.Match
import Text.Pandoc.Definition
import Text.Pandoc.Error (PandocError (PandocPDFProgramNotFoundError))
import Text.Pandoc.MIME (getMimeType)
@ -353,7 +355,13 @@ html2pdf :: Verbosity -- ^ Verbosity level
-> [String] -- ^ Args to program
-> Text -- ^ HTML5 source
-> IO (Either ByteString ByteString)
html2pdf verbosity program args source = do
html2pdf verbosity program args htmlSource = do
cwd <- getCurrentDirectory
let tags = parseTags htmlSource
(hd, tl) = break (tagClose (== "head")) tags
baseTag = TagOpen "base"
[("href", T.pack cwd <> T.singleton pathSeparator)] : [TagText "\n"]
source = renderTags $ hd ++ baseTag ++ tl
pdfFile <- withTempFile "." "html2pdf.pdf" $ \fp _ -> return fp
let pdfFileArgName = ["-o" | program == "prince"]
let programArgs = args ++ ["-"] ++ pdfFileArgName ++ [pdfFile]