Skip to content

Commit

Permalink
Add atom feed
Browse files Browse the repository at this point in the history
  • Loading branch information
jvanbruegge committed Mar 23, 2024
1 parent e606896 commit b522150
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
13 changes: 11 additions & 2 deletions src/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,12 @@ getRendered _ = error "Could not find content key in object"
buildRules :: Action ()
buildRules = do
allPosts <- mapP buildPost =<< getDirectoryFiles "." ["articles//*.md"]
buildPostList Nothing allPosts
sortedPosts <- buildPostList Nothing allPosts
buildAtomFeed (take 15 sortedPosts)

let allTags = Set.toList $ foldr (Set.union . Set.fromList . tags) Set.empty allPosts
void . forP allTags $ \tag -> buildPostList (Just (tag, "tags" </> tag)) (filter (elem tag . tags) allPosts)

copyStaticFiles

buildPost :: FilePath -> Action Post
Expand All @@ -96,7 +99,7 @@ buildPost srcPath = cacheAction ("build" :: T.Text, srcPath) $ do
writeFile' (outputFolder </> T.unpack postUrl </> "index.html") . T.unpack $ rendered
convert $ A.Object postData'

buildPostList :: Maybe (String, FilePath) -> [Post] -> Action ()
buildPostList :: Maybe (String, FilePath) -> [Post] -> Action [Post]
buildPostList tag posts = do
let posts' = sortOn (Down . date) posts
let postData = A.Object $ KM.fromList $
Expand All @@ -105,6 +108,12 @@ buildPostList tag posts = do
] <> maybe [] (\(t, _) -> [(fromText "tag", A.String (T.pack t))]) tag
rendered <- getRendered <$> renderTemplates postData ["postList.html", "shell.html"]
writeFile' (outputFolder <> maybe "" snd tag </> "index.html") . T.unpack $ rendered
pure posts'

buildAtomFeed :: [Post] -> Action ()
buildAtomFeed posts = do
rendered <- getRendered <$> renderTemplates (A.Object $ KM.fromList [(fromText "posts", A.toJSON posts)]) ["atom.xml"]
writeFile' (outputFolder </> "atom.xml") . T.unpack $ rendered

copyStaticFiles :: Action ()
copyStaticFiles = do
Expand Down
21 changes: 21 additions & 0 deletions templates/atom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

<title>Programming &amp; Proving</title>
<link href="https://jvanbruegge.github.io/blog/"/>
<updated>{{updated}}</updated>
<author>
<name>Jan van Brügge</name>
</author>
<id>urn:uuid:52dd550f-4e75-420d-a0a1-e501a1dec9fe</id>

{{#posts}}
<entry>
<title>{{title}}</title>
<link href="https://jvanbruegge.github.io/blog/{{url}}"/>
<id>https://jvanbruegge.github.io/blog/{{url}}</id>
<updated>{{date}}</updated>
<summary>{{description}}</summary>
</entry>
{{/posts}}
</feed>
9 changes: 6 additions & 3 deletions templates/shell.html
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" href="{{prefix}}/main.css" />
<title>Programming &amp; Proving by Jan van Brügge</title>
<link rel="stylesheet" type="text/css" href="{{prefix}}/main.css" />
<link rel="alternate" type="application/atom+xml" title="Atom Feed" href="{{prefix}}/atom.xml" />
</head>
<body>
<header>
<nav id="main-menu">
<a href="{{prefix}}">Programming & Proving</a>
<a href="{{prefix}}">Programming &amp; Proving</a>
<ul>
<li><a href="{{prefix}}">Blog</a></li>
<li><a href="{{prefix}}/feed.xml">RSS</a></li>
</ul>
</nav>
</header>
Expand All @@ -20,6 +22,7 @@
<footer>
<span>© 2020-2024, Jan van Brügge. Opinions are my own.</span>
<span>All articles licensed under <a href="{{prefix}}/LICENSE">CC BY-SA 4.0</a></span>
<span>Feeds are available via <a href="{{prefix}}/atom.xml" rel="nofollow">Atom</a></span>
</footer>
</body>
</html>

0 comments on commit b522150

Please sign in to comment.