@@ -39,8 +39,8 @@ import Text.Pandoc.Shared
3939import Text.Pandoc.Writers.Shared
4040import Text.Pandoc.Pretty
4141import Text.Pandoc.Templates (renderTemplate')
42−import Data.Char ( toLower )
43−import Data.List ( intersect, intersperse, partition, transpose )
42+import Data.Char ( isAlphaNum, toLower )
43+import Data.List ( isPrefixOf, intersect, intersperse, partition, transpose )
4444import Control.Monad.State
4545
4646data WriterState =
@@ -354,16 +354,34 @@ inlineToOrg (Link _ txt (src, _)) = do
354354 case txt of
355355 [Str x] | escapeURI x == src -> -- autolink
356356 do modify $ \s -> s{ stLinks = True }
357− return $ "[[" <> text x <> "]]"
357+ return $ "[[" <> text (orgPath x) <> "]]"
358358 _ -> do contents <- inlineListToOrg txt
359359 modify $ \s -> s{ stLinks = True }
360− return $ "[[" <> text src <> "][" <> contents <> "]]"
360+ return $ "[[" <> text (orgPath src) <> "][" <> contents <> "]]"
361361inlineToOrg (Image _ _ (source, _)) = do
362362 modify $ \s -> s{ stImages = True }
363− return $ "[[" <> text source <> "]]"
363+ return $ "[[" <> text (orgPath source) <> "]]"
364364inlineToOrg (Note contents) = do
365365 -- add to notes in state
366366 notes <- get >>= (return . stNotes)
367367 modify $ \st -> st { stNotes = contents:notes }
368368 let ref = show $ (length notes) + 1
369369 return $ " [" <> text ref <> "]"
370+
371+orgPath :: String -> String
372+orgPath src =
373+ case src of
374+ [] -> mempty -- wiki link
375+ ('#':xs) -> xs -- internal link
376+ _ | isUrl src -> src
377+ _ | isFilePath src -> src
378+ _ -> "file:" <> src
379+ where
380+ isFilePath :: String -> Bool
381+ isFilePath cs = any (`isPrefixOf` cs) ["/", "./", "../", "file:"]
382+
383+ isUrl :: String -> Bool
384+ isUrl cs =
385+ let (scheme, path) = break (== ':') cs
386+ in all (\c -> isAlphaNum c || c `elem` (".-"::String)) scheme
387+ && not (null path)