lib/bhook/md_file.rb in bhook-0.1.0 vs lib/bhook/md_file.rb in bhook-0.1.1

- old
+ new

@@ -1,50 +1,43 @@ -# typed: false +# typed: strict # frozen_string_literal: true module Bhook class MdFile extend T::Sig - PAGE_TEMPLATE = File.read(File.join(File.dirname(__FILE__), 'theme', 'page.erb')) - AFTER_H1_TEMPLATE = File.read(File.join(File.dirname(__FILE__), 'theme', '_after_h1.erb')) - + + 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 { params(src_file_path: Pathname, git: Git::Base).void } def initialize(src_file_path, git) @md = T.let(File.read(src_file_path), String) - @src_file_path = src_file_path.expand_path - file_meta_data = git.lib.send(:command, 'log', - '-n 1', - '--pretty=format:%ad|%h', - '--date=short', - '--', - @src_file_path) - @src_file_date, @src_file_sha = file_meta_data.split('|') + @src_file_path = T.let(src_file_path.expand_path, Pathname) + 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 { returns(Pathname) } attr_reader :src_file_path - sig { params(out_path: String).void } - def write!(out_path) + sig { params(out_path: String, theme: Bhook::Theme).void } + def write!(out_path, theme) out_file_name = File.basename(@src_file_path).gsub(/\.md$/, '.html') out_file_path = File.join(out_path, out_file_name) - src_file_sha = @src_file_sha - src_file_date = @src_file_date - src_title = '' - puts "Parsing: #{@src_file_sha} #{out_file_path}" - after_h1_strategy = ->(binding_instance) { ERB.new(AFTER_H1_TEMPLATE, trim_mode: '-').result(binding_instance) } - - doc = Kramdown::Document.new(@md) - output, warnings = Converter::Html.convert(doc.root, doc.options.merge( - after_h1_strategy: after_h1_strategy, - h1_callback: ->(str) { src_title = str } - )) - rendered_page = ERB.new(PAGE_TEMPLATE, trim_mode: '-').result(binding) + puts "Processing: #{@src_file_sha} #{@src_file_path}" + rendered_page = theme.render_page(@md, @src_file_sha, @src_file_date) puts "Writing: #{@src_file_sha} #{out_file_path}" File.write(out_file_path, rendered_page) end sig { returns(String) } def to_s - @src_file_path + @src_file_path.to_s end end end