Sha256: 0e9780dc7856555a388de0940334a92eb4f9340ca173911d08cbbf0e9570c01a

Contents?: true

Size: 1.79 KB

Versions: 1

Compression:

Stored size: 1.79 KB

Contents

# typed: false
# 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'))

    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('|')
    end

    sig { returns(Pathname) }
    attr_reader :src_file_path

    sig { params(out_path: String).void }
    def write!(out_path)
      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 "Writing: #{@src_file_sha} #{out_file_path}"
      File.write(out_file_path, rendered_page)
    end

    sig { returns(String) }
    def to_s
      @src_file_path
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bhook-0.1.0 lib/bhook/md_file.rb