Skip to content

Instantly share code, notes, and snippets.

@mpickering
Created December 17, 2014 22:32
Show Gist options
  • Save mpickering/fdc747b9c8306659cb43 to your computer and use it in GitHub Desktop.
Save mpickering/fdc747b9c8306659cb43 to your computer and use it in GitHub Desktop.
Pandoc filter to insert non-breaking spaces.
module Main where
import Text.Pandoc.JSON
import Data.List (isPrefixOf)
import Data.Char (isAlphaNum)
main = toJSONFilter insertSpaces
insertSpaces :: Inline -> Inline
insertSpaces l@(Link [Str x] (url, tit))
| x == url = Link [Str (insertSpaces' x)] (url, tit)
| otherwise = l
insertSpaces el = el
insertSpaces' :: String -> String
insertSpaces' s = start ++ foldr go "" end
where
(start, end) = findSplit s
go c xs
| isAlphaNum c = c : xs
| otherwise = nb : c : nb : xs
nb = '\x8203'
findSplit :: String -> (String, String)
findSplit xss@(x:xs)
| "://" `isPrefixOf` xss = ("://",drop 3 xss)
| otherwise = let (f, b) = findSplit xs in (x:f, b)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment