Skip to content

Commit

Permalink
Build the RSS feed using a template, so people can add stuff to the h…
Browse files Browse the repository at this point in the history
…eader. Especially useful for podcast.
  • Loading branch information
brentsimmons committed Apr 14, 2018
1 parent bf757d9 commit b38c01a
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 100 deletions.
2 changes: 1 addition & 1 deletion model/page.rb
Expand Up @@ -45,6 +45,6 @@ def context

def to_html
renderer = Renderer.new(@settings, 'page', context)
renderer.to_html
renderer.to_s
end
end
2 changes: 1 addition & 1 deletion model/post.rb
Expand Up @@ -138,6 +138,6 @@ def context
def render_with_template(template_name)

renderer = Renderer.new(@settings, template_name, context)
renderer.to_html + "\n"
renderer.to_s + "\n"
end
end
172 changes: 108 additions & 64 deletions model/rss_feed.rb
@@ -1,14 +1,12 @@
class RSSFeed

RSS_TAG = 'rss'
CHANNEL_TAG = 'channel'
TITLE_TAG = 'title'
DESCRIPTION_TAG = 'description'
LANGUAGE_TAG = 'language'
ITEM_TAG = 'item'
LINK_TAG = 'link'
GUID_TAG = 'guid'
PUB_DATE_TAG = 'pubDate'
# Feeds are rendered using an 'rss' template in the templates folder.
# The individual items are generated programatically.

ITEMS_KEY = 'items'
SITE_NAME_KEY = 'site_name'
SITE_URL_KEY = 'site_url'
FEED_DESCRIPTION_KEY = 'feed_description'

def self.rendered_feed(settings, posts)
feed = RSSFeed.new(settings, posts)
Expand All @@ -18,82 +16,110 @@ def self.rendered_feed(settings, posts)
def initialize(settings, posts)
@settings = settings
@posts = posts
@xml = '<?xml version="1.0" encoding="UTF-8"?>'
push_new_line
@indent_level = 0
add_header
add_posts
add_footer
end

def to_s
@xml
items = ''
@posts.each { |post| items += render_post(post) }

context = {}
context[ITEMS_KEY] = items
context[SITE_NAME_KEY] = xmlize(@settings.site_name)
context[SITE_URL_KEY] = xmlize(@settings.site_url)
context[FEED_DESCRIPTION_KEY] = xmlize(@settings.feed_description)

renderer = Renderer.new(@settings, 'rss', context)
renderer.to_s
end

private

def add_header
if @settings.feed_itunes_include
push_stand_alone_tag('rss version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:media="http://search.yahoo.com/mrss/"')
else
push_stand_alone_tag('rss version="2.0"')
end
@indent_level += 1
push_stand_alone_tag(CHANNEL_TAG)
@indent_level += 1
push_tag_with_value(TITLE_TAG, @settings.feed_title)
push_tag_with_value(DESCRIPTION_TAG, @settings.feed_description)
push_tag_with_value(LINK_TAG, @settings.site_url)
push_tag_with_value(LANGUAGE_TAG, @settings.feed_language)
def render_post(post)
rss_item = RSSItem.new(@settings, post, 2)
rss_item.to_s + "\n"
end
end

class RSSItem

def add_posts
@posts.each { |post| add_post(post) }
TITLE_TAG = 'title'
DESCRIPTION_TAG = 'description'
ITEM_TAG = 'item'
LINK_TAG = 'link'
GUID_TAG = 'guid'
PUB_DATE_TAG = 'pubDate'
ENCLOSURE_TAG = 'enclosure'
URL_ATTRIBUTE = 'url'
ENCLOSURE_LENGTH_ATTRIBUTE = 'length'
ENCLOSURE_TYPE_ATTRIBUTE = 'type'
ITUNES_AUTHOR_TAG = 'itunes:author'
ITUNES_SUMMARY_TAG = 'itunes:summary'
ITUNES_KEYWORDS_TAG = 'itunes:keywords'
ITUNES_EXPLICIT_TAG = 'itunes:explicit'
ITUNES_IMAGE_TAG = 'itunes:image'
HREF_ATTRIBUTE = 'href'
ITUNES_OWNER_TAG = 'itunes:owner'
ITUNES_NAME_TAG = 'itunes:name'
ITUNES_EMAIL_TAG = 'itunes:email'
ITUNES_DURATION_TAG = 'itunes:duration'
ITUNES_SUBTITLE_TAG = 'itunes:subtitle'
MEDIA_THUMBNAIL_TAG = 'media:thumbnail'

def initialize(settings, post, indent_level)
@settings = settings
@post = post
@indent_level = indent_level
@xml = ''
end

def add_post(post)
push_new_line
def to_s
push_stand_alone_tag(ITEM_TAG)
@indent_level += 1

if !post.title.nil?
push_tag_with_value(TITLE_TAG, post.title)
if !@post.title.nil?
push_tag_with_value(TITLE_TAG, @post.title)
end

if post.external_url.nil?
push_tag_with_value(LINK_TAG, post.permalink)
if @post.external_url.nil?
push_tag_with_value(LINK_TAG, @post.permalink)
else
push_tag_with_value(LINK_TAG, post.external_url)
push_tag_with_value(LINK_TAG, @post.external_url)
end

push_tag_with_value(GUID_TAG, post.permalink)
push_tag_with_value(GUID_TAG, @post.permalink)

pub_date_string = post.pub_date.strftime("%a, %d %b %Y %H:%M:%S %z")
pub_date_string = @post.pub_date.strftime("%a, %d %b %Y %H:%M:%S %z")
push_tag_with_value(PUB_DATE_TAG, pub_date_string)

push_tag_with_value(DESCRIPTION_TAG, post.content_html)
push_tag_with_value(DESCRIPTION_TAG, @post.content_html)
@indent_level -= 1

push_enclosure unless @post.enclosure.nil?

push_stand_alone_closing_tag(ITEM_TAG)
end

def add_footer
push_new_line
@indent_level -= 1
push_stand_alone_closing_tag(CHANNEL_TAG)
@indent_level -= 1
push_stand_alone_closing_tag(RSS_TAG)
def push_enclosure
push_indents
push('<enclosure')
push_attribute('url', @post.enclosure.url)
push_attribute_if_not_empty('length', @post.enclosure.size_in_bytes.to_s)
push_attribute_if_not_empty('type', @post.enclosure.mime_type)
push(' />')
push_new_line

push_tag_with_value_if_not_empty('itunes:duration', @post.itunes_duration)
push_tag_with_value_if_not_empty('itunes:subtitle', @post.itunes_subtitle)
push_tag_with_value_if_not_empty('itunes:summary', @post.itunes_summary)
push_tag_with_value_if_not_empty('itunes:explicit', @post.itunes_explicit)
push_tag_with_value_if_not_empty('media:thumbnail', @post.itunes_media_thumbnail)
end

def xmlize(s)
xml = s
xml.gsub!('&', '&amp;')
xml.gsub!('<', '&lt;')
xml.gsub!('>', '&gt;')
xml.gsub!("'", '&apos;')
xml.gsub!('"', '&quot;')
xml.strip!
xml
def push_attribute_if_not_empty(name, value)
push_attribute(name, value) unless value.nil? || value.empty?
end

def push_attribute(name, value)
push(" #{name}=\"#{xmlize(value)}\"")
end

def push_indents
Expand All @@ -102,16 +128,16 @@ def push_indents
end
end

def push_tag(tag)
push_indents
push("<#{tag}>")
end

def push_stand_alone_tag(tag)
push_tag(tag)
push_new_line
end

def push_tag(tag)
push_indents
push("<#{tag}>")
end

def push_closing_tag(tag)
push("</#{tag}>")
push_new_line
Expand All @@ -122,8 +148,9 @@ def push_stand_alone_closing_tag(tag)
push_closing_tag(tag)
end

def push_new_line
@xml += "\n"
def push_tag_with_value_if_not_empty(tag, value)
if value.nil? || value.empty? then return end
push_tag_with_value(tag, value)
end

def push_tag_with_value(tag, value)
Expand All @@ -133,7 +160,24 @@ def push_tag_with_value(tag, value)
push_closing_tag(tag)
end

def push_new_line
@xml += "\n"
end

def push(s)
@xml += s
end
end


def xmlize(s)
xml = s
xml.gsub!('&', '&amp;')
xml.gsub!('<', '&lt;')
xml.gsub!('>', '&gt;')
xml.gsub!("'", '&apos;')
xml.gsub!('"', '&quot;')
xml.strip!
xml
end

29 changes: 0 additions & 29 deletions model/website_settings.rb
Expand Up @@ -40,15 +40,6 @@ class WebsiteSettings
attr_reader :feed_author_url
attr_reader :feed_author_avatar_url
attr_reader :feed_url
attr_reader :feed_itunes_include
attr_reader :feed_itunes_graphic_url
attr_reader :feed_itunes_author
attr_reader :feed_itunes_summary
attr_reader :feed_itunes_keywords
attr_reader :feed_itunes_explicit
attr_reader :feed_itunes_owner_name
attr_reader :feed_itunes_owner_email
attr_reader :feed_itunes_category

