@@ -39,8 +39,8 @@ import Text.Pandoc.Shared
39import Text.Pandoc.Writers.Shared
39import Text.Pandoc.Writers.Shared
40import Text.Pandoc.Pretty
40import Text.Pandoc.Pretty
41import Text.Pandoc.Templates (renderTemplate')
41import Text.Pandoc.Templates (renderTemplate')
42−import Data.Char ( toLower )
42+import Data.Char ( isAlphaNum, toLower )
43−import Data.List ( intersect, intersperse, partition, transpose )
43+import Data.List ( isPrefixOf, intersect, intersperse, partition, transpose )
44import Control.Monad.State
44import Control.Monad.State
46data WriterState =
46data WriterState =
@@ -158,10 +158,9 @@ blockToOrg (Plain inlines) = inlineListToOrg inlines
158blockToOrg (Para [Image attr txt (src,'f':'i':'g':':':tit)]) = do
158blockToOrg (Para [Image attr txt (src,'f':'i':'g':':':tit)]) = do
159 capt <- if null txt
159 capt <- if null txt
160 then return empty
160 then return empty
161− else (\c -> "#+CAPTION: " <> c <> blankline) `fmap`
162− inlineListToOrg txt
161+ else ("#+CAPTION: " <>) `fmap` inlineListToOrg txt
163 img <- inlineToOrg (Image attr txt (src,tit))
162 img <- inlineToOrg (Image attr txt (src,tit))
164− return $ capt <> img
163+ return $ capt $$ img $$ blankline
165blockToOrg (Para inlines) = do
164blockToOrg (Para inlines) = do
166 contents <- inlineListToOrg inlines
165 contents <- inlineListToOrg inlines
167 return $ contents <> blankline
166 return $ contents <> blankline
@@ -355,16 +354,34 @@ inlineToOrg (Link _ txt (src, _)) = do
355 case txt of
354 case txt of
356 [Str x] | escapeURI x == src -> -- autolink
355 [Str x] | escapeURI x == src -> -- autolink
357 do modify $ \s -> s{ stLinks = True }
356 do modify $ \s -> s{ stLinks = True }
358− return $ "[[" <> text x <> "]]"
357+ return $ "[[" <> text (orgPath x) <> "]]"
359 _ -> do contents <- inlineListToOrg txt
358 _ -> do contents <- inlineListToOrg txt
360 modify $ \s -> s{ stLinks = True }
359 modify $ \s -> s{ stLinks = True }
361− return $ "[[" <> text src <> "][" <> contents <> "]]"
360+ return $ "[[" <> text (orgPath src) <> "][" <> contents <> "]]"
362inlineToOrg (Image _ _ (source, _)) = do
361inlineToOrg (Image _ _ (source, _)) = do
363 modify $ \s -> s{ stImages = True }
362 modify $ \s -> s{ stImages = True }
364− return $ "[[" <> text source <> "]]"
363+ return $ "[[" <> text (orgPath source) <> "]]"
365inlineToOrg (Note contents) = do
364inlineToOrg (Note contents) = do
366 -- add to notes in state
365 -- add to notes in state
367 notes <- get >>= (return . stNotes)
366 notes <- get >>= (return . stNotes)
368 modify $ \st -> st { stNotes = contents:notes }
367 modify $ \st -> st { stNotes = contents:notes }
369 let ref = show $ (length notes) + 1
368 let ref = show $ (length notes) + 1
370 return $ " [" <> text ref <> "]"
369 return $ " [" <> text ref <> "]"
371+orgPath :: String -> String
374+ [] -> mempty -- wiki link
375+ ('#':xs) -> xs -- internal link
376+ _ | isUrl src -> src
377+ _ | isFilePath src -> src
380+ isFilePath :: String -> Bool
381+ isFilePath cs = any (`isPrefixOf` cs) ["/", "./", "../", "file:"]
383+ isUrl :: String -> Bool
385+ let (scheme, path) = break (== ':') cs
386+ in all (\c -> isAlphaNum c || c `elem` (".-"::String)) scheme