diff --git a/Gemfile b/Gemfile index 55d20b7..7709ae7 100644 --- a/Gemfile +++ b/Gemfile @@ -9,6 +9,7 @@ gem "pg", "~> 1.1" gem "puma", "~> 5.0" gem 'slim-rails' gem 'kaminari' +gem 'redcarpet' gem 'redis' diff --git a/Gemfile.lock b/Gemfile.lock index a350854..97731b4 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -208,6 +208,7 @@ GEM rake (13.1.0) rdoc (6.6.3.1) psych (>= 4.0.0) + redcarpet (3.6.0) redis (5.1.0) redis-client (>= 0.17.0) redis-client (0.21.1) @@ -276,6 +277,7 @@ DEPENDENCIES pg (~> 1.1) puma (~> 5.0) rails (~> 7.0.8, >= 7.0.8.1) + redcarpet redis selenium-webdriver sidekiq diff --git a/README.md b/README.md index a969da6..e86a926 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,2 @@ # FoalFetch -A replacement for FiMFetch. \ No newline at end of file +A replacement for FiMFetch. diff --git a/app/controllers/chapters_controller.rb b/app/controllers/chapters_controller.rb index a612712..f63f8ac 100644 --- a/app/controllers/chapters_controller.rb +++ b/app/controllers/chapters_controller.rb @@ -1,6 +1,16 @@ +require 'redcarpet' + class ChaptersController < ApplicationController def show @story = Story.find(params[:story_id]) @chapter = @story.chapters.find_by(number: params[:id]) + + @rendered_html = markdown.render(@chapter.body) + end + + private + def markdown + @@markdown ||= + Redcarpet::Markdown.new(Redcarpet::Render::HTML, autolink: true, tables: true) end end diff --git a/app/controllers/images_controller.rb b/app/controllers/images_controller.rb index d385737..ef14b30 100644 --- a/app/controllers/images_controller.rb +++ b/app/controllers/images_controller.rb @@ -1,6 +1,8 @@ require 'open-uri' +# Handles proxying images from the FiMFiction CDN and caching them locally, in case they disappear. class ImagesController < ApplicationController + #noinspection RubyMismatchedArgumentType def show url = params[:url] parsed = URI.parse(url) @@ -10,9 +12,10 @@ class ImagesController < ApplicationController return end + ext = File.extname(url) hash = Digest::SHA256.hexdigest(url) - path = Rails.root.join('public', 'cached-images', hash + File.extname(url)) - our_url = '/cached-images/' + hash + File.extname(url) + path = Rails.root.join('public', 'cached-images', hash + ext) + our_url = '/cached-images/' + hash + ext if File.exist? path redirect_to our_url diff --git a/app/jobs/recount_words_job.rb b/app/jobs/recount_words_job.rb new file mode 100644 index 0000000..5c8af59 --- /dev/null +++ b/app/jobs/recount_words_job.rb @@ -0,0 +1,18 @@ +class RecountWordsJob < ApplicationJob + def perform(*) + Story.find_each do |story| + word_count = 0 + story.chapters.each do |chapter| + count = chapter.body.split(' ').size + chapter.update_columns( + num_words: count + ) + word_count += count + end + + story.update_columns( + num_words: word_count + ) + end + end +end \ No newline at end of file diff --git a/app/views/chapters/show.html.slim b/app/views/chapters/show.html.slim index 76945a9..c8ec31e 100644 --- a/app/views/chapters/show.html.slim +++ b/app/views/chapters/show.html.slim @@ -21,9 +21,8 @@ body.story - if @chapter.number < @story.chapters.count = link_to 'Next Chapter', story_chapter_path(@story, @chapter.number + 1) hr - == @chapter.body + == @rendered_html span.chapnav.bot - if @chapter.number < @story.chapters.count = link_to 'Next Chapter', story_chapter_path(@story, @chapter.number + 1) - diff --git a/app/views/search/index.html.slim b/app/views/search/index.html.slim index f237dc7..19449c2 100644 --- a/app/views/search/index.html.slim +++ b/app/views/search/index.html.slim @@ -1,15 +1,15 @@ .main #wrap nav.home - = link_to "Daily Fictions", "/ficoftheday" + /= link_to "Daily Fictions", "/ficoftheday" = link_to "News", "/news" = link_to "About", "/about" section.search.search-l - img alt="FoalFetch" src="/img/banner.png" + = image_tag '/img/banner.png', alt: 'FoalFetch' = form_tag "/search", method: :post .searchbox = search_field_tag "q", nil, placeholder: 'Search story titles...' = render partial: 'advanced', locals: { show_button: false } .buttons = submit_tag 'Go Fetch!', name: 'search' - = submit_tag 'Pick one for me!', name: 'luck' \ No newline at end of file + = submit_tag 'Pick one for me!', name: 'luck' diff --git a/config/routes.rb b/config/routes.rb index 8fbba7c..6faf6df 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -6,7 +6,8 @@ Rails.application.routes.draw do get '/images' => 'images#show' resources :authors, only: [:show] - resources :stories, only: [:show] do - resources :chapters, only: [:show] + # using singular-named routes to match FiMFetch/FiMFiction. + resources :stories, only: [:show], path: :story do + resources :chapters, only: [:show], path: :chapter end end