git.iko.soy/typeable/pandoc
on master help sign in
Muse writer: escape "]" brackets in URLs as "%5D"
d63bba3Alexander Krotov
src/Text/Pandoc/Writers/Muse.hs+7−2
@@ -345,6 +345,11 @@ fixNotes (Space : n@Note{} : rest) = Str " " : n : fixNotes rest
345fixNotes (SoftBreak : n@Note{} : rest) = Str " " : n : fixNotes rest
345fixNotes (SoftBreak : n@Note{} : rest) = Str " " : n : fixNotes rest
346fixNotes (x:xs) = x : fixNotes xs
346fixNotes (x:xs) = x : fixNotes xs
347
347
348+urlEscapeBrackets :: String -> String
349+urlEscapeBrackets (']':xs) = '%':'5':'D':urlEscapeBrackets xs
350+urlEscapeBrackets (x:xs) = x:urlEscapeBrackets xs
351+urlEscapeBrackets [] = []
352+
348-- | Convert list of Pandoc inline elements to Muse.
353-- | Convert list of Pandoc inline elements to Muse.
349inlineListToMuse :: PandocMonad m
354inlineListToMuse :: PandocMonad m
350 => [Inline]
355 => [Inline]
@@ -402,7 +407,7 @@ inlineToMuse (Link _ txt (src, _)) =
402 return $ "[[" <> text (escapeLink x) <> "]]"
407 return $ "[[" <> text (escapeLink x) <> "]]"
403 _ -> do contents <- inlineListToMuse txt
408 _ -> do contents <- inlineListToMuse txt
404 return $ "[[" <> text (escapeLink src) <> "][" <> contents <> "]]"
409 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
406 -- Taken from muse-image-regexp defined in Emacs Muse file lisp/muse-regexps.el
411 -- Taken from muse-image-regexp defined in Emacs Muse file lisp/muse-regexps.el
407 imageExtensions = [".eps", ".gif", ".jpg", ".jpeg", ".pbm", ".png", ".tiff", ".xbm", ".xpm"]
412 imageExtensions = [".eps", ".gif", ".jpg", ".jpeg", ".pbm", ".png", ".tiff", ".xbm", ".xpm"]
408 isImageUrl = (`elem` imageExtensions) . takeExtension
413 isImageUrl = (`elem` imageExtensions) . takeExtension
@@ -419,7 +424,7 @@ inlineToMuse (Image attr inlines (source, title)) = do
419 let width = case dimension Width attr of
424 let width = case dimension Width attr of
420 Just (Percent x) | isEnabled Ext_amuse opts -> " " ++ show (round x :: Integer)
425 Just (Percent x) | isEnabled Ext_amuse opts -> " " ++ show (round x :: Integer)
421 _ -> ""
426 _ -> ""
422− return $ "[[" <> text (source ++ width) <> "]" <> title' <> "]"
427+ return $ "[[" <> text (urlEscapeBrackets source ++ width) <> "]" <> title' <> "]"
423inlineToMuse (Note contents) = do
428inlineToMuse (Note contents) = do
424 -- add to notes in state
429 -- add to notes in state
425 notes <- gets stNotes
430 notes <- gets stNotes
test/Tests/Writers/Muse.hs+15
@@ -372,6 +372,21 @@ tests = [ testGroup "block elements"
372 =?> "[[URL:1.png][Link to image]]"
372 =?> "[[URL:1.png][Link to image]]"
373 , "link to image without description" =: link "1.png" "" (str "1.png")
373 , "link to image without description" =: link "1.png" "" (str "1.png")
374 =?> "[[URL:1.png]]"
374 =?> "[[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+ ]
375 ]
390 ]
376 , "image" =: image "image.png" "Image 1" (str "") =?> "[[image.png][Image 1]]"
391 , "image" =: image "image.png" "Image 1" (str "") =?> "[[image.png][Image 1]]"
377 , "image with width" =:
392 , "image with width" =: