bridgetown-sitemap 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +41 -0
  3. data/.rubocop.yml +25 -0
  4. data/Gemfile +13 -0
  5. data/LICENSE.md +21 -0
  6. data/README.md +69 -0
  7. data/Rakefile +13 -0
  8. data/bridgetown-sitemap.gemspec +26 -0
  9. data/lib/bridgetown-sitemap.rb +7 -0
  10. data/lib/bridgetown-sitemap/builder.rb +73 -0
  11. data/lib/bridgetown-sitemap/version.rb +5 -0
  12. data/lib/bridgetown/resource/base.rb +11 -0
  13. data/lib/robots.liquid +1 -0
  14. data/lib/sitemap.erb +36 -0
  15. data/script/cibuild +6 -0
  16. data/script/fmt +10 -0
  17. data/script/release +7 -0
  18. data/script/test +23 -0
  19. data/test/fixtures/bridgetown.config.yml +8 -0
  20. data/test/fixtures/plugins/builders/generated_pages_builder.rb +11 -0
  21. data/test/fixtures/plugins/site_builder.rb +3 -0
  22. data/test/fixtures/src/_data/.keep +0 -0
  23. data/test/fixtures/src/_layouts/default.html +10 -0
  24. data/test/fixtures/src/_other_things/test2.html +4 -0
  25. data/test/fixtures/src/_posts/2019-04-01-/351/224/231/350/257/257.html +2 -0
  26. data/test/fixtures/src/_posts/2019-07-14-last-modified-at.md +4 -0
  27. data/test/fixtures/src/_posts/2020-04-02-/351/224/231/350/257/257.html +3 -0
  28. data/test/fixtures/src/_posts/2020-04-03-/351/224/231/350/257/257.html +3 -0
  29. data/test/fixtures/src/_posts/2020-05-11-exclude-this-post.md +5 -0
  30. data/test/fixtures/src/_posts/2021-03-02-march-the-second.md +5 -0
  31. data/test/fixtures/src/_posts/2021-03-04-march-the-fourth.md +5 -0
  32. data/test/fixtures/src/_posts/2021-05-06-may-the-sixth.md +5 -0
  33. data/test/fixtures/src/about.html +6 -0
  34. data/test/fixtures/src/assets/sample_image.jpg +0 -0
  35. data/test/fixtures/src/assets/sample_pdf.pdf +0 -0
  36. data/test/fixtures/src/excluded_files/excluded_pdf.pdf +0 -0
  37. data/test/fixtures/src/excluded_files/html_file.html +0 -0
  38. data/test/fixtures/src/feeds/atom.xml +3 -0
  39. data/test/fixtures/src/index.html +5 -0
  40. data/test/fixtures/src/some-subfolder/exclude-this-page.html +5 -0
  41. data/test/fixtures/src/some-subfolder/htm.htm +1 -0
  42. data/test/fixtures/src/some-subfolder/index.html +1 -0
  43. data/test/fixtures/src/some-subfolder/test_index.html +4 -0
  44. data/test/fixtures/src/some-subfolder/this-is-a-subfile.html +0 -0
  45. data/test/fixtures/src/some-subfolder/this-is-a-subpage.html +4 -0
  46. data/test/fixtures/src/some-subfolder/xhtml.xhtml +1 -0
  47. data/test/fixtures/src/this-has-non-standard-chars.md +5 -0
  48. data/test/helper.rb +79 -0
  49. data/test/test_sitemap.rb +198 -0
  50. metadata +183 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b0baaab38ea6feb29653fec867f81a3cff27f8423497ebd91b5b43b786f4f3cc
