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 "puma", "~> 5.0"
gem 'slim-rails' gem 'slim-rails'
gem 'kaminari' gem 'kaminari'
gem 'redcarpet'
gem 'redis' gem 'redis'

View file

@ -208,6 +208,7 @@ GEM
rake (13.1.0) rake (13.1.0)
rdoc (6.6.3.1) rdoc (6.6.3.1)
psych (>= 4.0.0) psych (>= 4.0.0)
redcarpet (3.6.0)
redis (5.1.0) redis (5.1.0)
redis-client (>= 0.17.0) redis-client (>= 0.17.0)
redis-client (0.21.1) redis-client (0.21.1)
@ -276,6 +277,7 @@ DEPENDENCIES
pg (~> 1.1) pg (~> 1.1)
puma (~> 5.0) puma (~> 5.0)
rails (~> 7.0.8, >= 7.0.8.1) rails (~> 7.0.8, >= 7.0.8.1)
redcarpet
redis redis
selenium-webdriver selenium-webdriver
sidekiq sidekiq

View file

@ -1,2 +1,2 @@
# FoalFetch # FoalFetch
A replacement for FiMFetch. A replacement for FiMFetch.

View file

@ -1,6 +1,16 @@
require 'redcarpet'
class ChaptersController < ApplicationController class ChaptersController < ApplicationController
def show def show
@story = Story.find(params[:story_id]) @story = Story.find(params[:story_id])
@chapter = @story.chapters.find_by(number: params[: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
end end

View file

@ -1,6 +1,8 @@
require 'open-uri' require 'open-uri'
# Handles proxying images from the FiMFiction CDN and caching them locally, in case they disappear.
class ImagesController < ApplicationController class ImagesController < ApplicationController
#noinspection RubyMismatchedArgumentType
def show def show
url = params[:url] url = params[:url]
parsed = URI.parse(url) parsed = URI.parse(url)
@ -10,9 +12,10 @@ class ImagesController < ApplicationController
return return
end end
ext = File.extname(url)
hash = Digest::SHA256.hexdigest(url) hash = Digest::SHA256.hexdigest(url)
path = Rails.root.join('public', 'cached-images', hash + File.extname(url)) path = Rails.root.join('public', 'cached-images', hash + ext)
our_url = '/cached-images/' + hash + File.extname(url) our_url = '/cached-images/' + hash + ext
if File.exist? path if File.exist? path
redirect_to our_url 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 - if @chapter.number < @story.chapters.count
= link_to 'Next Chapter', story_chapter_path(@story, @chapter.number + 1) = link_to 'Next Chapter', story_chapter_path(@story, @chapter.number + 1)
hr hr
== @chapter.body == @rendered_html
span.chapnav.bot span.chapnav.bot
- if @chapter.number < @story.chapters.count - if @chapter.number < @story.chapters.count
= link_to 'Next Chapter', story_chapter_path(@story, @chapter.number + 1) = link_to 'Next Chapter', story_chapter_path(@story, @chapter.number + 1)

View file

@ -1,15 +1,15 @@
.main .main
#wrap #wrap
nav.home nav.home
= link_to "Daily Fictions", "/ficoftheday" /= link_to "Daily Fictions", "/ficoftheday"
= link_to "News", "/news" = link_to "News", "/news"
= link_to "About", "/about" = link_to "About", "/about"
section.search.search-l section.search.search-l
img alt="FoalFetch" src="/img/banner.png" = image_tag '/img/banner.png', alt: 'FoalFetch'
= form_tag "/search", method: :post = form_tag "/search", method: :post
.searchbox .searchbox
= search_field_tag "q", nil, placeholder: 'Search story titles...' = search_field_tag "q", nil, placeholder: 'Search story titles...'
= render partial: 'advanced', locals: { show_button: false } = render partial: 'advanced', locals: { show_button: false }
.buttons .buttons
= submit_tag 'Go Fetch!', name: 'search' = submit_tag 'Go Fetch!', name: 'search'
= submit_tag 'Pick one for me!', name: 'luck' = submit_tag 'Pick one for me!', name: 'luck'

View file

@ -6,7 +6,8 @@ Rails.application.routes.draw do
get '/images' => 'images#show' get '/images' => 'images#show'
resources :authors, only: [:show] resources :authors, only: [:show]
resources :stories, only: [:show] do # using singular-named routes to match FiMFetch/FiMFiction.
resources :chapters, only: [:show] resources :stories, only: [:show], path: :story do
resources :chapters, only: [:show], path: :chapter
end end
end end