Class: Bridgetown::Model::RepoOrigin
Constant Summary
collapse
- YAML_FRONT_MATTER_REGEXP =
%r!\A(---\s*\n.*?\n?)^((---|\.\.\.)\s*$\n?)!m.freeze
%r!\A[~`#-]{3,}(?:ruby|<%|{%)\s*\n!.freeze
- RUBY_FRONT_MATTER_REGEXP =
%r!#{RUBY_FRONT_MATTER_HEADER.source}(.*?\n?)^((?:%>|%})?[~`#-]{3,}\s*$\n?)!m.freeze
FrontMatterImporter::RUBY_BLOCK, FrontMatterImporter::RUBY_HEADER, FrontMatterImporter::YAML_BLOCK, FrontMatterImporter::YAML_HEADER
Constants inherited
from Origin
Origin::EAGER_LOAD_DESCENDANTS
Instance Attribute Summary collapse
Attributes inherited from Origin
#id, #site
Class Method Summary
collapse
Instance Method Summary
collapse
#front_matter
included, #process_ruby_data, #read_front_matter
Methods inherited from Origin
#initialize, #verify_model?
Instance Attribute Details
#content ⇒ String
15
16
17
|
# File 'lib/bridgetown-core/model/repo_origin.rb', line 15
def content
@content
end
|
#front_matter_line_count ⇒ Integer
18
19
20
|
# File 'lib/bridgetown-core/model/repo_origin.rb', line 18
def front_matter_line_count
@front_matter_line_count
end
|
Class Method Details
.data_file_extensions ⇒ Object
25
26
27
|
# File 'lib/bridgetown-core/model/repo_origin.rb', line 25
def data_file_extensions
%w(.yaml .yml .json .csv .tsv .rb).freeze
end
|
.handle_scheme?(scheme) ⇒ Boolean
21
22
23
|
# File 'lib/bridgetown-core/model/repo_origin.rb', line 21
def handle_scheme?(scheme)
scheme == "repo"
end
|
.new_with_collection_path(collection, relative_path, site: Bridgetown::Current.site) ⇒ Object
Initializes a new repo object using a collection and a relative source path.
You'll need to use this when you want to create and save a model to the source.
35
36
37
38
39
|
# File 'lib/bridgetown-core/model/repo_origin.rb', line 35
def new_with_collection_path(collection, relative_path, site: Bridgetown::Current.site)
collection = collection.label if collection.is_a?(Bridgetown::Collection)
new("repo://#{collection}.collection/#{relative_path}", site: site)
end
|
Instance Method Details
#collection ⇒ Object
88
89
90
91
92
93
94
95
96
97
|
# File 'lib/bridgetown-core/model/repo_origin.rb', line 88
def collection
return @collection if @collection
collection_name = if url.host.ends_with?(".collection")
url.host.chomp(".collection")
else
"pages"
end
@collection = site.collections[collection_name]
end
|
#exists? ⇒ Boolean
103
104
105
|
# File 'lib/bridgetown-core/model/repo_origin.rb', line 103
def exists?
File.exist?(original_path)
end
|
#original_path ⇒ Object
99
100
101
|
# File 'lib/bridgetown-core/model/repo_origin.rb', line 99
def original_path
@original_path ||= relative_path.expand_path(site.source)
end
|
#read ⇒ Object
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
# File 'lib/bridgetown-core/model/repo_origin.rb', line 42
def read
begin
@data = (in_data_collection? ? read_file_data : read_front_matter(original_path)) || {}
rescue SyntaxError => e
Bridgetown.logger.error "Error:",
"Ruby Exception in #{e.message}"
rescue StandardError => e
handle_read_error(e)
end
@data ||= {}
@data[:_id_] = id
@data[:_origin_] = self
@data[:_collection_] = collection
@data[:_content_] = content if content
@data
end
|
#relative_path ⇒ Object
82
83
84
85
86
|
# File 'lib/bridgetown-core/model/repo_origin.rb', line 82
def relative_path
@relative_path ||= Pathname.new(
Addressable::URI.unescape(url.path.delete_prefix("/"))
)
end
|
#url ⇒ Object
78
79
80
|
# File 'lib/bridgetown-core/model/repo_origin.rb', line 78
def url
@url ||= URI.parse(id)
end
|
#write(model) ⇒ Object
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
# File 'lib/bridgetown-core/model/repo_origin.rb', line 61
def write(model)
if File.exist?(original_path) && !Bridgetown::Utils.(original_path)
raise Bridgetown::Errors::InvalidYAMLFrontMatterError,
"Only existing files containing YAML front matter can be overwritten by the model"
end
contents = "#{front_matter_to_yaml(model)}---\n\n#{model.content}"
dir = File.dirname(original_path)
FileUtils.mkdir_p(dir) unless File.directory?(dir)
File.write(original_path, contents)
true
end
|