Class: Article

Inherits:
Object
  • Object
show all
Defined in:
lib/article.rb

Instance Method Summary collapse

Constructor Details

#initializeArticle

Returns a new instance of Article.



14
15
16
17
18
19
# File 'lib/article.rb', line 14

def initialize
    @meta = Meta.new
    @toc = TOC.new
    @util = Util.instance
    @setup = Setup.instance
end

Instance Method Details

#convert(relative_path) ⇒ Object

转换文件



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/article.rb', line 22

def convert(relative_path)
    result = Hash.new

    file = File::join @setup.content_dir, relative_path

    #原始路径
    result['file'] = file
    #路径的MD5值
    result['relative_path_md5'] = Digest::SHA256.hexdigest(relative_path.to_s)[0..15]

    result['mtime'] = File::ctime file

    #读取原始内容
    original = @util.read_file file
    result['original'] = original

    #相对路径
    result['relative_path'] = relative_path
    result['relative_url'] = relative_path.to_s.gsub(/\.md$/, '.html')


    meta_result = @meta.analysis original
    meta = meta_result['meta'] || {}
    body = meta_result['body']

    meta['title'] = File::basename(file, '.md') if !meta['title']
    meta['publish_date'] = result['mtime'] if !meta['publish_date']

    result['meta'] = meta
    result['body_markdown'] = body

    if body
        #创建markdown实例
        markdown = Kramdown::Document.new(body)
        html = result['body_html'] = markdown.to_html

        result['excerpt'] = Nokogiri::HTML(html).text[0..99]
        #获取toc相关的内容
        result['toc_html'] = @toc.to_html markdown.to_toc
    end

    result
end