gpt4 book ai didi

haskell - Splice 是否支持 Unicode?

转载 作者:行者123 更新时间:2023-12-01 02:22:21 25 4
gpt4 key购买 nike

这是我的代码:

 testSplice :: C.Splice Handler
testSplice = return $ C.yieldRuntimeText $ do
return "中文"

我将它绑定(bind)到一个标签:
  splices :: Splices (C.Splice Handler)
splices =
"testSplice" ## testSplice

并在 layout.tpl 上使用它:
   <meta charset="UTF-8"> 
<testSplice/>

浏览器上的输出是
    -�

我做错了什么?

抱歉耽搁了,我忙了一段时间,现在我回来了,我想我的问题可能不够具体@mightybyte
这是出现问题的代码,我希望它会使问题更明确:

测试.hs:
{-# LANGUAGE OverloadedStrings #-}
import Snap
import Heist
import qualified Heist.Compiled as C
import Data.Monoid
import Control.Monad.Trans.Either
import Data.Maybe

main :: IO ()
main = quickHttpServe site

site :: Snap ()
site =
route [("/", testSnap)]

testSnap :: Snap ()
testSnap = do
hs <- liftIO $ load "template" splices
let runtime = fromJust $ C.renderTemplate hs "test"
builder <-liftIO (fst runtime)
writeBuilder builder
where
splices :: Splices (C.Splice IO)
splices =
"testSplice" ## testSplice

load :: MonadIO n
=> FilePath
-> Splices (C.Splice n)
-> IO (HeistState n)
load baseDir splices = do
tmap <- runEitherT $ do
let t = loadTemplates baseDir
hc = HeistConfig
defaultInterpretedSplices
defaultLoadTimeSplices
splices
mempty
[t]
initHeist hc
either (error . concat) return tmap

testSplice :: C.Splice IO
testSplice = return $ C.yieldRuntimeText $ do return "中文"

模板/test.tpl
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<testSplice/>
</body>
</html>

现在我尝试了 heist-0.13.0.2,它现在工作正常,Daniel 干得好!

最佳答案

更新:此答案中描述的问题已在 heist 中得到纠正。版本 0.13.0.2 .

source code对于 yieldRuntimeText是:

yieldRuntimeText :: Monad n => RuntimeSplice n Text -> DList (Chunk n)
yieldRuntimeText = yieldRuntime . liftM fromText

来自哪个模块 fromText功能?在导入部分我们发现:
import           Blaze.ByteString.Builder
import Blaze.ByteString.Builder.Char8

documentation对于后一个包说:

Note: This package is intended for low-level use like implementing protocols. If you need to serialize Unicode characters use one of the UTF encodings (e.g. 'Blaze.ByteString.Builder.Char.UTF-8').



并且:
fromText :: Text -> BuilderSource

O(n). Serialize the lower 8-bits of all characters in the strict text.

嗯,所以问题可能是从 Text 到 Builder 的编码不是在 UTF-8 中完成的?尝试使用完全相同的代码定义您自己的 yieldRuntimeText 版本,但改用以下导入:
import           Blaze.ByteString.Builder
import Blaze.ByteString.Builder.Char.Utf8

yieldRuntimeTextUtf8 :: Monad n => RuntimeSplice n Text -> DList (Chunk n)
yieldRuntimeTextUtf8 = yieldRuntime . liftM fromText

关于haskell - Splice 是否支持 Unicode?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19329857/

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