2

背景:我正在研究哈姆雷特是如何工作的,有 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 = []
  }   

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

这是我希望修改的工作代码:

{-# 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>
|]
-------------------------------------------------------------------------------

提前感谢您的任何帮助、侮辱或其他评论。

4

1 回答 1

3

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

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

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

htmlDoc = [myHamlet|...|]

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

于 2012-12-03T08:39:45.847 回答