diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..dc1cdf3 --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,33 @@ +inherit_from: .rubocop_todo.yml + +AllCops: + TargetRubyVersion: 2.3 + + Exclude: + - 'db/**/*' + - 'script/*' + - 'bin/*' + - 'log/**/*' + - 'node_modules/**/*' + - 'public/**/*' + - 'vendor/**/*' + - 'tmp/**/*' + - '.git/**/*' + +IndentationConsistency: + EnforcedStyle: 'rails' + +Naming/FileName: + Exclude: + - 'Gemfile' + - 'Guardfile' + - 'Rakefile' + +Style/FrozenStringLiteralComment: + Enabled: false + +Style/AsciiComments: + Enabled: false + +Style/Documentation: + Enabled: false diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml new file mode 100644 index 0000000..bd4a7ee --- /dev/null +++ b/.rubocop_todo.yml @@ -0,0 +1,84 @@ +# This configuration was generated by +# `rubocop --auto-gen-config` +# on 2018-08-21 17:24:16 -0700 using RuboCop version 0.58.2. +# The point is for the user to remove these configuration records +# one by one as the offenses are removed from the code base. +# Note that changes in the inspected code, or installation of new +# versions of RuboCop, may require this file to be generated again. + +# Offense count: 4 +Lint/IneffectiveAccessModifier: + Exclude: + - 'model/page.rb' + - 'utilities/file_parser.rb' + +# Offense count: 1 +# Configuration parameters: ContextCreatingMethods, MethodCreatingMethods. +Lint/UselessAccessModifier: + Exclude: + - 'utilities/file_parser.rb' + +# Offense count: 6 +Metrics/AbcSize: + Max: 68 + +# Offense count: 2 +# Configuration parameters: CountComments. +Metrics/ClassLength: + Max: 106 + +# Offense count: 1 +Metrics/CyclomaticComplexity: + Max: 8 + +# Offense count: 8 +# Configuration parameters: CountComments. +Metrics/MethodLength: + Max: 42 + +# Offense count: 1 +Metrics/PerceivedComplexity: + Max: 8 + +# Offense count: 5 +# Configuration parameters: MinNameLength, AllowNamesEndingInNumbers, AllowedNames, ForbiddenNames. +# AllowedNames: io, id, to, by, on, in, at, ip +Naming/UncommunicativeMethodParamName: + Exclude: + - 'model/rss_feed.rb' + - 'renderer/renderer.rb' + +# Offense count: 3 +Style/ClassVars: + Exclude: + - 'renderer/renderer.rb' + - 'utilities/wildcat_file.rb' + +# Offense count: 1 +# Configuration parameters: MinBodyLength. +Style/GuardClause: + Exclude: + - 'utilities/file_parser.rb' + +# Offense count: 1 +Style/IfInsideElse: + Exclude: + - 'model/post.rb' + +# Offense count: 8 +# Cop supports --auto-correct. +Style/IfUnlessModifier: + Exclude: + - 'model/blog_archive.rb' + - 'model/enclosure.rb' + - 'model/page.rb' + - 'model/post.rb' + - 'model/website_settings.rb' + - 'renderer/renderer.rb' + - 'wildcat_publish.rb' + +# Offense count: 37 +# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns. +# URISchemes: http, https +Metrics/LineLength: + Max: 250 diff --git a/Gemfile b/Gemfile index 6c7c110..07347ab 100644 --- a/Gemfile +++ b/Gemfile @@ -2,3 +2,7 @@ source 'https://rubygems.org' gem 'kramdown' gem 'stringex' + +group :development do + gem 'rubocop' +end diff --git a/Gemfile.lock b/Gemfile.lock index 4e9bb1b..52f01a7 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,14 +1,32 @@ GEM remote: https://rubygems.org/ specs: + ast (2.4.0) + jaro_winkler (1.5.1) kramdown (1.17.0) + parallel (1.12.1) + parser (2.5.1.2) + ast (~> 2.4.0) + powerpack (0.1.2) + rainbow (3.0.0) + rubocop (0.58.2) + jaro_winkler (~> 1.5.1) + parallel (~> 1.10) + parser (>= 2.5, != 2.5.1.1) + powerpack (~> 0.1) + rainbow (>= 2.2.2, < 4.0) + ruby-progressbar (~> 1.7) + unicode-display_width (~> 1.0, >= 1.0.1) + ruby-progressbar (1.10.0) stringex (2.8.4) + unicode-display_width (1.4.0) PLATFORMS ruby DEPENDENCIES kramdown + rubocop stringex BUNDLED WITH diff --git a/model/blog.rb b/model/blog.rb index b0905ee..a07f90d 100644 --- a/model/blog.rb +++ b/model/blog.rb @@ -7,7 +7,6 @@ require_relative 'blog_archive' class Blog - def initialize(settings) @settings = settings @cached_posts = nil @@ -28,60 +27,57 @@ def recent_posts(count) end def posts - if @cached_posts.nil? - @cached_posts = all_blog_posts_reverse_sorted_by_date - end + @cached_posts = all_blog_posts_reverse_sorted_by_date if @cached_posts.nil? @cached_posts end private - def all_blog_posts_reverse_sorted_by_date + def all_blog_posts_reverse_sorted_by_date + paths = WildcatUtils.text_source_files_in_folder(@settings.posts_folder) - paths = WildcatUtils.text_source_files_in_folder(@settings.posts_folder) + unsorted_posts = paths.map do |path| + wildcat_file = WildcatFile.new(path) + Post.new(@settings, wildcat_file) + end - unsorted_posts = paths.map do |path| - wildcat_file = WildcatFile.new(path) - Post.new(@settings, wildcat_file) + posts = unsorted_posts.sort_by(&:pub_date) + posts.reverse end - posts = unsorted_posts.sort_by { |post| post.pub_date } - posts.reverse - end - - def build_home_page - context = {} - context[CONTEXT_TITLE_KEY] = @settings.blog_home_page_title + def build_home_page + context = {} + context[CONTEXT_TITLE_KEY] = @settings.blog_home_page_title - html = '' - posts_for_home_page.each { |post| html+= post.to_html(true) } - context[CONTEXT_CONTENT_HTML_KEY] = html + html = '' + posts_for_home_page.each { |post| html += post.to_html(true) } + context[CONTEXT_CONTENT_HTML_KEY] = html - destination_path = File.join(@settings.blog_output_folder, 'index') - destination_path = WildcatUtils.add_suffix_if_needed(destination_path, @settings.output_file_suffix) - PageBuilder.build(@settings, 'blog_home', context, destination_path) - end + destination_path = File.join(@settings.blog_output_folder, 'index') + destination_path = WildcatUtils.add_suffix_if_needed(destination_path, @settings.output_file_suffix) + PageBuilder.build(@settings, 'blog_home', context, destination_path) + end - def build_json_feed - feed_text = JSONFeed.rendered_feed(@settings, posts_for_feed) - write_feed(feed_text, 'feed.json') - end + def build_json_feed + feed_text = JSONFeed.rendered_feed(@settings, posts_for_feed) + write_feed(feed_text, 'feed.json') + end - def build_rss_feed - feed_text = RSSFeed.rendered_feed(@settings, posts_for_feed) - write_feed(feed_text, 'xml/rss.xml') - end + def build_rss_feed + feed_text = RSSFeed.rendered_feed(@settings, posts_for_feed) + write_feed(feed_text, 'xml/rss.xml') + end - def write_feed(feed_text, relative_path) - destination_path = File.join(@settings.output_folder, relative_path) - WildcatUtils.write_file_if_different(destination_path, feed_text) - end + def write_feed(feed_text, relative_path) + destination_path = File.join(@settings.output_folder, relative_path) + WildcatUtils.write_file_if_different(destination_path, feed_text) + end - def posts_for_feed - recent_posts(@settings.feed_number_of_posts) - end + def posts_for_feed + recent_posts(@settings.feed_number_of_posts) + end - def posts_for_home_page - recent_posts(@settings.blog_number_of_posts) - end + def posts_for_home_page + recent_posts(@settings.blog_number_of_posts) + end end diff --git a/model/blog_archive.rb b/model/blog_archive.rb index 5c470e3..590503c 100644 --- a/model/blog_archive.rb +++ b/model/blog_archive.rb @@ -4,7 +4,6 @@ require_relative '../utilities/wildcat_utils' class BlogArchive - def initialize(settings, posts) @settings = settings @posts = posts @@ -20,105 +19,105 @@ def build private - def sort_into_years_and_months - @posts.each { |post| add_to_year(post) } - end - - def add_to_year(post) - year = post.pub_date.year - blog_year = @years[year] - if blog_year.nil? - blog_year = BlogYear.new(year) - @years[year] = blog_year + def sort_into_years_and_months + @posts.each { |post| add_to_year(post) } end - blog_year.add_post(post) - end - def build_single_post_pages - @posts.each { |post| build_single_post_page(post) } - end + def add_to_year(post) + year = post.pub_date.year + blog_year = @years[year] + if blog_year.nil? + blog_year = BlogYear.new(year) + @years[year] = blog_year + end + blog_year.add_post(post) + end - def build_single_post_page(post) - title = post.title - if title.nil? || title.empty? - title = post.pub_date.strftime("%d %b %Y %H:%M:%S %z") + def build_single_post_pages + @posts.each { |post| build_single_post_page(post) } end - context = {} - context[CONTEXT_TITLE_KEY] = title - context[CONTEXT_CONTENT_HTML_KEY] = post.to_html(false) # not including permalink - PageBuilder.build(@settings, 'archive_single_post', context, post.destination_path) - end + def build_single_post_page(post) + title = post.title + if title.nil? || title.empty? + title = post.pub_date.strftime('%d %b %Y %H:%M:%S %z') + end - def build_month_pages - @years.values.each { |blog_year| build_month_pages_for_year(blog_year) } - end - - def build_month_pages_for_year(blog_year) - blog_year.months.values.each { |blog_month| build_month_page(blog_year, blog_month) } - end + context = {} + context[CONTEXT_TITLE_KEY] = title + context[CONTEXT_CONTENT_HTML_KEY] = post.to_html(false) # not including permalink + PageBuilder.build(@settings, 'archive_single_post', context, post.destination_path) + end - def build_month_page(blog_year, blog_month) - context = {} - month_name = @settings.blog_month_names[blog_month.month - 1] - context[CONTEXT_TITLE_KEY] = "#{month_name} #{blog_year.year}" - context[CONTEXT_CONTENT_HTML_KEY] = blog_month.to_html - - relative_path = month_page_relative_path(blog_year, blog_month) - destination_path = File.join(@settings.blog_output_folder, relative_path) - destination_path = File.join(destination_path, 'index') - destination_path = WildcatUtils.add_suffix_if_needed(destination_path, @settings.output_file_suffix) - PageBuilder.build(@settings, 'archive_month', context, destination_path) - end + def build_month_pages + @years.values.each { |blog_year| build_month_pages_for_year(blog_year) } + end - def build_index_page - context = {} - context[CONTEXT_TITLE_KEY] = @settings.blog_archive_title - context[CONTEXT_CONTENT_HTML_KEY] = archive_index_html + def build_month_pages_for_year(blog_year) + blog_year.months.values.each { |blog_month| build_month_page(blog_year, blog_month) } + end - destination_path = File.join(@settings.blog_output_folder, 'archive') - destination_path = WildcatUtils.add_suffix_if_needed(destination_path, @settings.output_file_suffix) - PageBuilder.build(@settings, 'archive_index', context, destination_path) - end + def build_month_page(blog_year, blog_month) + context = {} + month_name = @settings.blog_month_names[blog_month.month - 1] + context[CONTEXT_TITLE_KEY] = "#{month_name} #{blog_year.year}" + context[CONTEXT_CONTENT_HTML_KEY] = blog_month.to_html + + relative_path = month_page_relative_path(blog_year, blog_month) + destination_path = File.join(@settings.blog_output_folder, relative_path) + destination_path = File.join(destination_path, 'index') + destination_path = WildcatUtils.add_suffix_if_needed(destination_path, @settings.output_file_suffix) + PageBuilder.build(@settings, 'archive_month', context, destination_path) + end - def sorted_years - years = @years.values.sort_by { |blog_year| blog_year.year } - years.reverse - end + def build_index_page + context = {} + context[CONTEXT_TITLE_KEY] = @settings.blog_archive_title + context[CONTEXT_CONTENT_HTML_KEY] = archive_index_html - def archive_index_html - html = '' - for blog_year in sorted_years - html = html + render_year_index(blog_year) + destination_path = File.join(@settings.blog_output_folder, 'archive') + destination_path = WildcatUtils.add_suffix_if_needed(destination_path, @settings.output_file_suffix) + PageBuilder.build(@settings, 'archive_index', context, destination_path) end - html - end - def render_year_index(blog_year) - sorted_months = blog_year.months.values.sort_by { |blog_month| blog_month.month } - sorted_months.reverse! + def sorted_years + years = @years.values.sort_by(&:year) + years.reverse + end - html = "

