Class: WriteDown::Article
- Inherits:
-
Object
- Object
- WriteDown::Article
- Defined in:
- lib/write_down/article.rb
Instance Attribute Summary collapse
-
#content ⇒ Object
Returns the value of attribute content.
-
#created_at ⇒ Object
Returns the value of attribute created_at.
-
#file_name ⇒ Object
Returns the value of attribute file_name.
-
#link ⇒ Object
Returns the value of attribute link.
-
#series ⇒ Object
Returns the value of attribute series.
-
#title ⇒ Object
Returns the value of attribute title.
-
#updated_at ⇒ Object
Returns the value of attribute updated_at.
Class Method Summary collapse
-
.build(source_folder) ⇒ Object
返回 article 对象的集合.
Instance Method Summary collapse
Instance Attribute Details
#content ⇒ Object
Returns the value of attribute content.
3 4 5 |
# File 'lib/write_down/article.rb', line 3 def content @content end |
#created_at ⇒ Object
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_name ⇒ Object
Returns the value of attribute file_name.
3 4 5 |
# File 'lib/write_down/article.rb', line 3 def file_name @file_name end |
#link ⇒ Object
Returns the value of attribute link.
3 4 5 |
# File 'lib/write_down/article.rb', line 3 def link @link end |
#series ⇒ Object
Returns the value of attribute series.
3 4 5 |
# File 'lib/write_down/article.rb', line 3 def series @series end |
#title ⇒ Object
Returns the value of attribute title.
3 4 5 |
# File 'lib/write_down/article.rb', line 3 def title @title end |
#updated_at ⇒ Object
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_title ⇒ Object
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 |