feat(downloads): improve epub generation

This commit is contained in:
Neetpone 2024-07-30 13:44:41 -04:00
parent 3d7f2a51ce
commit 425eadee63
8 changed files with 33 additions and 13 deletions

View file

@ -61,7 +61,3 @@ Style/NumericPredicate:
# I've always been an "unnecessary this->" guy myself.
Style/RedundantSelf:
Enabled: false
# Story downloads rely on this.
Rails/RenderInline:
Enabled: false

View file

@ -6,6 +6,6 @@ class ChaptersController < ApplicationController
@story = Story.find(params[:story_id])
@chapter = @story.chapters.find_by(number: params[:id])
@rendered_html = StoryRenderer.render_chapter(@chapter)
@rendered_html = Ebook::EpubGenerator.render_chapter(@chapter)
end
end

View file

@ -1,7 +1,7 @@
# frozen_string_literal: true
class Stories::DownloadsController < ApplicationController
MIME_TYPES = {
text: 'text/plain',
txt: 'text/plain',
html: 'text/html',
epub: 'application/x-epub'
}.freeze
@ -12,6 +12,6 @@ class Stories::DownloadsController < ApplicationController
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'
send_data renderer.send(format), type: MIME_TYPES[format], disposition: :attachment, filename: "#{@story.title}.#{format}"
end
end

View file

@ -1,4 +1,5 @@
require 'gepub'
require 'ostruct'
TEMPLATE_DIRECTORY = Rails.root.join('app/lib/ebook')
@ -24,7 +25,7 @@ class Ebook::EpubGenerator
end
end
book
book.generate_epub_stream.string
end
#private
@ -38,6 +39,25 @@ class Ebook::EpubGenerator
end
def generate_chapter(chapter)
StringIO.new render_template('chapter', chapter)
StringIO.new render_template('chapter', OpenStruct.new( chapter: chapter, rendered: self.render_chapter(chapter)))
end
def self.render_chapter(chapter)
body = chapter.body
body.lstrip!
body = body.split "\n"
# This is fucking bad, this gets rid of the redundant title - this should be fixed upstairs,
# in the actual generation of the Markdown.
if body.length >= 2 && body[0] == chapter.title && !body[1].empty? && body[1][0] == '='
body = body[2..]
end
self.markdown.render body.join("\n")
end
def self.markdown
@@markdown ||=
Redcarpet::Markdown.new(Redcarpet::Render::HTML, autolink: true, tables: true)
end
end

View file

@ -3,8 +3,8 @@ html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"
head
meta http-equiv="Content-Type" content="text/html; charset=UTF-8"
link rel="stylesheet" href="styles.css"
title= title
title= chapter.title
body
h1= title
h1= chapter.title
.calibre2#content
== StoryRenderer.render_chapter(self)
== rendered

View file

@ -6,7 +6,7 @@ class StoryRenderer
@story = story
end
def text
def txt
markdown = Redcarpet::Markdown.new(Redcarpet::Render::StripDown)
separator = '//-------------------------------------------------------//'
@ -31,5 +31,6 @@ class StoryRenderer
end
def epub
Ebook::EpubGenerator.new(@story).generate
end
end

View file

@ -42,6 +42,8 @@ div.story
span Download:
= link_to story_download_path(@story, fmt: 'txt') do
img src="/img/icons/txt32.png" alt="text" title="Download condensed text format"
= link_to story_download_path(@story, fmt: 'epub') do
img src="/img/icons/epub32.png" alt="ePUB" title="Download ePUB format"
.chapterlist
h3= pluralize(@chapters.count, 'Chapter') + ':'

View file

@ -33,6 +33,7 @@ Bundler.require(*Rails.groups)
#
# config.time_zone = "Central Time (US & Canada)"
# config.eager_load_paths << Rails.root.join("extras")
module Foalfetch; end
class Foalfetch::Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 7.0