Created
December 28, 2016 03:45
-
-
Save paulohrpinheiro/20130e06355fc5bffe5865ce903dce63 to your computer and use it in GitHub Desktop.
The ancestral script for this project: https://github.com/paulohrpinheiro/gerablog
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'redcarpet' | |
require 'rss' | |
def write_file(filename, content, description, meta) | |
File.write( | |
filename, | |
%(<!--#include virtual="/parts/header_begin.html" -->\n\n) + | |
"#{meta}\n" + | |
%(<meta name="description" content="#{description}">\n) + | |
%(<!--#include virtual="/parts/header_end.html" -->\n\n) + | |
content + | |
%(\n\n<!--#include virtual="/parts/footer.html" -->\n) | |
) | |
end | |
main_rss = [] | |
Dir['*'].each do |category| | |
puts category | |
rss_file = "#{category}/feed.rss" | |
rss_content = '' | |
text_list = %(<h2><a href="/texts/#{rss_file}"><img src="/images/icon-rss.png"></a> #{category.capitalize}</h2>\n\n) | |
class CustomRender < Redcarpet::Render::HTML | |
def initialize(lang) | |
super(prettify: true, escape_html: true) | |
@@lang = lang | |
end | |
def block_code(code, language) | |
%(<pre><code class="language-#{@@lang}">\n#{html_escape(code)}\n</code></pre>) | |
end | |
def html_escape(string) | |
string.gsub(/['&\"<>\/]/, { | |
'&' => '&', | |
'<' => '<', | |
'>' => '>', | |
'"' => '"', | |
"'" => ''', | |
"/" => '/', | |
}) | |
end | |
end | |
markdown = Redcarpet::Markdown.new( | |
CustomRender.new(category) | |
) | |
rss = RSS::Maker.make("atom") do |maker| | |
maker.channel.author = "Paulo Henrique Rodrigues Pinheiro" | |
maker.channel.updated = Time.now.to_s | |
maker.channel.about = "https://paulohrpinheiro.xyz/texts/#{rss_file}" | |
maker.channel.title = category.capitalize | |
Dir["#{category}/*.md"].sort.reverse.each do |arq| | |
puts "\t#{arq}" | |
newfile = arq.gsub(/md$/, 'html') | |
md_content = File.read(arq) | |
lines = md_content.split("\n") | |
title = lines[0][2..-1] | |
description = lines[2][3..-1] | |
puts "\t#{title}\n" | |
sharer = '<h5>Compartilhe:</h5>' | |
['twitter', 'facebook', 'linkedin', 'email', 'googleplus'].each do |social| | |
sharer += %(\n<button class="sharer button" ) + | |
%(data-sharer="#{social}" ) + | |
%(data-title="#{title}" ) + %(data-url="https://paulohrpinheiro.xyz/texts/#{newfile}">) + | |
%(<img src="/images/icon-#{social}.png"></button>\n) | |
end | |
write_file(newfile, markdown.render(md_content) + sharer, description, '<link href="/assets/css/prism.css" rel="stylesheet" />') | |
text_list += %(<a href="/texts/#{newfile}"><h3 class="textcal">#{title}</h3>\n<p class="textcal">#{description}</p></a>\n\n) | |
maker.items.new_item do |item| | |
item.link = "https://paulohrpinheiro.xyz/texts/#{newfile}" | |
item.title = title | |
item.updated = Date.parse(newfile.match('\A.*/(....-..-..)')[1]).rfc822 | |
puts newfile if item.updated==0; | |
main_rss.push({link: item.link, title: item.title, date: item.updated}) | |
end | |
end | |
end | |
File.write(rss_file, rss) | |
write_file("#{category}/index.html", text_list, "Textos e artigos sobre #{category.capitalize}", '') | |
end | |
main_rss.sort_by! { |h| h[:date] }.reverse! | |
rss = RSS::Maker.make("atom") do |maker| | |
maker.channel.author = "Paulo Henrique Rodrigues Pinheiro" | |
maker.channel.updated = Time.now.to_s | |
maker.channel.about = "https://paulohrpinheiro.xyz/feed.rss" | |
maker.channel.title = "Blog do PauloHRPinheiro" | |
main_rss.take(1).each do |entry| | |
maker.items.new_item do |item| | |
item.link = entry[:link] | |
item.title = entry[:title] | |
item.updated = entry[:date] | |
end | |
end | |
end | |
File.write('feed.rss', rss) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment