perron 0.16.0 → 0.17.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: 709c1a304332c522dd7e425824f2718b1a84994db9695b842cddab07fa2a6c2d
4
- data.tar.gz: cd636faddbff65dd1cc27652fb9141a936ce7df7e5f4fca1e156fd7b1ff27522
3
+ metadata.gz: fbf1b0d5483938df7b88423f3ecd72bffaf938eadcc49a3001a34fcb0156dfb9
4
+ data.tar.gz: cd8871c7ef91a102a45bcebbcdc8bbf385db3a83ffb34948cd0d3e49c14d8237
5
5
  SHA512:
6
- metadata.gz: 40df8027fb5974b0670a80b4c61e918da1f3dd7cec833e5fab48cdb606ddb9d9e56480cb69ad3c3033c2fdf1deb09e0c105cb15730cb92581e1d57c3db57ce3f
7
- data.tar.gz: af0d331538e600ad27d23f9377d6cdabcc1f3142ad7d07de517b002334c0414896edb45d6d0d5d6ab50a6af81be12f4b2bf667c15e8bc50e211a824b6701d1b2
6
+ metadata.gz: 4d21cfbf61b54fb704990a23cbbf73285364e37ab722246c0202ecea37616398edcfe3fc9f841bc3cae9100108cb61e37200780c0e0ef667515f6e27319498e4
7
+ data.tar.gz: bed9e2b68bf2a5697a72a6b3bcb2dfcdb6898b93e31b6bc68b169583462d9e4a4f3e345dec8470077b2566dd31860e8ca97ce83cd82b8a01aff889ad0d5a7d14
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- perron (0.16.0)
4
+ perron (0.17.0)
5
5
  csv
6
6
  json
7
7
  psych
@@ -1,25 +1,21 @@
1
1
  Perron.configure do |config|
2
- # config.output = "output"
2
+ # View all options: https://perron.railsdesigner.com/docs/configuration/
3
3
 
4
4
  # config.site_name = "Chirp Form"
5
5
 
6
- # The build mode for Perron. Can be :standalone or :integrated
6
+ # The build mode for Perron. Can be :standalone (SSG) or :integrated (alongside your Rails app)
7
7
  # config.mode = :standalone
8
8
 
9
- # In `integrated` mode, the root is skipped by default. Set to `true` to enable
10
- # config.include_root = false
11
-
12
- # config.default_url_options = {host: "helptail.com", protocol: "https", trailing_slash: true}
9
+ # config.default_url_options = {host: "chirpform.com", protocol: "https", trailing_slash: true}
13
10
 
14
11
  # The options hash is passed directly to the chosen library
15
12
  # config.markdown_options = {}
16
13
 
17
- # Set default meta values
18
- # Examples:
19
- # - `config.metadata.description = "Add forms to any static site. Display responses anywhere."`
20
- # - `config.metadata.author = "Chirp Form Team"`
21
-
22
14
  # Set meta title suffix
23
15
  # config.metadata.title_suffix = nil
24
16
  # config.metadata.title_separator = " — "
17
+
18
+ # Set default meta values
19
+ # config.metadata.description = "Add forms to any static site. Display responses anywhere."
20
+ # config.metadata.author = "Chirp Form Team"
25
21
  end
@@ -17,6 +17,27 @@ Examples:
17
17
 
18
18
  And adds: resources :posts, module: :content, only: %w[index show]
19
19
 
20
+ Generate pages scaffold with root:
21
+ rails generate content Page
22
+
23
+ This will additionally create:
24
+ app/content/pages/root.erb
25
+ def root action in Content::PagesController
26
+ root route in config/routes.rb (if not already defined)
27
+
28
+ Skip root generation for pages:
29
+ rails generate content Page --no-include-root
30
+
31
+ Include root for non-pages content:
32
+ rails generate content Article --include-root
33
+
34
+ Generate content scaffold with data sources:
35
+ rails generate content Product --data countries products
36
+ rails generate content Product --data countries.json products.yml
37
+
38
+ This will additionally create data source files in app/content/products/
39
+ and add sources/template_source class methods to the model.
40
+
20
41
  Create new content file from template:
