bridgetown-core 0.19.1 → 0.21.0.beta2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bridgetown-core.gemspec +3 -4
- data/lib/bridgetown-core.rb +32 -16
- data/lib/bridgetown-core/cleaner.rb +7 -1
- data/lib/bridgetown-core/collection.rb +176 -77
- data/lib/bridgetown-core/commands/apply.rb +4 -3
- data/lib/bridgetown-core/commands/base.rb +9 -0
- data/lib/bridgetown-core/commands/build.rb +0 -11
- data/lib/bridgetown-core/commands/configure.rb +66 -0
- data/lib/bridgetown-core/commands/console.rb +4 -0
- data/lib/bridgetown-core/commands/doctor.rb +1 -19
- data/lib/bridgetown-core/commands/new.rb +8 -0
- data/lib/bridgetown-core/commands/plugins.rb +1 -0
- data/lib/bridgetown-core/commands/serve.rb +0 -14
- data/lib/bridgetown-core/component.rb +178 -0
- data/lib/bridgetown-core/concerns/data_accessible.rb +1 -0
- data/lib/bridgetown-core/concerns/front_matter_importer.rb +52 -0
- data/lib/bridgetown-core/concerns/site/configurable.rb +7 -3
- data/lib/bridgetown-core/concerns/site/content.rb +56 -15
- data/lib/bridgetown-core/concerns/site/processable.rb +1 -0
- data/lib/bridgetown-core/concerns/site/renderable.rb +26 -0
- data/lib/bridgetown-core/concerns/site/writable.rb +12 -2
- data/lib/bridgetown-core/concerns/validatable.rb +1 -4
- data/lib/bridgetown-core/configuration.rb +50 -29
- data/lib/bridgetown-core/configurations/.keep +0 -0
- data/lib/bridgetown-core/configurations/bt-postcss.rb +26 -0
- data/lib/bridgetown-core/configurations/bt-postcss/postcss.config.js +21 -0
- data/lib/bridgetown-core/configurations/minitesting.rb +95 -0
- data/lib/bridgetown-core/configurations/netlify.rb +6 -0
- data/lib/bridgetown-core/configurations/netlify/netlify.sh +14 -0
- data/lib/bridgetown-core/configurations/netlify/netlify.toml +44 -0
- data/lib/bridgetown-core/configurations/purgecss.rb +49 -0
- data/lib/bridgetown-core/configurations/stimulus.rb +49 -0
- data/lib/bridgetown-core/configurations/swup.rb +37 -0
- data/lib/bridgetown-core/configurations/tailwindcss.rb +29 -0
- data/lib/bridgetown-core/configurations/tailwindcss/css_imports.css +4 -0
- data/lib/bridgetown-core/configurations/tailwindcss/postcss.config.js +12 -0
- data/lib/bridgetown-core/configurations/turbo.rb +16 -0
- data/lib/bridgetown-core/converter.rb +23 -0
- data/lib/bridgetown-core/converters/erb_templates.rb +50 -41
- data/lib/bridgetown-core/converters/identity.rb +0 -9
- data/lib/bridgetown-core/converters/markdown.rb +14 -4
- data/lib/bridgetown-core/converters/markdown/kramdown_parser.rb +5 -38
- data/lib/bridgetown-core/converters/ruby_templates.rb +17 -0
- data/lib/bridgetown-core/converters/smartypants.rb +3 -1
- data/lib/bridgetown-core/current.rb +10 -0
- data/lib/bridgetown-core/document.rb +7 -14
- data/lib/bridgetown-core/drops/collection_drop.rb +1 -1
- data/lib/bridgetown-core/drops/page_drop.rb +4 -0
- data/lib/bridgetown-core/drops/relations_drop.rb +23 -0
- data/lib/bridgetown-core/drops/resource_drop.rb +83 -0
- data/lib/bridgetown-core/drops/site_drop.rb +33 -8
- data/lib/bridgetown-core/drops/unified_payload_drop.rb +5 -0
- data/lib/bridgetown-core/entry_filter.rb +12 -25
- data/lib/bridgetown-core/errors.rb +0 -2
- data/lib/bridgetown-core/filters.rb +4 -27
- data/lib/bridgetown-core/filters/from_liquid.rb +23 -0
- data/lib/bridgetown-core/filters/url_filters.rb +12 -0
- data/lib/bridgetown-core/generator.rb +2 -1
- data/lib/bridgetown-core/generators/prototype_generator.rb +37 -19
- data/lib/bridgetown-core/helpers.rb +48 -9
- data/lib/bridgetown-core/layout.rb +28 -13
- data/lib/bridgetown-core/liquid_renderer/file.rb +1 -0
- data/lib/bridgetown-core/liquid_renderer/table.rb +1 -0
- data/lib/bridgetown-core/model/base.rb +138 -0
- data/lib/bridgetown-core/model/builder_origin.rb +40 -0
- data/lib/bridgetown-core/model/origin.rb +38 -0
- data/lib/bridgetown-core/model/repo_origin.rb +126 -0
- data/lib/bridgetown-core/page.rb +11 -2
- data/lib/bridgetown-core/plugin.rb +2 -26
- data/lib/bridgetown-core/plugin_manager.rb +1 -3
- data/lib/bridgetown-core/publisher.rb +7 -1
- data/lib/bridgetown-core/reader.rb +36 -21
- data/lib/bridgetown-core/readers/data_reader.rb +4 -4
- data/lib/bridgetown-core/readers/page_reader.rb +1 -0
- data/lib/bridgetown-core/readers/post_reader.rb +5 -4
- data/lib/bridgetown-core/regenerator.rb +8 -1
- data/lib/bridgetown-core/related_posts.rb +5 -5
- data/lib/bridgetown-core/renderer.rb +6 -13
- data/lib/bridgetown-core/resource/base.rb +317 -0
- data/lib/bridgetown-core/resource/destination.rb +49 -0
- data/lib/bridgetown-core/resource/permalink_processor.rb +179 -0
- data/lib/bridgetown-core/resource/relations.rb +132 -0
- data/lib/bridgetown-core/resource/taxonomy_term.rb +34 -0
- data/lib/bridgetown-core/resource/taxonomy_type.rb +56 -0
- data/lib/bridgetown-core/resource/transformer.rb +175 -0
- data/lib/bridgetown-core/ruby_template_view.rb +12 -4
- data/lib/bridgetown-core/site.rb +9 -1
- data/lib/bridgetown-core/static_file.rb +33 -10
- data/lib/bridgetown-core/tags/include.rb +1 -1
- data/lib/bridgetown-core/tags/post_url.rb +2 -2
- data/lib/bridgetown-core/url.rb +1 -0
- data/lib/bridgetown-core/utils.rb +49 -43
- data/lib/bridgetown-core/utils/require_gems.rb +60 -0
- data/lib/bridgetown-core/utils/ruby_exec.rb +6 -9
- data/lib/bridgetown-core/utils/ruby_front_matter.rb +39 -0
- data/lib/bridgetown-core/version.rb +2 -2
- data/lib/bridgetown-core/watcher.rb +1 -1
- data/lib/site_template/README.md +70 -0
- data/lib/site_template/package.json.erb +2 -2
- data/lib/site_template/src/_posts/0000-00-00-welcome-to-bridgetown.md.erb +1 -1
- data/lib/site_template/webpack.config.js.erb +26 -6
- metadata +56 -44
- data/lib/bridgetown-core/external.rb +0 -58
- data/lib/bridgetown-core/page_without_a_file.rb +0 -17
- data/lib/bridgetown-core/path_manager.rb +0 -31
- data/lib/bridgetown-core/readers/collection_reader.rb +0 -23
- data/lib/bridgetown-core/readers/static_file_reader.rb +0 -25
- data/lib/bridgetown-core/utils/exec.rb +0 -26
- data/lib/bridgetown-core/utils/internet.rb +0 -37
- data/lib/bridgetown-core/utils/platforms.rb +0 -80
- data/lib/bridgetown-core/utils/thread_event.rb +0 -31
- data/lib/bridgetown-core/utils/win_tz.rb +0 -75
@@ -4,8 +4,13 @@ module Bridgetown
|
|
4
4
|
class RubyTemplateView
|
5
5
|
class Helpers
|
6
6
|
include Bridgetown::Filters
|
7
|
+
include Bridgetown::Filters::FromLiquid
|
7
8
|
|
8
|
-
|
9
|
+
# @return [Bridgetown::RubyTemplateView]
|
10
|
+
attr_reader :view
|
11
|
+
|
12
|
+
# @return [Bridgetown::Site]
|
13
|
+
attr_reader :site
|
9
14
|
|
10
15
|
Context = Struct.new(:registers)
|
11
16
|
|
@@ -28,20 +33,44 @@ module Bridgetown
|
|
28
33
|
pairs.select { |_key, truthy| truthy }.keys.join(" ")
|
29
34
|
end
|
30
35
|
|
36
|
+
# Convert a Markdown string into HTML output.
|
37
|
+
#
|
38
|
+
# @param input [String] the Markdown to convert, if no block is passed
|
39
|
+
# @return [String]
|
40
|
+
def markdownify(input = nil, &block)
|
41
|
+
content = Bridgetown::Utils.reindent_for_markdown(
|
42
|
+
block.nil? ? input.to_s : view.capture(&block)
|
43
|
+
)
|
44
|
+
converter = site.find_converter_instance(Bridgetown::Converters::Markdown)
|
45
|
+
safe(converter.convert(content).strip)
|
46
|
+
end
|
47
|
+
|
31
48
|
# This helper will generate the correct permalink URL for the file path.
|
32
49
|
#
|
33
50
|
# @param relative_path [String, Object] source file path, e.g.
|
34
|
-
# "_posts/2020-10-20-my-post.md", or object that responds to
|
51
|
+
# "_posts/2020-10-20-my-post.md", or object that responds to either
|
52
|
+
# `url` or `relative_url`
|
35
53
|
# @return [String] the permalink URL for the file
|
36
|
-
# @raise [ArgumentError] if the file cannot be found
|
37
54
|
def url_for(relative_path)
|
38
|
-
|
55
|
+
if relative_path.respond_to?(:relative_url)
|
56
|
+
return safe(relative_path.relative_url) # new resource engine
|
57
|
+
elsif relative_path.respond_to?(:url)
|
58
|
+
return safe(relative_url(relative_path.url)) # old legacy engine
|
59
|
+
elsif relative_path.start_with?("/", "http")
|
60
|
+
return safe(relative_path)
|
61
|
+
end
|
39
62
|
|
40
|
-
|
63
|
+
find_relative_url_for_path(relative_path)
|
64
|
+
end
|
65
|
+
alias_method :link, :url_for
|
41
66
|
|
67
|
+
# @param relative_path [String] source file path, e.g.
|
68
|
+
# "_posts/2020-10-20-my-post.md"
|
69
|
+
# @raise [ArgumentError] if the file cannot be found
|
70
|
+
def find_relative_url_for_path(relative_path)
|
42
71
|
site.each_site_file do |item|
|
43
|
-
if item.relative_path ==
|
44
|
-
return relative_url(item)
|
72
|
+
if item.relative_path == relative_path || item.relative_path == "/#{relative_path}"
|
73
|
+
return safe(item.respond_to?(:relative_url) ? item.relative_url : relative_url(item))
|
45
74
|
end
|
46
75
|
end
|
47
76
|
|
@@ -51,7 +80,6 @@ module Bridgetown
|
|
51
80
|
Make sure the document exists and the path is correct.
|
52
81
|
MSG
|
53
82
|
end
|
54
|
-
alias_method :link, :url_for
|
55
83
|
|
56
84
|
# This helper will generate the correct permalink URL for the file path.
|
57
85
|
#
|
@@ -69,7 +97,8 @@ module Bridgetown
|
|
69
97
|
attr = attr.to_s.tr("_", "-")
|
70
98
|
segments << "#{attr}=\"#{Utils.xml_escape(option)}\""
|
71
99
|
end
|
72
|
-
|
100
|
+
# TODO: this might leak an XSS string into text, need to check
|
101
|
+
safe("<#{segments.join(" ")}>#{text}</a>")
|
73
102
|
end
|
74
103
|
|
75
104
|
# Forward all arguments to I18n.t method
|
@@ -79,6 +108,16 @@ module Bridgetown
|
|
79
108
|
def t(*args)
|
80
109
|
I18n.send :t, *args
|
81
110
|
end
|
111
|
+
|
112
|
+
# For template contexts where ActiveSupport's output safety is loaded, we
|
113
|
+
# can ensure a string has been marked safe
|
114
|
+
#
|
115
|
+
# @param input [Object]
|
116
|
+
# @return [String]
|
117
|
+
def safe(input)
|
118
|
+
input.to_s.html_safe
|
119
|
+
end
|
120
|
+
alias_method :raw, :safe
|
82
121
|
end
|
83
122
|
end
|
84
123
|
end
|
@@ -3,8 +3,8 @@
|
|
3
3
|
module Bridgetown
|
4
4
|
class Layout
|
5
5
|
include DataAccessible
|
6
|
+
include FrontMatterImporter
|
6
7
|
include LiquidRenderable
|
7
|
-
include Validatable
|
8
8
|
|
9
9
|
# Gets the Site object.
|
10
10
|
attr_reader :site
|
@@ -21,12 +21,18 @@ module Bridgetown
|
|
21
21
|
# Gets/Sets the extension of this layout.
|
22
22
|
attr_accessor :ext
|
23
23
|
|
24
|
+
alias_method :extname, :ext
|
25
|
+
|
24
26
|
# Gets/Sets the Hash that holds the metadata for this layout.
|
25
27
|
attr_accessor :data
|
26
28
|
|
27
29
|
# Gets/Sets the content of this layout.
|
30
|
+
# @return [String]
|
28
31
|
attr_accessor :content
|
29
32
|
|
33
|
+
# @return [Integer]
|
34
|
+
attr_accessor :front_matter_line_count
|
35
|
+
|
30
36
|
# Gets/Sets the current document (for layout-compatible converters)
|
31
37
|
attr_accessor :current_document
|
32
38
|
|
@@ -52,11 +58,29 @@ module Bridgetown
|
|
52
58
|
@path = site.in_source_dir(base, name)
|
53
59
|
end
|
54
60
|
@relative_path = @path.sub(@base_dir, "")
|
61
|
+
@ext = File.extname(name)
|
62
|
+
|
63
|
+
@data = read_front_matter(@path)&.with_dot_access
|
64
|
+
rescue SyntaxError => e
|
65
|
+
Bridgetown.logger.error "Error:",
|
66
|
+
"Ruby Exception in #{e.message}"
|
67
|
+
rescue StandardError => e
|
68
|
+
handle_read_error(e)
|
69
|
+
ensure
|
70
|
+
@data ||= HashWithDotAccess::Hash.new
|
71
|
+
end
|
55
72
|
|
56
|
-
|
73
|
+
def handle_read_error(error)
|
74
|
+
if error.is_a? Psych::SyntaxError
|
75
|
+
Bridgetown.logger.warn "YAML Exception reading #{@path}: #{error.message}"
|
76
|
+
else
|
77
|
+
Bridgetown.logger.warn "Error reading file #{@path}: #{error.message}"
|
78
|
+
end
|
57
79
|
|
58
|
-
|
59
|
-
|
80
|
+
if site.config["strict_front_matter"] ||
|
81
|
+
error.is_a?(Bridgetown::Errors::FatalException)
|
82
|
+
raise error
|
83
|
+
end
|
60
84
|
end
|
61
85
|
|
62
86
|
# The inspect string for this document.
|
@@ -67,15 +91,6 @@ module Bridgetown
|
|
67
91
|
"#<#{self.class} #{@path}>"
|
68
92
|
end
|
69
93
|
|
70
|
-
# Extract information from the layout filename.
|
71
|
-
#
|
72
|
-
# name - The String filename of the layout file.
|
73
|
-
#
|
74
|
-
# Returns nothing.
|
75
|
-
def process(name)
|
76
|
-
self.ext = File.extname(name)
|
77
|
-
end
|
78
|
-
|
79
94
|
# Provide this Layout's data to a Hash suitable for use by Liquid.
|
80
95
|
#
|
81
96
|
# Returns the Hash representation of this Layout.
|
@@ -0,0 +1,138 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_model"
|
4
|
+
|
5
|
+
module Bridgetown
|
6
|
+
module Model
|
7
|
+
class Base
|
8
|
+
include ActiveModel::Model
|
9
|
+
extend ActiveModel::Callbacks # also extends with DescendantsTracker
|
10
|
+
define_model_callbacks :load, :save, :destroy
|
11
|
+
|
12
|
+
def self.loads_id?(id)
|
13
|
+
name == ActiveSupport::Inflector.classify(
|
14
|
+
URI.parse(id).host.chomp(".collection")
|
15
|
+
)
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.find(id)
|
19
|
+
unless Bridgetown::Current.site
|
20
|
+
raise "A Bridgetown site must be initialized and added to Current"
|
21
|
+
end
|
22
|
+
|
23
|
+
model_klass = klass_for_id(id)
|
24
|
+
model_klass.new(read_data_for_id(id))
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.klass_for_id(id)
|
28
|
+
descendants.find do |klass|
|
29
|
+
klass.loads_id?(id)
|
30
|
+
end || self
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.read_data_for_id(id)
|
34
|
+
origin_for_id(id).read
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.origin_for_id(id)
|
38
|
+
scheme = URI.parse(id).scheme
|
39
|
+
origin_klass = Origin.descendants.find do |klass|
|
40
|
+
klass.handle_scheme?(scheme)
|
41
|
+
end
|
42
|
+
|
43
|
+
raise "No origin could be found for #{id}" unless origin_klass
|
44
|
+
|
45
|
+
origin_klass.new(id)
|
46
|
+
end
|
47
|
+
|
48
|
+
class << self
|
49
|
+
def build(collection_name, path, data)
|
50
|
+
data = Bridgetown::Model::BuilderOrigin.new("builder://#{path}").read do
|
51
|
+
data[:_collection_] = Bridgetown::Current.site.collections[collection_name]
|
52
|
+
data
|
53
|
+
end
|
54
|
+
new(data)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def initialize(attributes = {})
|
59
|
+
run_callbacks :load do
|
60
|
+
super
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def id
|
65
|
+
attributes[:id] || attributes[:_id_]
|
66
|
+
end
|
67
|
+
|
68
|
+
# @return [Bridgetown::Model::Origin]
|
69
|
+
def origin
|
70
|
+
attributes[:_origin_]
|
71
|
+
end
|
72
|
+
|
73
|
+
def persisted?
|
74
|
+
id && origin.exists?
|
75
|
+
end
|
76
|
+
|
77
|
+
# @return [Bridgetown::Resource::Base]
|
78
|
+
def to_resource
|
79
|
+
Bridgetown::Resource::Base.new(model: self)
|
80
|
+
end
|
81
|
+
|
82
|
+
def as_resource_in_collection
|
83
|
+
collection.resources << to_resource.read!
|
84
|
+
collection.resources.last
|
85
|
+
end
|
86
|
+
|
87
|
+
# override if need be
|
88
|
+
# @return [Bridgetown::Site]
|
89
|
+
def site
|
90
|
+
Bridgetown::Current.site
|
91
|
+
end
|
92
|
+
|
93
|
+
# @return [Bridgetown::Collection]
|
94
|
+
def collection
|
95
|
+
attributes[:_collection_]
|
96
|
+
end
|
97
|
+
|
98
|
+
# @return [String]
|
99
|
+
def content
|
100
|
+
attributes[:_content_]
|
101
|
+
end
|
102
|
+
|
103
|
+
def attributes
|
104
|
+
@attributes ||= HashWithDotAccess::Hash.new
|
105
|
+
end
|
106
|
+
|
107
|
+
# Strip out keys like _origin_, _collection_, etc.
|
108
|
+
# @return [HashWithDotAccess::Hash]
|
109
|
+
def data_attributes
|
110
|
+
attributes.reject { |k| k.starts_with?("_") && k.ends_with?("_") }
|
111
|
+
end
|
112
|
+
|
113
|
+
def respond_to_missing?(method_name, include_private = false)
|
114
|
+
attributes.key?(method_name) || method_name.to_s.end_with?("=") || super
|
115
|
+
end
|
116
|
+
|
117
|
+
def method_missing(method_name, *args) # rubocop:disable Style/MethodMissingSuper
|
118
|
+
return attributes[method_name] if attributes.key?(method_name)
|
119
|
+
|
120
|
+
key = method_name.to_s
|
121
|
+
if key.end_with?("=")
|
122
|
+
key.chop!
|
123
|
+
# attribute_will_change!(key)
|
124
|
+
attributes[key] = args.first
|
125
|
+
return attributes[key]
|
126
|
+
end
|
127
|
+
|
128
|
+
Bridgetown.logger.warn "key `#{method_name}' not found in attributes for" \
|
129
|
+
" #{attributes[:id].presence || ("new " + self.class.to_s)}"
|
130
|
+
nil
|
131
|
+
end
|
132
|
+
|
133
|
+
def inspect
|
134
|
+
"#<#{self.class} #{data_attributes.inspect.delete_prefix("{").delete_suffix("}")}>"
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Bridgetown
|
4
|
+
module Model
|
5
|
+
# Abstract Superclass
|
6
|
+
class BuilderOrigin < Origin
|
7
|
+
# @return [Pathname]
|
8
|
+
attr_reader :relative_path
|
9
|
+
|
10
|
+
# Override in subclass
|
11
|
+
def self.handle_scheme?(scheme)
|
12
|
+
scheme == "builder"
|
13
|
+
end
|
14
|
+
|
15
|
+
def initialize(id)
|
16
|
+
self.id = id
|
17
|
+
@relative_path = Pathname.new(id.delete_prefix("builder://"))
|
18
|
+
end
|
19
|
+
|
20
|
+
def read
|
21
|
+
@data = if block_given?
|
22
|
+
yield
|
23
|
+
elsif defined?(SiteBuilder) && SiteBuilder.respond_to?(:data_for_id)
|
24
|
+
SiteBuilder.data_for_id(id)
|
25
|
+
else
|
26
|
+
raise "No builder exists which can read #{id}"
|
27
|
+
end
|
28
|
+
@data[:_id_] = id
|
29
|
+
@data[:_origin_] = self
|
30
|
+
@relative_path = Pathname.new(@data[:_relative_path_]) if @data[:_relative_path_]
|
31
|
+
|
32
|
+
@data
|
33
|
+
end
|
34
|
+
|
35
|
+
def exists?
|
36
|
+
false
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Bridgetown
|
4
|
+
module Model
|
5
|
+
# Abstract Superclass
|
6
|
+
class Origin
|
7
|
+
extend ActiveSupport::DescendantsTracker
|
8
|
+
|
9
|
+
# @return [String]
|
10
|
+
attr_accessor :id
|
11
|
+
|
12
|
+
# Override in subclass
|
13
|
+
def self.handle_scheme?(_scheme)
|
14
|
+
false
|
15
|
+
end
|
16
|
+
|
17
|
+
def initialize(id)
|
18
|
+
self.id = id
|
19
|
+
end
|
20
|
+
|
21
|
+
def read
|
22
|
+
raise "Implement #read in a subclass of Bridgetown::Model::Origin"
|
23
|
+
end
|
24
|
+
|
25
|
+
# @return [Pathname]
|
26
|
+
def relative_path
|
27
|
+
raise "Implement #relative_path in a subclass of Bridgetown::Model::Origin"
|
28
|
+
end
|
29
|
+
|
30
|
+
def exists?
|
31
|
+
raise "Implement #exists? in a subclass of Bridgetown::Model::Origin"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
require "bridgetown-core/model/builder_origin"
|
38
|
+
require "bridgetown-core/model/repo_origin"
|
@@ -0,0 +1,126 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Bridgetown
|
4
|
+
module Model
|
5
|
+
class RepoOrigin < Origin
|
6
|
+
include Bridgetown::FrontMatterImporter
|
7
|
+
include Bridgetown::Utils::RubyFrontMatterDSL
|
8
|
+
|
9
|
+
YAML_FRONT_MATTER_REGEXP = %r!\A(---\s*\n.*?\n?)^((---|\.\.\.)\s*$\n?)!m.freeze
|
10
|
+
RUBY_FRONT_MATTER_HEADER = %r!\A[~`#\-]{3,}(?:ruby|<%|{%)\s*\n!.freeze
|
11
|
+
RUBY_FRONT_MATTER_REGEXP =
|
12
|
+
%r!#{RUBY_FRONT_MATTER_HEADER.source}(.*?\n?)^((?:%>|%})?[~`#\-]{3,}\s*$\n?)!m.freeze
|
13
|
+
|
14
|
+
# @return [String]
|
15
|
+
attr_accessor :content
|
16
|
+
|
17
|
+
# @return [Integer]
|
18
|
+
attr_accessor :front_matter_line_count
|
19
|
+
|
20
|
+
class << self
|
21
|
+
def handle_scheme?(scheme)
|
22
|
+
scheme == "repo"
|
23
|
+
end
|
24
|
+
|
25
|
+
def data_file_extensions
|
26
|
+
%w(.yaml .yml .json .csv .tsv .rb).freeze
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def read
|
31
|
+
begin
|
32
|
+
@data = (in_data_collection? ? read_file_data : read_front_matter(original_path)) || {}
|
33
|
+
rescue SyntaxError => e
|
34
|
+
Bridgetown.logger.error "Error:",
|
35
|
+
"Ruby Exception in #{e.message}"
|
36
|
+
rescue StandardError => e
|
37
|
+
handle_read_error(e)
|
38
|
+
end
|
39
|
+
|
40
|
+
@data ||= {}
|
41
|
+
@data[:_id_] = id
|
42
|
+
@data[:_origin_] = self
|
43
|
+
@data[:_collection_] = collection
|
44
|
+
@data[:_content_] = content if content
|
45
|
+
|
46
|
+
@data
|
47
|
+
end
|
48
|
+
|
49
|
+
def url
|
50
|
+
@url = URI.parse(id)
|
51
|
+
end
|
52
|
+
|
53
|
+
def relative_path
|
54
|
+
@relative_path ||= Pathname.new(
|
55
|
+
Addressable::URI.unescape(url.path.delete_prefix("/"))
|
56
|
+
)
|
57
|
+
end
|
58
|
+
|
59
|
+
def collection
|
60
|
+
return @collection if @collection
|
61
|
+
|
62
|
+
collection_name = if url.host.ends_with?(".collection")
|
63
|
+
url.host.chomp(".collection")
|
64
|
+
else
|
65
|
+
"pages"
|
66
|
+
end
|
67
|
+
@collection = Bridgetown::Current.site.collections[collection_name]
|
68
|
+
end
|
69
|
+
|
70
|
+
def original_path
|
71
|
+
@original_path ||= relative_path.expand_path(Bridgetown::Current.site.source)
|
72
|
+
end
|
73
|
+
|
74
|
+
def exists?
|
75
|
+
File.exist?(original_path)
|
76
|
+
end
|
77
|
+
|
78
|
+
private
|
79
|
+
|
80
|
+
def in_data_collection?
|
81
|
+
original_path.extname.downcase.in?(self.class.data_file_extensions) &&
|
82
|
+
collection.data?
|
83
|
+
end
|
84
|
+
|
85
|
+
def read_file_data # rubocop:todo Metrics/MethodLength
|
86
|
+
case original_path.extname.downcase
|
87
|
+
when ".csv"
|
88
|
+
{
|
89
|
+
rows:
|
90
|
+
CSV.read(original_path,
|
91
|
+
headers: true,
|
92
|
+
encoding: Bridgetown::Current.site.config["encoding"]).map(&:to_hash),
|
93
|
+
}
|
94
|
+
when ".tsv"
|
95
|
+
{
|
96
|
+
rows:
|
97
|
+
CSV.read(original_path,
|
98
|
+
col_sep: "\t",
|
99
|
+
headers: true,
|
100
|
+
encoding: Bridgetown::Current.site.config["encoding"]).map(&:to_hash),
|
101
|
+
}
|
102
|
+
when ".rb"
|
103
|
+
process_ruby_data(File.read(original_path), original_path, 1)
|
104
|
+
else
|
105
|
+
yaml_data = SafeYAML.load_file(original_path)
|
106
|
+
yaml_data.is_a?(Array) ? { rows: yaml_data } : yaml_data
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
def handle_read_error(error)
|
111
|
+
if error.is_a? Psych::SyntaxError
|
112
|
+
Bridgetown.logger.error "Error:",
|
113
|
+
"YAML Exception reading #{original_path}: #{error.message}"
|
114
|
+
else
|
115
|
+
Bridgetown.logger.error "Error:",
|
116
|
+
"could not read file #{original_path}: #{error.message}"
|
117
|
+
end
|
118
|
+
|
119
|
+
if Bridgetown::Current.site.config["strict_front_matter"] ||
|
120
|
+
error.is_a?(Bridgetown::Errors::FatalException)
|
121
|
+
raise error
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|