jekyll-readme-index 0.4.0 → 0.5.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3ff724c2f3f4731b73dc6b4b4ced3bcd84793669c41913c477ea0c926b25a11b
4
- data.tar.gz: 49768447b305e6dda9605997b1ef8b45b513dfe6ebd9a95136db3bdc383291df
3
+ metadata.gz: a962fee2eeeb3f2afc21c0e7a4d99d50db52e11750358d7ea13a1cfc76ea5b01
4
+ data.tar.gz: ed15d139b9f068004a0a7e36defdc56bcfd2fc289299f404548531724b18dad5
5
5
  SHA512:
6
- metadata.gz: cea83b276e2017f4c150400e77f39a6e985bee2a0c0cd7b4fff61da9f0e6f2dce75a2ac0a9be4fd76ea768252cc1fd71457751816c56db91486b908b7cc17526
7
- data.tar.gz: 3d538fec44f058263ea888acc364f3741304eb9482c4b28e6ea171634d00250f513d6f330fb13da9d7f15bcd5ebc882582ce086cc3a56a178ae3ca233077bdaa
6
+ metadata.gz: 259e6eabc8d4b5ea02b77377f331099ec70413fede4d8dc4d2d9ef5ae9ca120d8cdb7892f17b54d00745f48277a0edd976e770d2a6eabba27940fefdedf36a85
7
+ data.tar.gz: d3770314f5f85253a15ad8b471f13ae6fe265f1b6eac759fd56cd62cff09ccd5fd8a8665ed644a5fb68e790ee717f806f1dca3f71fc3b7e54b54f6591d3429a6
@@ -1,8 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "set"
4
+
3
5
  module JekyllReadmeIndex
4
6
  class Generator < Jekyll::Generator
5
- INDEX_REGEX = %r!$|index\.(html?|xhtml|xml)$!i.freeze
7
+ # Captures the directory a URL is the index of, e.g. "/a/" for "/a/" or "/a/index.html"
8
+ INDEX_URL_REGEX = %r!\A(.*/)(?:index\.(?:html?|xhtml|xml))?\z!i.freeze
6
9
  GITHUB_DIR = "/.github"
7
10
  DOCS_DIR = "/docs"
8
11
  SPECIAL_DIRS = [GITHUB_DIR, DOCS_DIR].freeze
@@ -27,12 +30,15 @@ module JekyllReadmeIndex
27
30
 
28
31
  def generate(site)
29
32
  @site = site
33
+ @index_dirs = nil
30
34
  return if disabled?
31
35
 
32
36
  readmes.each do |readme|
33
37
  next unless should_be_index?(readme)
34
38
 
35
- site.pages << readme.to_page
39
+ page = to_page(readme)
40
+ site.pages << page
41
+ add_index_dir(page.url)
36
42
  site.static_files.delete(readme) if cleanup?
37
43
  end
38
44
 
@@ -40,7 +46,7 @@ module JekyllReadmeIndex
40
46
  readmes_with_frontmatter.each do |readme|
41
47
  next unless should_be_index?(readme)
42
48
 
43
- readme.update_permalink
49
+ add_index_dir(update_permalink(readme))
44
50
  end
45
51
  end
46
52
  end
@@ -60,7 +66,6 @@ module JekyllReadmeIndex
60
66
 
61
67
  # Prioritize READMEs according to GitHub's order: .github > root > docs
62
68
  # For each target directory, keep only the highest priority README
63
- # rubocop:disable Metrics/PerceivedComplexity
64
69
  def prioritize_readmes(candidates)
65
70
  grouped = candidates.group_by do |file|
66
71
  # Get the directory that would become the index
@@ -80,8 +85,7 @@ module JekyllReadmeIndex
80
85
  grouped.flat_map do |_dir, files|
81
86
  # Sort by priority: .github first, then root, then docs, then others
82
87
  files.min_by do |file|
83
- path = file.respond_to?(:relative_path) ? file.relative_path : "/" + file.path
84
- case path
88
+ case readme_path(file)
85
89
  when GITHUB_README_PATTERN then 0
86
90
  when ROOT_README_PATTERN then 1
87
91
  when DOCS_README_PATTERN then 2
@@ -90,22 +94,81 @@ module JekyllReadmeIndex
90
94
  end
91
95
  end.compact
92
96
  end
93
- # rubocop:enable Metrics/PerceivedComplexity
97
+
98
+ # Convert a README StaticFile to a Page that serves as its directory's index
99
+ def to_page(static_file)
100
+ # StaticFile doesn't expose its base, dir, or name (the last only since
101
+ # Jekyll 4), so read them the same way jekyll-optional-front-matter does.
102
+ base = static_file.instance_variable_get(:@base)
103
+ dir = static_file.instance_variable_get(:@dir)
104
+ name = static_file.instance_variable_get(:@name)
105
+ page = Jekyll::Page.new(site, base, dir, name)
106
+
107
+ page.data["permalink"] = target_dir(static_file)
108
+ page
109
+ end
110
+
111
+ # Point a README Page's permalink at its directory
112
+ def update_permalink(page)
113
+ # If URL already ends with '/', it's a directory URL and should be used as-is
114
+ url = page.url
115
+ page.data["permalink"] = url.end_with?("/") ? url : target_dir(page)
116
+ # Page#url is memoized; drop it so it's rebuilt from the new permalink
117
+ page.instance_variable_set(:@url, nil)
118
+ page.url
119
+ end
120
+
121
+ # The directory a README should be the index for
122
+ def target_dir(file)
123
+ # For READMEs in .github or docs at root level, they should be the root index
124
+ return "/" if special_readme?(file)
125
+
126
+ File.join(File.dirname(file.url), "/")
127
+ end
128
+
129
+ # Check if this is a README in a special directory (.github or docs)
130
+ def special_readme?(file)
131
+ path = readme_path(file)
132
+ path =~ GITHUB_README_PATTERN || path =~ DOCS_README_PATTERN
133
+ end
134
+
135
+ # The file's path relative to the site source, with a leading slash.
136
+ #
137
+ # StaticFile#relative_path starts with "/", but Page#relative_path doesn't
138
+ # on Jekyll 4 (or for root pages on Jekyll 3), so the priority patterns
139
+ # never matched READMEs with front matter.
140
+ def readme_path(file)
141
+ path = file.relative_path
142
+ path.start_with?("/") ? path : "/#{path}"
143
+ end
94
144
 