4
+ data.tar.gz: 1ee7250c217916417007949bc5238fffa139a7ecbe3e46b87e3dd289a61e813a
5
+ SHA512:
6
+ metadata.gz: be2ff77fd2e15d1741fa8a57938059d0c475a44a226353b9be2fd12d8469a9c073b103794957390c95a5d157a64332d1196bdfb33e3f7a80407fef228d9ff9ac
7
+ data.tar.gz: 2fcab96c2edaae32d3355328e8afec2d84878af173ec9d02c452b37aa75c3e4566a116b3cf5dc77aa2b9e989c1053e09dc8c9772a8fc4d0b2b2b5c1e6124465b
data/.gitignore ADDED
@@ -0,0 +1,41 @@
1
+ /vendor
2
+ /.bundle/
3
+ /.yardoc
4
+ /Gemfile.lock
5
+ /_yardoc/
6
+ /coverage/
7
+ /doc/
8
+ /pkg/
9
+ /spec/reports/
10
+ /tmp/
11
+ *.bundle
12
+ *.so
13
+ *.o
14
+ *.a
15
+ mkmf.log
16
+ *.gem
17
+ Gemfile.lock
18
+ .bundle
19
+ .ruby-version
20
+
21
+ # Node
22
+ node_modules
23
+ .npm
24
+ .node_repl_history
25
+
26
+ # Yarn
27
+ yarn-error.log
28
+ yarn-debug.log*
29
+ .pnp/
30
+ .pnp.js
31
+
32
+ # Yarn Integrity file
33
+ .yarn-integrity
34
+
35
+ test/dest
36
+ .bridgetown-metadata
37
+ .bridgetown-cache
38
+ .bridgetown-webpack
39
+
40
+ # macOS
41
+ .DS_Store
data/.rubocop.yml ADDED
@@ -0,0 +1,25 @@
1
+ require: rubocop-bridgetown
2
+
3
+ inherit_gem:
4
+ rubocop-bridgetown: .rubocop.yml
5
+
6
+ AllCops:
7
+ TargetRubyVersion: 2.5
8
+ Include:
9
+ - lib/**/*.rb
10
+
11
+ Exclude:
12
+ - .gitignore
13
+ - .rspec
14
+ - .rubocop.yml
15
+
16
+ - Gemfile.lock
17
+ - CHANGELOG.md
18
+ - LICENSE.txt
19
+ - README.md
20
+
21
+ - script/**/*
22
+ - vendor/**/*
23
+
24
+ Layout/LineLength:
25
+ Max: 120
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+ gemspec
5
+
6
+ gem "bridgetown", ENV["BRIDGETOWN_VERSION"] if ENV["BRIDGETOWN_VERSION"]
7
+
8
+ group :test do
9
+ gem "minitest"
10
+ gem "minitest-profile"
11
+ gem "minitest-reporters"
12
+ gem "shoulda"
13
+ end
data/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020-present.
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,69 @@
1
+ # Bridgetown Sitemap Generator Plugin
2
+
3
+ **_Bridgetown plugin to silently generate a sitemaps.org compliant sitemap for your Bridgetown site_**
4
+
5
+ ## Usage
6
+
7
+ 1. Install the plugin with the following command:
8
+
9
+ ```shell
10
+ bundle add bridgetown-sitemap -g bridgetown_plugins
11
+ ```
12
+
13
+ 2. Add the following to your site's `bridgetown.config.yml`:
14
+
15
+ ```yml
16
+ url: "https://example.com" # the base hostname & protocol for your site
17
+ ```
18
+
19
+ <br>
20
+
21
+ **This plugin only supports Bridgetown sites that use the [resource content engine](https://www.bridgetownrb.com/docs/resources).**
22
+
23
+ This can be configured by adding the following line to your site's `bridgetown.config.yml`:
24
+
25
+ ```yml
26
+ content_engine: "resource"
27
+ ```
28
+
29
+
30
+ ## `<lastmod>` tag
31
+ The `<lastmod>` tag in the `sitemap.xml` will reflect by priority:
32
+
33
+ 1. A personalised date if you add the variable `last_modified_at:` with a date in the Front Matter. (*Dates need to be formatted as* `%Y-%m-%d %H:%M:%S %z`)
34
+ 2. The modified date of the file as reported by the filesystem.
35
+
36
+
37
+ ## Exclusions
38
+
39
+ If you would like to exclude specific pages from the sitemap set the
40
+ sitemap flag to `false` in the front matter for the page.
41
+
42
+ ```yml
43
+ sitemap: false
44
+ ```
45
+
46
+ To exclude multiple files, add a glob config to your `bridgetown.config.yml` file.
47
+
48
+ ```yml
49
+ defaults:
50
+ -
51
+ scope:
52
+ path: "assets/**/*.pdf"
53
+ values:
54
+ sitemap: false
55
+ ```
56
+
57
+ ## Testing
58
+
59
+ * Run `bundle exec rake test` to run the test suite
60
+ * Or run `script/cibuild` to validate with Rubocop and run tests together.
61
+
62
+ ## Contributing
63
+
64
+ 1. Fork it (https://github.com/ayushn21/bridgetown-sitemap/fork)
65
+ 2. Clone the fork using `git clone` to your local development machine.
66
+ 3. Create your feature branch (`git checkout -b my-new-feature`)
67
+ 4. Commit your changes (`git commit -am 'Add some feature'`)
68
+ 5. Push to the branch (`git push origin my-new-feature`)
69
+ 6. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+
5
+ task spec: :test
6
+ require "rake/testtask"
7
+
8
+ Rake::TestTask.new(:test) do |test|
9
+ test.libs << "lib" << "test"
10
+ test.pattern = "test/**/test_*.rb"
11
+ test.verbose = true
12
+ test.warning = false
13
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/bridgetown-sitemap/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "bridgetown-sitemap"
7
+ spec.summary = "Automatically generate a sitemap.xml for your Bridgetown site."
8
+ spec.version = BridgetownSitemap::VERSION
9
+ spec.authors = ["Ayush Newatia"]
10
+ spec.email = "ayush@hey.com"
11
+ spec.homepage = "https://github.com/ayushn21/bridgetown-sitemap"
12
+ spec.licenses = ["MIT"]
13
+
14
+ spec.files = `git ls-files -z`.split("\x0")
15
+ spec.executables = spec.files.grep(%r!^bin/!) { |f| File.basename(f) }
16
+ spec.test_files = spec.files.grep(%r!^(test|spec|features)/!)
17
+ spec.require_paths = ["lib"]
18
+
19
+ spec.required_ruby_version = ">= 2.5.0"
20
+
21
+ spec.add_dependency "bridgetown", ">= 0.20", "< 2.0"
22
+
23
+ spec.add_development_dependency "bundler"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "rubocop-bridgetown", "~> 0.2"
26
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bridgetown"
4
+ require "bridgetown/resource/base"
5
+ require "bridgetown-sitemap/builder"
6
+
7
+ BridgetownSitemap::Builder.register
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "fileutils"
4
+
5
+ module BridgetownSitemap
6
+ class UnsupportedContentEngine < StandardError; end
7
+
8
+ class Builder < Bridgetown::Builder
9
+ def build
10
+ hook :site, :pre_render, priority: :low do |site|
11
+ unless site.uses_resource?
12
+ Bridgetown.logger.error "\n\nbridgetown-sitemap only supports the resource content engine"
13
+ Bridgetown.logger.info "Add `content_engine: 'resource'` to your bridgetown.config.yml\n\n"
14
+ raise UnsupportedContentEngine
15
+ end
16
+
17
+ @site = site
18
+
19
+ @site.generated_pages << sitemap unless file_exists?("sitemap.xml")
20
+ @site.generated_pages << robots unless file_exists?("robots.txt")
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ INCLUDED_EXTENSIONS = %w(
27
+ .htm
28
+ .html
29
+ .xhtml
30
+ .pdf
31
+ ).freeze
32
+
33
+ # Array of all non-bridgetown site files with an HTML extension
34
+ def static_files
35
+ @site.static_files.select { |file| INCLUDED_EXTENSIONS.include? file.extname }
36
+ end
37
+
38
+ def source_path(file)
39
+ File.expand_path "../#{file}", __dir__
40
+ end
41
+
42
+ def destination_path(file)
43
+ @site.in_dest_dir(file)
44
+ end
45
+
46
+ def sitemap
47
+ site_map = Bridgetown::GeneratedPage.new(@site, @site.source, "/", "sitemap.erb", from_plugin: true)
48
+ site_map.content = File.read(source_path(site_map.name))
49
+ site_map.data.permalink = "/sitemap.xml"
50
+ site_map.data.layout = "none"
51
+ site_map.data.static_files = static_files
52
+ site_map.data.xsl = file_exists?("sitemap.xsl")
53
+ site_map
54
+ end
55
+
56
+ def robots
57
+ robots = Bridgetown::GeneratedPage.new(@site, @site.source, "/", "robots.liquid", from_plugin: true)
58
+ robots.content = File.read(source_path(robots.name))
59
+ robots.data.layout = "none"
60
+ robots.data.permalink = "/robots.txt"
61
+ robots
62
+ end
63
+
64
+ # Checks if a file already exists in the site source
65
+ def file_exists?(file_path)
66
+ pages_and_files.any? { |p| p.relative_path == "/#{file_path}" }
67
+ end
68
+
69
+ def pages_and_files
70
+ @pages_and_files ||= @site.collections.pages.resources + @site.static_files
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BridgetownSitemap
4
+ VERSION = "1.0.0"
5
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bridgetown
4
+ module Resource
5
+ class Base
6
+ def sitemap_last_modified_at
7
+ data.last_modified_at || File.mtime(path) || date
8
+ end
9
+ end
10
+ end
11
+ end
data/lib/robots.liquid ADDED
@@ -0,0 +1 @@
1
+ Sitemap: {{ "sitemap.xml" | absolute_url }}
data/lib/sitemap.erb ADDED
@@ -0,0 +1,36 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+
3
+ <% if page.data.xsl.present? %>
4
+ <?xml-stylesheet type="text/xsl" href="/sitemap.xsl"?>
5
+ <% end %>
6
+
7
+ <urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
8
+ <% output_collection_names = site.config.collections.select { |_name, data| data.output }.keys %>
9
+
10
+ <% collections.slice(*output_collection_names).each do |_name, collection| %>
11
+ <% collection.resources.each do |resource| %>
12
+ <% next if resource.id == "/404" || resource.data.sitemap == false %>
13
+ <url>
14
+ <loc><%= xml_escape resource.absolute_url %></loc>
15
+ <lastmod><%= resource.sitemap_last_modified_at.localtime.xmlschema %></lastmod>
16
+ </url>
17
+ <% end %>
18
+ <% end %>
19
+
20
+ <% site.generated_pages.each do |generated_page| %>
21
+ <% next if ["sitemap.erb", "robots.liquid"].include? generated_page.name %>
22
+ <% next if generated_page.data.sitemap == false %>
23
+ <url>
24
+ <loc><%= xml_escape absolute_url(generated_page.url) %></loc>
25
+ <lastmod><%= (generated_page.data.last_modified_at || site.time).localtime.xmlschema %></lastmod>
26
+ </url>
27
+ <% end %>
28
+
29
+ <% page.data.static_files.each do |file| %>
30
+ <% next if file.data.sitemap == false %>
31
+ <url>
32
+ <loc><%= xml_escape absolute_url(file.relative_path) %></loc>
33
+ <lastmod><%= file.modified_time.localtime.xmlschema %></lastmod>
34
+ </url>
35
+ <% end %>
36
+ </urlset>
data/script/cibuild ADDED
@@ -0,0 +1,6 @@
1
+ #! /bin/bash
2
+
3
+ set -e
4
+
5
+ script/fmt
6
+ script/test
data/script/fmt ADDED
@@ -0,0 +1,10 @@
1
+ #!/bin/bash
2
+ set -e
3
+
4
+ echo "Rubocop $(bundle exec rubocop --version)"
5
+ bundle exec rubocop -D -E $@
6
+ success=$?
7
+ if ((success != 0)); then
8
+ echo -e "\nTry running \`script/fmt -a\` to automatically fix errors"
9
+ fi
10
+ exit $success
data/script/release ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/sh
2
+ # Tag and push a release to Rubygems
3
+
4
+ set -e
5
+
6
+ script/cibuild
7
+ bundle exec rake release
data/script/test ADDED
@@ -0,0 +1,23 @@
1
+ #!/usr/bin/env bash
2
+ set -e
3
+
4
+ # Usage:
5
+ # script/test <test_file>
6
+ # script/test
7
+
8
+ if [ -d test/dest ]
9
+ then rm -r test/dest
10
+ fi
11
+
12
+ testopts="--profile"
13
+
14
+ if [[ $# -lt 1 ]]
15
+ then
16
+ set -x
17
+ time ruby -S bundle exec \
18
+ rake TESTOPTS=$testopts test
19
+ else
20
+ set -x
21
+ time ruby -S bundle exec ruby -I test \
22
+ "$@" $testops
23
+ fi
@@ -0,0 +1,8 @@
1
+ timezone: UTC
2
+
3
+ defaults:
4
+ -
5
+ scope:
6
+ path: "excluded_files/**/*"
7
+ values:
8
+ sitemap: false
@@ -0,0 +1,11 @@
1
+ class GeneratedPagesBuilder < SiteBuilder
2
+ def build
3
+ generator do
4
+ generated_page = Bridgetown::GeneratedPage.new(site, site.source, "/", "generated_page.erb")
5
+ generated_page.content = "<%= 'created from a plugin'.capitalize %>"
6
+ generated_page.data.layout = "default"
7
+
8
+ site.generated_pages << generated_page
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ class SiteBuilder < Bridgetown::Builder
2
+ # write builders which subclass SiteBuilder in plugins/builder
3
+ end
File without changes
@@ -0,0 +1,10 @@
1
+ ---
2
+ ---
3
+ <html>
4
+ <head>
5
+ </head>
6
+ <body>
7
+ THIS IS MY LAYOUT
8
+ {{ content }}
9
+ </body>
10
+ </html>
@@ -0,0 +1,4 @@
1
+ ---
2
+ ---
3
+
4
+ This file shouldn't show up in the sitemap.
@@ -0,0 +1,4 @@
1
+ ---
2
+ ---
3
+
4
+ Please don't modify this file. It's modified time is important.
@@ -0,0 +1,3 @@
1
+ ---
2
+ permalink: "/2020/04/02/错误.html"
3
+ ---
@@ -0,0 +1,3 @@
1
+ ---
2
+ permalink: "/2020/04/03/%E9%94%99%E8%AF%AF.html"
3
+ ---
@@ -0,0 +1,5 @@
1
+ ---
2
+ sitemap: false
3
+ ---
4
+
5
+ This post should not appear in the sitemap.
@@ -0,0 +1,5 @@
1
+ ---
2
+ last_modified_at: 2021-03-02 00:00:00 +0000
3
+ ---
4
+
5
+ March the second!
@@ -0,0 +1,5 @@
1
+ ---
2
+ last_modified_at: 2021-03-04 00:00:00 +0000
3
+ ---
4
+
5
+ March the fourth!
@@ -0,0 +1,5 @@
1
+ ---
2
+ last_modified_at: 2021-05-06 00:00:00 +0000
3
+ ---
4
+
5
+ 6th of May!
@@ -0,0 +1,6 @@
1
+ ---
2
+ layout: default
3
+ sitemap: false
4
+ ---
5
+
6
+ <h1>About</h1>
File without changes
@@ -0,0 +1,3 @@
1
+ <?xml version="1.0" encoding="utf-8"?>
2
+ <feed xmlns="http://www.w3.org/2005/Atom">
3
+ </feed>
@@ -0,0 +1,5 @@
1
+ ---
2
+ layout: default
3
+ ---
4
+
5
+ <h1>Index</h1>
@@ -0,0 +1,5 @@
1
+ ---
2
+ sitemap: false
3
+ ---
4
+
5
+ Exclude this page
@@ -0,0 +1 @@
1
+ This file has an .htm extension, and should be included in the sitemap
@@ -0,0 +1 @@
1
+ static subfolder index.html file that should be indexed as permalink
@@ -0,0 +1,4 @@
1
+ ---
2
+ ---
3
+
4
+ The permalink of this page does not end with a '/', but with a filename
@@ -0,0 +1,4 @@
1
+ ---
2
+ ---
3
+
4
+ This is a subpage!
@@ -0,0 +1 @@
1
+ This file has an .xhtml extension, and should be included in the sitemap
@@ -0,0 +1,5 @@
1
+ ---
2
+ permalink: this url has an ümlaut
3
+ ---
4
+
5
+ # URL contains characters that need to be URI encoded
data/test/helper.rb ADDED
@@ -0,0 +1,79 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "minitest/autorun"
4
+ require "minitest/reporters"
5
+ require "minitest/profile"
6
+ require "shoulda"
7
+ require_relative "../lib/bridgetown-sitemap"
8
+
9
+ # Report with color.
10
+ Minitest::Reporters.use! [
11
+ Minitest::Reporters::DefaultReporter.new(
12
+ color: true
13
+ ),
14
+ ]
15
+
16
+ class BridgetownSitemap::Test < Minitest::Test
17
+ ROOT_DIR = File.expand_path("fixtures", __dir__)
18
+ SOURCE_DIR = File.join(ROOT_DIR, "src")
19
+ DEST_DIR = File.expand_path("dest", __dir__)
20
+
21
+ def build_site
22
+ @site = Bridgetown::Site.new(config)
23
+ process_site
24
+ end
25
+
26
+ def root_dir(*files)
27
+ File.join(ROOT_DIR, *files)
28
+ end
29
+
30
+ def source_dir(*files)
31
+ File.join(SOURCE_DIR, *files)
32
+ end
33
+
34
+ def dest_dir(*files)
35
+ File.join(DEST_DIR, *files)
36
+ end
37
+
38
+ def make_context(registers = {})
39
+ Liquid::Context.new({}, {}, { :site => site }.merge(registers))
40
+ end
41
+
42
+ def config_overrides
43
+ {}
44
+ end
45
+
46
+ def metadata_overrides
47
+ {}
48
+ end
49
+
50
+ private
51
+
52
+ def process_site
53
+ @metadata = {
54
+ "name" => "My Awesome Site",
55
+ "author" => {
56
+ "name" => "Ada Lovejoy",
57
+ }
58
+ }
59
+
60
+ metadata = @metadata.merge(metadata_overrides).to_yaml.sub("---\n", "")
61
+ File.write(source_dir("_data/site_metadata.yml"), metadata)
62
+ @site.process
63
+ FileUtils.rm(source_dir("_data/site_metadata.yml"))
64
+ end
65
+
66
+ def config
67
+ @config ||= Bridgetown.configuration(Bridgetown::Utils.deep_merge_hashes({
68
+ "full_rebuild" => true,
69
+ "root_dir" => root_dir,
70
+ "source" => source_dir,
71
+ "destination" => dest_dir,
72
+ "content_engine" => "resource",
73
+ "url" => "https://example.com",
74
+ "quiet" => true
75
+ },
76
+ config_overrides
77
+ ))
78
+ end
79
+ end
@@ -0,0 +1,198 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "helper"
4
+
5
+ class TestSitemap < BridgetownSitemap::Test
6
+
7
+ context "rendering the site with defaults" do
8
+ setup { build_site }
9
+
10
+ should "create the sitemap" do
11
+ assert File.exist?(dest_dir("sitemap.xml"))
12
+ end
13
+
14
+ should "create robots.txt" do
15
+ assert File.exist?(dest_dir("robots.txt"))
16
+ end
17
+
18
+ context "the sitemap" do
19
+ setup { @sitemap = File.read(dest_dir("sitemap.xml")) }
20
+
21
+ should "have no layout" do
22
+ refute_match %r!THIS IS MY LAYOUT!, @sitemap
23
+ end
24
+
25
+ should "put all the pages in the sitemap" do
26
+ assert_match %r!<loc>https://example\.com/</loc>!, @sitemap
27
+ assert_match %r!<loc>https://example\.com/some-subfolder/this-is-a-subpage/</loc>!, @sitemap
28
+ end
29
+
30
+ should "not put files with output: false into the sitemap" do
31
+ refute_match %r!/other_things/test2\.html</loc>!, @sitemap
32
+ end
33
+
34
+ should "performs URI encoding of site paths" do
35
+ assert_match %r!<loc>https://example\.com/this%20url%20has%20an%20%C3%BCmlaut</loc>!, @sitemap
36
+ end
37
+
38
+ should "put all the posts in the sitemap" do
39
+ assert_match %r!<loc>https://example.com/2021/05/06/may-the-sixth/</loc>!, @sitemap
40
+ assert_match %r!<loc>https://example.com/2021/03/04/march-the-fourth/</loc>!, @sitemap
41
+ assert_match %r!<loc>https://example.com/2021/03/02/march-the-second/</loc>!, @sitemap
42
+ assert_match %r!<loc>https://example.com/2019/07/14/last-modified-at/</loc>!, @sitemap
43
+ end
44
+
45
+ should "generate the correct date for each of the posts" do
46
+ assert_match %r!<lastmod>2021-05-06T00:00:00(-|\+)\d+:\d+</lastmod>!, @sitemap
47
+ assert_match %r!<lastmod>2021-03-04T00:00:00(-|\+)\d+:\d+</lastmod>!, @sitemap
48
+ assert_match %r!<lastmod>2021-03-02T00:00:00(-|\+)\d+:\d+</lastmod>!, @sitemap
49
+ assert_match %r!<lastmod>2020-12-24T03:51:50\+00:00</lastmod>!, @sitemap
50
+ end
51
+
52
+ should "puts all the static HTML files in the sitemap.xml file" do
53
+ assert_match %r!<loc>https://example\.com/some-subfolder/this-is-a-subfile\.html</loc>!, @sitemap
54
+ end
55
+
56
+ should "does not include assets or any static files that aren't .html" do
57
+ refute_match %r!/assets/sample_image\.jpg</loc>!, @sitemap
58
+ refute_match %r!/feeds/atom\.xml</loc>!, @sitemap
59
+ end
60
+
61
+ should "include assets or any static files with .xhtml and .htm extensions" do
62
+ assert_match %r!<loc>https://example\.com/some-subfolder/xhtml\.xhtml</loc>!, @sitemap
63
+ assert_match %r!<loc>https://example\.com/some-subfolder/htm\.htm</loc>!, @sitemap
64
+ end
65
+
66
+ should "include assets or any static files with .pdf extension" do
67
+ assert_match %r!<loc>https://example\.com/assets/sample_pdf\.pdf</loc>!, @sitemap
68
+ end
69
+
70
+ should "not include any files named 404.html" do
71
+ refute_match %r!404.html!, @sitemap
72
+ end
73
+
74
+ should "not include any static files that have set 'sitemap: false'" do
75
+ refute_match %r!/excluded_files/excluded\.pdf!, @sitemap
76
+ end
77
+
78
+ should "not include any static html files that have set 'sitemap: false'" do
79
+ refute_match %r!/excluded_files/html_file\.html!, @sitemap
80
+ end
81
+
82
+ should "not include posts that have set 'sitemap: false'" do
83
+ refute_match %r!exclude-this-post!, @sitemap
84
+ end
85
+
86
+ should "not include pages that have set 'sitemap: false'" do
87
+ refute_match %r!exclude-this-page!, @sitemap
88
+ refute_match %r!about!, @sitemap
89
+ end
90
+
91
+ should "correctly format timestamps of static files" do
92
+ assert_match %r!/this-is-a-subfile\.html</loc>\s+<lastmod>\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(-|\+)\d{2}:\d{2}</lastmod>!, @sitemap
93
+ end
94
+
95
+ should "include the correct number of items" do
96
+ assert_equal 17, @sitemap.scan(%r!(?=<url>)!).count
97
+ end
98
+
99
+ should "include generated pages" do
100
+ assert_match %r!<loc>https://example.com/generated_page/</loc>!, @sitemap
101
+ end
102
+ end
103
+
104
+ context "the robots.txt" do
105
+ setup { @robots = File.read(dest_dir("robots.txt")) }
106
+
107
+ should "have no layout" do
108
+ refute_match %r!\ATHIS IS MY LAYOUT!, @robots
109
+ end
110
+
111
+ should "renders liquid" do
112
+ assert_match "Sitemap: https://example.com/sitemap.xml", @robots
113
+ end
114
+ end
115
+ end
116
+
117
+ context "rendering the site with a baseurl" do
118
+ setup do
119
+ config.baseurl = "/baseurl"
120
+ build_site
121
+ end
122
+
123
+ context "the sitemap" do
124
+ setup { @sitemap = File.read(dest_dir("sitemap.xml")) }
125
+
126
+ should "add the baseurl to the static files" do
127
+ assert_match %r!<loc>https://example\.com/baseurl/some-subfolder/this-is-a-subfile\.html</loc>!, @sitemap
128
+ end
129
+
130
+ should "add the baseurl to the pages" do
131
+ assert_match %r!<loc>https://example\.com/baseurl/</loc>!, @sitemap
132
+ assert_match %r!<loc>https://example\.com/baseurl/some-subfolder/this-is-a-subpage/</loc>!, @sitemap
133
+ end
134
+
135
+ should "add the baseurl to the posts" do
136
+ assert_match %r!<loc>https://example.com/baseurl/2021/05/06/may-the-sixth/</loc>!, @sitemap
137
+ assert_match %r!<loc>https://example.com/baseurl/2021/03/04/march-the-fourth/</loc>!, @sitemap
138
+ assert_match %r!<loc>https://example.com/baseurl/2021/03/02/march-the-second/</loc>!, @sitemap
139
+ assert_match %r!<loc>https://example.com/baseurl/2019/07/14/last-modified-at/</loc>!, @sitemap
140
+ end
141
+ end
142
+
143
+ context "the robots.txt" do
144
+ setup { @robots = File.read(dest_dir("robots.txt")) }
145
+
146
+ should "add contain the baseurl" do
147
+ assert_match "Sitemap: https://example.com/baseurl/sitemap.xml", @robots
148
+ end
149
+ end
150
+ end
151
+
152
+ context "rendering the site with a url that needs URI encoding" do
153
+ setup do
154
+ config.url = "http://ümlaut.example.org"
155
+ build_site
156
+ end
157
+
158
+ context "the sitemap" do
159
+ setup { @sitemap = File.read(dest_dir("sitemap.xml")) }
160
+
161
+ should "performs URI encoding of site url" do
162
+ assert_match %r!<loc>http://xn--mlaut-jva.example.org/</loc>!, @sitemap
163
+ assert_match %r!<loc>http://xn--mlaut-jva.example.org/some-subfolder/this-is-a-subpage/</loc>!, @sitemap
164
+ assert_match %r!<loc>http://xn--mlaut-jva.example.org/2021/03/04/march-the-fourth/</loc>!, @sitemap
165
+ assert_match %r!<loc>http://xn--mlaut-jva.example.org/2020/04/03/%E9%94%99%E8%AF%AF</loc>!, @sitemap
166
+ assert_match %r!<loc>http://xn--mlaut-jva.example.org/2020/04/02/%E9%94%99%E8%AF%AF</loc>!, @sitemap
167
+ assert_match %r!<loc>http://xn--mlaut-jva.example.org/2019/04/01/%E9%94%99%E8%AF%AF/</loc>!, @sitemap
168
+ end
169
+ end
170
+ end
171
+
172
+ context "rendering the site with a user defined robots.txt" do
173
+ setup do
174
+ File.write(source_dir("robots.txt"), "ROBOT")
175
+ build_site
176
+ @robots = File.read(dest_dir("robots.txt"))
177
+ end
178
+
179
+ teardown do
180
+ File.delete(source_dir("robots.txt"))
181
+ end
182
+
183
+ should "not overwrite the robots.txt" do
184
+ assert_match %r!ROBOT!, @robots
185
+ refute_match %r!Sitemap!, @robots
186
+ end
187
+ end
188
+
189
+ context "rendering the site without the resource content engine" do
190
+ setup { config.delete "content_engine" }
191
+
192
+ should "throw an error" do
193
+ capture_io do
194
+ assert_raises(BridgetownSitemap::UnsupportedContentEngine) { build_site }
195
+ end
196
+ end
197
+ end
198
+ end
metadata ADDED
@@ -0,0 +1,183 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bridgetown-sitemap
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Ayush Newatia
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-05-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bridgetown
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0.20'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '2.0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '0.20'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '2.0'
33
+ - !ruby/object:Gem::Dependency
34
+ name: bundler
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: rake
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: rubocop-bridgetown
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '0.2'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '0.2'
75
+ description:
76
+ email: ayush@hey.com
77
+ executables: []
78
+ extensions: []
79
+ extra_rdoc_files: []
80
+ files:
81
+ - ".gitignore"
82
+ - ".rubocop.yml"
83
+ - Gemfile
84
+ - LICENSE.md
85
+ - README.md
86
+ - Rakefile
87
+ - bridgetown-sitemap.gemspec
88
+ - lib/bridgetown-sitemap.rb
89
+ - lib/bridgetown-sitemap/builder.rb
90
+ - lib/bridgetown-sitemap/version.rb
91
+ - lib/bridgetown/resource/base.rb
92
+ - lib/robots.liquid
93
+ - lib/sitemap.erb
94
+ - script/cibuild
95
+ - script/fmt
96
+ - script/release
97
+ - script/test
98
+ - test/fixtures/bridgetown.config.yml
99
+ - test/fixtures/plugins/builders/generated_pages_builder.rb
100
+ - test/fixtures/plugins/site_builder.rb
101
+ - test/fixtures/src/_data/.keep
102
+ - test/fixtures/src/_layouts/default.html
103
+ - test/fixtures/src/_other_things/test2.html
104
+ - test/fixtures/src/_posts/2019-04-01-错误.html
105
+ - test/fixtures/src/_posts/2019-07-14-last-modified-at.md
106
+ - test/fixtures/src/_posts/2020-04-02-错误.html
107
+ - test/fixtures/src/_posts/2020-04-03-错误.html
108
+ - test/fixtures/src/_posts/2020-05-11-exclude-this-post.md
109
+ - test/fixtures/src/_posts/2021-03-02-march-the-second.md
110
+ - test/fixtures/src/_posts/2021-03-04-march-the-fourth.md
111
+ - test/fixtures/src/_posts/2021-05-06-may-the-sixth.md
112
+ - test/fixtures/src/about.html
113
+ - test/fixtures/src/assets/sample_image.jpg
114
+ - test/fixtures/src/assets/sample_pdf.pdf
115
+ - test/fixtures/src/excluded_files/excluded_pdf.pdf
116
+ - test/fixtures/src/excluded_files/html_file.html
117
+ - test/fixtures/src/feeds/atom.xml
118
+ - test/fixtures/src/index.html
119
+ - test/fixtures/src/some-subfolder/exclude-this-page.html
120
+ - test/fixtures/src/some-subfolder/htm.htm
121
+ - test/fixtures/src/some-subfolder/index.html
122
+ - test/fixtures/src/some-subfolder/test_index.html
123
+ - test/fixtures/src/some-subfolder/this-is-a-subfile.html
124
+ - test/fixtures/src/some-subfolder/this-is-a-subpage.html
125
+ - test/fixtures/src/some-subfolder/xhtml.xhtml
126
+ - test/fixtures/src/this-has-non-standard-chars.md
127
+ - test/helper.rb
128
+ - test/test_sitemap.rb
129
+ homepage: https://github.com/ayushn21/bridgetown-sitemap
130
+ licenses:
131
+ - MIT
132
+ metadata: {}
133
+ post_install_message:
134
+ rdoc_options: []
135
+ require_paths:
136
+ - lib
137
+ required_ruby_version: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - ">="
140
+ - !ruby/object:Gem::Version
141
+ version: 2.5.0
142
+ required_rubygems_version: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - ">="
145
+ - !ruby/object:Gem::Version
146
+ version: '0'
147
+ requirements: []
148
+ rubygems_version: 3.1.4
149
+ signing_key:
150
+ specification_version: 4
151
+ summary: Automatically generate a sitemap.xml for your Bridgetown site.
152
+ test_files:
153
+ - test/fixtures/bridgetown.config.yml
154
+ - test/fixtures/plugins/builders/generated_pages_builder.rb
155
+ - test/fixtures/plugins/site_builder.rb
156
+ - test/fixtures/src/_data/.keep
157
+ - test/fixtures/src/_layouts/default.html
158
+ - test/fixtures/src/_other_things/test2.html
159
+ - test/fixtures/src/_posts/2019-04-01-错误.html
160
+ - test/fixtures/src/_posts/2019-07-14-last-modified-at.md
161
+ - test/fixtures/src/_posts/2020-04-02-错误.html
162
+ - test/fixtures/src/_posts/2020-04-03-错误.html
163
+ - test/fixtures/src/_posts/2020-05-11-exclude-this-post.md
164
+ - test/fixtures/src/_posts/2021-03-02-march-the-second.md
165
+ - test/fixtures/src/_posts/2021-03-04-march-the-fourth.md
166
+ - test/fixtures/src/_posts/2021-05-06-may-the-sixth.md
167
+ - test/fixtures/src/about.html
168
+ - test/fixtures/src/assets/sample_image.jpg
169
+ - test/fixtures/src/assets/sample_pdf.pdf
170
+ - test/fixtures/src/excluded_files/excluded_pdf.pdf
171
+ - test/fixtures/src/excluded_files/html_file.html
172
+ - test/fixtures/src/feeds/atom.xml
173
+ - test/fixtures/src/index.html
174
+ - test/fixtures/src/some-subfolder/exclude-this-page.html
175
+ - test/fixtures/src/some-subfolder/htm.htm
176
+ - test/fixtures/src/some-subfolder/index.html
177
+ - test/fixtures/src/some-subfolder/test_index.html
178
+ - test/fixtures/src/some-subfolder/this-is-a-subfile.html
179
+ - test/fixtures/src/some-subfolder/this-is-a-subpage.html
180
+ - test/fixtures/src/some-subfolder/xhtml.xhtml
181
+ - test/fixtures/src/this-has-non-standard-chars.md
182
+ - test/helper.rb
183
+ - test/test_sitemap.rb