Skip to content

Commit 5a05be8

Browse files
Adding RSS integration (#5)
* Adding RSS integration * where is the date * action jackson * action jackson
1 parent 67133d8 commit 5a05be8

File tree

11 files changed

+120
-40
lines changed

11 files changed

+120
-40
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
strategy:
1414
matrix:
1515
ruby:
16-
- '3.2.0'
16+
- '3.4.0'
1717
steps:
1818
- uses: actions/checkout@v3
1919
- name: Set up Ruby

CHANGELOG.md

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,72 +5,76 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to
66
[Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8-
* `Added` for new features.
9-
* `Changed` for changes in existing functionality.
10-
* `Deprecated` for soon-to-be removed features.
11-
* `Removed` for now removed features.
12-
* `Fixed` for any bug fixes.
8+
- `Added` for new features.
9+
- `Changed` for changes in existing functionality.
10+
- `Deprecated` for soon-to-be removed features.
11+
- `Removed` for now removed features.
12+
- `Fixed` for any bug fixes.
13+
14+
## [1.4.0] - 2025-08-23
1315

14-
## [Unreleased]
16+
### Added
17+
18+
- RSS
1519

1620
## [1.3.0] - 2024-01-05
1721

1822
### Added
1923

20-
[#3](https://github.com/carlwiedemann/foresite/pull/3) version & watch commands (not sure how to test these best right now).
24+
- version & watch commands (not sure how to test these best right now).
2125

2226
## [1.2.0] - 2023-02-11
2327

2428
### Added
2529

26-
[#2](https://github.com/carlwiedemann/foresite/pull/2) Dynamic `<title>` tag based on post title
30+
- Dynamic `<title>` tag based on post title
2731

2832
### Fixed
2933

30-
* Typo in README.
31-
* Link to blog post in README.
34+
- Typo in README.
35+
- Link to blog post in README.
3236

3337
## [1.1.3] - 2023-01-16
3438

3539
### Fixed
3640

37-
* Use the top-level directory for the index.html file because you can't set subdirectories for github pages.
41+
- Use the top-level directory for the index.html file because you can't set subdirectories for github pages.
3842

3943
## [1.1.2] - 2023-01-16
4044

4145
### Fixed
4246

43-
* DRYing things up.
47+
- DRYing things up.
4448

4549
## [1.1.1] - 2023-01-15
4650

4751
### Fixed
4852

49-
* Small typo in gemspec.
53+
- Small typo in gemspec.
5054

5155
## [1.1.0] - 2023-01-15
5256

5357
### Added
5458

55-
* Can use templates for both post markdown and links list on index page.
59+
- Can use templates for both post markdown and links list on index page.
5660

5761
### Fixed
5862

59-
* Reverse chronological order for posts on index page.
60-
* Fleshed-out README and fixed typos.
63+
- Reverse chronological order for posts on index page.
64+
- Fleshed-out README and fixed typos.
6165

6266
## [1.0.1] - 2023-01-15
6367

6468
### Changed
6569

66-
* Fleshed-out README and CHANGELOG files from default boilerplate.
70+
- Fleshed-out README and CHANGELOG files from default boilerplate.
6771

6872
### Fixed
6973

70-
* Bug with trailing non-alphanumeric characters in post titles.
74+
- Bug with trailing non-alphanumeric characters in post titles.
7175

7276
## [1.0.0] - 2023-01-14
7377

7478
### Added
7579

76-
* Initial stable release, a global executable that provides the ability to generate HTML from markdown.
80+
- Initial stable release, a global executable that provides the ability to generate HTML from markdown.

README.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ Create a project directory for your site and run `foresite init` from within it:
4141
Created erb/post.md.erb
4242
Created erb/wrapper.html.erb
4343
Created erb/_list.html.erb
44+
Created erb/feed.xml.erb
4445

4546
Three subdirectories are created, along with three [ERB](https://docs.ruby-lang.org/en/3.2/ERB.html) template files.
4647

@@ -52,6 +53,7 @@ Some facts:
5253
* `post.md.erb` is the default markdown file for every post.
5354
* `wrapper.html.erb` is a HTML wrapper template for every generated HTML file.
5455
* `_list.html.erb` is a HTML template partial for the list of posts on the `index.html` page.
56+
* `feed.xml.erb` is a XML template for an RSS feed.
5557

5658
### 2. Write your first post
5759

@@ -69,8 +71,8 @@ A single markdown file is created in the `md` subdirectory. **This file is meant
6971

7072
Some facts:
7173

72-
* The title is the first line formatted as H1 (mandatory).
73-
* Current date in YYYY-MM-DD format is the first markdown paragraph (optional).
74+
* The title is the first line formatted as H1.
75+
* Current date in YYYY-MM-DD format is the first markdown paragraph.
7476
* Current date and title are "slugified" for filename.
7577

7678
### 3. Modify templates as desired
@@ -81,23 +83,27 @@ Some facts:
8183

8284
`_list.html.erb` is used to generate the `<ul>` list of posts on the `index.html` file. Modify to show posts in a different way.
8385

86+
`feed.xml.erb` is an RSS feed, it will require a `title` as well as a `base_url` for where you host your site.
87+
8488
### 4. Generate HTML from markdown
8589

8690
Run `foresite build` to create HTML in the `post` subdirectory and the `index.html` file:
8791

8892
$ foresite build
8993
Created post/2023-01-15-welcome-to-my-site.html
9094
Created index.html
95+
Created feed.xml
9196

92-
In this example, two HTML files are created.
97+
In this example, two HTML files and an XML file are created.
9398

9499
Some facts:
95100

96101
* For every post markdown file in the `md` subdirectory an equivalent HTML file is generated in the `post` subdirectory, each wrapped with wrapper template markup.
97102
* A single `index.html` file shows a list of links to all posts in reverse-chronological order, prefixed with post date.
98103
* Post titles are parsed from the first H1 tag in each post markdown file.
99104
* Post dates are parsed from the post markdown filename.
100-
* Re-running `foresite build` removes and recreates all HTML files in the `post` subdirectory as well as the `index.html` file.
105+
* The `feed.xml` file reflects the list posts in RSS 2.0 format.
106+
* Re-running `foresite build` removes and recreates all HTML files in the `post` subdirectory as well as the `index.html` file and `feed.xml` file.
101107

102108
In this example, the `index.html` will contain:
103109

foresite.gemspec

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Gem::Specification.new do |spec|
1111
spec.summary = "An extremely minimal static site generator."
1212
spec.homepage = "https://github.com/carlwiedemann/foresite"
1313
spec.license = "MIT"
14-
spec.required_ruby_version = ">= 2.7.0"
14+
spec.required_ruby_version = ">= 3.3.0"
1515

1616
spec.metadata["homepage_uri"] = spec.homepage
1717
spec.metadata["source_code_uri"] = spec.homepage
@@ -28,19 +28,20 @@ Gem::Specification.new do |spec|
2828
"lib/foresite/version.rb",
2929
"lib/skeleton/_list.html.erb",
3030
"lib/skeleton/post.md.erb",
31-
"lib/skeleton/wrapper.html.erb"
31+
"lib/skeleton/wrapper.html.erb",
32+
"lib/skeleton/feed.xml.erb"
3233
]
3334

3435
spec.bindir = "bin"
3536
spec.executables = spec.files.grep(%r{\Abin/}) { |f| File.basename(f) }
3637
spec.require_paths = ["lib"]
3738

38-
spec.add_dependency "kramdown", "~> 2.4"
39-
spec.add_dependency "thor", "~> 1.2"
40-
spec.add_dependency "zeitwerk", "~> 2.6"
39+
spec.add_dependency "kramdown", "~> 2.5"
40+
spec.add_dependency "thor", "~> 1.4"
41+
spec.add_dependency "zeitwerk", "~> 2.7"
4142
spec.add_dependency "filewatcher", "~> 2.1"
4243

43-
spec.add_development_dependency "rspec", "~> 3.2"
44-
spec.add_development_dependency "standard", "~> 1.3"
44+
spec.add_development_dependency "rspec", "~> 3.13"
45+
spec.add_development_dependency "standard", "~> 1.50"
4546
spec.add_development_dependency "rake", "~> 13"
4647
end

lib/foresite.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ module Foresite
1717
FILENAME_POST_MD = "post.md.erb"
1818
FILENAME_WRAPPER_HTML = "wrapper.html.erb"
1919
FILENAME_LIST_HTML = "_list.html.erb"
20+
FILENAME_FEED_XML = "feed.xml.erb"
2021

2122
ENV_ROOT = "FORESITE_ROOT"
2223

@@ -68,6 +69,10 @@ def self.get_path_to_index_file
6869
File.join(get_path_to_root, "index.html")
6970
end
7071

72+
def self.get_path_to_feed_file
73+
File.join(get_path_to_root, "feed.xml")
74+
end
75+
7176
def self.relative_path(full_path)
7277
full_path.gsub(get_path_to_root, "").gsub(Regexp.new("^#{File::SEPARATOR}"), "")
7378
end
@@ -98,6 +103,13 @@ def self.render_wrapped_index(links)
98103
})
99104
end
100105

106+
def self.render_feed(items, date_build_822)
107+
render_erb_file(FILENAME_FEED_XML, {
108+
items: items.reverse,
109+
date_build_822: date_build_822
110+
})
111+
end
112+
101113
def self.touch_directories
102114
[get_path_to_md, get_path_to_out, get_path_to_erb].map do |path|
103115
if Dir.exist?(path)
@@ -110,7 +122,7 @@ def self.touch_directories
110122
end
111123

112124
def self.copy_templates
113-
[FILENAME_POST_MD, FILENAME_WRAPPER_HTML, FILENAME_LIST_HTML].map do |filename|
125+
[FILENAME_POST_MD, FILENAME_WRAPPER_HTML, FILENAME_LIST_HTML, FILENAME_FEED_XML].map do |filename|
114126
full_file_path = File.join(get_path_to_erb, filename)
115127
if File.exist?(full_file_path)
116128
"#{relative_path(full_file_path)} already exists"

lib/foresite/cli.rb

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require "date"
2+
13
module Foresite
24
##
35
# Cli class.
@@ -88,6 +90,7 @@ def build
8890
# Wipe all output files.
8991
Dir.glob(File.join(Foresite.get_path_to_out, "*.html")).each { File.delete(_1) }
9092
File.delete(Foresite.get_path_to_index_file) if File.exist?(Foresite.get_path_to_index_file)
93+
File.delete(Foresite.get_path_to_feed_file) if File.exist?(Foresite.get_path_to_feed_file)
9194

9295
markdown_paths = Dir.glob(File.join(Foresite.get_path_to_md, "*.md"))
9396

@@ -106,11 +109,14 @@ def build
106109
File.write(html_path, Foresite.render_wrapped(title, markdown_content))
107110
$stdout.puts("Created #{Foresite.relative_path(html_path)}")
108111

109-
# Extract date if it exists.
112+
# Extract date.
110113
match_data = /\d{4}-\d{2}-\d{2}/.match(filename_markdown)
114+
date_ymd = match_data[0]
115+
date_822 = DateTime.strptime(date_ymd, "%F").strftime("%a, %d %b %Y %H:%M:%S %z")
111116

112117
{
113-
date_ymd: match_data.nil? ? "" : match_data[0],
118+
date_ymd: date_ymd,
119+
date_822: date_822,
114120
href: Foresite.relative_path(html_path),
115121
title: title
116122
}
@@ -119,8 +125,11 @@ def build
119125
# Generate index file.
120126
index_html_path = Foresite.get_path_to_index_file
121127
File.write(index_html_path, Foresite.render_wrapped_index(links))
122-
123128
$stdout.puts("Created #{Foresite.relative_path(index_html_path)}")
129+
130+
feed_xml_path = Foresite.get_path_to_feed_file
131+
File.write(feed_xml_path, Foresite.render_feed(links, links.last[:date_822]))
132+
$stdout.puts("Created #{Foresite.relative_path(feed_xml_path)}")
124133
end
125134

126135
desc "watch", "Watches markdown and templates files and runs `build` when they change"

lib/foresite/renderer.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class Renderer
1212
# @param [Hash] vars Variables for template.
1313
def initialize(path, vars)
1414
@path = path
15+
@vars_original = @vars
1516
vars.each do |k, v|
1617
if k.is_a?(Symbol)
1718
instance_variable_set(:"@#{k}", v)

lib/foresite/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module Foresite
4-
VERSION = "1.3.0"
4+
VERSION = "1.4.0"
55
end

lib/skeleton/feed.xml.erb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<%
2+
title = 'Another Foresite Blog'
3+
base_url = 'https://example.com'
4+
-%>
5+
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
6+
<channel>
7+
<title><%= title %></title>
8+
<link><%= base_url %>/feed.xml</link>
9+
<language>en-us</language>
10+
<pubDate><%= @date_build_822 %></pubDate>
11+
<lastBuildDate><%= @date_build_822 %></lastBuildDate>
12+
<generator>Foresite</generator>
13+
<atom:link href="<%= base_url %>/feed.xml" rel="self" type="application/rss+xml"/>
14+
<% @items.each do |item| -%>
15+
<item>
16+
<title><%= item[:title] %></title>
17+
<link><%= "#{base_url}/#{item[:href]}" %></link>
18+
<guid><%= "#{base_url}/#{item[:href]}" %></guid>
19+
<pubDate><%= item[:date_822] %></pubDate>
20+
</item>
21+
<%- end %>
22+
</channel>
23+
</rss>

lib/skeleton/wrapper.html.erb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<head>
77
<meta charset="utf-8">
88
<title><%= @title ? "#{@title} - #{index_title}" : index_title %></title>
9+
<link rel="alternate" type="application/rss+xml" title="<%= index_title %>" href="/feed.xml">
910
<style></style>
1011
</head>
1112
<body>

0 commit comments

Comments
 (0)