早些时候我问了一个关于在给定页面上呈现两个独立列表的问题,得到了很好的回答。现在,我希望按字母顺序排列该列表 - 并显示列表中的所有项目(我相信我可以看到如何做,但我一直专注于第一个问题。)
在尝试这样做时,我在几次失败的尝试后想出了类似的东西(这实际上与这里相同,因为它比我的代码更优雅):
alphaOrder :: MonadMetadata m => [Item a] -> m [Item a]
alphaOrder =
sortByM $ getItemPath . itemIdentifier
where
sortByM :: (Monad m, Ord k) => (a -> m k) -> [a] -> m [a]
sortByM f xs = liftM (map fst . sortBy (comparing snd)) $
mapM (\x -> liftM (x,) (f x)) xs
getItemPath :: MonadMetadata m
=> Identifier -- ^ Input page
-> m FilePath -- ^ Parsed UTCTime
getItemPath id' = return $ toFilePath id'
-- | page-id order
data ItemTree a = ItemTree (Item a) [ItemTree a] String String
type ItemPath a = ( Item a, [FilePath])
itemPath :: [ Item a] -> [ ItemPath a]
itemPath =
map (\i -> ( i, splitDirectories
. dropExtensions . toFilePath . itemIdentifier $ i) )
我试图这样申请:
create ["bibs.html"] $ do
route idRoute
compile $ do
list <- bibList tags "bibs/*" alphaOrder
makeItem list
>>= loadAndApplyTemplate "templates/posts.html" allBibsCtx
>>= loadAndApplyTemplate "templates/default.html" allBibsCtx
>>= relativizeUrls
-- Index
create ["index.html"] $ do
route idRoute
compile $ do
let mkposts = postList tags "posts/*" (fmap (Prelude.take 10) . recentFirst)
mkbibs = bibList tags "bibs/*" (fmap (Prelude.take 10) . alphaOrder)
homeCtx' = field "posts" (const mkposts)
<> field "bibs" (const mkbibs)
<> homeCtx
makeItem ""
>>= loadAndApplyTemplate "templates/index.html" homeCtx'
>>= loadAndApplyTemplate "templates/default.html" homeCtx'
>>= relativizeUrls
但是,在尝试构建时,出现错误:
error: Illegal tuple section: use TupleSections
|
55 | mapM (\x -> liftM (x,) (f x)) xs
| ^^^^
所以现在我启用了 TupleSections 并且它构建了 - 但列表仍然没有按字母顺序呈现。
我究竟做错了什么?
提前谢谢你,如果我能澄清任何事情,请告诉我。
编辑:
bibList 的实现:
bibList :: Tags -> Pattern -> ([Item String] -> Compiler [Item String]) -> Compiler String
bibList tags pattern preprocess' = do
postItemTpl <- loadBody "templates/postitem.html"
posts <- preprocess' =<< loadAll pattern
applyTemplateList postItemTpl (tagsCtx tags) posts