21
42
  rails generate content Post --new
22
43
  rails generate content Post --new "My First Post"
@@ -38,4 +59,6 @@ Arguments:
38
59
 
39
60
  Options:
40
61
  --new [TITLE]: Create new content file instead of scaffold
62
+ --data [source1(.ext) source2(.ext)]: Create data source files (default extension: .yml)
41
63
  --force-plural: Use plural form for model name and class
64
+ --[no-]include-root: Include root action and route (default: true for pages, false otherwise)
@@ -8,8 +8,11 @@ module Rails
8
8
  source_root File.expand_path("templates", __dir__)
9
9
 
10
10
  class_option :force_plural, type: :boolean, default: false, desc: "Forces the use of a plural model name and class"
11
+ class_option :include_root, type: :boolean, default: nil, desc: "Include root action and route (defaults to true for pages)"
11
12
  class_option :new, type: :string, default: nil, banner: "TITLE",
12
13
  desc: "Create a new content file from template instead of generating scaffold"
14
+ class_option :data, type: :array, default: [], banner: "source1(.ext) source2(.ext)",
15
+ desc: "Specify data sources with optional extensions (defaults to .yml)"
13
16
 
14
17
  argument :actions, type: :array, default: %w[index show], banner: "actions", desc: "Specify which actions to generate (index/show)"
15
18
 
@@ -60,25 +63,52 @@ module Rails
60
63
  FileUtils.mkdir_p(content_directory)
61
64
  end
62
65
 
63
- def create_pages_root
66
+ def add_content_route
64
67
  return if @content_mode
65
- return unless pages_controller?
66
68
 
67
- template "root.erb.tt", File.join(content_directory, "root.erb")
69
+ route "resources :#{plural_file_name}, module: :content, only: %w[#{actions.join(" ")}]"
68
70
  end
69
71
 
70
- def add_content_route
72
+ def create_data_sources
71
73
  return if @content_mode
74
+ return if options[:data].empty?
72
75
 
73
- route "resources :#{plural_file_name}, module: :content, only: %w[#{actions.join(" ")}]"
76
+ options[:data].each do |source|
77
+ name, extension = source.split(".", 2)
78
+
79
+ create_file File.join(content_directory, "#{name}.#{extension || "yml"}"), ""
80
+ end
81
+ end
82
+
83
+ def add_root_action
84
+ return if @content_mode
85
+ return unless should_include_root?
86
+
87
+ inject_into_class "app/controllers/content/#{plural_file_name}_controller.rb", "Content::#{plural_class_name}Controller" do
88
+ <<-RUBY
89
+
90
+ def root
91
+ @resource = Content::#{class_name}.root
92
+
93
+ render :show
94
+ end
95
+ RUBY
96
+ end
97
+ end
98
+
99
+ def create_root_content_file
100
+ return if @content_mode
101
+ return unless should_include_root?
102
+
103
+ template "root.erb.tt", File.join(content_directory, "root.erb")
74
104
  end
75
105
 
76
106
  def add_root_route
77
107
  return if @content_mode
78
- return unless pages_controller?
108
+ return unless should_include_root?
79
109
  return if root_route_exists?
80
110
 
81
- inject_into_file "config/routes.rb", " root to: \"content/pages#root\"\n", before: /^\s*end\s*$/
111
+ route "root to: \"content/#{plural_file_name}#root\""
82
112
  end
83
113
 
84
114
  private
@@ -99,14 +129,6 @@ module Rails
99
129
 
100
130
  def pages_controller? = plural_file_name == "pages"
101
131
 
102
- def root_route_exists?
103
- routes = File.join(destination_root, "config", "routes.rb")
104
-
105
- return false unless File.exist?(routes)
106
-
107
- File.read(routes).match?(/\broot\s+to:/)
108
- end
109
-
110
132
  def template_file
111
133
  @template_file ||= Dir.glob(File.join(content_directory, "{YYYY-MM-DD-,}template.*.tt")).first
112
134
  end
@@ -121,6 +143,23 @@ module Rails
121
143
  end
122
144
  end
123
145
  end
