feat(downloads): add text downloads

This commit is contained in:
Neetpone 2024-07-30 09:53:39 -04:00
parent 0a3a2edcf0
commit 14f2591c1c
9 changed files with 59 additions and 2 deletions

View file

@ -0,0 +1,17 @@
# frozen_string_literal: true
class Stories::DownloadsController < ApplicationController
MIME_TYPES = {
text: 'text/plain',
html: 'text/html',
epub: 'application/x-epub'
}.freeze
def show
@story = Story.find(params[:story_id])
format = MIME_TYPES.keys.detect { |fmt| fmt.to_s == params[:fmt] } || :text
renderer = StoryRenderer.new @story
render inline: renderer.send(format), content_type: MIME_TYPES[format], content_disposition: 'attachment'
end
end

35
app/lib/story_renderer.rb Normal file
View file

@ -0,0 +1,35 @@
# frozen_string_literal: true
require 'redcarpet/render_strip'
class StoryRenderer
def initialize(story)
@story = story
end
def text
markdown = Redcarpet::Markdown.new(Redcarpet::Render::StripDown)
separator = '//-------------------------------------------------------//'
# Title and author
text = "#{separator}\n".dup
text += "#{@story.title.center(separator.length, ' ')}\n"
text += "#{"-by #{@story.author.name}-".center(separator.length, ' ')}\n"
text += "#{separator}\n"
# Chapters
@story.chapters.each do |chapter|
text += "#{separator}\n"
text += "#{chapter.title.center(separator.length, ' ')}\n"
text += "#{separator}\n"
text += "#{markdown.render chapter.body}\n" # FIXME: My Markdown still has the title at the top for no reason.
end
text
end
def html
end
def epub
end
end

View file

@ -14,8 +14,7 @@ section.fic-cell
span.popular
span.stats
- if story.rating
span.likes
| Rating: #{story.rating}%
span.likes Rating: #{story.rating}%
- if story.short_description
p.description= story.short_description
- else

View file

@ -40,6 +40,9 @@ div.story
span.cached
.dl-links
span Download:
= link_to story_download_path(@story, fmt: 'txt') do
img src="/img/icons/txt32.png" alt="text" title="Download condensed text format"
.chapterlist
h3= pluralize(@chapters.count, 'Chapter') + ':'
ol

View file

@ -11,6 +11,9 @@ Rails.application.routes.draw do
# using singular-named routes to match FiMFetch/FiMFiction.
resources :stories, only: [:show], path: :story do
resources :chapters, only: [:show], path: :chapter
scope module: :stories do
resource :download, only: :show
end
end
# Different route name again to match FiMFetch

BIN
public/img/icons/epub32.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

BIN
public/img/icons/html32.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

BIN
public/img/icons/mobi32.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 KiB

BIN
public/img/icons/txt32.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 197 B