gpt4 book ai didi

haskell - 如何将 HamletSettings 应用于 Hamlet 中的 quasiquote?

转载 作者:行者123 更新时间:2023-12-04 05:21:10 24 4
gpt4 key购买 nike

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

规范 : 我想知道如何更改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>
|]
-------------------------------------------------------------------------------

谢谢 提前获得任何帮助、侮辱或其他评论。

最佳答案

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

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

然后使用 myHamlet代替 hamlet ,例如:
htmlDoc = [myHamlet|...|]

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

关于haskell - 如何将 HamletSettings 应用于 Hamlet 中的 quasiquote?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13679433/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com