~yoctocell/yoctocell.xyz

5a6846b9acba7013857e815187435743f96a78c7 — Xinglu Chen 1 year, 5 months ago 0cc0bf1
app: Generate post list for gemini
1 files changed, 25 insertions(+), 9 deletions(-)

M app/Main.hs
M app/Main.hs => app/Main.hs +25 -9
@@ 42,7 42,7 @@ context :: Context String
context = defaultContext `mappend` sourceContext

geminiContext :: Context String
geminiContext = defaultContext `mappend` geminiUrlContext
geminiContext = geminiUrlContext `mappend` titleField "title"

main :: IO ()
main = hakyllWith config $ do


@@ 56,17 56,24 @@ main = hakyllWith config $ do

  match "src/posts/*.gmi" $ do
    route srcRoute
    compile
      $   getResourceString
      >>= loadAndApplyTemplate "src/templates/default.gmi" context
    compile $ getResourceString >>= loadAndApplyTemplate
      "src/templates/default.gmi"
      (bodyField "body")

  -- TODO: Sort chronologically
  -- TODO: Use real title
  match "src/index.gmi" $ do
    route srcRoute
    compile $ do
      -- posts <- recentFirst =<< loadAll "src/posts/*.gmi"
      -- let geminiContext = listField "posts" geminiUrlContext (return posts) 
  
      getResourceString >>= loadAndApplyTemplate "src/templates/index.gmi" context
      posts <- loadAll "src/posts/*.gmi"
      let geminiContext =
            listField "posts" geminiContext (return posts)
              `mappend` (bodyField "body")
              `mappend` (titleField "title")
              `mappend` geminiUrlContext

      getResourceString
        >>= loadAndApplyTemplate "src/templates/index.gmi" geminiContext

  match (fromList ["src/about.org", "src/contact.org"]) $ do
    route $ setExtension' "html"


@@ 142,7 149,7 @@ feedCtx = postCtx <> bodyField "description"

-- | Return the url for the source code, this reads the @baseUrl@
-- variable.  Based on @urlField@.
sourceContext :: Context a
sourceContext :: Context String
sourceContext = field "source" $ \i -> do
  let id     = itemIdentifier i
      empty' = fail $ "No route url found for item " ++ show id


@@ 157,6 164,15 @@ geminiUrlContext = field "geminiUrl" $ \i -> do
      empty' = fail "No route url found for item " ++ show id
  mappend geminiUrl <$> maybe empty' toUrl <$> getRoute id

-- geminiTitleContext :: Context String
-- geminiTitleContext = title' "geminiTitle"
--  where
--   title' :: String -> Context a
--   title' = mapContext (fmap hyphenToSpace) . takeBaseName . pathField
--   hyphenToSpace :: Char -> Char
--   hyphenToSpace '-' = ' '
--   hyphenToSpace c   = c

-- | From org get metadatas.
orgCompiler :: Item String -> Compiler (Item String)
orgCompiler = pure . fmap (\s -> (metadatasToStr . orgMetadatas) s ++ s)