#{blog_year.year}

\n\n" - html - end + def render_year_index(blog_year) + sorted_months = blog_year.months.values.sort_by(&:month) + sorted_months.reverse! - def render_month_item(blog_year, blog_month) - month_name = @settings.blog_month_names[blog_month.month - 1] - url = month_item_link(blog_year, blog_month) - "
  • #{month_name}
  • \n" - end + html = "

    #{blog_year.year}

    \n\n" + html + end - def month_page_relative_path(blog_year, blog_month) - month_string = blog_month.month.to_s.rjust(2, '0') - "#{blog_year.year}/#{month_string}/" - end + def render_month_item(blog_year, blog_month) + month_name = @settings.blog_month_names[blog_month.month - 1] + url = month_item_link(blog_year, blog_month) + "
  • #{month_name}
  • \n" + end + + def month_item_link(blog_year, blog_month) + month_page_relative_path(blog_year, blog_month) + end + + def month_page_relative_path(blog_year, blog_month) + month_string = blog_month.month.to_s.rjust(2, '0') + "#{blog_year.year}/#{month_string}/" + end end diff --git a/model/blog_month.rb b/model/blog_month.rb index 7c2ed97..7f5bca8 100644 --- a/model/blog_month.rb +++ b/model/blog_month.rb @@ -1,7 +1,6 @@ require_relative 'post' class BlogMonth - attr_reader :month def initialize(month) @@ -15,7 +14,7 @@ def add_post(post) def to_html html = '' - @posts.each { |post| html+= post.to_html(true) } + @posts.each { |post| html += post.to_html(true) } html end end diff --git a/model/blog_year.rb b/model/blog_year.rb index 92e966f..d85189c 100644 --- a/model/blog_year.rb +++ b/model/blog_year.rb @@ -2,17 +2,15 @@ require_relative 'blog_month' class BlogYear + attr_reader :year + attr_reader :months - attr_reader :year - attr_reader :months - - def initialize(year) - - @year = year - @months = {} - end + def initialize(year) + @year = year + @months = {} + end - def add_post(post) + def add_post(post) month_num = post.pub_date.month blog_month = @months[month_num] @@ -22,13 +20,11 @@ def add_post(post) end blog_month.add_post(post) - end + end - private + private - def add_month(month_num) - if !@months.include?(month_num) - @months.push(month_num) + def add_month(month_num) + @months.push(month_num) unless @months.include?(month_num) end - end end diff --git a/model/enclosure.rb b/model/enclosure.rb index a8e9b3f..ecd176a 100644 --- a/model/enclosure.rb +++ b/model/enclosure.rb @@ -1,14 +1,13 @@ require_relative '../wildcat_constants' class Enclosure - attr_reader :url attr_reader :mime_type attr_reader :size_in_bytes # String - ENCLOSURE_URL_KEY = 'enclosure' - ENCLOSURE_TYPE_KEY = 'enclosureType' - ENCLOSURE_LENGTH_KEY = 'enclosureLength' + ENCLOSURE_URL_KEY = 'enclosure'.freeze + ENCLOSURE_TYPE_KEY = 'enclosureType'.freeze + ENCLOSURE_LENGTH_KEY = 'enclosureLength'.freeze def initialize(attributes) @url = attributes[ENCLOSURE_URL_KEY] @@ -17,14 +16,13 @@ def initialize(attributes) end def to_json_feed_component - - if url.nil? || url.empty? then return nil end + return nil if url.nil? || url.empty? json = {} json[JSON_FEED_ENCLOSURE_URL] = @url add_if_not_empty(json, JSON_FEED_ENCLOSURE_MIME_TYPE, @mime_type) - if !@size_in_bytes.nil? + unless @size_in_bytes.nil? json[JSON_FEED_ENCLOSURE_SIZE_IN_BYTES] = @size_in_bytes.to_i end @@ -33,7 +31,7 @@ def to_json_feed_component private - def add_if_not_empty(hash, key, value) - hash[key] = value unless (value.nil? || value.empty?) - end + def add_if_not_empty(hash, key, value) + hash[key] = value unless value.nil? || value.empty? + end end diff --git a/model/json_feed.rb b/model/json_feed.rb index a73ccab..dd9745c 100644 --- a/model/json_feed.rb +++ b/model/json_feed.rb @@ -1,7 +1,6 @@ require 'json' class JSONFeed - def self.rendered_feed(settings, posts) feed = JSONFeed.new(settings, posts) feed.to_text @@ -18,46 +17,45 @@ def to_text private - def build_feed - json_data = {} - add_header(json_data) - add_posts(json_data) - JSON.pretty_generate(json_data) - end - - def add_header(json_data) - - json_data['version'] = 'https://jsonfeed.org/version/1' - - json_data['user_comment'] = "This feed allows you to read the posts from this site in any feed reader that supports the JSON Feed format. To add this feed to your reader, copy the following URL — #{@settings.feed_url} — and add it your reader." - - json_data['title'] = @settings.feed_title - json_data['description'] = @settings.feed_description - json_data['home_page_url'] = @settings.blog_url - json_data['feed_url'] = @settings.feed_url - - add_if_not_empty(json_data, 'favicon', @settings.favicon_url) - add_if_not_empty(json_data, 'icon', @settings.icon_url) - add_if_not_empty(json_data, 'author', author) - end - - def add_posts(json_data) - items = @posts.map { |post| post.to_json_feed_component } - json_data['items'] = items - end - - def author - author_name = @settings.feed_author_name - if !author_name || author_name.empty? then return end - - author_json = {} - author_json['name'] = author_name - add_if_not_empty(author_json, 'url', @settings.feed_author_url) - add_if_not_empty(author_json, 'avatar', @settings.feed_author_avatar_url) - author_json - end - - def add_if_not_empty(hash, key, value) - hash[key] = value unless (value.nil? || value.empty?) - end + def build_feed + json_data = {} + add_header(json_data) + add_posts(json_data) + JSON.pretty_generate(json_data) + end + + def add_header(json_data) + json_data['version'] = 'https://jsonfeed.org/version/1' + + json_data['user_comment'] = "This feed allows you to read the posts from this site in any feed reader that supports the JSON Feed format. To add this feed to your reader, copy the following URL — #{@settings.feed_url} — and add it your reader." + + json_data['title'] = @settings.feed_title + json_data['description'] = @settings.feed_description + json_data['home_page_url'] = @settings.blog_url + json_data['feed_url'] = @settings.feed_url + + add_if_not_empty(json_data, 'favicon', @settings.favicon_url) + add_if_not_empty(json_data, 'icon', @settings.icon_url) + add_if_not_empty(json_data, 'author', author) + end + + def add_posts(json_data) + items = @posts.map(&:to_json_feed_component) + json_data['items'] = items + end + + def author + author_name = @settings.feed_author_name + return if !author_name || author_name.empty? + + author_json = {} + author_json['name'] = author_name + add_if_not_empty(author_json, 'url', @settings.feed_author_url) + add_if_not_empty(author_json, 'avatar', @settings.feed_author_avatar_url) + author_json + end + + def add_if_not_empty(hash, key, value) + hash[key] = value unless value.nil? || value.empty? + end end diff --git a/model/page.rb b/model/page.rb index 17ebafc..3cf21f6 100644 --- a/model/page.rb +++ b/model/page.rb @@ -2,10 +2,9 @@ require_relative '../renderer/renderer' class Page - def self.build_all_pages(settings) pages = all_pages(settings) - pages.each { |page| page.build } + pages.each(&:build) end def build @@ -14,37 +13,36 @@ def build private - def self.all_pages(settings) - paths = WildcatUtils.text_source_files_in_folder(settings.pages_folder) - paths.map { |path| Page.new(settings, WildcatFile.new(path)) } - end + def self.all_pages(settings) + paths = WildcatUtils.text_source_files_in_folder(settings.pages_folder) + paths.map { |path| Page.new(settings, WildcatFile.new(path)) } + end - def initialize(settings, wildcat_file) - @path = wildcat_file.path - @settings = settings - @output_path, @permalink, _ = WildcatUtils.paths(@path, settings.pages_folder, settings.output_folder, settings.site_url, settings.output_file_suffix) - @content_html = wildcat_file.to_html - @title = wildcat_file.attributes[TITLE_KEY] - @pub_date = wildcat_file.attributes[PUB_DATE_KEY] - end + def initialize(settings, wildcat_file) + @path = wildcat_file.path + @settings = settings + @output_path, @permalink, = WildcatUtils.paths(@path, settings.pages_folder, settings.output_folder, settings.site_url, settings.output_file_suffix) + @content_html = wildcat_file.to_html + @title = wildcat_file.attributes[TITLE_KEY] + @pub_date = wildcat_file.attributes[PUB_DATE_KEY] + end - def context + def context + context = {} - context = {} + context[CONTEXT_PERMALINK_KEY] = @permalink + context[CONTEXT_TITLE_KEY] = @title + context[CONTEXT_CONTENT_HTML_KEY] = @content_html - context[CONTEXT_PERMALINK_KEY] = @permalink - context[CONTEXT_TITLE_KEY] = @title - context[CONTEXT_CONTENT_HTML_KEY] = @content_html + unless @pub_date.nil? + context[CONTEXT_DISPLAY_DATE_KEY] = @pub_date.strftime('%d %b %Y') + end - if !@pub_date.nil? - context[CONTEXT_DISPLAY_DATE_KEY] = @pub_date.strftime("%d %b %Y") + context end - context - end - - def to_html - renderer = Renderer.new(@settings, 'page', context) - renderer.to_s - end + def to_html + renderer = Renderer.new(@settings, 'page', context) + renderer.to_s + end end diff --git a/model/post.rb b/model/post.rb index 0e89ff1..6cce288 100644 --- a/model/post.rb +++ b/model/post.rb @@ -3,7 +3,6 @@ require_relative 'enclosure' class Post - attr_reader :title attr_reader :content_html attr_reader :source_text @@ -31,9 +30,7 @@ def initialize(settings, wildcat_file) @title = @attributes[TITLE_KEY] @content_html = wildcat_file.to_html - if !@content_html.start_with?('

    ') - @content_html = '

    ' + content_html - end + @content_html = '

    ' + content_html unless @content_html.start_with?('

    ') @pub_date = @attributes[PUB_DATE_KEY] @mod_date = @attributes[MOD_DATE_KEY] @@ -41,22 +38,18 @@ def initialize(settings, wildcat_file) @rendered_html = nil enclosure_url = @attributes[ENCLOSURE_URL_KEY] - if !enclosure_url.nil? && !enclosure_url.empty? - @enclosure = Enclosure.new(@attributes) - else - @enclosure = nil - end + @enclosure = if !enclosure_url.nil? && !enclosure_url.empty? + Enclosure.new(@attributes) + end @itunes_duration = @attributes[ITUNES_DURATION_KEY] @itunes_subtitle = @attributes[ITUNES_SUBTITLE_KEY] @itunes_summary = @attributes[ITUNES_SUMMARY_KEY] @itunes_explicit = @attributes[ITUNES_EXPLICIT_KEY] @media_thumbnail = @attributes[MEDIA_THUMBNAIL_KEY] - end def to_json_feed_component - json = {} add_if_not_empty(json, JSON_FEED_TITLE_KEY, @title) @@ -71,7 +64,7 @@ def to_json_feed_component json[JSON_FEED_CONTENT_HTML_KEY] = @content_html - if !@enclosure.nil? + unless @enclosure.nil? enclosure_json = @enclosure.to_json_feed_component json[JSON_FEED_ATTACHMENTS_KEY] = [enclosure_json] end @@ -80,15 +73,14 @@ def to_json_feed_component end def to_html(including_link) - # Render post. # If including_link is true, then this is for the home page or other multi-post page. # If including_link is false, then this is the single-post-on-a-page version. Where the permalink points to. if including_link - if @rendered_html_including_link then return @rendered_html_including_link end + return @rendered_html_including_link if @rendered_html_including_link else - if @rendered_html then return @rendered_html end + return @rendered_html if @rendered_html end template_name = template_name(including_link) @@ -103,53 +95,49 @@ def to_html(including_link) s end - private - def add_if_not_empty(json, key, value) - json[key] = value unless (!value || value.empty?) - end + def add_if_not_empty(json, key, value) + json[key] = value unless !value || value.empty? + end - def template_name(including_link) + def template_name(including_link) + # A post may not have a title. There are four possible templates: + # post + # post_including_link + # post_no_title + # post_including_link_no_title - # A post may not have a title. There are four possible templates: - # post - # post_including_link - # post_no_title - # post_including_link_no_title + template_name = 'post' - template_name = 'post' + template_name += '_including_link' if including_link + template_name += '_no_title' if @title.nil? || @title.empty? - if including_link then template_name += '_including_link' end - if @title.nil? || @title.empty? then template_name += '_no_title' end + template_name + end - template_name - end + def context + context = {} - def context + context[CONTEXT_PERMALINK_KEY] = @permalink + context[CONTEXT_EXTERNAL_URL_KEY] = @external_url - context = {} + context[CONTEXT_LINK_PREFERRING_EXTERNAL_URL_KEY] = if !@external_url.nil? + @external_url + else + @permalink + end - context[CONTEXT_PERMALINK_KEY] = @permalink - context[CONTEXT_EXTERNAL_URL_KEY] = @external_url + context[CONTEXT_TITLE_KEY] = @title + context[CONTEXT_CONTENT_HTML_KEY] = @content_html + context[CONTEXT_PUB_DATE_KEY] = @pub_date + context[CONTEXT_DISPLAY_DATE_KEY] = @pub_date.strftime('%d %b %Y') - if !@external_url.nil? - context[CONTEXT_LINK_PREFERRING_EXTERNAL_URL_KEY] = @external_url - else - context[CONTEXT_LINK_PREFERRING_EXTERNAL_URL_KEY] = @permalink + context end - context[CONTEXT_TITLE_KEY] = @title - context[CONTEXT_CONTENT_HTML_KEY] = @content_html - context[CONTEXT_PUB_DATE_KEY] = @pub_date - context[CONTEXT_DISPLAY_DATE_KEY] = @pub_date.strftime("%d %b %Y") - - context - end - - def render_with_template(template_name) - - renderer = Renderer.new(@settings, template_name, context) - renderer.to_s + "\n" - end + def render_with_template(template_name) + renderer = Renderer.new(@settings, template_name, context) + renderer.to_s + "\n" + end end diff --git a/model/rss_feed.rb b/model/rss_feed.rb index cec2696..2e1812e 100644 --- a/model/rss_feed.rb +++ b/model/rss_feed.rb @@ -1,12 +1,11 @@ class RSSFeed - # 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' + ITEMS_KEY = 'items'.freeze + SITE_NAME_KEY = 'site_name'.freeze + SITE_URL_KEY = 'site_url'.freeze + FEED_DESCRIPTION_KEY = 'feed_description'.freeze def self.rendered_feed(settings, posts) feed = RSSFeed.new(settings, posts) @@ -34,36 +33,35 @@ def to_s private - def render_post(post) - rss_item = RSSItem.new(@settings, post, 2) - rss_item.to_s + "\n" - end + def render_post(post) + rss_item = RSSItem.new(@settings, post, 2) + rss_item.to_s + "\n" + end end class RSSItem - - 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' + TITLE_TAG = 'title'.freeze + DESCRIPTION_TAG = 'description'.freeze + ITEM_TAG = 'item'.freeze + LINK_TAG = 'link'.freeze + GUID_TAG = 'guid'.freeze + PUB_DATE_TAG = 'pubDate'.freeze + ENCLOSURE_TAG = 'enclosure'.freeze + URL_ATTRIBUTE = 'url'.freeze + ENCLOSURE_LENGTH_ATTRIBUTE = 'length'.freeze + ENCLOSURE_TYPE_ATTRIBUTE = 'type'.freeze + ITUNES_AUTHOR_TAG = 'itunes:author'.freeze + ITUNES_SUMMARY_TAG = 'itunes:summary'.freeze + ITUNES_KEYWORDS_TAG = 'itunes:keywords'.freeze + ITUNES_EXPLICIT_TAG = 'itunes:explicit'.freeze + ITUNES_IMAGE_TAG = 'itunes:image'.freeze + HREF_ATTRIBUTE = 'href'.freeze + ITUNES_OWNER_TAG = 'itunes:owner'.freeze + ITUNES_NAME_TAG = 'itunes:name'.freeze + ITUNES_EMAIL_TAG = 'itunes:email'.freeze + ITUNES_DURATION_TAG = 'itunes:duration'.freeze + ITUNES_SUBTITLE_TAG = 'itunes:subtitle'.freeze + MEDIA_THUMBNAIL_TAG = 'media:thumbnail'.freeze def initialize(settings, post, indent_level) @settings = settings @@ -75,9 +73,7 @@ def initialize(settings, post, indent_level) def to_s push_stand_alone_tag(ITEM_TAG) @indent_level += 1 - if !@post.title.nil? - push_tag_with_value(TITLE_TAG, @post.title) - end + push_tag_with_value(TITLE_TAG, @post.title) unless @post.title.nil? if @post.external_url.nil? push_tag_with_value(LINK_TAG, @post.permalink) @@ -87,7 +83,7 @@ def to_s 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) @@ -128,9 +124,7 @@ def push_attribute(name, value) end def push_indents - if @indent_level > 0 - (1..@indent_level).each { push(' ') } - end + (1..@indent_level).each { push(' ') } if @indent_level.positive? end def push_stand_alone_tag(tag) @@ -154,7 +148,7 @@ def push_stand_alone_closing_tag(tag) end def push_tag_with_value_if_not_empty(tag, value) - if value.nil? || value.empty? then return end + return if value.nil? || value.empty? push_tag_with_value(tag, value) end @@ -174,7 +168,6 @@ def push(s) end end - def xmlize(s) xml = s xml.gsub!('&', '&') @@ -185,4 +178,3 @@ def xmlize(s) xml.strip! xml end - diff --git a/model/website.rb b/model/website.rb index e8b093a..7896af6 100644 --- a/model/website.rb +++ b/model/website.rb @@ -3,51 +3,45 @@ require_relative '../utilities/wildcat_utils' class Website - attr_reader :blog def initialize(settings) @settings = settings - if @settings.has_blog - @blog = Blog.new(@settings) - end + @blog = Blog.new(@settings) if @settings.has_blog end def build - build_pages - if @settings.has_blog - @blog.build - end + @blog.build if @settings.has_blog copy_files end private - def build_pages - Page.build_all_pages(@settings) - end + def build_pages + Page.build_all_pages(@settings) + end - # Files in images/, styles/, and downloads/ are copied - # to corresponding folders in output. + # Files in images/, styles/, and downloads/ are copied + # to corresponding folders in output. - def copy_files - copy_images - copy_downloads - copy_style_sheets - end + def copy_files + copy_images + copy_downloads + copy_style_sheets + end - def copy_images - WildcatUtils.rsync_local(@settings.images_folder, @settings.images_destination) - end + def copy_images + WildcatUtils.rsync_local(@settings.images_folder, @settings.images_destination) + end - def copy_style_sheets - WildcatUtils.rsync_local(@settings.styles_folder, @settings.styles_destination) - end + def copy_style_sheets + WildcatUtils.rsync_local(@settings.styles_folder, @settings.styles_destination) + end - def copy_downloads - WildcatUtils.rsync_local(@settings.downloads_folder, @settings.downloads_destination) - end + def copy_downloads + WildcatUtils.rsync_local(@settings.downloads_folder, @settings.downloads_destination) + end end diff --git a/model/website_settings.rb b/model/website_settings.rb index 29acc87..c38ad80 100644 --- a/model/website_settings.rb +++ b/model/website_settings.rb @@ -1,7 +1,6 @@ require_relative '../utilities/wildcat_file' class WebsiteSettings - attr_reader :attributes # everything attr_reader :project_folder attr_reader :site_name @@ -42,36 +41,36 @@ class WebsiteSettings attr_reader :feed_url attr_reader :feed_media_thumbnail - SITE_NAME_KEY = 'site_name' - SITE_URL_KEY = 'site_url' - OUTPUT_FILE_SUFFIX_KEY = 'output_file_suffix' - OUTPUT_FOLDER_KEY = 'output_folder' - RSYNC_REMOTE_PATH_KEY = 'rsync_remote_path' - FAVICON_URL_KEY = 'favicon_url' - ICON_URL_KEY = 'icon_url' - HAS_BLOG_KEY = 'has_blog' - - BLOG_NUMBER_OF_POSTS_KEY = 'blog_number_of_posts_on_home_page' - BLOG_HOME_PAGE_TITLE_KEY = 'blog_home_page_title' - BLOG_ARCHIVE_TITLE_KEY = 'blog_archive_title' - BLOG_RELATIVE_PATH_KEY = 'blog_relative_path' - BLOG_MONTH_NAMES_KEY = 'blog_month_names' - - FEED_NUMBER_OF_POSTS_KEY = 'feed_number_of_posts' - FEED_TITLE_KEY = 'feed_title' - FEED_DESCRIPTION_KEY = 'feed_description' - FEED_AUTHOR_NAME_KEY = 'feed_author' - FEED_AUTHOR_URL_KEY = 'feed_author_url' - FEED_AUTHOR_AVATAR_KEY = 'feed_author_avatar_url' - FEED_MEDIA_THUMBNAIL_KEY = 'feed_media_thumbnail' - - FOLDER_NAME_POSTS = 'posts' - FOLDER_NAME_PAGES = 'pages' - FOLDER_NAME_TEMPLATES = 'templates' - FOLDER_NAME_SNIPPETS = 'snippets' - FOLDER_NAME_IMAGES = 'images' - FOLDER_NAME_STYLES = 'styles' - FOLDER_NAME_DOWNLOADS = 'downloads' + SITE_NAME_KEY = 'site_name'.freeze + SITE_URL_KEY = 'site_url'.freeze + OUTPUT_FILE_SUFFIX_KEY = 'output_file_suffix'.freeze + OUTPUT_FOLDER_KEY = 'output_folder'.freeze + RSYNC_REMOTE_PATH_KEY = 'rsync_remote_path'.freeze + FAVICON_URL_KEY = 'favicon_url'.freeze + ICON_URL_KEY = 'icon_url'.freeze + HAS_BLOG_KEY = 'has_blog'.freeze + + BLOG_NUMBER_OF_POSTS_KEY = 'blog_number_of_posts_on_home_page'.freeze + BLOG_HOME_PAGE_TITLE_KEY = 'blog_home_page_title'.freeze + BLOG_ARCHIVE_TITLE_KEY = 'blog_archive_title'.freeze + BLOG_RELATIVE_PATH_KEY = 'blog_relative_path'.freeze + BLOG_MONTH_NAMES_KEY = 'blog_month_names'.freeze + + FEED_NUMBER_OF_POSTS_KEY = 'feed_number_of_posts'.freeze + FEED_TITLE_KEY = 'feed_title'.freeze + FEED_DESCRIPTION_KEY = 'feed_description'.freeze + FEED_AUTHOR_NAME_KEY = 'feed_author'.freeze + FEED_AUTHOR_URL_KEY = 'feed_author_url'.freeze + FEED_AUTHOR_AVATAR_KEY = 'feed_author_avatar_url'.freeze + FEED_MEDIA_THUMBNAIL_KEY = 'feed_media_thumbnail'.freeze + + FOLDER_NAME_POSTS = 'posts'.freeze + FOLDER_NAME_PAGES = 'pages'.freeze + FOLDER_NAME_TEMPLATES = 'templates'.freeze + FOLDER_NAME_SNIPPETS = 'snippets'.freeze + FOLDER_NAME_IMAGES = 'images'.freeze + FOLDER_NAME_STYLES = 'styles'.freeze + FOLDER_NAME_DOWNLOADS = 'downloads'.freeze def initialize(project_folder, settings_file_path) @project_folder = project_folder @@ -112,7 +111,7 @@ def initialize(project_folder, settings_file_path) if !@blog_relative_path.nil? && !@blog_relative_path.empty? @blog_url = File.join(blog_url, @blog_relative_path) end - @blog_month_names = @attributes.fetch(BLOG_MONTH_NAMES_KEY, ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']) + @blog_month_names = @attributes.fetch(BLOG_MONTH_NAMES_KEY, %w[January February March April May June July August September October November December]) @feed_number_of_posts = @attributes.fetch(FEED_NUMBER_OF_POSTS_KEY, 20) @feed_title = @attributes.fetch(FEED_TITLE_KEY, @site_name) diff --git a/renderer/page_builder.rb b/renderer/page_builder.rb index 81c4134..b1d09ba 100644 --- a/renderer/page_builder.rb +++ b/renderer/page_builder.rb @@ -2,8 +2,7 @@ require_relative '../model/website_settings' module PageBuilder - - def PageBuilder.build(settings, template_name, context, destination_path) + def self.build(settings, template_name, context, destination_path) renderer = Renderer.new(settings, template_name, context) WildcatUtils.write_file_if_different(destination_path, renderer.to_s) end diff --git a/renderer/renderer.rb b/renderer/renderer.rb index 2c002e8..6519b59 100644 --- a/renderer/renderer.rb +++ b/renderer/renderer.rb @@ -1,23 +1,21 @@ require_relative '../utilities/wildcat_utils' class Renderer - @@templates = {} @@snippets = {} - BEGIN_SNIPPET_CHARACTERS = "[[=" + BEGIN_SNIPPET_CHARACTERS = '[[='.freeze BEGIN_SNIPPET_CHARACTERS_COUNT = 3 - END_SNIPPET_CHARACTERS = "]]" + END_SNIPPET_CHARACTERS = ']]'.freeze END_SNIPPET_CHARACTERS_COUNT = 2 - BEGIN_SUBSTITUTION_CHARACTERS = "[[@" + BEGIN_SUBSTITUTION_CHARACTERS = '[[@'.freeze BEGIN_SUBSTITUTION_CHARACTERS_COUNT = 3 - END_SUBSTITUTION_CHARACTERS = "]]" + END_SUBSTITUTION_CHARACTERS = ']]'.freeze END_SUBSTITUTION_CHARACTERS_COUNT = 2 - UNDEFINED_SUBSTITUTION = '[ERROR: UNDEFINED SUBSTITUTION]' + UNDEFINED_SUBSTITUTION = '[ERROR: UNDEFINED SUBSTITUTION]'.freeze def initialize(settings, template_name, context) - @settings = settings @context = context @@ -37,69 +35,66 @@ def to_s private - def read_template(template_name) - path = File.join(@settings.templates_folder, template_name) - if !FileTest.exist?(path) - path = File.join(@settings.templates_folder, template_name + '.html') + def read_template(template_name) + path = File.join(@settings.templates_folder, template_name) + unless FileTest.exist?(path) + path = File.join(@settings.templates_folder, template_name + '.html') + end + WildcatUtils.read_text_file(path) end - WildcatUtils.read_text_file(path) - end - def read_snippet(snippet_filename) - text = @@snippets[snippet_filename] - if !text.nil? then return text end + def read_snippet(snippet_filename) + text = @@snippets[snippet_filename] + return text unless text.nil? - path = File.join(@settings.snippets_folder, snippet_filename) - snippet_text = WildcatUtils.read_text_file(path) - @@snippets[snippet_filename] = snippet_text + path = File.join(@settings.snippets_folder, snippet_filename) + snippet_text = WildcatUtils.read_text_file(path) + @@snippets[snippet_filename] = snippet_text - snippet_text.dup - end - - def process_snippets(text) - indexesOfCharacters(text, BEGIN_SNIPPET_CHARACTERS).reverse_each {|index| process_one_snippet(text, index)} - end + snippet_text.dup + end - def process_one_snippet(text, ix) - ix_end = text.index(END_SNIPPET_CHARACTERS, ix) - if ix_end.nil? then return end - snippet_filename = text[ix + BEGIN_SNIPPET_CHARACTERS_COUNT, ix_end - (ix + BEGIN_SNIPPET_CHARACTERS_COUNT)] - snippet_text = read_snippet(snippet_filename) - if snippet_text.nil? then return end - process_snippets(snippet_text) - text[ix, (ix_end + END_SNIPPET_CHARACTERS_COUNT) - ix] = snippet_text - end + def process_snippets(text) + indexes_of_characters(text, BEGIN_SNIPPET_CHARACTERS).reverse_each { |index| process_one_snippet(text, index) } + end - def process_substitutions(text) - indexesOfCharacters(text, BEGIN_SUBSTITUTION_CHARACTERS).reverse_each {|ix| process_one_substitution(text, ix)} - text - end + def process_one_snippet(text, ix) + ix_end = text.index(END_SNIPPET_CHARACTERS, ix) + return if ix_end.nil? + snippet_filename = text[ix + BEGIN_SNIPPET_CHARACTERS_COUNT, ix_end - (ix + BEGIN_SNIPPET_CHARACTERS_COUNT)] + snippet_text = read_snippet(snippet_filename) + return if snippet_text.nil? + process_snippets(snippet_text) + text[ix, (ix_end + END_SNIPPET_CHARACTERS_COUNT) - ix] = snippet_text + end - def process_one_substitution(text, ix) - ix_end = text.index(END_SUBSTITUTION_CHARACTERS, ix) - if ix_end == nil then return end - substitution = text[ix + BEGIN_SUBSTITUTION_CHARACTERS_COUNT, ix_end - (ix + BEGIN_SUBSTITUTION_CHARACTERS_COUNT)] - result = @context[substitution] - if result.nil? - result = UNDEFINED_SUBSTITUTION - WildcatUtils.print_to_console("Undefined substitution: #{substitution}") + def process_substitutions(text) + indexes_of_characters(text, BEGIN_SUBSTITUTION_CHARACTERS).reverse_each { |ix| process_one_substitution(text, ix) } + text end - text[ix, (ix_end + END_SUBSTITUTION_CHARACTERS_COUNT) - ix] = result - end - def indexesOfCharacters(text, searchFor) - ix = 0 - indexes = Array.new - while true - ix = text.index(searchFor, ix) - if ix == nil - break + def process_one_substitution(text, ix) + ix_end = text.index(END_SUBSTITUTION_CHARACTERS, ix) + return if ix_end.nil? + substitution = text[ix + BEGIN_SUBSTITUTION_CHARACTERS_COUNT, ix_end - (ix + BEGIN_SUBSTITUTION_CHARACTERS_COUNT)] + result = @context[substitution] + if result.nil? + result = UNDEFINED_SUBSTITUTION + WildcatUtils.print_to_console("Undefined substitution: #{substitution}") end - if ix == 0 || text[ix-1,1] != "\\" #escape char is \ - indexes << ix + text[ix, (ix_end + END_SUBSTITUTION_CHARACTERS_COUNT) - ix] = result + end + + def indexes_of_characters(text, search_for) + ix = 0 + indexes = [] + + loop do + ix = text.index(search_for, ix) + break if ix.nil? + indexes << ix if ix.zero? || text[ix - 1, 1] != '\\' # escape char is \ + ix += 1 end - ix = ix + 1 + indexes end - return indexes - end end diff --git a/utilities/file_parser.rb b/utilities/file_parser.rb index 1ef3fae..7311ac5 100644 --- a/utilities/file_parser.rb +++ b/utilities/file_parser.rb @@ -7,12 +7,11 @@ require 'fileutils' module FileParser - def self.attributes_and_text(path) text = read_whole_file(path) attributes = attributes_from_text(text) body = body_from_text(text) - return attributes, body + [attributes, body] end def self.read_whole_file(path) @@ -24,53 +23,50 @@ def self.read_whole_file(path) private - def self.attributes_from_text(text) - - attributes = {} + def self.attributes_from_text(text) + attributes = {} - text.each_line do |line| - one_key, one_value = key_value_with_line(line) - if one_key.nil? - break - else - attributes[one_key] = one_value + text.each_line do |line| + one_key, one_value = key_value_with_line(line) + if one_key.nil? + break + else + attributes[one_key] = one_value + end end - end - attributes - end + attributes + end - def self.body_from_text(text) - # Remove @attributes. - ix = text.index(/^[^@]/) - if ix == nil then return "" end - text[0,ix] = "" - text - end + def self.body_from_text(text) + # Remove @attributes. + ix = text.index(/^[^@]/) + return '' if ix.nil? + text[0, ix] = '' + text + end - def self.key_value_with_line(line) - if line[0,1] != '@' then return nil, nil end + def self.key_value_with_line(line) + return nil, nil if line[0, 1] != '@' - index_of_space = line.index(' ') - if index_of_space == nil then return nil, nil end + index_of_space = line.index(' ') + return nil, nil if index_of_space.nil? - key = line[1, index_of_space - 1] - value = line[index_of_space + 1, line.length - (index_of_space + 1)] - value.strip! + key = line[1, index_of_space - 1] + value = line[index_of_space + 1, line.length - (index_of_space + 1)] + value.strip! - if /\D/.match(value) == nil && key != 'title' #it's an integer - value = value.to_i - end + value = value.to_i if /\D/.match(value).nil? && key != 'title' # it's an integer - if value == '(empty-string)' then value = '' end + value = '' if value == '(empty-string)' - if /Date$/.match(key) != nil then value = Time.parse(value) end + value = Time.parse(value) unless /Date$/.match(key).nil? - if /Array$/.match(key) != nil - value = value.split(', ') - value.map!(&:strip) - end + unless /Array$/.match(key).nil? + value = value.split(', ') + value.map!(&:strip) + end - return key, value - end + [key, value] + end end diff --git a/utilities/wildcat_auth.rb b/utilities/wildcat_auth.rb index c3fb399..dab7093 100644 --- a/utilities/wildcat_auth.rb +++ b/utilities/wildcat_auth.rb @@ -1,10 +1,9 @@ require 'argon2' module WildcatAuth - - def self.verify_password(password, hashed_password) - if password.nil? || password.empty? then return false end - if hashed_password.nil? || hashed_password.empty? then return false end - Argon2::Password.verify_password(password, hashed_password) - end + def self.verify_password(password, hashed_password) + return false if password.nil? || password.empty? + return false if hashed_password.nil? || hashed_password.empty? + Argon2::Password.verify_password(password, hashed_password) + end end diff --git a/utilities/wildcat_file.rb b/utilities/wildcat_file.rb index f009546..125b3f3 100644 --- a/utilities/wildcat_file.rb +++ b/utilities/wildcat_file.rb @@ -6,52 +6,49 @@ require_relative '../wildcat_constants' class WildcatFile - attr_reader :attributes attr_reader :text attr_reader :path - TEXT_TYPE_MARKDOWN = 'markdown' - TEXT_TYPE_HTML = 'html' - TEXT_TYPE_UNKNOWN = 'unknown' + TEXT_TYPE_MARKDOWN = 'markdown'.freeze + TEXT_TYPE_HTML = 'html'.freeze + TEXT_TYPE_UNKNOWN = 'unknown'.freeze def initialize(path) -# if cached_file = @@cache[path] then return cached_file end + # if cached_file = @@cache[path] then return cached_file end @path = path @text_type = text_type_from_path(path) @attributes, @text = FileParser.attributes_and_text(path) - @rendered_text = "" -# @@cache[path] = self + @rendered_text = '' + # @@cache[path] = self end def to_html - if @rendered_text.empty? - @rendered_text = render_text - end + @rendered_text = render_text if @rendered_text.empty? @rendered_text end private - @@cache = {} + @@cache = {} - def text_type_from_path(path) - if path.end_with?(MARKDOWN_SUFFIX) then return TEXT_TYPE_MARKDOWN end - if path.end_with?(HTML_SUFFIX) then return TEXT_TYPE_HTML end - return TEXT_TYPE_UNKNOWN - end + def text_type_from_path(path) + return TEXT_TYPE_MARKDOWN if path.end_with?(MARKDOWN_SUFFIX) + return TEXT_TYPE_HTML if path.end_with?(HTML_SUFFIX) + TEXT_TYPE_UNKNOWN + end - def render_text - if @text_type == TEXT_TYPE_MARKDOWN - Kramdown::Document.new( - @text, - input: :kramdown, - remove_block_html_tags: false, - transliterated_header_ids: true - ).to_html - else - @text + def render_text + if @text_type == TEXT_TYPE_MARKDOWN + Kramdown::Document.new( + @text, + input: :kramdown, + remove_block_html_tags: false, + transliterated_header_ids: true + ).to_html + else + @text + end end - end end diff --git a/utilities/wildcat_utils.rb b/utilities/wildcat_utils.rb index 416e414..3947cc1 100644 --- a/utilities/wildcat_utils.rb +++ b/utilities/wildcat_utils.rb @@ -4,16 +4,14 @@ require_relative '../wildcat_constants' module WildcatUtils - def self.write_file_if_different(path, text) - # Make sure the folder exists. # Skip writing the file if existing file has the same text. FileUtils.mkdir_p(File.dirname(path)) should_write_file = !FileTest.exist?(path) || !WildcatUtils.file_equals_string?(path, text) - if !should_write_file then return end + return unless should_write_file print_to_console("Writing #{path}") write_text_file(path, text) @@ -22,14 +20,14 @@ def self.write_file_if_different(path, text) def self.write_text_file(path, text) f = File.open(path, 'w:UTF-8') f.puts(text) - f.close() + f.close end def self.read_text_file(path) - file = File.open(path, 'r:UTF-8') - text = file.read() - file.close() - text + file = File.open(path, 'r:UTF-8') + text = file.read + file.close + text end def self.file_equals_string?(path, text) @@ -39,34 +37,29 @@ def self.file_equals_string?(path, text) def self.rsync_local(source, dest) FileUtils.mkdir_p(File.dirname(dest)) - Open3.popen3("rsync", "-azu", source, dest)[1].read + Open3.popen3('rsync', '-azu', source, dest)[1].read end def self.rsync_remote(source, dest) print_to_console("Syncing to #{dest}") - Open3.popen3("rsync", "-avzu", source, dest)[1].read + Open3.popen3('rsync', '-avzu', source, dest)[1].read end def self.files_in_folder(folder) - # Doesn’t look in folders that start with a . character. paths = [] Find.find(folder) do |f| - if File.basename(f)[0] == ?. - Find.prune - end - if !FileTest.directory?(f) - paths << f - end + Find.prune if File.basename(f)[0] == '.' + paths << f unless FileTest.directory?(f) end paths end def self.file_is_text_source_file?(path) - path.end_with?(MARKDOWN_SUFFIX) || path.end_with?(HTML_SUFFIX) + path.end_with?(MARKDOWN_SUFFIX, HTML_SUFFIX) end def self.text_source_files_in_folder(folder) @@ -75,7 +68,6 @@ def self.text_source_files_in_folder(folder) end def self.paths(path, input_folder, output_folder, site_url, output_file_suffix) - # Return destination file path *and* permalink. relative_path = path.dup @@ -87,11 +79,10 @@ def self.paths(path, input_folder, output_folder, site_url, output_file_suffix) permalink = File.join(site_url, relative_path) permalink = change_source_suffix_to_output_suffix(permalink, output_file_suffix) - return destination_path, permalink, relative_path + [destination_path, permalink, relative_path] end def self.change_source_suffix_to_output_suffix(path, output_suffix) - # output_suffix should start with a . or be empty. # Only chops off .html and .markdown suffixes. @@ -106,7 +97,7 @@ def self.change_source_suffix_to_output_suffix(path, output_suffix) end def self.add_suffix_if_needed(path, suffix) - if suffix.nil? || suffix.empty? then return path end + return path if suffix.nil? || suffix.empty? path + suffix end diff --git a/wildcat.rb b/wildcat.rb index e599d3f..8711004 100755 --- a/wildcat.rb +++ b/wildcat.rb @@ -3,10 +3,9 @@ require_relative 'model/website_settings' require_relative 'model/website' -DEFAULT_SETTINGS_FILE_NAME = 'wildcat_settings' +DEFAULT_SETTINGS_FILE_NAME = 'wildcat_settings'.freeze class Wildcat - attr_reader :website attr_reader :settings @@ -20,19 +19,17 @@ def build perform_rsync_if_needed end - def Wildcat.settings_with_file_name(project_folder, file_name) - if file_name.nil? || file_name.empty? - file_name = DEFAULT_SETTINGS_FILE_NAME - end + def self.settings_with_file_name(project_folder, file_name) + file_name = DEFAULT_SETTINGS_FILE_NAME if file_name.nil? || file_name.empty? settings_file_path = File.join(project_folder, file_name) WebsiteSettings.new(project_folder, settings_file_path) end private - def perform_rsync_if_needed - rsync_path = @settings.rsync_remote_path - if rsync_path.nil? || rsync_path.empty? then return end - WildcatUtils.rsync_remote(@settings.output_folder, rsync_path) - end + def perform_rsync_if_needed + rsync_path = @settings.rsync_remote_path + return if rsync_path.nil? || rsync_path.empty? + WildcatUtils.rsync_remote(@settings.output_folder, rsync_path) + end end diff --git a/wildcat_constants.rb b/wildcat_constants.rb index 3dd6ebb..490d3b1 100644 --- a/wildcat_constants.rb +++ b/wildcat_constants.rb @@ -1,48 +1,47 @@ -ENV_KEY_WEBSITES_FOLDER = 'WILDCAT_WEBSITES_FOLDER' -ENV_KEY_USERNAME = 'WILDCAT_USERNAME' -ENV_KEY_HASHED_PASSWORD = 'WILDCAT_HASHED_PASSWORD' -ENV_KEY_RUNNING_AS_SERVER = 'WILDCAT_RUNNING_AS_SERVER' +ENV_KEY_WEBSITES_FOLDER = 'WILDCAT_WEBSITES_FOLDER'.freeze +ENV_KEY_USERNAME = 'WILDCAT_USERNAME'.freeze +ENV_KEY_HASHED_PASSWORD = 'WILDCAT_HASHED_PASSWORD'.freeze +ENV_KEY_RUNNING_AS_SERVER = 'WILDCAT_RUNNING_AS_SERVER'.freeze -MARKDOWN_SUFFIX = '.markdown' -HTML_SUFFIX = '.html' -MARKDOWN_NO_LEADING_DOT = 'markdown' -HTML_NO_LEADING_DOT = 'html' +MARKDOWN_SUFFIX = '.markdown'.freeze +HTML_SUFFIX = '.html'.freeze +MARKDOWN_NO_LEADING_DOT = 'markdown'.freeze +HTML_NO_LEADING_DOT = 'html'.freeze # These appear inside source files, at the top. -TITLE_KEY = 'title' -LINK_KEY = 'link' -PUB_DATE_KEY = 'pubDate' -MOD_DATE_KEY = 'modDate' -ENCLOSURE_URL_KEY = 'enclosure' -ENCLOSURE_TYPE_KEY = 'enclosureType' -ENCLOSURE_LENGTH_KEY = 'enclosureLength' -ITUNES_DURATION_KEY = 'itunesDuration' -ITUNES_SUBTITLE_KEY = 'itunesItemSubtitle' -ITUNES_SUMMARY_KEY = 'itunesItemSummary' -ITUNES_EXPLICIT_KEY = 'itunesExplicit' -MEDIA_THUMBNAIL_KEY = 'mediaThumbnail' +TITLE_KEY = 'title'.freeze +LINK_KEY = 'link'.freeze +PUB_DATE_KEY = 'pubDate'.freeze +MOD_DATE_KEY = 'modDate'.freeze +ENCLOSURE_URL_KEY = 'enclosure'.freeze +ENCLOSURE_TYPE_KEY = 'enclosureType'.freeze +ENCLOSURE_LENGTH_KEY = 'enclosureLength'.freeze +ITUNES_DURATION_KEY = 'itunesDuration'.freeze +ITUNES_SUBTITLE_KEY = 'itunesItemSubtitle'.freeze +ITUNES_SUMMARY_KEY = 'itunesItemSummary'.freeze +ITUNES_EXPLICIT_KEY = 'itunesExplicit'.freeze +MEDIA_THUMBNAIL_KEY = 'mediaThumbnail'.freeze # These appear in context tables when rendering. -CONTEXT_PERMALINK_KEY = 'permalink' -CONTEXT_EXTERNAL_URL_KEY = 'external_url' -CONTEXT_LINK_PREFERRING_EXTERNAL_URL_KEY = 'link_preferring_external_url' # Use external_url when present, falling back to permalink. -CONTEXT_TITLE_KEY = 'title' -CONTEXT_CONTENT_HTML_KEY = 'content_html' -CONTEXT_PUB_DATE_KEY = 'pub_date' -CONTEXT_DISPLAY_DATE_KEY = 'display_date' +CONTEXT_PERMALINK_KEY = 'permalink'.freeze +CONTEXT_EXTERNAL_URL_KEY = 'external_url'.freeze +CONTEXT_LINK_PREFERRING_EXTERNAL_URL_KEY = 'link_preferring_external_url'.freeze # Use external_url when present, falling back to permalink. +CONTEXT_TITLE_KEY = 'title'.freeze +CONTEXT_CONTENT_HTML_KEY = 'content_html'.freeze +CONTEXT_PUB_DATE_KEY = 'pub_date'.freeze +CONTEXT_DISPLAY_DATE_KEY = 'display_date'.freeze # JSON Feed generation -JSON_FEED_URL_KEY = 'url' -JSON_FEED_EXTERNAL_URL_KEY = 'external_url' -JSON_FEED_ID_KEY = 'id' -JSON_FEED_TITLE_KEY = 'title' -JSON_FEED_CONTENT_HTML_KEY = 'content_html' -JSON_FEED_PUB_DATE_KEY = 'date_published' -JSON_FEED_ATTACHMENTS_KEY = 'attachments' -JSON_FEED_ENCLOSURE_URL = 'url' -JSON_FEED_ENCLOSURE_MIME_TYPE = 'mime_type' -JSON_FEED_ENCLOSURE_SIZE_IN_BYTES = 'size_in_bytes' - +JSON_FEED_URL_KEY = 'url'.freeze +JSON_FEED_EXTERNAL_URL_KEY = 'external_url'.freeze +JSON_FEED_ID_KEY = 'id'.freeze +JSON_FEED_TITLE_KEY = 'title'.freeze +JSON_FEED_CONTENT_HTML_KEY = 'content_html'.freeze +JSON_FEED_PUB_DATE_KEY = 'date_published'.freeze +JSON_FEED_ATTACHMENTS_KEY = 'attachments'.freeze +JSON_FEED_ENCLOSURE_URL = 'url'.freeze +JSON_FEED_ENCLOSURE_MIME_TYPE = 'mime_type'.freeze +JSON_FEED_ENCLOSURE_SIZE_IN_BYTES = 'size_in_bytes'.freeze diff --git a/wildcat_local_server.rb b/wildcat_local_server.rb index a9b5583..8cbbbfc 100755 --- a/wildcat_local_server.rb +++ b/wildcat_local_server.rb @@ -10,6 +10,6 @@ require 'webrick' require_relative 'config' -server=WEBrick::HTTPServer.new(:Port => 9344, :DocumentRoot => File.join(Dir::pwd, "server/")) -trap("INT"){ server.shutdown } +server = WEBrick::HTTPServer.new(Port: 9344, DocumentRoot: File.join(Dir.pwd, 'server/')) +trap('INT') { server.shutdown } server.start