From 91afa513ad495bdc277c94ac2a4a4b829423f222 Mon Sep 17 00:00:00 2001 From: Albert Krewinkel Date: Thu, 18 Aug 2016 14:32:59 +0200 Subject: [PATCH 1/3] Org writer: remove blank line after figure caption Org-mode only treats an image as a figure if it is directly preceded by a caption. --- src/Text/Pandoc/Writers/Org.hs | 5 ++--- tests/writer.org | 1 - 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Text/Pandoc/Writers/Org.hs b/src/Text/Pandoc/Writers/Org.hs index e903e9e42..283bb7d9c 100644 --- a/src/Text/Pandoc/Writers/Org.hs +++ b/src/Text/Pandoc/Writers/Org.hs @@ -158,10 +158,9 @@ blockToOrg (Plain inlines) = inlineListToOrg inlines blockToOrg (Para [Image attr txt (src,'f':'i':'g':':':tit)]) = do capt <- if null txt then return empty - else (\c -> "#+CAPTION: " <> c <> blankline) `fmap` - inlineListToOrg txt + else ("#+CAPTION: " <>) `fmap` inlineListToOrg txt img <- inlineToOrg (Image attr txt (src,tit)) - return $ capt <> img + return $ capt $$ img blockToOrg (Para inlines) = do contents <- inlineListToOrg inlines return $ contents <> blankline diff --git a/tests/writer.org b/tests/writer.org index cf6305ec9..100328c2c 100644 --- a/tests/writer.org +++ b/tests/writer.org @@ -808,7 +808,6 @@ Auto-links should not occur here: == From "Voyage dans la Lune" by Georges Melies (1902): #+CAPTION: lalune - [[lalune.jpg]] Here is a movie [[movie.jpg]] icon. From d6694256404973a3a7675b97261e7a1914e8f059 Mon Sep 17 00:00:00 2001 From: Albert Krewinkel Date: Thu, 18 Aug 2016 14:32:59 +0200 Subject: [PATCH 2/3] Org writer: ensure blank line after figure An Org-mode figure should be surrounded by blank lines. The figure would be recognized regardless, but images in the following line would unintentionally be treated as figures as well. --- src/Text/Pandoc/Writers/Org.hs | 2 +- tests/writer.org | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Text/Pandoc/Writers/Org.hs b/src/Text/Pandoc/Writers/Org.hs index 283bb7d9c..85b7433e6 100644 --- a/src/Text/Pandoc/Writers/Org.hs +++ b/src/Text/Pandoc/Writers/Org.hs @@ -160,7 +160,7 @@ blockToOrg (Para [Image attr txt (src,'f':'i':'g':':':tit)]) = do then return empty else ("#+CAPTION: " <>) `fmap` inlineListToOrg txt img <- inlineToOrg (Image attr txt (src,tit)) - return $ capt $$ img + return $ capt $$ img $$ blankline blockToOrg (Para inlines) = do contents <- inlineListToOrg inlines return $ contents <> blankline diff --git a/tests/writer.org b/tests/writer.org index 100328c2c..b29a1865c 100644 --- a/tests/writer.org +++ b/tests/writer.org @@ -809,6 +809,7 @@ From "Voyage dans la Lune" by Georges Melies (1902): #+CAPTION: lalune [[lalune.jpg]] + Here is a movie [[movie.jpg]] icon. -------------- From dbf4d77091f8bcde6d43f0945baf4905c18aebfc Mon Sep 17 00:00:00 2001 From: Albert Krewinkel Date: Thu, 18 Aug 2016 14:32:59 +0200 Subject: [PATCH 3/3] Org writer: ensure link targets are paths or URLs Org-mode treats links as document internal searches unless the link target looks like a URL or file path, either relative or absolute. This change ensures that this is always the case. --- src/Text/Pandoc/Writers/Org.hs | 28 +++++++++++++++++++++++----- tests/writer.org | 4 ++-- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/src/Text/Pandoc/Writers/Org.hs b/src/Text/Pandoc/Writers/Org.hs index 85b7433e6..431223f7d 100644 --- a/src/Text/Pandoc/Writers/Org.hs +++ b/src/Text/Pandoc/Writers/Org.hs @@ -39,8 +39,8 @@ import Text.Pandoc.Shared import Text.Pandoc.Writers.Shared import Text.Pandoc.Pretty import Text.Pandoc.Templates (renderTemplate') -import Data.Char ( toLower ) -import Data.List ( intersect, intersperse, partition, transpose ) +import Data.Char ( isAlphaNum, toLower ) +import Data.List ( isPrefixOf, intersect, intersperse, partition, transpose ) import Control.Monad.State data WriterState = @@ -354,16 +354,34 @@ inlineToOrg (Link _ txt (src, _)) = do case txt of [Str x] | escapeURI x == src -> -- autolink do modify $ \s -> s{ stLinks = True } - return $ "[[" <> text x <> "]]" + return $ "[[" <> text (orgPath x) <> "]]" _ -> do contents <- inlineListToOrg txt modify $ \s -> s{ stLinks = True } - return $ "[[" <> text src <> "][" <> contents <> "]]" + return $ "[[" <> text (orgPath src) <> "][" <> contents <> "]]" inlineToOrg (Image _ _ (source, _)) = do modify $ \s -> s{ stImages = True } - return $ "[[" <> text source <> "]]" + return $ "[[" <> text (orgPath source) <> "]]" inlineToOrg (Note contents) = do -- add to notes in state notes <- get >>= (return . stNotes) modify $ \st -> st { stNotes = contents:notes } let ref = show $ (length notes) + 1 return $ " [" <> text ref <> "]" + +orgPath :: String -> String +orgPath src = + case src of + [] -> mempty -- wiki link + ('#':xs) -> xs -- internal link + _ | isUrl src -> src + _ | isFilePath src -> src + _ -> "file:" <> src + where + isFilePath :: String -> Bool + isFilePath cs = any (`isPrefixOf` cs) ["/", "./", "../", "file:"] + + isUrl :: String -> Bool + isUrl cs = + let (scheme, path) = break (== ':') cs + in all (\c -> isAlphaNum c || c `elem` (".-"::String)) scheme + && not (null path) diff --git a/tests/writer.org b/tests/writer.org index b29a1865c..6a86a4e3f 100644 --- a/tests/writer.org +++ b/tests/writer.org @@ -808,9 +808,9 @@ Auto-links should not occur here: == From "Voyage dans la Lune" by Georges Melies (1902): #+CAPTION: lalune -[[lalune.jpg]] +[[file:lalune.jpg]] -Here is a movie [[movie.jpg]] icon. +Here is a movie [[file:movie.jpg]] icon. --------------