Class: Summary
- Inherits:
-
Object
- Object
- Summary
- Defined in:
- lib/jekyll-books/summary.rb
Instance Attribute Summary collapse
-
#folder ⇒ Object
readonly
Returns the value of attribute folder.
Instance Method Summary collapse
- #chapter_files ⇒ Object
- #chapters ⇒ Object
- #file_path ⇒ Object
- #get_relative_path(file) ⇒ Object
- #get_title(file) ⇒ Object
- #index_path ⇒ Object
-
#initialize(folder) ⇒ Summary
constructor
A new instance of Summary.
- #parse_chapter(chapter, level) ⇒ Object
- #read ⇒ Object
- #recursive_parse_chapter(content, level, result) ⇒ Object
- #summary_title ⇒ Object
- #write(autoload = false) ⇒ Object
Constructor Details
#initialize(folder) ⇒ Summary
Returns a new instance of Summary.
8 9 10 |
# File 'lib/jekyll-books/summary.rb', line 8 def initialize(folder) @folder = folder end |
Instance Attribute Details
#folder ⇒ Object (readonly)
Returns the value of attribute folder.
6 7 8 |
# File 'lib/jekyll-books/summary.rb', line 6 def folder @folder end |
Instance Method Details
#chapter_files ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/jekyll-books/summary.rb', line 20 def chapter_files files = [] Find.find(folder) do |f| if File.file?(f) and ![file_path, index_path].include?(f) files << f if File.file?(f) end end files.sort! do |f1, f2| File.ctime(f1) <=> File.ctime(f2) end end |
#chapters ⇒ Object
33 34 35 36 37 38 39 40 |
# File 'lib/jekyll-books/summary.rb', line 33 def chapters chapter_files.map do |f| { :title => get_title(f), :path => get_relative_path(f) } end end |
#file_path ⇒ Object
12 13 14 |
# File 'lib/jekyll-books/summary.rb', line 12 def file_path File.join(folder, "SUMMARY.md") end |
#get_relative_path(file) ⇒ Object
49 50 51 |
# File 'lib/jekyll-books/summary.rb', line 49 def get_relative_path(file) file.sub(folder + "/", "") end |
#get_title(file) ⇒ Object
42 43 44 45 46 47 |
# File 'lib/jekyll-books/summary.rb', line 42 def get_title(file) content = File.read(file) html = Kramdown::Document.new(content).to_html parsed_html = Nokogiri::HTML(html) parsed_html.xpath("//h1/text()").to_s end |
#index_path ⇒ Object
16 17 18 |
# File 'lib/jekyll-books/summary.rb', line 16 def index_path File.join(folder, "index.md") end |
#parse_chapter(chapter, level) ⇒ Object
84 85 86 87 88 89 90 |
# File 'lib/jekyll-books/summary.rb', line 84 def parse_chapter(chapter, level) { "link" => chapter.xpath("a/@href").to_s, "title" => chapter.xpath("a/text()").to_s, "level" => level } end |
#read ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/jekyll-books/summary.rb', line 64 def read content = File.read(file_path) html = Kramdown::Document.new(content).to_html parsed_html = Nokogiri::HTML(html) chapters = [] parsed_html.xpath("//body/ul/li").each do |li| recursive_parse_chapter(li, 1, chapters) end chapters end |
#recursive_parse_chapter(content, level, result) ⇒ Object
76 77 78 79 80 81 82 |
# File 'lib/jekyll-books/summary.rb', line 76 def recursive_parse_chapter(content, level, result) result << parse_chapter(content, level) content.xpath("ul/li").each do |li| recursive_parse_chapter(li, level + 1, result) end end |
#summary_title ⇒ Object
92 93 94 |
# File 'lib/jekyll-books/summary.rb', line 92 def summary_title "# Summary\n\n" end |
#write(autoload = false) ⇒ Object
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/jekyll-books/summary.rb', line 53 def write(autoload = false) File.open(file_path, "w+") do |f| f.write summary_title if autoload chapters.each do |chapter| f.write "* [#{chapter[:title]}](#{chapter[:path]})\n" end end end end |