# Prefixbox.com Feed Generator is a Jekyll plugin that generates a prefixbox.csv file by # traversing all of the available posts and pages. # # See readme file for documenation # # Based on the Sitemap generator by Michael Levin # Site: https://github.com/kinnetica/jekyll-plugins # # Author: Sverrir Sigmundarson # Site: https://sverrirs.com # Distributed Under A Creative Commons License # - http://creativecommons.org/licenses/by/3.0/ require 'jekyll/document' require 'csv' module Jekyll class Jekyll::Document attr_accessor :name def path_to_source File.join(*[@name].compact) end def location_on_server(my_url) "#{my_url}#{url}" end end class Page attr_accessor :name def path_to_source File.join(*[@dir, @name].compact) end def location_on_server(my_url) location = "#{my_url}#{url}" location.gsub(/index.html$/, "") end end # Recover from strange exception when starting server without --auto class PrefixboxFile < StaticFile def write(dest) true end end class PrefixboxGenerator < Generator priority :lowest # Config defaults PREFIXBOX_FILE_NAME = "/prefixbox.csv" EXCLUDE = ["/atom.xml", "/feed.xml", "/feed/index.xml", "/sitemap.xml"] #INCLUDE_POSTS = ["/index.html"] INCLUDE_POSTS = [] # Goes through pages and posts and generates prefixbox.csv file # # Returns nothing def generate(site) # Configuration prefixbox_config = site.config['prefixbox'] || {} @config = {} @config['filename'] = prefixbox_config['filename'] || PREFIXBOX_FILE_NAME @config['exclude'] = prefixbox_config['exclude'] || EXCLUDE @config['include_posts'] = prefixbox_config['include_posts'] || INCLUDE_POSTS # Create destination directory if it doesn't exist yet. Otherwise, we cannot write our file there. Dir::mkdir(site.dest) if !File.directory? site.dest # Create the file name and paths filename = @config['filename'] # Write all the data to the file and close it CSV.open(File.join(site.dest, filename), "wb") do |csv| fill_posts(site, csv) #fill_pages(site, csv) end # Keep the prefixbox.csv file from being cleaned by Jekyll site.static_files << Jekyll::PrefixboxFile.new(site, site.dest, "/", filename) end # Create url elements for all the normal pages and find the date of the # index to use with the pagination pages # # Returns last_modified_date of index page def fill_pages(site, csv) site.pages.each do |page| if !excluded?(site, page.path_to_source) if File.exists?(page.path) url = fill_url(site, page) csv << [page.title, 100, page.title, url, "", "page"] end end end end # Create url elements for all the posts and find the date of the latest one # # Returns last_modified_date of latest post def fill_posts(site, csv) last_modified_date = nil site.collections["posts"].docs.each do |post| # Write the page data if !excluded?(site, post.name) url = fill_url(site, post) title = post.to_liquid['title'] title = title.length > 80 ? title[0..79] : title csv << [title, 100, title, url, "", "blog"] end # Determine the last modified date date = File.mtime(post.path) last_modified_date = date if last_modified_date == nil or date > last_modified_date end last_modified_date end # Fill data of each URL element: location, last modified, # change frequency (optional), and priority. # # Returns url REXML::Element def fill_url(site, page_or_post) url = 'https:' + site.config['url'] + site.config['baseurl'] + page_or_post.location_on_server(url) url end # Is the page or post listed as something we want to exclude? # # Returns boolean def excluded?(site, name) @config['exclude'].include? name end def posts_included?(site, name) @config['include_posts'].include? name end end end