jekyll-books 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 05d7de00a1cc4d87c6bb5556da5932e93ef225c0
4
+ data.tar.gz: cf0c0764cb29c908a9bb3db3f6f69276cdb99e19
5
+ SHA512:
6
+ metadata.gz: eb4c197bf9f1535383a6348ad7d1bd12f88024c7dd83d69bd97f9f0b556a1ea8a87383acb85749b515b488a31b869cd83745793537f3c5a9180668eb6329270e
7
+ data.tar.gz: 3f2caa14d4f23cd9b10f055706ab1ea5fc193e54e9ddc2c87692d84a049f16dbe37de46d18f83b2109a7d62e94878c4a2781c5387e7189b7521b9b50c4d78b2e
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 zhangshiyu
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,32 @@
1
+ # Jekyll Books Generator
2
+
3
+ ## Installation
4
+
5
+ Add this line to your Jekyll site's `Gemfile`:
6
+
7
+ ```ruby
8
+ gem "jekyll-books"
9
+ ```
10
+
11
+ Add `jekyll-books` to your Jekyll site's `_config.yml`:
12
+
13
+ ```yaml
14
+ plugins:
15
+ - jekyll-books
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install jekyll-books
25
+
26
+ ## Contributing
27
+
28
+ Bug reports and pull requests are welcome on GitHub at https://github.com/erlzhang/jekyll-books. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
29
+
30
+ ## License
31
+
32
+ The theme is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "jekyll-books"
5
+ spec.version = "0.1.0"
6
+ spec.authors = ["Sharon Zhang"]
7
+ spec.email = ["zhangshiyu1992@hotmail.com"]
8
+
9
+ spec.summary = "Jekyll books generatoer."
10
+ spec.homepage = "https://github.com/erlzhang/jekyll-books"
11
+ spec.license = "MIT"
12
+
13
+ spec.files = `git ls-files -z`.split("\x0")
14
+
15
+ spec.required_ruby_version = '>= 2.3'
16
+
17
+ spec.add_runtime_dependency "jekyll", "~> 3.5"
18
+ spec.add_runtime_dependency 'nokogiri', '~> 1.9'
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.16"
21
+ spec.add_development_dependency "rake", "~> 12.0"
22
+ end
@@ -0,0 +1,186 @@
1
+ module Jekyll
2
+ require_relative "util"
3
+
4
+ class BookPage < Page
5
+ def initialize(site, base, dir)
6
+ @site = site
7
+ @base = base
8
+ @dir = dir.gsub(/^_/, "").downcase
9
+ @name = "index.md"
10
+
11
+ self.process(@name)
12
+
13
+ read_yaml(File.join(@base, "_books", @dir), @name)
14
+
15
+ self.data["layout"] = 'book'
16
+
17
+ Util.init_date_of_book(self.data)
18
+ end
19
+ end
20
+
21
+ class ChapterPage < Page
22
+ def initialize(site, base, dir, name, book, config)
23
+ @site = site
24
+ @base = base
25
+
26
+ if name.include?("/")
27
+ names = File.split(name)
28
+ @dir = File.join(dir, names[0])
29
+ @name = names[1]
30
+ else
31
+ @dir = dir
32
+ @name = name
33
+ end
34
+
35
+ self.process(@name)
36
+
37
+ read_yaml(File.join(base, "_books", @dir), @name)
38
+
39
+ self.data["layout"] = 'chapter'
40
+ self.data["book"] = book
41
+ self.data["title"] = config["title"]
42
+ self.data["level"] = config["level"]
43
+ end
44
+
45
+ def is_top_level?
46
+ self.data["level"] == 1
47
+ end
48
+ end
49
+
50
+ class BooksGenerator < Generator
51
+ def generate(site)
52
+
53
+ books = []
54
+
55
+ dir = "_books"
56
+ begin
57
+ Dir.foreach(dir) do |book_dir|
58
+ book_path = File.join(dir, book_dir)
59
+ if File.directory?(book_path) and book_dir.chars.first != "."
60
+ book = BookPage.new(site, site.source, book_dir)
61
+
62
+ summary = File.read(File.join(book_path, "SUMMARY.md"))
63
+
64
+ parts = get_parts_from_summary(summary)
65
+
66
+ chapters = create_chapters(site, parts, book_dir, book)
67
+
68
+ add_prev_and_next_to_every_chapter(chapters)
69
+
70
+ push_chapters_to_site_pages(site, chapters)
71
+
72
+ add_next_to_book_and_prev_to_chapter(book, chapters.first)
73
+
74
+ books << book
75
+ site.pages << book
76
+ end
77
+ end
78
+
79
+ books += get_books_from_data(site)
80
+
81
+ sort_books_by_date(books)
82
+
83
+ site.config["books"] = books
84
+ rescue Exception => e
85
+ puts e
86
+ end
87
+ end
88
+
89
+ private
90
+ def get_parts_from_summary(summary)
91
+ require "nokogiri"
92
+
93
+ html = Kramdown::Document.new(summary).to_html
94
+ parsed_html = Nokogiri::HTML(html)
95
+
96
+ chapters = []
97
+
98
+ list = parsed_html.xpath("//body/ul/li")
99
+ list.each do |li|
100
+ chapters.push( get_chapter_from_li(li, 1) )
101
+
102
+ li.xpath("ul/li").each do |sub_li|
103
+ chapters.push( get_chapter_from_li(sub_li, 2) )
104
+ end
105
+ end
106
+ return chapters
107
+ end
108
+
109
+ def get_chapter_from_li(li, level)
110
+ chapter = Hash.new
111
+ chapter["link"] = li.xpath("a/@href").to_s
112
+ chapter["title"] = li.xpath("a/text()").to_s
113
+ chapter["level"] = level
114
+ return chapter
115
+ end
116
+
117
+ def add_prev_and_next_to_every_chapter(chapters)
118
+ chapters.each_with_index do |chapter, index|
119
+ if index > 0
120
+ chapter.data["prev"] = chapters[index - 1]
121
+ end
122
+ if index < chapters.size - 1
123
+ chapter.data["next"] = chapters[index + 1]
124
+ end
125
+ end
126
+ end
127
+
128
+ def push_chapters_to_site_pages(site, chapters)
129
+ chapters.each do |chapter|
130
+ site.pages << chapter
131
+ end
132
+ end
133
+
134
+ def add_next_to_book_and_prev_to_chapter(book, chapter)
135
+ book.data["next"] = chapter
136
+ chapter.data["prev"] = book
137
+ end
138
+
139
+ def create_index_page(site, books)
140
+ book_index = IndexPage.new(site, site.source, "", books[0..3])
141
+ book_index.render(site.layouts, site.site_payload)
142
+ book_index.write(site.dest)
143
+ return book_index
144
+ end
145
+
146
+ def create_chapters(site, parts, book_dir, book)
147
+ chapters = []
148
+ book.data["parts"] = []
149
+
150
+ current = nil
151
+
152
+ parts.each do |part|
153
+ chapter = ChapterPage.new(site, site.source, book_dir, part["link"], book, part)
154
+
155
+ if chapter.is_top_level?
156
+ book.data["parts"].push(chapter)
157
+ current = chapter
158
+ chapter.data["parts"] = []
159
+ else
160
+ current.data["parts"].push(chapter)
161
+ end
162
+
163
+ chapters.push(chapter)
164
+ end
165
+ return chapters
166
+ end
167
+
168
+ def sort_books_by_date(books)
169
+ books.sort_by! do |book|
170
+ if book.respond_to? "data"
171
+ [book.data["end"], book.data["start"]]
172
+ else
173
+ [book["end"], book["end"]]
174
+ end
175
+ end
176
+ books.reverse!
177
+ end
178
+
179
+ def get_books_from_data(site)
180
+ books = site.data["books"]
181
+ books.each do |book|
182
+ Util.init_date_of_book(book)
183
+ end
184
+ end
185
+ end
186
+ end
data/lib/util.rb ADDED
@@ -0,0 +1,17 @@
1
+ module Util
2
+ def Util.init_date_of_book(book)
3
+ if not book["end"]
4
+ book["end"] = Time.now.year
5
+ end
6
+
7
+ if not book["start"]
8
+ book["start"] = book["end"]
9
+ end
10
+
11
+ if book["start"] == book["end"]
12
+ book["date"] = book["start"].to_s
13
+ else
14
+ book["date"] = "#{book["start"]}-#{book["end"]}"
15
+ end
16
+ end
17
+ end
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jekyll-books
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Sharon Zhang
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-06-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: jekyll
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.5'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: nokogiri
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.9'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.9'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.16'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.16'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '12.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '12.0'
69
+ description:
70
+ email:
71
+ - zhangshiyu1992@hotmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - jekyll-books.gemspec
81
+ - lib/jekyll-books.rb
82
+ - lib/util.rb
83
+ homepage: https://github.com/erlzhang/jekyll-books
84
+ licenses:
85
+ - MIT
86
+ metadata: {}
87
+ post_install_message:
88
+ rdoc_options: []
89
+ require_paths:
90
+ - lib
91
+ required_ruby_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '2.3'
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ requirements: []
102
+ rubyforge_project:
103
+ rubygems_version: 2.5.2.1
104
+ signing_key:
105
+ specification_version: 4
106
+ summary: Jekyll books generatoer.
107
+ test_files: []