Muse writer: escape "]" brackets in URLs as "%5D"

This commit is contained in:
Alexander Krotov 2018-03-20 11:15:43 +03:00
parent 43677682d8
commit d63bba3066
2 changed files with 22 additions and 2 deletions

View file

@ -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

View file

@ -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" =: