【问题标题】:Ordering a list alphabetically with Hakyll使用 Hakyll 按字母顺序排列列表
【发布时间】:2020-08-13 21:46:24
【问题描述】:

早些时候,我向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

【问题讨论】:

  • 为什么要使用这种复杂的一元排序?您可以从getItemPath 中删除return,返回一个普通的FilePath,这应该会让一切变得更容易。
  • 可以发一下bibList的实现吗?
  • @Bergi 我添加了bibList。至于排序,我真的没有理由——我对 Haskell 和 Hakyll 很陌生,而且大多是盲目地尝试。我将进一步研究以尝试使维护此站点更容易,很明显我正在尝试在走路之前跑步。

标签: haskell hakyll


【解决方案1】:

所需要的只是

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多