git.iko.soy/typeable/pandoc
on master help sign in
Merge branch 'org-figure-fix'
commit29ac4ae813749c79a47fd0c89f011b43df48636d authorAlbert Krewinkel <albert@zeitkraut.de> dateThu 18 Aug 2016, 14:34 (10 years ago) parent3a44ee6dbf4d77
2 files changed+27−10
src/Text/Pandoc/Writers/Org.hs+25−8 tests/writer.org+2−2
src/Text/Pandoc/Writers/Org.hs+25−8
@@ -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 =
@@ -158,10 +158,9 @@ blockToOrg (Plain inlines) = inlineListToOrg inlines
158158blockToOrg (Para [Image attr txt (src,'f':'i':'g':':':tit)]) = do
159159 capt <- if null txt
160160 then return empty
161− else (\c -> "#+CAPTION: " <> c <> blankline) `fmap`
162− inlineListToOrg txt
161+ else ("#+CAPTION: " <>) `fmap` inlineListToOrg txt
163162 img <- inlineToOrg (Image attr txt (src,tit))
164− return $ capt <> img
163+ return $ capt $$ img $$ blankline
165164blockToOrg (Para inlines) = do
166165 contents <- inlineListToOrg inlines
167166 return $ contents <> blankline
@@ -355,16 +354,34 @@ inlineToOrg (Link _ txt (src, _)) = do
355354 case txt of
356355 [Str x] | escapeURI x == src -> -- autolink
357356 do modify $ \s -> s{ stLinks = True }
358− return $ "[[" <> text x <> "]]"
357+ return $ "[[" <> text (orgPath x) <> "]]"
359358 _ -> do contents <- inlineListToOrg txt
360359 modify $ \s -> s{ stLinks = True }
361− return $ "[[" <> text src <> "][" <> contents <> "]]"
360+ return $ "[[" <> text (orgPath src) <> "][" <> contents <> "]]"
362361inlineToOrg (Image _ _ (source, _)) = do
363362 modify $ \s -> s{ stImages = True }
364− return $ "[[" <> text source <> "]]"
363+ return $ "[[" <> text (orgPath source) <> "]]"
365364inlineToOrg (Note contents) = do
366365 -- add to notes in state
367366 notes <- get >>= (return . stNotes)
368367 modify $ \st -> st { stNotes = contents:notes }
369368 let ref = show $ (length notes) + 1
370369 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)
tests/writer.org+2−2
@@ -808,9 +808,9 @@ Auto-links should not occur here: =<http://example.com/>=
808808From "Voyage dans la Lune" by Georges Melies (1902):
809809
810810#+CAPTION: lalune
811+[[file:lalune.jpg]]
811812
812−[[lalune.jpg]]
813−Here is a movie [[movie.jpg]] icon.
813+Here is a movie [[file:movie.jpg]] icon.
814814
815815--------------
816816