require 'front_matter_parser' module WriteDown module Model # 基类, 定义了一些接口 class Base attr_accessor :draft attr_accessor :created_at attr_accessor :link_to attr_accessor :content def initialize(source_file, target_file = nil) @source_file = source_file @base_name = source_file.split("/").last.split(".").first @ext_name = source_file.split("/").last.split(".").last meta_file = File.join("source", "meta", "#{@base_name}.json") @meta = File.exist?(meta_file) ? JSON.parse(File.open(meta_file).read) : read_from_head @draft = @meta['draft'].to_s == "true" ? true : false @created_at = @meta['date'] || File.open(@source_file).ctime #.iso8610 @content = File.open(@source_file).read if target_file @target_file = target_file elsif @base_name.end_with?"index" @target_file = "dist/#{@base_name}.html" else @target_file = "dist/#{@base_name}/index.html" end @link_to = @target_file.gsub("index.html", "") end def converter if @ext_name == "md" || @ext_name == "markdown" return WriteDown::Converter::Markdown end if @ext_name == "org" return WriteDown::Converter::Org end end def body # binding.pry converter.new(self).render end # 接口 render # 用于找到自己的 erb 然后生成 html 字符串 def render raise "Not impl..." end # 接口 build # 用于把自己序列化到磁盘上 def build raise "Not impl..." end def read_from_head FrontMatterParser.parse_file(@source_file).front_matter end end end end