From d63bba30661182f15e56d07997ee0f7b539eb2ce Mon Sep 17 00:00:00 2001 From: Alexander Krotov <ilabdsf@gmail.com> Date: Tue, 20 Mar 2018 11:15:43 +0300 Subject: [PATCH] Muse writer: escape "]" brackets in URLs as "%5D" --- src/Text/Pandoc/Writers/Muse.hs | 9 +++++++-- test/Tests/Writers/Muse.hs | 15 +++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/Text/Pandoc/Writers/Muse.hs b/src/Text/Pandoc/Writers/Muse.hs index 2d53ca9a1..e72fcf52b 100644 --- a/src/Text/Pandoc/Writers/Muse.hs +++ b/src/Text/Pandoc/Writers/Muse.hs @@ -345,6 +345,11 @@ fixNotes (Space : n@Note{} : rest) = Str " " : n : fixNotes rest fixNotes (SoftBreak : n@Note{} : rest) = Str " " : n : fixNotes rest fixNotes (x:xs) = x : fixNotes xs +urlEscapeBrackets :: String -> String +urlEscapeBrackets (']':xs) = '%':'5':'D':urlEscapeBrackets xs +urlEscapeBrackets (x:xs) = x:urlEscapeBrackets xs +urlEscapeBrackets [] = [] + -- | Convert list of Pandoc inline elements to Muse. inlineListToMuse :: PandocMonad m => [Inline] @@ -402,7 +407,7 @@ inlineToMuse (Link _ txt (src, _)) = return $ "[[" <> text (escapeLink x) <> "]]" _ -> do contents <- inlineListToMuse txt return $ "[[" <> text (escapeLink src) <> "][" <> contents <> "]]" - where escapeLink lnk = if isImageUrl lnk then "URL:" ++ lnk else lnk + where escapeLink lnk = if isImageUrl lnk then "URL:" ++ urlEscapeBrackets lnk else urlEscapeBrackets lnk -- Taken from muse-image-regexp defined in Emacs Muse file lisp/muse-regexps.el imageExtensions = [".eps", ".gif", ".jpg", ".jpeg", ".pbm", ".png", ".tiff", ".xbm", ".xpm"] isImageUrl = (`elem` imageExtensions) . takeExtension @@ -419,7 +424,7 @@ inlineToMuse (Image attr inlines (source, title)) = do let width = case dimension Width attr of Just (Percent x) | isEnabled Ext_amuse opts -> " " ++ show (round x :: Integer) _ -> "" - return $ "[[" <> text (source ++ width) <> "]" <> title' <> "]" + return $ "[[" <> text (urlEscapeBrackets source ++ width) <> "]" <> title' <> "]" inlineToMuse (Note contents) = do -- add to notes in state notes <- gets stNotes diff --git a/test/Tests/Writers/Muse.hs b/test/Tests/Writers/Muse.hs index acd834173..2728fd9f9 100644 --- a/test/Tests/Writers/Muse.hs +++ b/test/Tests/Writers/Muse.hs @@ -372,6 +372,21 @@ tests = [ testGroup "block elements" =?> "[[URL:1.png][Link to image]]" , "link to image without description" =: link "1.png" "" (str "1.png") =?> "[[URL:1.png]]" + + , testGroup "escape brackets in links" + [ "link with description" + =: link "https://example.com/foo].txt" "" (str "Description") + =?> "[[https://example.com/foo%5D.txt][Description]]" + , "link without description" + =: link "https://example.com/foo].txt" "" (str "https://example.com/foo].txt") + =?> "[[https://example.com/foo%5D.txt][<verbatim>https://example.com/foo].txt</verbatim>]]" + , "image link with description" + =: link "foo]bar.png" "" (str "Image link") + =?> "[[URL:foo%5Dbar.png][Image link]]" + , "image link without description" + =: link "foo]bar.png" "" (str "foo]bar.png") + =?> "[[URL:foo%5Dbar.png][<verbatim>foo]bar.png</verbatim>]]" + ] ] , "image" =: image "image.png" "Image 1" (str "") =?> "[[image.png][Image 1]]" , "image with width" =: