refactor newsletter script

This commit is contained in:
David Eisinger
2024-02-26 10:28:34 -05:00
parent 7028dddad4
commit 071778f859

View File

@@ -11,29 +11,34 @@ feed = Nokogiri::XML(
post = feed.search("rss channel item").first post = feed.search("rss channel item").first
title = post.search("title").text title = post.search("title").text
url = post.search("link").text url = post.search("link").text
body = post.search("description").text
body = %(<h1>#{title}</h1> body = %(<h1>#{title}</h1>
<p> <p>
By David Eisinger &middot; By David Eisinger &middot;
<a href="#{url}">View original post</a> <a href="#{url}">View original post</a>
</p> </p>
#{post.search("description").text} #{body})
)
# escape curly braces (they're important in listmonk)
body = body
.gsub("{", "&#123;") .gsub("{", "&#123;")
.gsub("}", "&#125;") .gsub("}", "&#125;")
.gsub(/width="(\d+)"/) do
%(width="#{$1.to_i/2}") # half-size images
end body = body
.gsub(/height="(\d+)"/) do .gsub(/width="(\d+)"/) { %(width="#{$1.to_i/2}") }
%(height="#{$1.to_i/2}") .gsub(/height="(\d+)"/) { %(height="#{$1.to_i/2}") }
end
.gsub(/<audio controls src="([^"]+)"><\/audio>/) do # replace audio tags with links
body = body.gsub(/<audio controls src="([^"]+)"><\/audio>/) do
filename = $1.split("/").last filename = $1.split("/").last
%(<a href="#{$1}">#{filename}</a>) %(<a href="#{$1}">#{filename}</a>)
end end
.gsub(/<a href="#fn[^>]+>(\d+)<\/a>/) do
$1 # remove footnote jump links
end body = body
.gsub(/<a href="#fn[^>]+>(\d+)<\/a>/) { $1 }
.gsub(/&#160;<a href="#fnref:[^>]+>&#x21a9;&#xfe0e;<\/a>/, "") .gsub(/&#160;<a href="#fnref:[^>]+>&#x21a9;&#xfe0e;<\/a>/, "")
p HTTParty.post( p HTTParty.post(