Class: WriteDown::Article

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#contentObject

Returns the value of attribute content.



3
4
5
# File 'lib/write_down/article.rb', line 3

def content
  @content
end

#created_atObject

Returns the value of attribute created_at.



3
4
5
# File 'lib/write_down/article.rb', line 3

def created_at
  @created_at
end

#file_nameObject

Returns the value of attribute file_name.



3
4
5
# File 'lib/write_down/article.rb', line 3

def file_name
  @file_name
end

Returns the value of attribute link.



3
4
5
# File 'lib/write_down/article.rb', line 3

def link
  @link
end

#seriesObject

Returns the value of attribute series.



3
4
5
# File 'lib/write_down/article.rb', line 3

def series
  @series
end

#titleObject

Returns the value of attribute title.



3
4
5
# File 'lib/write_down/article.rb', line 3

def title
  @title
end

#updated_atObject

Returns the value of attribute updated_at.



3
4
5
# File 'lib/write_down/article.rb', line 3

def updated_at
  @updated_at
end

Class Method Details

.build(source_folder) ⇒ Object

返回 article 对象的集合



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/write_down/article.rb', line 8

def self.build source_folder
  articles = Array.new

  Dir.foreach(source_folder) do |file|
    unless file.to_s.start_with?"." #排除.开头的文件
      begin
        file_path = "#{source_folder}/#{file}"
        f = File.new(file_path,"r")
        if File.directory?(f) #是目录
          series = Series.new
          series.name = File.basename(file_path) #系列的名字就是文件夹的名字
          
          Dir.foreach(f) do |sub_f|  
            unless sub_f.to_s.start_with?"." #排除.开头的文件
              file_path = "#{f.path}/#{sub_f}"
              article = Article.new
              article.build_from_file file_path
              articles << article #所有的文章集合
              series.articles << article #某个系列的文章集合
            end
          end
          $site.series << series
        else #最外层的文章
          article = Article.new
          article.build_from_file f
          articles << article
        end
      rescue => err
        puts "build from source folder error: #{err}"
      end
    end
  end

  articles
end

Instance Method Details

#build_from_file(file_path) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/write_down/article.rb', line 44

def build_from_file file_path
  basename = File.basename(file_path)
  file = File.new(file_path)
  file_content = file.read #读取所有内容
  html_content = Converter.convert_markdown file_content #转成 html 了
  puts self
  self.content = html_content
  self.file_name = basename #文件名
  self.link = basename.chomp(File.extname(file_path)) + ".html" #链接形式以后要改
  self.title = self.get_title #读取合适的 title
  self.created_at = file.ctime
  self.updated_at = file.mtime
  file.close
end

#get_titleObject



59
60
61
62
# File 'lib/write_down/article.rb', line 59

def get_title
  doc = Nokogiri::HTML(self.content)
  doc.css('h1').first.content ||= self.file_name
end