95
145
  # Should the given readme be the containing directory's index?
96
146
  def should_be_index?(readme)
97
147
  return false unless readme
98
148
 
99
- !dir_has_index? File.dirname(readme.url)
149
+ !dir_has_index? target_dir(readme)
100
150
  end
101
151
 
102
152
  # Does the given directory have an index?
103
153
  #
104
154
  # relative_path - the directory path relative to the site root
105
155
  def dir_has_index?(relative_path)
106
- relative_path << "/" unless relative_path.end_with? "/"
107
- regex = %r!^#{Regexp.escape(relative_path)}#{INDEX_REGEX}!i
108
- (site.pages + site.static_files).any? { |file| file.url =~ regex }
156
+ relative_path = File.join(relative_path, "/") unless relative_path.end_with?("/")
157
+ index_dirs.include?(relative_path.downcase)
158
+ end
159
+
160
+ # The (downcased) directories that already have an index page or file.
161
+ # Built once per generate instead of scanning every file for every README.
162
+ def index_dirs
163
+ @index_dirs ||= Set.new.tap do |dirs|
164
+ site.pages.each { |page| add_index_dir(page.url, dirs) }
165
+ site.static_files.each { |file| add_index_dir(file.url, dirs) }
166
+ end
167
+ end
168
+
169
+ def add_index_dir(url, dirs = index_dirs)
170
+ match = INDEX_URL_REGEX.match(url)
171
+ dirs << match[1].downcase if match
109
172
  end
110
173
 
111
174
  # Regexp to match a file path against to detect if the given file is a README
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JekyllReadmeIndex
4
- VERSION = "0.4.0"
4
+ VERSION = "0.5.0"
5
5
  end
@@ -2,7 +2,6 @@
2
2
 
3
3
  require "jekyll"
4
4
  require "jekyll-readme-index/generator"
5
- require_relative "./jekyll/static_file_ext"
6
5
 
7
6
  module JekyllReadmeIndex
8
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-readme-index
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Balter
@@ -36,7 +36,7 @@ dependencies:
36
36
  - - "~>"
37
37
  - !ruby/object:Gem::Version
38
38
  version: '1.1'
39
- type: :runtime
39
+ type: :development
40
40
  prerelease: false
41
41
  version_requirements: !ruby/object:Gem::Requirement
42
42
  requirements:
@@ -178,7 +178,6 @@ files:
178
178
  - lib/jekyll-readme-index.rb
179
179
  - lib/jekyll-readme-index/generator.rb
180
180
  - lib/jekyll-readme-index/version.rb
181
- - lib/jekyll/static_file_ext.rb
182
181
  homepage: https://github.com/benbalter/jekyll-readme-index
183
182
  licenses:
184
183
  - MIT
@@ -190,14 +189,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
190
189
  requirements:
191
190
  - - ">="
192
191
  - !ruby/object:Gem::Version
193
- version: '0'
192
+ version: '3.0'
194
193
  required_rubygems_version: !ruby/object:Gem::Requirement
195
194
  requirements:
196
195
  - - ">="
197
196
  - !ruby/object:Gem::Version
198
197
  version: '0'
199
198
  requirements: []
200
- rubygems_version: 4.0.16
199
+ rubygems_version: 3.6.9
201
200
  specification_version: 4
202
201
  summary: A Jekyll plugin to render a project's README as the site's index.
203
202
  test_files: []
@@ -1,43 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Jekyll
4
- class StaticFile
5
- # Convert this static file to a Page
6
- def to_page
7
- page = Jekyll::Page.new(@site, @base, @dir, @name)
8
-
9
- # For READMEs in .github or docs at root level, they should be the root index
10
- target_dir = if special_readme?
11
- "/"
12
- else
13
- File.dirname(url) + "/"
14
- end
15
-
16
- page.data["permalink"] = target_dir
17
- page
18
- end
19
-
20
- private
21
-
22
- # Check if this is a README in a special directory (.github or docs)
23
- def special_readme?
24
- relative_path =~ JekyllReadmeIndex::Generator::GITHUB_README_PATTERN ||
25
- relative_path =~ JekyllReadmeIndex::Generator::DOCS_README_PATTERN
26
- end
27
- end
28
-
29
- class Page
30
- def update_permalink
31
- # If URL already ends with '/', it's a directory URL and should be used as-is
32
- # Otherwise, extract the directory from the file URL
33
- target_dir = url.end_with?("/") ? url : File.dirname(url) + "/"
34
-
35
- data["permalink"] = target_dir
36
- @url = URL.new(
37
- :template => template,
38
- :placeholders => url_placeholders,
39
- :permalink => permalink
40
- ).to_s
41
- end
42
- end
43
- end