2017-11-02 24 views
6

Tak to działa:Jak uniknąć cyklu zależności podczas generowania listy najnowszych postów w postach?

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 

stworzyć listę ostatnich stanowisk archive.html; to jest standardowe, pochodzi z jednego z tutoriali, które myślę. Z wyjątkiem postsCtx, który jest odrobinę skomplikowany, ale nie powinien być tutaj istotny.

Jednak chcę umieścić listę kilku ostatnich wpisów na pasku bocznym normalnych postów. Problem polega na tym, że ostatnie posty kończą się w zależności od nich samych. Próbowałem wyłączyć wpis sam z wygenerowanej listy, ale nie mogłem znaleźć odpowiedniego miejsca. Oto co mam do tej pory:

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 powinny wytwarzają pole ze wszystkimi Niedawnych wyjątkiem samego, ale nie wydaje się działać lub jest to złe miejsce, aby to zrobić, ponieważ:

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 

Utknąłem do tej pory.

EDIT:

Widziałem Hakyll says "Dependency cycle detected: ...", że jest to loadPosts że to robi, więc próbowałem to:

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 

ale to po prostu staje mi:

[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 

w inny słowa kończę dwoma najnowszymi rowerami wokół siebie.

Odpowiedz

1

Działa na moją korzyść technika w https://stackoverflow.com/a/35651294/1461430 iw rzeczywistości działa lepiej dla mnie niż autor powiedział, że to zrobi. Oto stosowne fragmenty mojego kodu teraz:

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 

musiałem dodać listField i niedawne do kilku innych miejscach wykorzystywanych szablonów/default.html, ale to było proste.

miałem też zmodyfikować funkcję używany identyfikator patrzeć listę razy wyciągnął z 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 

nie mam dublowania plików w wyniku, i nie miałem problemu podejmowania linki do nowe pliki; wszystko po prostu działa.