feat: markdown parsing and some other stuff

This commit is contained in:
Neetpone 2024-04-02 16:05:15 -04:00
parent 563aa6a0f8
commit 96ec959166
9 changed files with 44 additions and 10 deletions

View file

@ -9,6 +9,7 @@ gem "pg", "~> 1.1"
gem "puma", "~> 5.0"
gem 'slim-rails'
gem 'kaminari'
gem 'redcarpet'
gem 'redis'

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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)

View file

@ -1,11 +1,11 @@
.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...'

View file

@ -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