69 lines
1.8 KiB
Ruby
69 lines
1.8 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
module FicTracker::Renderers
|
|
class Markdown
|
|
attr_accessor :cover, :preface
|
|
attr_reader :story
|
|
|
|
def initialize(story, io:, cover: false, preface: true)
|
|
@story = story
|
|
@io = io
|
|
|
|
@cover = cover && story.image
|
|
@preface = preface
|
|
end
|
|
|
|
def render
|
|
logger.info "Rendering markdown for #{story}"
|
|
|
|
@io.puts build_preface, nil if preface
|
|
|
|
story.chapters.each do |chapter|
|
|
@io.puts build_chapter(chapter), nil
|
|
end
|
|
|
|
@io.puts build_chapter_references
|
|
end
|
|
|
|
private
|
|
|
|
def logger
|
|
Logging.logger[self]
|
|
end
|
|
|
|
def build_preface
|
|
require_relative '../converters/from_html'
|
|
<<~EOF
|
|
#{story.name}
|
|
#{'=' * story.name.size}
|
|
|
|
By __*[#{story.author}][aut]*__#{' '}
|
|
Published #{story.published_at.to_date}#{(story.updated_at && story.updated_at != story.published_at) ? ", updated #{story.updated_at.to_date}" : ''}.#{' '}
|
|
[The original work.](#{story.url})
|
|
#{cover ? "\n" : ''}
|
|
#{FicTracker::Converters::FromHTML.to_md(story.synopsis)}
|
|
EOF
|
|
end
|
|
|
|
def build_chapter(chapter)
|
|
head = "[#{chapter}][ch#{chapter.index}]\n#{'-' * (chapter.to_s.size + 6 + chapter.index.to_s.size)}\n\n"
|
|
|
|
head + case chapter.content_type
|
|
when 'text/html'
|
|
require_relative '../converters/from_html'
|
|
FicTracker::Converters::FromHTML.to_md(chapter.content)
|
|
when 'text/markdown'
|
|
chapter.content
|
|
when 'text/plain'
|
|
chapter.content
|
|
end
|
|
end
|
|
|
|
def build_chapter_references
|
|
refs = []
|
|
refs << "[aut]: #{story.author.url}" if preface
|
|
refs += story.chapters.map { |c| "[ch#{c.index}]: #{c.url}" }
|
|
refs.join "\n"
|
|
end
|
|
end
|
|
end
|