git.iko.soy/typeable/pandoc
on master help sign in
Muse writer: escape "]" brackets in URLs as "%5D"
commitd63bba30661182f15e56d07997ee0f7b539eb2ce authorAlexander Krotov <ilabdsf@gmail.com> dateTue 20 Mar 2018, 11:15 (9 years ago) parent4367768
2 files changed+22−2
src/Text/Pandoc/Writers/Muse.hs+7−2 test/Tests/Writers/Muse.hs+15
src/Text/Pandoc/Writers/Muse.hs+7−2
@@ -345,6 +345,11 @@ fixNotes (Space : n@Note{} : rest) = Str " " : n : fixNotes rest
345345fixNotes (SoftBreak : n@Note{} : rest) = Str " " : n : fixNotes rest
346346fixNotes (x:xs) = x : fixNotes xs
347347
348+urlEscapeBrackets :: String -> String
349+urlEscapeBrackets (']':xs) = '%':'5':'D':urlEscapeBrackets xs
350+urlEscapeBrackets (x:xs) = x:urlEscapeBrackets xs
351+urlEscapeBrackets [] = []
352+
348353-- | Convert list of Pandoc inline elements to Muse.
349354inlineListToMuse :: PandocMonad m
350355 => [Inline]
@@ -402,7 +407,7 @@ inlineToMuse (Link _ txt (src, _)) =
402407 return $ "[[" <> text (escapeLink x) <> "]]"
403408 _ -> do contents <- inlineListToMuse txt
404409 return $ "[[" <> text (escapeLink src) <> "][" <> contents <> "]]"
405− where escapeLink lnk = if isImageUrl lnk then "URL:" ++ lnk else lnk
410+ where escapeLink lnk = if isImageUrl lnk then "URL:" ++ urlEscapeBrackets lnk else urlEscapeBrackets lnk
406411 -- Taken from muse-image-regexp defined in Emacs Muse file lisp/muse-regexps.el
407412 imageExtensions = [".eps", ".gif", ".jpg", ".jpeg", ".pbm", ".png", ".tiff", ".xbm", ".xpm"]
408413 isImageUrl = (`elem` imageExtensions) . takeExtension
@@ -419,7 +424,7 @@ inlineToMuse (Image attr inlines (source, title)) = do
419424 let width = case dimension Width attr of
420425 Just (Percent x) | isEnabled Ext_amuse opts -> " " ++ show (round x :: Integer)
421426 _ -> ""
422− return $ "[[" <> text (source ++ width) <> "]" <> title' <> "]"
427+ return $ "[[" <> text (urlEscapeBrackets source ++ width) <> "]" <> title' <> "]"
423428inlineToMuse (Note contents) = do
424429 -- add to notes in state
425430 notes <- gets stNotes
test/Tests/Writers/Muse.hs+15
@@ -372,6 +372,21 @@ tests = [ testGroup "block elements"
372372 =?> "[[URL:1.png][Link to image]]"
373373 , "link to image without description" =: link "1.png" "" (str "1.png")
374374 =?> "[[URL:1.png]]"
375+
376+ , testGroup "escape brackets in links"
377+ [ "link with description"
378+ =: link "https://example.com/foo].txt" "" (str "Description")
379+ =?> "[[https://example.com/foo%5D.txt][Description]]"
380+ , "link without description"
381+ =: link "https://example.com/foo].txt" "" (str "https://example.com/foo].txt")
382+ =?> "[[https://example.com/foo%5D.txt][<verbatim>https://example.com/foo].txt</verbatim>]]"
383+ , "image link with description"
384+ =: link "foo]bar.png" "" (str "Image link")
385+ =?> "[[URL:foo%5Dbar.png][Image link]]"
386+ , "image link without description"
387+ =: link "foo]bar.png" "" (str "foo]bar.png")
388+ =?> "[[URL:foo%5Dbar.png][<verbatim>foo]bar.png</verbatim>]]"
389+ ]
375390 ]
376391 , "image" =: image "image.png" "Image 1" (str "") =?> "[[image.png][Image 1]]"
377392 , "image with width" =: