2024-04-04 10:21:23 +02:00
|
|
|
# frozen_string_literal: true
|
2024-04-02 22:05:15 +02:00
|
|
|
require 'redcarpet'
|
|
|
|
|
2024-04-02 19:33:19 +02:00
|
|
|
class ChaptersController < ApplicationController
|
|
|
|
def show
|
|
|
|
@story = Story.find(params[:story_id])
|
|
|
|
@chapter = @story.chapters.find_by(number: params[:id])
|
2024-04-02 22:05:15 +02:00
|
|
|
|
2024-04-03 11:18:15 +02:00
|
|
|
@rendered_html = render_story
|
2024-04-02 22:05:15 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2024-04-03 11:18:15 +02:00
|
|
|
|
|
|
|
def render_story
|
|
|
|
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.
|
2024-04-04 10:21:23 +02:00
|
|
|
if body.length >= 2 && body[0] == @chapter.title && !body[1].empty? && body[1][0] == '='
|
2024-04-03 11:18:15 +02:00
|
|
|
body = body[2..]
|
|
|
|
end
|
|
|
|
|
|
|
|
markdown.render body.join("\n")
|
|
|
|
end
|
|
|
|
|
2024-04-02 22:05:15 +02:00
|
|
|
def markdown
|
|
|
|
@@markdown ||=
|
|
|
|
Redcarpet::Markdown.new(Redcarpet::Render::HTML, autolink: true, tables: true)
|
2024-04-02 19:33:19 +02:00
|
|
|
end
|
|
|
|
end
|