Permalink
Browse files

Fix fixURLs to employ the same logic as in the Wiki

When including an image in a wiki page, the server will look for it in
the static dir first, in the default static dir second and lastly in
the repository dir on a browser request.

When exporting pages e.g. to PDF though, the image's URL was always
mapped into the static dir, making it impossible to export pages with
images stored in the repository itself.
  • Loading branch information...
1 parent 0fb5a1c commit b63f5f2b80273aae321ce6268dc2239e3da1244e @avdv avdv committed Feb 4, 2013
Showing with 23 additions and 10 deletions.
  1. +23 −10 Network/Gitit/Export.hs
View
33 Network/Gitit/Export.hs
@@ -41,13 +41,15 @@ import Control.Exception (throwIO)
import System.Environment (getEnvironment)
import System.Exit (ExitCode(..))
import System.IO (openTempFile)
-import System.Directory (getCurrentDirectory, setCurrentDirectory, removeFile)
+import System.Directory (getCurrentDirectory, setCurrentDirectory, removeFile,
+ doesFileExist)
import System.Process (runProcess, waitForProcess)
import Codec.Binary.UTF8.String (encodeString)
import Text.HTML.SanitizeXSS
import Text.Pandoc.Writers.RTF (writeRTFWithEmbeddedImages)
import qualified Data.Text as T
import Data.List (isPrefixOf)
+import Paths_gitit (getDataFileName)
defaultRespOptions :: WriterOptions
defaultRespOptions = def { writerStandalone = True }
@@ -274,16 +276,27 @@ respondPDF page old_pndc = fixURLs page old_pndc >>= \pndc -> do
-- images in the staticDir with their correct absolute file path.
fixURLs :: String -> Pandoc -> GititServerPart Pandoc
fixURLs page pndc = do
- curdir <- liftIO getCurrentDirectory
cfg <- getConfig
-
- let go (Image ils (url, title)) = Image ils (fixURL url, title)
- go x = x
-
- fixURL ('/':url) = curdir </> staticDir cfg </> url
- fixURL url = curdir </> repositoryPath cfg </> takeDirectory page </> url
-
- return $ bottomUp go pndc
+ defaultStatic <- liftIO $ getDataFileName $ "data" </> "static"
+
+ let static = staticDir cfg
+ let repoPath = repositoryPath cfg
+
+ let go (Image ils (url, title)) = do
+ fixedURL <- fixURL url
+ return $ Image ils (fixedURL, title)
+ go x = return x
+
+ fixURL ('/':url) = resolve url
+ fixURL url = resolve $ takeDirectory page </> url
+
+ resolve p = do
+ sp <- doesFileExist $ static </> p
+ dsp <- doesFileExist $ defaultStatic </> p
+ return (if sp then static </> p
+ else (if dsp then defaultStatic </> p
+ else repoPath </> p))
+ liftIO $ bottomUpM go pndc
exportFormats :: Config -> [(String, String -> Pandoc -> Handler)]
exportFormats cfg = if pdfExport cfg

0 comments on commit b63f5f2

Please sign in to comment.