使用Hakyll按字母顺序排列列表

问题描述

我早些时候问过question关于在给定页面上呈现两个独立列表的问题,对此很好地回答。现在,我希望按字母顺序排列该列表,并显示列表中的所有项目(我相信我可以看到该怎么做,但我一直专注于第一个问题。)

在尝试执行此操作时,经过几次失败的尝试(实际上与here相同,因为它比我的代码要优雅得多),我想出了类似的东西:

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

解决方法

所有需要的是

alphaOrder :: [Item a] -> Compiler [Item a]
alphaOrder items = return $
              sortBy (comparing (takeBaseName . toFilePath . itemIdentifier)) items