如何将哈姆雷特设置应用于哈姆雷特的quasiquote?

时间:2012-12-03 08:18:00

标签: haskell template-haskell hamlet

背景:我正在研究哈姆雷特如何使用WAI,但没有Yesod。我没有掌握模板Haskell,但在我深入研究之前,我想知道是否有一个已知/快速的解决方案来完成这项任务。

细节:我想知道如何在Hamlet quasiquote的上下文中更改NewlineStyle

探索:我认为它与看起来像这样的函数调用有关

hamletWithSettings 
htmlRules 
HamletSettings 
  {
      hamletDoctype             = "<!DOCTYPE html>"
     ,hamletNewlines            = DefaultNewlineStyle
     ,Hamlet.hamletCloseStyle   = htmlCloseStyle -- this fn is in a hidden module 
     ,Hamlet.hamletDoctypeNames = []
  }   

...但是,在quasiquote的上下文中,我不知道如何编写执行此操作的代码。

以下是工作代码,我希望修改:

{-# LANGUAGE OverloadedStrings #-} 
{-# LANGUAGE QuasiQuotes #-}

import Control.Monad.Trans.Resource
import Text.Hamlet                                  as Hamlet
import qualified Data.ByteString.Lazy.Char8         as ByteString
import qualified Network.Wai                        as Wai
import qualified Network.HTTP.Types                 as Http
import qualified Network.Wai.Handler.Warp           as Warp
import qualified Text.Blaze                         as Blaze 
import qualified Text.Blaze.Html.Renderer.String    as Blaze
-------------------------------------------------------------------------------
main :: IO ()
main = Warp.run 3000 application
-------------------------------------------------------------------------------
application :: Wai.Request -> ResourceT IO Wai.Response
application request = return $
    case (head $ Wai.pathInfo request) of
        "html" ->   
            Wai.responseLBS Http.status200  []  $ ByteString.pack 
                                                $ Blaze.renderHtml 
                                                $ htmlDoc -- defined below
        "d3" ->     
            Wai.ResponseFile Http.status200 [] "./d3.v2.min.js" Nothing
        _ ->        
            Wai.responseLBS Http.status400  [] "" 
-------------------------------------------------------------------------------
htmlDoc :: Hamlet.Html
htmlDoc = [shamlet|
!!!
<html>
    <head>
        <title>Study Graph
        <!-- <link rel="stylesheet" type="text/css" href="/css"> -->
        <script type="text/javascript" src="/d3" />
        <style>
        <script>
            window.onload = function()
            {
                svg = 
                    d3  .select("body")
                        .append("svg")
                        .attr("width", "100%")
                        .attr("height", "100%")
                /* SOLUTION to strawman: a solitary "\" on this code row, will render a single newline character; i.e. "\n\\\n" renders as "\n" */
                svg .selectAll("circle")
                    .data([ {"cx": 1.0, "cy": 1.1, "r":1}, 
                            {"cx": 2.0, "cy": 2.5, "r":0.9} ])

            }
    <body>
|]
-------------------------------------------------------------------------------

提前提前获取任何帮助,侮辱或其他评论。

1 个答案:

答案 0 :(得分:3)

基本思想是创建一个新的标识符,例如:

myHamlet = hamletWithSettings 
  htmlRules 
  (   HamletSettings 
    "<!DOCTYPE html>" 
    DefaultNewlineStyle htmlCloseStyle doctypeNames )

然后使用myHamlet代替hamlet,例如:

htmlDoc = [myHamlet|...|]

由于阶段限制,您可能需要在与您使用它的模块不同的模块中声明myHamlet