Commits

Luke Plant  committed 2ed8eb5

Switched to ByteString StringTemplates

  • Participants
  • Parent commits 4286ca8

Comments (0)

Files changed (5)

File src/Blog/Templates.hs

 where
 
 import Blog.Links
-import Blog.Utils (escapeHtmlStringT)
+import Blog.Utils (escapeHtmlStringBS)
 import Data.Maybe (fromJust)
 import Data.Time.Clock.POSIX (posixSecondsToUTCTime)
 import Data.Time.Format (formatTime)
 
 -- Templates
 
-get_templates :: IO (STGroup LT.Text)
+get_templates :: IO (STGroup UTF8.ByteString)
 get_templates = do
   g1 <- directoryGroup Settings.template_path
-  let g2 = setEncoderGroup escapeHtmlStringT g1
-      g3 = groupStringTemplates [("noescape", newSTMP "$it$" :: StringTemplate LT.Text)]
+  let g2 = setEncoderGroup escapeHtmlStringBS g1
+      g3 = groupStringTemplates [("noescape", newSTMP "$it$" :: StringTemplate UTF8.ByteString)]
       g4 = mergeSTGroups g2 g3
   return g4
 
-get_template :: String -> IO (StringTemplate LT.Text)
+get_template :: String -> IO (StringTemplate UTF8.ByteString)
 get_template name = do
   templates <- get_templates
   return $ fromJust $ getStringTemplate name templates

File src/Blog/Utils.hs

                     repl "\'" "&#39;" .
                     repl "&" "&amp;"
     where repl x y = replaceLT (LT.pack x) (LT.pack y)
+
+-- | Replace a ByteString in a ByteString with another ByteString
+replaceBS find repl src
+    | LB.null src = src
+    | otherwise = let l = LB.length find
+                  in if LB.take (fromIntegral l) src == find
+                     then LB.append repl (replaceBS find repl (LB.drop (fromIntegral l) src))
+                     else LB.cons (LB.head src) (replaceBS find repl (LB.tail src))
+
+escapeHtmlStringBS = repl "<" "&lt;" .
+                     repl ">" "&gt;" .
+                     repl "\"" "&quot;" .
+                     repl "\'" "&#39;" .
+                     repl "&" "&amp;"
+    where repl x y = replaceBS (LB.pack x) (LB.pack y)

File src/Blog/Views.hs

                              ] utf8HtmlResponse
 
 -- | Standard response, taking a Request and StringTemplate Text as input
-standardResponseTT :: Request -> StringTemplate LT.Text -> Response
+standardResponseTT :: Request -> StringTemplate LB.ByteString -> Response
 standardResponseTT req template =
     let csrffield = mkCsrfField req
         t2 = setAttribute "csrffield" csrffield template
-        rendered = (LT.encodeUtf8 $ render t2)
+        rendered = render t2
     in buildResponse [ addContent rendered
                      ] utf8HtmlResponse
 

File src/templates/comment.st

     <div class="comment$if(comment.isAuthor)$ author$endif$$if(comment.hidden)$ hiddenComment$endif$" id="commentdiv$comment.uid$">
       <div class="togglebar" $if(comment.hidden)$ title="Hidden comment" $endif$></div>
       <div class="introline">
-        <a id="comment$comment.uid$" href="#comment$comment.uid$" class="anchor">§</a> <span>On <span class="timestamp">$comment.date$</span>, <span class="commentBy">$comment.formattedName$</span> wrote:</span>
+        <a id="comment$comment.uid$" href="#comment$comment.uid$" class="anchor">&sect;</a> <span>On <span class="timestamp">$comment.date$</span>, <span class="commentBy">$comment.formattedName$</span> wrote:</span>
       </div>
       <div class="commenttext">
       <span class="commentid">$comment.uid$</span>
       </div>
       <div class="response">
         $if(comment.response)$
-        <div class="responseinner">$comment.response$ <span class="responsename"> luke</span> </div>
+        <div class="responseinner">$comment.response$ <span class="responsename">&mdash; luke</span> </div>
         $endif$
       </div>
       <div class="commentControls">

File src/templates/pagestart.st

     <!--[if lte IE 6]><link rel="stylesheet" href="/newblog_IE6.css" type="text/css"><![endif]-->
     <title>
       $if(pagetitle)$
-        $pagetitle$ « All Unkept
+        $pagetitle$ &laquo; All Unkept
       $else$
         All Unkept
       $endif$
                         response: \$("#responseinput").val()
                       },
                       function(data) {
-                         \$("#commentdiv" + cid.toString()).find(".response").html('<div class="responseinner">' + data + ' <span class="responsename">— luke</span></div>');
+                         \$("#commentdiv" + cid.toString()).find(".response").html('<div class="responseinner">' + data + ' <span class="responsename">&mdash; luke</span></div>');
                       }
                     )
                     \$("#responsebox").hide();