Class: Nanoc3::DataSources::Filesystem

The filesystem data source is the default data source for a new nanoc site. It stores all data as files on the hard disk.

None of the methods are documented in this file. See Nanoc3::DataSource for documentation on the overridden methods instead.

Items

The filesystem data source stores its items in nested directories. Each directory represents a single item. The root directory is the ‘content’ directory.

Every directory has a content file and a meta file. The content file contains the actual item content, while the meta file contains the item’s metadata, formatted as YAML.

Both content files and meta files are named after its parent directory (i.e. item). For example, a item named ‘foo’ will have a directory named ‘foo’, with e.g. a ‘foo.markdown’ content file and a ‘foo.yaml’ meta file.

Content file extensions are not used for determining the filter that should be run; the meta file defines the list of filters. The meta file extension must always be ‘yaml’, though.

Content files can also have the ‘index’ basename. Similarly, meta files can have the ‘meta’ basename. For example, a parent directory named ‘foo’ can have an ‘index.txt’ content file and a ‘meta.yaml’ meta file. This is to preserve backward compatibility.

Layouts

Layouts are stored as directories in the ‘layouts’ directory. Each layout contains a content file and a meta file. The content file contain the actual layout, and the meta file describes how the item should be handled (contains the filter that should be used).

For backward compatibility, a layout can also be a single file in the ‘layouts’ directory. Such a layout cannot have any metadata; the filter used for this layout is determined from the file extension.

Code Snippets

Code snippets are stored in ’.rb’ files in the ‘lib’ directory. Code snippets can reside in sub-directories.

Attributes

Instance Attributes

vcs [RW] public

VCSes ##########.

Constants Inherited from Nanoc3::Plugin

MAP

Constructor Summary

This class inherits a constructor from Nanoc3::DataSource.

Public Visibility

Public Class Methods Inherited from Nanoc3::DataSource

identifier, identifiers, register

Public Class Methods Inherited from Nanoc3::Plugin

all, named

Public Instance Method Summary

#create_item(content, attributes, identifier)

Creating data ########## Creates a new item with the given content, attributes and identifier.

#create_layout(content, attributes, identifier)

Creates a new layout with the given content, attributes and identifier.

#down
#items

Loading data ##########.

#layouts
#setup
#up

Preparation ##########.

#vcs

VCSes ##########.

Public Instance Methods Inherited from Nanoc3::DataSource

code_snippets, loading, unuse, update, use

Public Instance Methods Included from Nanoc3::DataSources::FilesystemCommon

code_snippets

Public Instance Method Details

create_item

public create_item(content, attributes, identifier)

Creating data ########## Creates a new item with the given content, attributes and identifier.

[View source]


129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/nanoc3/data_sources/filesystem.rb', line 129

def create_item(content, attributes, identifier)
  # Determine base path
  last_component = identifier.split('/')[-1] || 'content'
  base_path = 'content' + identifier + last_component

  # Get filenames
  dir_path         = 'content' + identifier
  meta_filename    = 'content' + identifier + last_component + '.yaml'
  content_filename = 'content' + identifier + last_component + '.html'
                                 
  # Notify
  Nanoc3::NotificationCenter.post(:file_created, meta_filename)
  Nanoc3::NotificationCenter.post(:file_created, content_filename)

  # Create files
  FileUtils.mkdir_p(dir_path)
  File.open(meta_filename,    'w') { |io| io.write(YAML.dump(attributes.stringify_keys)) }
  File.open(content_filename, 'w') { |io| io.write(content) }
end

create_layout

public create_layout(content, attributes, identifier)

Creates a new layout with the given content, attributes and identifier.

[View source]


150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/nanoc3/data_sources/filesystem.rb', line 150

def create_layout(content, attributes, identifier)
  # Determine base path
  last_component = identifier.split('/')[-1]
  base_path = 'layouts' + identifier + last_component

  # Get filenames
  dir_path         = 'layouts' + identifier
  meta_filename    = 'layouts' + identifier + last_component + '.yaml'
  content_filename = 'layouts' + identifier + last_component + '.html'

  # Notify
  Nanoc3::NotificationCenter.post(:file_created, meta_filename)
  Nanoc3::NotificationCenter.post(:file_created, content_filename)

  # Create files
  FileUtils.mkdir_p(dir_path)
  File.open(meta_filename,    'w') { |io| io.write(YAML.dump(attributes.stringify_keys)) }
  File.open(content_filename, 'w') { |io| io.write(content) }
end

down

public down
[View source]


66
67
# File 'lib/nanoc3/data_sources/filesystem.rb', line 66

def down
end

items

public items

Loading data ##########

[View source]


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/nanoc3/data_sources/filesystem.rb', line 79

def items
  meta_filenames('content').map do |meta_filename|
    # Read metadata
    meta = YAML.load_file(meta_filename) || {}

    # Get content
    content_filename = content_filename_for_dir(File.dirname(meta_filename))
    content = File.read(content_filename)

    # Get attributes
    attributes = meta.merge(:file => Nanoc3::Extra::FileProxy.new(content_filename))

    # Get identifier
    identifier = meta_filename.sub(/^content/, '').sub(/[^\/]+\.yaml$/, '')

    # Get modification times
    meta_mtime    = File.stat(meta_filename).mtime
    content_mtime = File.stat(content_filename).mtime
    mtime         = meta_mtime > content_mtime ? meta_mtime : content_mtime

    # Create item object
    Nanoc3::Item.new(content, attributes, identifier, mtime)
  end
end

layouts

public layouts
[View source]


104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/nanoc3/data_sources/filesystem.rb', line 104

def layouts
  meta_filenames('layouts').map do |meta_filename|
    # Get content
    content_filename  = content_filename_for_dir(File.dirname(meta_filename))
    content           = File.read(content_filename)

    # Get attributes
    attributes = YAML.load_file(meta_filename) || {}

    # Get identifier
    identifier = meta_filename.sub(/^layouts\//, '').sub(/\/[^\/]+\.yaml$/, '')

    # Get modification times
    meta_mtime    = File.stat(meta_filename).mtime
    content_mtime = File.stat(content_filename).mtime
    mtime         = meta_mtime > content_mtime ? meta_mtime : content_mtime

    # Create layout object
    Nanoc3::Layout.new(content, attributes, identifier, mtime)
  end
end

setup

public setup
[View source]


69
70
71
72
73
74
75
# File 'lib/nanoc3/data_sources/filesystem.rb', line 69

def setup
  # Create directories
  %w( content layouts lib ).each do |dir|
    FileUtils.mkdir_p(dir)
    vcs.add(dir)
  end
end

up

public up

Preparation ##########

[View source]


63
64
# File 'lib/nanoc3/data_sources/filesystem.rb', line 63

def up
end

vcs

public vcs

VCSes ##########

[View source]


55
56
57
# File 'lib/nanoc3/data_sources/filesystem.rb', line 55

def vcs
  @vcs ||= Nanoc3::Extra::VCSes::Dummy.new
end
Generated on Sunday, August 09 2009 at 01:43:10 PM by YARD 0.2.3.2 (ruby-1.8.7).