# typed: strict # frozen_string_literal: true module Bhook class MdFile extend T::Sig PAGE_TEMPLATE = T.let(File.read(Bhook::PAGE_TEMPLATE_PATH), String) AFTER_H1_TEMPLATE = T.let(File.read(Bhook::AFTER_H1_TEMPLATE_PATH), String) sig { returns(Pathname) } attr_reader :src_file_path sig { params(src_file_path: Pathname, out_path: Pathname, git: Git::Base).void } def initialize(src_file_path, out_path, git) L.debug "Reading: #{src_file_path}" @md = T.let(File.read(src_file_path), String) @src_file_path = src_file_path @out_path = out_path src_file_date, src_file_sha = git.lib.send(:command, 'log', '-n 1', '--pretty=format:%ad|%h', '--date=short', '--', @src_file_path).split('|') @src_file_date = T.let(src_file_date, T.nilable(String)) @src_file_sha = T.let(src_file_sha, T.nilable(String)) end sig { params(theme: Bhook::Theme).void } def write!(theme) out_file_name = @src_file_path.basename.sub(/\.md$/, '.html') out_file_path = @out_path.join(out_file_name) L.debug "Processing: #{@src_file_sha || 'unversioned'} #{@src_file_path}" rendered_page = theme.render_page(@md, @src_file_sha, @src_file_date) L.debug "Writing: #{@src_file_sha} #{out_file_path}" File.write(out_file_path, rendered_page) end sig { returns(String) } def to_s @src_file_path.to_s end end end