146
+
147
+ def should_include_root?
148
+ options[:include_root].nil? ? pages_controller? : options[:include_root]
149
+ end
150
+
151
+ def root_route_exists?
152
+ routes = File.join(destination_root, "config", "routes.rb")
153
+ return false unless File.exist?(routes)
154
+
155
+ File.read(routes).match?(/\broot\s+to:/)
156
+ end
157
+
158
+ def data_sources
159
+ options[:data].map { it.split(".").first }
160
+ end
161
+
162
+ def data_sources? = !options[:data].empty?
124
163
  end
125
164
  end
126
165
  end
@@ -1,2 +1,13 @@
1
1
  class Content::<%= class_name %> < Perron::Resource
2
+ <% if data_sources? -%>
3
+
4
+ sources <%= data_sources.map { ":#{it}" }.join(", ") %>
5
+
6
+ def self.source_template(sources)
7
+ <<~TEMPLATE
8
+ ---
9
+ ---
10
+ TEMPLATE
11
+ end
12
+ <% end -%>
2
13
  end
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  ---
3
3
 
4
- Find me in `app/content/pages/root.erb`
4
+ Find me in `app/content/<%= plural_file_name %>/root.erb`
@@ -3,5 +3,5 @@
3
3
  <%%= @resource.filename %>
4
4
  </h1>
5
5
 
6
- <%%= markdownify @resource.content %>
6
+ <%%= @resource.content %>
7
7
  </article>
@@ -19,7 +19,6 @@ module Perron
19
19
  @config.output = "output"
20
20
 
21
21
  @config.mode = :standalone
22
- @config.include_root = false
23
22
 
24
23
  @config.allowed_extensions = %w[erb md]
25
24
 
@@ -53,7 +52,11 @@ module Perron
53
52
 
54
53
  def mode = @config.mode.to_s.inquiry
55
54
 
56
- def exclude_root? = !@config.include_root
55
+ def additional_routes
56
+ @additional_routes || (mode.integrated? ? [] : %w[root_path])
57
+ end
58
+
59
+ attr_writer :additional_routes
57
60
 
58
61
  def url
59
62
  options = Perron.configuration.default_url_options
data/lib/perron/data.rb CHANGED
@@ -4,6 +4,8 @@ require "csv"
4
4
 
5
5
  module Perron
6
6
  class Data < SimpleDelegator
7
+ include Enumerable
8
+
7
9
  def initialize(identifier)
8
10
  @identifier = identifier
9
11
  @file_path = self.class.path_for!(identifier)
@@ -12,6 +14,21 @@ module Perron
12
14
  super(records)
13
15
  end
14
16
 
17
+ def each(&block) = @records.each(&block)
18
+
19
+ def count = @records.count
20
+
21
+ def first(n = nil)
22
+ n ? @records.first(n) : @records.first
23
+ end
24
+
25
+ def last = @records.last
26
+
27
+ def [](index) = @records[index]
28
+
29
+ def size = @records.size
30
+ alias_method :length, :size
31
+
15
32
  class << self
16
33
  def all
17
34
  parts = name.to_s.split("::").drop(2)
@@ -24,6 +41,24 @@ module Perron
24
41
  all.find { it[:id] == id || it["id"] == id }
25
42
  end
26
43
 
44
+ def count = all.size
45
+
46
+ def first = all.first
47
+
48
+ def second = all[1]
49
+
50
+ def third = all[2]
51
+
52
+ def fourth = all[3]
53
+
54
+ def fifth = all[4]
55
+
56
+ def forty_two = all[41]
57
+
58
+ def last = all.last
59
+
60
+ def take(n) = all.first(n)
61
+
27
62
  def path_for(identifier)
28
63
  path = Pathname.new(identifier)
29
64
 
@@ -12,7 +12,9 @@ module Perron
12
12
 
13
13
  def count = all.size
14
14
 
15
- def first = all[0]
15
+ def first(n = nil)
16
+ n ? all.first(n) : all[0]
17
+ end
16
18
 
17
19
  def second = all[1]
18
20
 
@@ -30,9 +32,7 @@ module Perron
30
32
 
31
33
  def collection = Collection.new(collection_name)
32
34
 
