/
filesystem_unified.rb
102 lines (97 loc) · 3.85 KB
/
filesystem_unified.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# encoding: utf-8
module Nanoc::DataSources
# The filesystem_unified data source stores its items and layouts in nested
# directories. Items and layouts are represented by one or two files; if it
# is represented using one file, the metadata can be contained in this file.
#
# The default root directory for items is the `content` directory; for
# layouts, this is the `layouts` directory. This can be overridden
# in the data source configuration:
#
# data_sources:
# - type: filesystem_unified
# content_dir: items
# layouts_dir: layouts
#
# The metadata for items and layouts can be stored in a separate file with
# the same base name but with the `.yaml` extension. If such a file is
# found, metadata is read from that file. Alternatively, the content file
# itself can start with a metadata section: it can be stored at the top of
# the file, between `---` (three dashes) separators. For example:
#
# ---
# title: "Moo!"
# ---
# h1. Hello!
#
# The metadata section can be omitted. If the file does not start with
# three or five dashes, the entire file will be considered as content.
#
# The identifier of items and layouts is determined as follows. A file with
# an `index.*` filename, such as `index.txt`, will have the filesystem path
# with the `index.*` part stripped as a identifier. For example:
#
# foo/bar/index.html → /foo/bar/
#
# In other cases, the identifier is calculated by stripping the extension.
# If the `allow_periods_in_identifiers` attribute in the configuration is
# true, only the last extension will be stripped if the file has multiple
# extensions; if it is false or unset, all extensions will be stripped.
# For example:
#
# (`allow_periods_in_identifiers` set to true)
# foo.entry.html → /foo.entry/
#
# (`allow_periods_in_identifiers` set to false)
# foo.html.erb → /foo/
#
# Note that each item must have an unique identifier. nanoc will display an
# error if two items with the same identifier are found.
#
# Some more examples:
#
# content/index.html → /
# content/foo.html → /foo/
# content/foo/index.html → /foo/
# content/foo/bar.html → /foo/bar/
# content/foo/bar.baz.html → /foo/bar/ OR /foo/bar.baz/
# content/foo/bar/index.html → /foo/bar/
# content/foo.bar/index.html → /foo.bar/
#
# The file extension does not determine the filters to run on items; the
# Rules file is used to specify processing instructors for each item.
#
# It is possible to set an explicit encoding that should be used when reading
# files. In the data source configuration, set `encoding` to an encoding
# understood by Ruby’s `Encoding`. If no encoding is set in the configuration,
# one will be inferred from the environment.
#
# @api private
class FilesystemUnified < Nanoc::DataSource
include Nanoc::DataSources::Filesystem
private
# See {Nanoc::DataSources::Filesystem#filename_for}.
def filename_for(base_filename, ext)
if ext.nil?
nil
elsif ext.empty?
base_filename
else
base_filename + '.' + ext
end
end
# Returns the identifier derived from the given filename, first stripping
# the given directory name off the filename.
def identifier_for_filename(filename)
if config[:identifier_type] == 'full'
return Nanoc::Identifier.new(filename)
end
if filename =~ /(^|\/)index(\.[^\/]+)?$/
regex = @config && @config[:allow_periods_in_identifiers] ? /\/?(index)?(\.[^\/\.]+)?$/ : /\/?index(\.[^\/]+)?$/
else
regex = @config && @config[:allow_periods_in_identifiers] ? /\.[^\/\.]+$/ : /\.[^\/]+$/
end
Nanoc::Identifier.new(filename.sub(regex, ''), type: :legacy)
end
end
end