The code escaper I use for this blog
Edit: numeric HTML escaping added (not Unicode compliant); minor refactoring (readFile and writeFile improvement, some renames)
Previously, I had two bash scripts for this task, but ironically, I had escaping problems within them, so I've decided to go for the clean, well-written solution below. I usually patch issues like this in my scripts, but sometimes it just isn't worth it. Fixing these is more akin to juggling, than to real bug fixing. So this is what I use from now on:
Previously, I had two bash scripts for this task, but ironically, I had escaping problems within them, so I've decided to go for the clean, well-written solution below. I usually patch issues like this in my scripts, but sometimes it just isn't worth it. Fixing these is more akin to juggling, than to real bug fixing. So this is what I use from now on:
import Prelude hiding(readFile,writeFile)
import qualified Prelude(readFile,writeFile)
import System.Environment(getArgs)
import Data.List(group)
import Data.Char(ord)
data FileName = FileName String
readFile (FileName s) = Prelude.readFile s
writeFile (FileName s) d = Prelude.writeFile s d
toHtml = concatMap code where
code '&' = "&"
code '<' = "<"
code '>' = ">"
code c | ord(c)<32 || ord(c)>126 = "&#" ++ show (ord c) ++ ";"
code c = [c]
blankNbsp = concatMap (\' ' -> " ")
multiNbsp = concatMap inner . group where
inner bl@(' ':' ':_) = blankNbsp bl
inner x = x
eachLine s = blankNbsp init ++ multiNbsp tail where
(init,tail) = span (==' ') s
tr = unlines . map (eachLine . toHtml) . lines where
trf fi fo = do
i <- readFile fi
writeFile fo (tr i)
main = do
args <- getArgs
case args of
[fi@(_:_),fo@(_:_)] -> trf (FileName fi) (FileName fo)
otherwise -> fail "params: inputfile outputfile"
Comments
Post a Comment