SITE_NAME_KEY = 'site_name'
SITE_URL_KEY = 'site_url'
Expand All @@ -68,19 +59,9 @@ class WebsiteSettings
FEED_NUMBER_OF_POSTS_KEY = 'feed_number_of_posts'
FEED_TITLE_KEY = 'feed_title'
FEED_DESCRIPTION_KEY = 'feed_description'
FEED_LANGUAGE_KEY = 'feed_language'
FEED_AUTHOR_NAME_KEY = 'feed_author'
FEED_AUTHOR_URL_KEY = 'feed_author_url'
FEED_AUTHOR_AVATAR_KEY = 'feed_author_avatar_url'
FEED_ITUNES_INCLUDE = 'feed_itunes_include'
FEED_ITUNES_GRAPHIC_URL = 'feed_itunes_graphic_url'
FEED_ITUNES_AUTHOR = 'feed_itunes_author'
FEED_ITUNES_SUMMARY = 'feed_itunes_summary'
FEED_ITUNES_KEYWORDS = 'feed_itunes_keywords'
FEED_ITUNES_EXPLICIT = 'feed_itunes_explicit'
FEED_ITUNES_OWNER_NAME = 'feed_itunes_owner_name'
FEED_ITUNES_OWNER_EMAIL = 'feed_itunes_owner_email'
FEED_ITUNES_CATEGORY = 'feed_itunes_category'

FOLDER_NAME_POSTS = 'posts'
FOLDER_NAME_PAGES = 'pages'
Expand Down Expand Up @@ -134,19 +115,9 @@ def initialize(project_folder, settings_file_path)
@feed_number_of_posts = @attributes.fetch(FEED_NUMBER_OF_POSTS_KEY, 20)
@feed_title = @attributes.fetch(FEED_TITLE_KEY, @site_name)
@feed_description = @attributes[FEED_DESCRIPTION_KEY]
@feed_language = @attributes.fetch[FEED_LANGUAGE_KEY, 'en')
@feed_author_name = @attributes[FEED_AUTHOR_NAME_KEY]
@feed_author_url = @attributes[FEED_AUTHOR_URL_KEY]
@feed_author_avatar_url = @attributes[FEED_AUTHOR_AVATAR_KEY]
@feed_itunes_include = @attributes[FEED_ITUNES_INCLUDE]
@feed_itunes_graphic_url = @attributes[FEED_ITUNES_GRAPHIC_URL]
@feed_itunes_author = @attributes[FEED_ITUNES_AUTHOR]
@feed_itunes_summary = @attributes[FEED_ITUNES_SUMMARY]
@feed_itunes_keywords = @attributes[FEED_ITUNES_KEYWORDS]
@feed_itunes_explicit = @attributes[FEED_ITUNES_EXPLICIT]
@feed_itunes_owner_name = @attributes[FEED_ITUNES_OWNER_NAME]
@feed_itunes_owner_email = @attributes[FEED_ITUNES_OWNER_EMAIL]
@feed_itunes_category = @attributes[FEED_ITUNES_CATEGORY]

@feed_url = site_url + 'feed.json'
end
Expand Down
2 changes: 1 addition & 1 deletion renderer/page_builder.rb
Expand Up @@ -5,6 +5,6 @@ module PageBuilder

def PageBuilder.build(settings, template_name, context, destination_path)
renderer = Renderer.new(settings, template_name, context)
WildcatUtils.write_file_if_different(destination_path, renderer.to_html)
WildcatUtils.write_file_if_different(destination_path, renderer.to_s)
end
end
7 changes: 5 additions & 2 deletions renderer/renderer.rb
Expand Up @@ -29,7 +29,7 @@ def initialize(settings, template_name, context)
@html = @template_text.dup
end

def to_html
def to_s
process_snippets(@html)
process_substitutions(@html)
@html
Expand All @@ -38,7 +38,10 @@ def to_html
private

def read_template(template_name)
path = File.join(@settings.templates_folder, template_name + '.html')
path = File.join(@settings.templates_folder, template_name)
if !FileTest.exist?(path)
path = File.join(@settings.templates_folder, template_name + '.html')
end
WildcatUtils.read_text_file(path)
end

Expand Down
6 changes: 4 additions & 2 deletions server/wildcat.cgi
Expand Up @@ -87,7 +87,7 @@ class MetaWeblogCommand
new_post_id = post_id_with_relative_path(relative_path)

rebuild_site

new_post_id
end

Expand Down Expand Up @@ -124,7 +124,7 @@ class MetaWeblogCommand
WildcatUtils.write_file_if_different(path, s)

rebuild_site

true
end

Expand Down Expand Up @@ -205,6 +205,8 @@ class MetaWeblogCommand
file_name.gsub!('”', '')
file_name.gsub!(' ', '_')
file_name.gsub!(/\W/, '_')
file_name.gsub!("a_href_https", '')
file_name.gsub!("a_href_http", '')
while file_name.include?('__')
file_name.gsub!('__', '_')
end
Expand Down

0 comments on commit b38c01a

Please sign in to comment.