Class: Jekyll::Reposter
- Inherits:
-
Object
- Object
- Jekyll::Reposter
- Defined in:
- lib/jekyll-reposter.rb
Constant Summary collapse
- VERSION =
"0.0.1"
Instance Attribute Summary collapse
-
#replacings ⇒ Object
Returns the value of attribute replacings.
Instance Method Summary collapse
- #create_entry(entry) ⇒ Object
-
#create_if(&block) ⇒ Object
creates the blog entry file, if passed block returns true.
-
#initialize(feed, options = {}) ⇒ Reposter
constructor
fetching a single feed.
Constructor Details
#initialize(feed, options = {}) ⇒ Reposter
fetching a single feed
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/jekyll-reposter.rb', line 18 def initialize(feed, ={}) = { :tags => "[notes, external]", :dir => "source/_posts", :allowed_tags => %w[h2 ul li ol h3 h4 h5 code pre quote blockquote cite hr a img strong b em i], :meta => { "comments" => true, "layout" => "post" }, :pretend => false }.merge @replacings = { "“" => '"', "”" => '"', "<" => "<", ">" => ">", "</li>" => "", "</ul>" => "", "<ul>" => "", "<pre>" => "\n\n```ruby\n", "</pre>" => "\n```\n\n", "<code>" => "\n\n```ruby\n", "</code>" => "\n```\n\n", /[\t ]*<li>/ => "* ", /[\t ]*<h3>/ => "\n###", /<\/h3>/ => "\n", /[\t ]*<h2>/ => "\n##", /<\/h2>/ => "\n", "\t" => " ", } @feed = Feedzirra::Feed.fetch_and_parse(feed) @uri = URI.parse(feed) @dir = File.join [:dir], @uri.host FileUtils.mkdir_p(@dir) puts "Working dir: #{@dir}" end |
Instance Attribute Details
#replacings ⇒ Object
Returns the value of attribute replacings.
15 16 17 |
# File 'lib/jekyll-reposter.rb', line 15 def replacings @replacings end |
Instance Method Details
#create_entry(entry) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/jekyll-reposter.rb', line 70 def create_entry(entry) date = entry.published.strftime("%Y-%m-%d") slug = entry.title.to_url slug.gsub!(/[^\w]+/,"-") filename = File.join @dir, date + "-" + slug + ".markdown" # stringex ignores "->", url error at webrick unless File.exists? filename main_part = entry.content || entry.summary content = Sanitize.clean main_part, :elements => [:allowed_tags], :attributes => {'a' => ['href', 'title'], "img" => ["alt","src"]} @replacings.each do |from,to| content.gsub! from, to end = { "title" => entry.title, "date" => entry.published.strftime("%Y-%m-%d %H:%M"), }.merge([:meta]) = [:tags] file = "\#{meta.to_yaml}\ncategories: \#{tags}\n---\n\#{content}\n\n---\n<i>Reposted from <a href='\#{entry.url}' rel='canonical'>\#{@uri.host}</a></i>\n" if ![:pretend] File.open filename, "w+" do |f| puts "Writing #{filename}" f.write file end else breakwater = (["="] * 20).join + "\n" puts "#{breakwater}Would create #{filename} with content:\n#{breakwater}#{file}" end end end |
#create_if(&block) ⇒ Object
creates the blog entry file, if passed block returns true. block is a entry object from feedzirra, like filtering
62 63 64 65 66 67 68 |
# File 'lib/jekyll-reposter.rb', line 62 def create_if(&block) @feed.entries.each do |entry| if block.call(entry) create_entry(entry) end end end |