git.iko.soy/typeable/pandoc
on master help sign in
Options: Changed `writerSourceDir` to `writerSourceURL` (now a Maybe).

Previously we used to store the directory of the first input file, even if it was local, and used this as a base directory for finding images in ODT, EPUB, Docx, and PDF. This has been confusing to many users. It seems better to look for images relative to the current working directory, even if the first file argument is in another directory. writerSourceURL is set to 'Just url' when the first command-line argument is an absolute URL. (So, relative links will be resolved in relation to the first page.) Otherwise, 'Nothing'. The ODT, EPUB, Docx, and PDF writers have been modified accordingly. Note that this change may break some existing workflows. If you have been assuming that relative links will be interpreted relative to the directory of the first file argument, you'll need to make that the current directory before running pandoc. Closes #942.

commite279175ea517e2df65fe5d716bc02e383b04fc36 authorJohn MacFarlane <fiddlosopher@gmail.com> dateSun 11 Aug 2013, 15:58 (13 years ago) parent6f736df
7 files changed+34−41
pandoc.hs+7−5 src/Text/Pandoc/Options.hs+2−2 src/Text/Pandoc/PDF.hs+3−3 src/Text/Pandoc/Shared.hs+10−10 src/Text/Pandoc/Writers/Docx.hs+1−2 src/Text/Pandoc/Writers/EPUB.hs+7−14 src/Text/Pandoc/Writers/ODT.hs+4−5
pandoc.hs+7−5
@@ -1034,13 +1034,15 @@ main = do
10341034 return $ Just csl { CSL.styleAbbrevs = abbrevs }
10351035 else return Nothing
10361036
1037− let sourceDir = case sources of
1038− [] -> "."
1037+ let sourceURL = case sources of
1038+ [] -> Nothing
10391039 (x:_) -> case parseURI x of
10401040 Just u
10411041 | uriScheme u `elem` ["http:","https:"] ->
1042− show u{ uriPath = "", uriQuery = "", uriFragment = "" }
1043− _ -> takeDirectory x
1042+ Just $ show u{ uriPath = "",
1043+ uriQuery = "",
1044+ uriFragment = "" }
1045+ _ -> Nothing
10441046
10451047 let readerOpts = def{ readerSmart = smart || (texLigatures &&
10461048 (laTeXOutput || "context" `isPrefixOf` writerName'))
@@ -1074,7 +1076,7 @@ main = do
10741076 writerColumns = columns,
10751077 writerEmailObfuscation = obfuscationMethod,
10761078 writerIdentifierPrefix = idPrefix,
1077− writerSourceDirectory = sourceDir,
1079+ writerSourceURL = sourceURL,
10781080 writerUserDataDir = datadir,
10791081 writerHtml5 = html5,
10801082 writerHtmlQTags = htmlQTags,
src/Text/Pandoc/Options.hs+2−2
@@ -286,7 +286,7 @@ data WriterOptions = WriterOptions
286286 , writerEmailObfuscation :: ObfuscationMethod -- ^ How to obfuscate emails
287287 , writerIdentifierPrefix :: String -- ^ Prefix for section & note ids in HTML
288288 -- and for footnote marks in markdown
289− , writerSourceDirectory :: FilePath -- ^ Directory path of 1st source file
289+ , writerSourceURL :: Maybe String -- ^ Absolute URL + directory of 1st source file
290290 , writerUserDataDir :: Maybe FilePath -- ^ Path of user data directory
291291 , writerCiteMethod :: CiteMethod -- ^ How to print cites
292292 , writerBiblioFiles :: [FilePath] -- ^ Biblio files to use for citations
@@ -329,7 +329,7 @@ instance Default WriterOptions where
329329 , writerColumns = 72
330330 , writerEmailObfuscation = JavascriptObfuscation
331331 , writerIdentifierPrefix = ""
332− , writerSourceDirectory = "."
332+ , writerSourceURL = Nothing
333333 , writerUserDataDir = Nothing
334334 , writerCiteMethod = Citeproc
335335 , writerBiblioFiles = []
src/Text/Pandoc/PDF.hs+3−3
@@ -65,17 +65,17 @@ makePDF :: String -- ^ pdf creator (pdflatex, lualatex, xelatex)
6565 -> Pandoc -- ^ document
6666 -> IO (Either ByteString ByteString)
6767makePDF program writer opts doc = withTempDir "tex2pdf." $ \tmpdir -> do
68− doc' <- handleImages (writerSourceDirectory opts) tmpdir doc
68+ doc' <- handleImages (writerSourceURL opts) tmpdir doc
6969 let source = writer opts doc'
7070 tex2pdf' tmpdir program source
7171
72−handleImages :: String -- ^ source directory/base URL
72+handleImages :: Maybe String -- ^ source base URL
7373 -> FilePath -- ^ temp dir to store images
7474 -> Pandoc -- ^ document
7575 -> IO Pandoc
7676handleImages baseURL tmpdir = walkM (handleImage' baseURL tmpdir)
7777
78−handleImage' :: String
78+handleImage' :: Maybe String
7979 -> FilePath
8080 -> Inline
8181 -> IO Inline
src/Text/Pandoc/Shared.hs+10−10
@@ -612,18 +612,18 @@ readDataFileUTF8 userDir fname =
612612
613613-- | Fetch an image or other item from the local filesystem or the net.
614614-- Returns raw content and maybe mime type.
615−fetchItem :: String -> String
615+fetchItem :: Maybe String -> String
616616 -> IO (Either E.SomeException (BS.ByteString, Maybe String))
617−fetchItem sourceDir s =
618− case s of
619− _ | isAbsoluteURI s -> openURL s
620− | isAbsoluteURI sourceDir -> openURL $ sourceDir ++ "/" ++ s
621− | otherwise -> E.try $ do
617+fetchItem sourceURL s
618+ | isAbsoluteURI s = openURL s
619+ | otherwise = case sourceURL of
620+ Just u -> openURL (u ++ "/" ++ s)
621+ Nothing -> E.try readLocalFile
622+ where readLocalFile = do
622623 let mime = case takeExtension s of
623− ".gz" -> getMimeType $ dropExtension s
624− x -> getMimeType x
625− let f = sourceDir </> s
626− cont <- BS.readFile f
624+ ".gz" -> getMimeType $ dropExtension s
625+ x -> getMimeType x
626+ cont <- BS.readFile s
627627 return (cont, mime)
628628
629629-- | Read from a URL and return raw data and maybe mime type.
src/Text/Pandoc/Writers/Docx.hs+1−2
@@ -728,8 +728,7 @@ inlineToOpenXML opts (Image alt (src, tit)) = do
728728 case M.lookup src imgs of
729729 Just (_,_,_,elt,_) -> return [elt]
730730 Nothing -> do
731− let sourceDir = writerSourceDirectory opts
732− res <- liftIO $ fetchItem sourceDir src
731+ res <- liftIO $ fetchItem (writerSourceURL opts) src
733732 case res of
734733 Left (_ :: E.SomeException) -> do
735734 liftIO $ warn $ "Could not find image `" ++ src ++ "', skipping..."
src/Text/Pandoc/Writers/EPUB.hs+7−14
@@ -55,7 +55,7 @@ import Text.Pandoc.UUID
5555import Text.Pandoc.Writers.HTML
5656import Text.Pandoc.Writers.Markdown ( writePlain )
5757import Data.Char ( toLower )
58−import Network.URI ( isAbsoluteURI, unEscapeString )
58+import Network.URI ( unEscapeString )
5959import Text.Pandoc.MIME (getMimeType)
6060#if MIN_VERSION_base(4,6,0)
6161#else
@@ -93,7 +93,6 @@ writeEPUB opts doc@(Pandoc meta _) = do
9393 then MathML Nothing
9494 else writerHTMLMathMethod opts
9595 , writerWrapText = False }
96− let sourceDir = writerSourceDirectory opts'
9796 let mbCoverImage = lookup "epub-cover-image" vars
9897
9998 -- cover page
@@ -117,10 +116,10 @@ writeEPUB opts doc@(Pandoc meta _) = do
117116 -- handle pictures
118117 picsRef <- newIORef []
119118 Pandoc _ blocks <- walkM
120− (transformInline opts' sourceDir picsRef) doc
119+ (transformInline opts' picsRef) doc
121120 pics <- readIORef picsRef
122121 let readPicEntry entries (oldsrc, newsrc) = do
123− res <- fetchItem sourceDir oldsrc
122+ res <- fetchItem (writerSourceURL opts') oldsrc
124123 case res of
125124 Left _ -> do
126125 warn $ "Could not find image `" ++ oldsrc ++ "', skipping..."
@@ -414,19 +413,13 @@ showDateTimeISO8601 :: UTCTime -> String
414413showDateTimeISO8601 = formatTime defaultTimeLocale "%FT%TZ"
415414
416415transformInline :: WriterOptions
417− -> FilePath
418416 -> IORef [(FilePath, FilePath)] -- ^ (oldpath, newpath) images
419417 -> Inline
420418 -> IO Inline
421−transformInline opts sourceDir picsRef (Image lab (src,tit))
422− | isAbsoluteURI src = do
423− raw <- makeSelfContained Nothing
424− $ writeHtmlInline opts (Image lab (src,tit))
425− return $ RawInline (Format "html") raw
426− | otherwise = do
419+transformInline opts picsRef (Image lab (src,tit)) = do
427420 let src' = unEscapeString src
428421 pics <- readIORef picsRef
429− let oldsrc = sourceDir </> src'
422+ let oldsrc = maybe src' (</> src) $ writerSourceURL opts
430423 let ext = takeExtension src'
431424 newsrc <- case lookup oldsrc pics of
432425 Just n -> return n
@@ -435,11 +428,11 @@ transformInline opts sourceDir picsRef (Image lab (src,tit))
435428 modifyIORef picsRef ( (oldsrc, new): )
436429 return new
437430 return $ Image lab (newsrc, tit)
438−transformInline opts _ _ (x@(Math _ _))
431+transformInline opts _ (x@(Math _ _))
439432 | WebTeX _ <- writerHTMLMathMethod opts = do
440433 raw <- makeSelfContained Nothing $ writeHtmlInline opts x
441434 return $ RawInline (Format "html") raw
442−transformInline _ _ _ x = return x
435+transformInline _ _ x = return x
443436
444437writeHtmlInline :: WriterOptions -> Inline -> String
445438writeHtmlInline opts z = trimr $
src/Text/Pandoc/Writers/ODT.hs+4−5
@@ -62,8 +62,7 @@ writeODT opts doc@(Pandoc meta _) = do
6262 readDataFile datadir "reference.odt"
6363 -- handle pictures
6464 picEntriesRef <- newIORef ([] :: [Entry])
65− let sourceDir = writerSourceDirectory opts
66− doc' <- walkM (transformPic sourceDir picEntriesRef) doc
65+ doc' <- walkM (transformPic opts picEntriesRef) doc
6766 let newContents = writeOpenDocument opts{writerWrapText = False} doc'
6867 epochtime <- floor `fmap` getPOSIXTime
6968 let contentEntry = toEntry "content.xml" epochtime $ fromStringLazy newContents
@@ -111,9 +110,9 @@ writeODT opts doc@(Pandoc meta _) = do
111110 let archive'' = addEntryToArchive metaEntry archive'
112111 return $ fromArchive archive''
113112
114−transformPic :: FilePath -> IORef [Entry] -> Inline -> IO Inline
115−transformPic sourceDir entriesRef (Image lab (src,_)) = do
116− res <- fetchItem sourceDir src
113+transformPic :: WriterOptions -> IORef [Entry] -> Inline -> IO Inline
114+transformPic opts entriesRef (Image lab (src,_)) = do
115+ res <- fetchItem (writerSourceURL opts) src
117116 case res of
118117 Left (_ :: E.SomeException) -> do
119118 warn $ "Could not find image `" ++ src ++ "', skipping..."