-- | Shake action for generating the static site
generateSite :: Action ()
generateSite = do
  -- Copy over the static files
  Rib.buildStaticFiles [ "assets/**"
                       , "images/**"
                       , "projects/**"
                       , "presentations/**"
                       ]
  let writeHtmlRoute :: Route a -> a -> Action ()
      writeHtmlRoute r = Rib.writeRoute r . Lucid.renderText . renderPage r
      writeXmlRoute r = Rib.writeRoute r . RSS.renderFeed . toPosts r
  -- Build individual sources, generating .html for each.
  articles <-
    Rib.forEvery ["posts/*.md"] $ \srcPath -> do
      let r = Route_Article $ cleanPath srcPath
      doc <- Pandoc.parse Pandoc.readMarkdown srcPath
      writeHtmlRoute r doc
      pure (r, doc)
  writeHtmlRoute Route_CV articles
  writeHtmlRoute Route_Tags $ groupByTag articles
  writeHtmlRoute Route_Index $ reverse articles
  writeXmlRoute Route_Feed $ articles
  where
    cleanPath path = drop 6 (take (length path - 3) path)
    groupByTag as =
      Map.fromListWith (<>) $ flip concatMap as $ \(r, doc) ->
        (,[(r, doc)]) <$> tags (getMeta doc)

-- | Convert the posts we've read into Post types that can be read
-- by the RSS/Atom module.
generateSite :: Action ()
generateSite = do
  -- Copy over the static files
  Rib.buildStaticFiles [ "assets/**"
                       , "images/**"
                       , "projects/**"
                       , "presentations/**"
                       ]
  let writeHtmlRoute :: Route a -> a -> Action ()
      writeHtmlRoute r = Rib.writeRoute r . Lucid.renderText . renderPage r
      writeXmlRoute r = Rib.writeRoute r . RSS.renderFeed . toPosts r
  -- Build individual sources, generating .html for each.
  articles <-
    Rib.forEvery ["posts/*.md"] $ \srcPath -> do
      let r = Route_Article $ cleanPath srcPath
      doc <- Pandoc.parse Pandoc.readMarkdown srcPath
      writeHtmlRoute r doc
      pure (r, doc)
  writeHtmlRoute Route_CV articles
  writeHtmlRoute Route_Tags $ groupByTag articles
  writeHtmlRoute Route_Index $ reverse articles
  writeXmlRoute Route_Feed $ articles
  where
    cleanPath path = drop 6 (take (length path - 3) path)
    groupByTag as =
      Map.fromListWith (<>) $ flip concatMap as $ \(r, doc) ->
        (,[(r, doc)]) <$> tags (getMeta doc)

-- | Convert the posts we've read into Post types that can be read
-- by the RSS/Atom module.
generateSite = do
  -- Copy over the static files
  Rib.buildStaticFiles [ "assets/**"
                       , "images/**"
                       , "projects/**"
                       , "presentations/**"
                       ]
  let writeHtmlRoute :: Route a -> a -> Action ()
      writeHtmlRoute r = Rib.writeRoute r . Lucid.renderText . renderPage r
      writeXmlRoute r = Rib.writeRoute r . RSS.renderFeed . toPosts r
  -- Build individual sources, generating .html for each.
  articles <-
    Rib.forEvery ["posts/*.md"] $ \srcPath -> do
      let r = Route_Article $ cleanPath srcPath
      doc <- Pandoc.parse Pandoc.readMarkdown srcPath
      writeHtmlRoute r doc
      pure (r, doc)
  writeHtmlRoute Route_CV articles
  writeHtmlRoute Route_Tags $ groupByTag articles
  writeHtmlRoute Route_Index $ reverse articles
  writeXmlRoute Route_Feed $ articles
  where
    cleanPath path = drop 6 (take (length path - 3) path)
    groupByTag as =
      Map.fromListWith (<>) $ flip concatMap as $ \(r, doc) ->
        (,[(r, doc)]) <$> tags (getMeta doc)

-- | Convert the posts we've read into Post types that can be read
-- by the RSS/Atom module.