33
- def root
34
- collection_name.pages? && collection.find_by_file_name("root", name.constantize)
35
- end
35
+ def root = all.find(&:root?)
36
36
 
37
37
  def model_name
38
38
  @model_name ||= ActiveModel::Name.new(self, nil, name.demodulize.to_s)
@@ -47,7 +47,7 @@ module Perron
47
47
 
48
48
  def canonical_url
49
49
  return @frontmatter[:canonical_url] if @frontmatter[:canonical_url]
50
- return Rails.application.routes.url_helpers.root_url(**Perron.configuration.default_url_options) if @resource.slug == "/"
50
+ return Rails.application.routes.url_helpers.root_url(**Perron.configuration.default_url_options) if @resource.root?
51
51
 
52
52
  begin
53
53
  Rails.application.routes.url_helpers.polymorphic_url(
@@ -82,6 +82,10 @@ module Perron
82
82
  def related_resources(limit: 5) = Perron::Site::Resource::Related.new(self).find(limit:)
83
83
  alias_method :related, :related_resources
84
84
 
85
+ def root?
86
+ slug == "/"
87
+ end
88
+
85
89
  private
86
90
 
87
91
  def frontmatter
@@ -103,9 +107,5 @@ module Perron
103
107
  def erb_processing?
104
108
  @file_path.ends_with?(".erb") || metadata.erb == true
105
109
  end
106
-
107
- def root?
108
- collection.name.inquiry.pages? && File.basename(filename) == "root"
109
- end
110
110
  end
111
111
  end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Perron
4
+ module Site
5
+ class Builder
6
+ class AdditionalRoutes
7
+ def initialize(paths)
8
+ @paths = paths
9
+ end
10
+
11
+ def get
12
+ Perron.configuration.additional_routes.each do |route_name|
13
+ @paths << routes.public_send(route_name) if routes.respond_to?(route_name)
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ def routes = Rails.application.routes.url_helpers
20
+ end
21
+ end
22
+ end
23
+ end
@@ -8,7 +8,8 @@ module Perron
8
8
  private
9
9
 
10
10
  def author(resource)
11
- author = resource.author || feed_configuration.author
11
+ author = (resource&.respond_to?(:author) && resource.author) ||
12
+ feed_configuration.author
12
13
 
13
14
  Author.new(author) if author
14
15
  end
@@ -13,11 +13,9 @@ module Perron
13
13
 
14
14
  if routes.respond_to?(show_path)
15
15
  @collection.send(:load_resources).select(&:buildable?).each do |resource|
16
- root = resource.slug == "/"
16
+ next if resource.root?
17
17
 
18
- next if skip? root
19
-
20
- @paths << (root ? routes.root_path : routes.public_send(show_path, resource))
18
+ @paths << routes.public_send(show_path, resource)
21
19
 
22
20
  (resource.class.try(:nested_routes) || []).each do |nested|
23
21
  @paths << routes.polymorphic_path([resource, nested])
@@ -28,11 +26,6 @@ module Perron
28
26
 
29
27
  private
30
28
 
31
- def skip?(root)
32
- root &&
33
- Perron.configuration.mode.integrated? && Perron.configuration.exclude_root?
34
- end
35
-
36
29
  def routes = Rails.application.routes.url_helpers
37
30
 
38
31
  def index_path = "#{@collection.name}_path"
@@ -13,6 +13,8 @@ module Perron
13
13
 
14
14
  xml = Nokogiri::XML::Builder.new(encoding: "UTF-8") do |builder|
15
15
  builder.urlset(xmlns: "http://www.sitemaps.org/schemas/sitemap/0.9") do
16
+ add_additional_routes(with: builder)
17
+
16
18
  Perron::Site.collections.each do |collection|
17
19
  add_urls_for(collection, with: builder)
18
20
  end
@@ -24,6 +26,21 @@ module Perron
24
26
 
25
27
  private
26
28
 
29
+ def add_additional_routes(with:)
30
+ (Perron.configuration.additional_routes || []).each do |route_name|
31
+ next unless routes.respond_to?(route_name)
32
+
33
+ routes.with_options(Perron.configuration.default_url_options) do |url|
34
+ with.url do
35
+ with.loc url.public_send(route_name.to_s.gsub("_path", "_url"))
36
+ with.priority Perron.configuration.sitemap.priority
37
+ with.changefreq Perron.configuration.sitemap.change_frequency
38
+ with.lastmod Time.current.iso8601
39
+ end
40
+ end
41
+ end
42
+ end
43
+
27
44
  def add_urls_for(collection, with:)
28
45
  return if collection.configuration.blank?
29
46
  return if collection.configuration.sitemap.exclude == true
@@ -31,13 +48,12 @@ module Perron
31
48
  collection.resources.each do |resource|
32
49
  next if resource.metadata.sitemap == false
33
50
 
34
- root = resource.slug == "/"
35
51
  priority = resource.metadata.sitemap_priority || collection.configuration.sitemap.priority || Perron.configuration.sitemap.priority
36
52
  change_frequency = resource.metadata.sitemap_change_frequency || collection.configuration.sitemap.change_frequency || Perron.configuration.sitemap.change_frequency
37
53
 
38
54
  Rails.application.routes.url_helpers.with_options(Perron.configuration.default_url_options) do |url|
39
55
  with.url do
40
- with.loc root ? url.root_url : url.polymorphic_url(resource)
56
+ with.loc resource.root? ? url.root_url : url.polymorphic_url(resource)
41
57
  with.priority priority
42
58
  with.changefreq change_frequency
43
59
  begin
@@ -49,6 +65,8 @@ module Perron
49
65
  end
50
66
  end
51
67
  end
68
+
69
+ def routes = Rails.application.routes.url_helpers
52
70
  end
53
71
  end
54
72
  end
@@ -5,6 +5,7 @@ require "perron/site/builder/sitemap"
5
5
  require "perron/site/builder/feeds"
6
6
  require "perron/site/builder/public_files"
7
7
  require "perron/site/builder/paths"
8
+ require "perron/site/builder/additional_routes"
8
9
  require "perron/site/builder/page"
9
10
 
10
11
  module Perron
@@ -40,6 +41,7 @@ module Perron
40
41
 
41
42
  def paths
42
43
  Set.new.tap do |paths|
44
+ Perron::Site::Builder::AdditionalRoutes.new(paths).get
43
45
  Perron::Site.collections.each { Perron::Site::Builder::Paths.new(it, paths).get }
44
46
  end
45
47
  end
@@ -1,3 +1,3 @@
1
1
  module Perron
2
- VERSION = "0.16.0"
2
+ VERSION = "0.17.0"
3
3
  end
data/lib/perron.rb CHANGED
@@ -4,7 +4,6 @@ require "perron/version"
4
4
  require "perron/configuration"
5
5
  require "perron/deprecator"
6
6
  require "perron/errors"
7
- require "perron/root"
8
7
  require "perron/site"
9
8
  require "perron/content/data"
10
9
  require "perron/resource"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: perron
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.16.0
4
+ version: 0.17.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rails Designer Developers
@@ -131,9 +131,9 @@ files:
131
131
  - lib/perron/resource/slug.rb
132
132
  - lib/perron/resource/sourceable.rb
133
133
  - lib/perron/resource/table_of_content.rb
134
- - lib/perron/root.rb
135
134
  - lib/perron/site.rb
136
135
  - lib/perron/site/builder.rb
136
+ - lib/perron/site/builder/additional_routes.rb
137
137
  - lib/perron/site/builder/assets.rb
138
138
  - lib/perron/site/builder/feeds.rb
139
139
  - lib/perron/site/builder/feeds/author.rb
@@ -170,7 +170,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
170
170
  - !ruby/object:Gem::Version
171
171
  version: '0'
172
172
  requirements: []
173
- rubygems_version: 4.0.3
173
+ rubygems_version: 4.0.4
174
174
  specification_version: 4
175
175
  summary: Rails-based static site generator
176
176
  test_files: []
data/lib/perron/root.rb DELETED
@@ -1,13 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Perron
4
- module Root
5
- include ActiveSupport::Concern
6
-
7
- def root
8
- @resource = Content::Page.root
9
-
10
- render :show
11
- end
12
- end
13
- end