haskell – 如何在帖子上生成最近帖子列表时避免依赖循环?
|
这样可行: create ["archive.html"] $do
route idRoute
compile $do
posts <- (myRecentFirst gitTimes) =<< loadAll "posts/**"
let archiveCtx =
listField "posts" (postCtx allTags allCategories gitTimes) (return posts) `mappend`
constField "title" "Archives" `mappend`
(postCtx allTags allCategories gitTimes)
makeItem ""
>>= loadAndApplyTemplate "templates/archive.html" archiveCtx
>>= loadAndApplyTemplate "templates/default.html" archiveCtx
>>= relativizeUrls
在archive.html中创建最近的帖子列表;这是沼泽标准,它来自我认为的其中一个教程.除了postsCtx,这有点复杂,但在这里不应该相关. 但是,我想在正常帖子的侧边栏中列出一些最近的帖子.问题在于最近的帖子最终取决于他们自己.我试着从自己生成的列表中排除帖子本身,但我找不到一个好的地方去做.这是我到目前为止所得到的: match "posts/**" $do
route $(gsubRoute "posts/" (const "")) `composeRoutes` setExtension "html"
compile $do
recents <- (myRecentFirst gitTimes) =<< loadAll "posts/**"
let postsContext = postCtx allTags allCategories gitTimes `mappend`
-- Show recent posts
recentsNotSelfField "recents" (postCtx allTags allCategories gitTimes) (return $take 3 recents)
pandocCompilerWithTransform hblogPandocReaderOptions hblogPandocWriterOptions (titleFixer titles)
>>= loadAndApplyTemplate "templates/post.html" postsContext
>>= loadAndApplyTemplate "templates/default.html" postsContext
>>= relativizeUrls
recentsNotSelfField :: String -> Context a -> Compiler [Item a] -> Context b
recentsNotSelfField key context items = Context $k _ i ->·
if k == key then do
let myId = itemIdentifier i
strippedItems <- items
let remains = filter (x -> (itemIdentifier x) /= myId) strippedItems
return $ListField context remains
else
CA.empty
recentsNotSelfField应该生成一个除了本身以外的所有最近的字段,但它似乎没有工作或者它是错误的地方,因为: Initialising... Creating store... Creating provider... Running rules... Checking for out-of-date items Compiling [ERROR] Hakyll.Core.Runtime.chase: Dependency cycle detected: posts/computing/contact.md depends on posts/computing/contact.md 到目前为止,我被困住了. 编辑: 我看到Hakyll says “Dependency cycle detected: …”,这是loadPosts做的,所以我尝试了这个: match "posts/**" $do
route $(gsubRoute "posts/" (const "")) `composeRoutes` setExtension "html"
compile $do
myId <- getUnderlying
recents <- (myRecentFirst gitTimes) =<< loadAll ("posts/**" .&&. complement (fromList [myId]))
let postsContext = postCtx allTags allCategories gitTimes `mappend`
-- Show recent posts
listField "recents" (postCtx allTags allCategories gitTimes) (return $take 3 recents)
pandocCompilerWithTransform hblogPandocReaderOptions hblogPandocWriterOptions (titleFixer titles)
>>= loadAndApplyTemplate "templates/post.html" postsContext
>>= loadAndApplyTemplate "templates/default.html" postsContext
>>= relativizeUrls
但这只是让我: [ERROR] Hakyll.Core.Runtime.chase: Dependency cycle detected: posts/computing/contact.md depends on posts/computing/general/ched.md depends on posts/computing/contact.md 换句话说,最终我最近两次骑自行车. 原来我在 https://stackoverflow.com/a/35651294/1461430工作的技术对我来说,事实上它对我来说比作者说它会更好.这是我的代码的相关位现在:match "posts/**" $version "recents" $do
route $(gsubRoute "posts/" (const "")) `composeRoutes` setExtension "html"
compile $do
pandocCompilerWithTransform hblogPandocReaderOptions hblogPandocWriterOptions (titleFixer titles)
>>= loadAndApplyTemplate "templates/post.html" (postCtx allTags allCategories gitTimes)
>>= relativizeUrls
match "posts/**" $do
route $(gsubRoute "posts/" (const "")) `composeRoutes` setExtension "html"
compile $do
myId <- getUnderlying
recents <- (myRecentFirst gitTimes) =<< loadAll ("posts/**" .&&. hasVersion "recents")
let postsContext = postCtx allTags allCategories gitTimes `mappend`
-- Show recent posts
listField "recents" (postCtx allTags allCategories gitTimes) (return $take 3 recents)
pandocCompilerWithTransform hblogPandocReaderOptions hblogPandocWriterOptions (titleFixer titles)
>>= loadAndApplyTemplate "templates/post.html" postsContext
>>= loadAndApplyTemplate "templates/default.html" postsContext
>>= relativizeUrls
我不得不将listField和recents添加到使用templates / default.html的其他几个地方,但这很简单. 我还必须修改一个使用Identifier来查找从git中提取的时间列表的函数: -- Pull a file's time out of the list
getGitTimeUTC :: Identifier -> [GitTimes] -> (GitTimes -> UTCTime) -> UTCTime
getGitTimeUTC ident times typeF =
-- The identifier for the things compiled with the "recents"
-- version has the identifierVersion "recents",but we don't care
-- about that since the only reason that exists is to avoid loops,-- so we strip it here for our lookup.
let fixedIdent = ident { identifierVersion = Nothing }
timeList = filter (x -> fixedIdent == (gtid x)) times in
if length timeList /= 1 then
-- It's not obvious to me how this could occur even in theory; I'd expect it to error out during getGitTimes
error $"getGitTimeUTC: Couldn't find the time for " ++ (show fixedIdent) ++ " in GitTimes list " ++ (show times)
else
typeF $head timeList
结果我没有重复的文件,并且没有问题链接到最近的文件;一切正常. (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
