Created
December 17, 2014 22:32
-
-
Save mpickering/fdc747b9c8306659cb43 to your computer and use it in GitHub Desktop.
Pandoc filter to insert non-breaking spaces.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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