Class: Mill::Site

Inherits:
Object
  • Object
show all
Defined in:
lib/mill/site.rb

Constant Summary collapse

DefaultResourceClasses =
ObjectSpace.each_object(Class).select { |c| c < Resource }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input_dir: 'content', output_dir: 'public_html', site_rsync: nil, site_title: nil, site_uri: 'http://localhost', site_email: nil, site_control_date: Date.today.to_s, html_version: :html4_transitional, shorten_uris: true, make_feed: true, make_sitemap: true, make_robots: true, allow_robots: true, htpasswd_file: nil, navigator: nil, google_site_verification: nil, resource_classes: [], redirects: {}) ⇒ Site

Returns a new instance of Site.



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
# File 'lib/mill/site.rb', line 29

def initialize(input_dir: 'content',
               output_dir: 'public_html',
               site_rsync: nil,
               site_title: nil,
               site_uri: 'http://localhost',
               site_email: nil,
               site_control_date: Date.today.to_s,
               html_version: :html4_transitional,
               shorten_uris: true,
               make_feed: true,
               make_sitemap: true,
               make_robots: true,
               allow_robots: true,
               htpasswd_file: nil,
               navigator: nil,
               google_site_verification: nil,
               resource_classes: [],
               redirects: {})

  @input_dir = Path.new(input_dir)
  @output_dir = Path.new(output_dir)
  @site_rsync = site_rsync
  @site_title = site_title
  @site_uri = Addressable::URI.parse(site_uri)
  @site_email = Addressable::URI.parse(site_email) if site_email
  @site_control_date = Date.parse(site_control_date)
  @html_version = html_version
  @shorten_uris = shorten_uris
  @make_feed = make_feed
  @make_sitemap = make_sitemap
  @make_robots = make_robots
  @allow_robots = allow_robots
  @htpasswd_file = htpasswd_file ? Path.new(htpasswd_file) : nil
  @resource_classes = resource_classes
  @navigator = navigator
  @google_site_verification = google_site_verification
  @redirects = redirects

  @resources = []
  @resources_by_uri = {}
  build_file_types
end

Instance Attribute Details

#allow_robotsObject

Returns the value of attribute allow_robots.



20
21
22
# File 'lib/mill/site.rb', line 20

def allow_robots
  @allow_robots
end

#feed_resourceObject

Returns the value of attribute feed_resource.



13
14
15
# File 'lib/mill/site.rb', line 13

def feed_resource
  @feed_resource
end

#html_versionObject

Returns the value of attribute html_version.



12
13
14
# File 'lib/mill/site.rb', line 12

def html_version
  @html_version
end

#htpasswd_fileObject

Returns the value of attribute htpasswd_file.



21
22
23
# File 'lib/mill/site.rb', line 21

def htpasswd_file
  @htpasswd_file
end

#input_dirObject

Returns the value of attribute input_dir.



5
6
7
# File 'lib/mill/site.rb', line 5

def input_dir
  @input_dir
end

#make_feedObject

Returns the value of attribute make_feed.



17
18
19
# File 'lib/mill/site.rb', line 17

def make_feed
  @make_feed
end

#make_robotsObject

Returns the value of attribute make_robots.



19
20
21
# File 'lib/mill/site.rb', line 19

def make_robots
  @make_robots
end

#make_sitemapObject

Returns the value of attribute make_sitemap.



18
19
20
# File 'lib/mill/site.rb', line 18

def make_sitemap
  @make_sitemap
end

Returns the value of attribute navigator.



22
23
24
# File 'lib/mill/site.rb', line 22

def navigator
  @navigator
end

#output_dirObject

Returns the value of attribute output_dir.



6
7
8
# File 'lib/mill/site.rb', line 6

def output_dir
  @output_dir
end

#redirectsObject

Returns the value of attribute redirects.



24
25
26
# File 'lib/mill/site.rb', line 24

def redirects
  @redirects
end

#resource_classesObject

Returns the value of attribute resource_classes.



23
24
25
# File 'lib/mill/site.rb', line 23

def resource_classes
  @resource_classes
end

#resourcesObject

Returns the value of attribute resources.



25
26
27
# File 'lib/mill/site.rb', line 25

def resources
  @resources
end

#robots_resourceObject

Returns the value of attribute robots_resource.



15
16
17
# File 'lib/mill/site.rb', line 15

def robots_resource
  @robots_resource
end

#shorten_urisObject

Returns the value of attribute shorten_uris.



16
17
18
# File 'lib/mill/site.rb', line 16

def shorten_uris
  @shorten_uris
end

#site_control_dateObject

Returns the value of attribute site_control_date.



11
12
13
# File 'lib/mill/site.rb', line 11

def site_control_date
  @site_control_date
end

#site_emailObject

Returns the value of attribute site_email.



10
11
12
# File 'lib/mill/site.rb', line 10

def site_email
  @site_email
end

#site_rsyncObject

Returns the value of attribute site_rsync.



7
8
9
# File 'lib/mill/site.rb', line 7

def site_rsync
  @site_rsync
end

#site_titleObject

Returns the value of attribute site_title.



8
9
10
# File 'lib/mill/site.rb', line 8

def site_title
  @site_title
end

#site_uriObject

Returns the value of attribute site_uri.



9
10
11
# File 'lib/mill/site.rb', line 9

def site_uri
  @site_uri
end

#sitemap_resourceObject

Returns the value of attribute sitemap_resource.



14
15
16
# File 'lib/mill/site.rb', line 14

def sitemap_resource
  @sitemap_resource
end

Instance Method Details

#add_resource(resource) ⇒ Object



81
82
83
84
85
86
# File 'lib/mill/site.rb', line 81

def add_resource(resource)
  resource.site = self
  @resources << resource
  @resources_by_uri[resource.uri] = resource
  # ;;warn "added #{resource} as #{resource.uri}"
end

#buildObject



190
191
192
193
194
# File 'lib/mill/site.rb', line 190

def build
  import_resources
  load_resources
  build_resources
end

#build_file_typesObject



72
73
74
75
76
77
78
79
# File 'lib/mill/site.rb', line 72

def build_file_types
  @file_types = {}
  (DefaultResourceClasses + @resource_classes).each do |resource_class|
    resource_class.const_get(:FileTypes).each do |type|
      @file_types[type] = resource_class
    end
  end
end

#build_resourcesObject



213
214
215
216
217
218
# File 'lib/mill/site.rb', line 213

def build_resources
  on_each_resource do |resource|
    # ;;warn "#{resource.uri}: building"
    resource.build
  end
end

#checkObject



237
238
239
240
# File 'lib/mill/site.rb', line 237

def check
  build
  checker = WebChecker.new(site_uri: @site_uri, site_dir: @output_dir)
end

#cleanObject



229
230
231
232
233
234
235
# File 'lib/mill/site.rb', line 229

def clean
  if @output_dir.exist?
    @output_dir.children.reject { |p| p.basename.to_s == '.git' }.each do |path|
      path.rm_rf
    end
  end
end

#delete_resource(resource) ⇒ Object



88
89
90
91
# File 'lib/mill/site.rb', line 88

def delete_resource(resource)
  @resources.delete(resource)
  @resources_by_uri.delete(resource.uri)
end

#diffObject



255
256
257
258
259
260
# File 'lib/mill/site.rb', line 255

def diff
  @output_dir.chdir do
    system('git',
      'diff')
  end
end

#feed_author_emailObject



134
135
136
# File 'lib/mill/site.rb', line 134

def feed_author_email
  @site_email
end

#feed_author_nameObject



126
127
128
# File 'lib/mill/site.rb', line 126

def feed_author_name
  @site_title
end

#feed_author_uriObject



130
131
132
# File 'lib/mill/site.rb', line 130

def feed_author_uri
  @site_uri
end

#feed_resourcesObject



138
139
140
# File 'lib/mill/site.rb', line 138

def feed_resources
  public_resources.sort_by(&:date)
end

#find_resource(uri) ⇒ Object



93
94
95
96
97
98
99
100
101
# File 'lib/mill/site.rb', line 93

def find_resource(uri)
  uri = Addressable::URI.parse(uri.to_s) unless uri.kind_of?(Addressable::URI)
  resource = @resources_by_uri[uri]
  if resource.nil? && @shorten_uris
    uri.path = uri.path.sub(%r{\.html$}, '')
    resource = @resources_by_uri[uri]
  end
  resource
end

#home_resourceObject



113
114
115
# File 'lib/mill/site.rb', line 113

def home_resource
  find_resource('/')
end

#import_resourcesObject



196
197
198
199
200
201
202
203
204
# File 'lib/mill/site.rb', line 196

def import_resources
  add_files
  add_redirects
  add_google_site_verification if @google_site_verification
  add_feed if @make_feed
  add_sitemap if @make_sitemap
  add_robots if @make_robots
  add_htpasswd if @htpasswd_file
end

#listObject



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/mill/site.rb', line 159

def list

  list_keys = {
    class:        proc { |v| v.to_s.sub(/::Resource::/, '::') },
    input_file:   proc { |v| v.relative_to(@input_dir) },
    output_file:  proc { |v| v.relative_to(@output_dir) },
    public:       proc { |v| v },
    content:      proc { |v| '%dKB' % (v.to_s.length / 1024.0).ceil },
  }

  build
  list = @resources.map do |resource|
    Hash[
      list_keys.map do |k, p|
        v = resource.send(k)
        [
          k,
          v ? p.call(v).to_s : '-'
        ]
      end
    ]
  end
  format = list_keys.keys.map do |key|
    '%%-%ds' % list.map { |item| item[key].length }.max
  end.join(' ')
  puts format % list_keys.keys
  list.each do |item|
    puts format % item.values
  end
end

#load_resourcesObject



206
207
208
209
210
211
# File 'lib/mill/site.rb', line 206

def load_resources
  on_each_resource do |resource|
    # ;;warn "#{resource.uri}: loading"
    resource.load
  end
end

#makeObject



154
155
156
157
# File 'lib/mill/site.rb', line 154

def make
  build
  save
end

#on_each_resource(&block) ⇒ Object



274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/mill/site.rb', line 274

def on_each_resource(&block)
  @resources.each do |resource|
    old_uri = resource.uri.dup
    begin
      yield(resource)
    rescue Error => e
      raise e, "#{resource.input_file || '-'} (#{old_uri}): #{e}"
    end
    if resource.uri != old_uri
      # ;;warn "URI changed: #{old_uri} => #{resource.uri}"
      @resources_by_uri.delete(old_uri)
      @resources_by_uri[resource.uri] = resource
    end
  end
end

#private_resourcesObject



146
147
148
# File 'lib/mill/site.rb', line 146

def private_resources
  @resources.select { |r| r.kind_of?(Resource::Text) && !r.public }
end

#public_resourcesObject



142
143
144
# File 'lib/mill/site.rb', line 142

def public_resources
  @resources.select(&:public)
end

#redirect_resourcesObject



150
151
152
# File 'lib/mill/site.rb', line 150

def redirect_resources
  @resources.select { |r| r.kind_of?(Resource::Redirect) }
end

#resource_for_file(path) ⇒ Object



103
104
105
106
107
108
109
110
111
# File 'lib/mill/site.rb', line 103

def resource_for_file(path)
  find_resource(
    URI.parse(
      '/' + URI.encode(
        path.relative_to(@output_dir).to_s
      )
    )
  )
end

#saveObject



220
221
222
223
224
225
226
227
# File 'lib/mill/site.rb', line 220

def save
  clean
  @output_dir.mkpath
  on_each_resource do |resource|
    # ;;warn "#{resource.uri}: saving"
    resource.save
  end
end

#snapshotObject



242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/mill/site.rb', line 242

def snapshot
  @output_dir.chdir do
    system('git',
      'add',
      '.')
    system('git',
      'commit',
      '-a',
      '-m',
      'Update.')
  end
end

#tag_uriObject



117
118
119
120
121
122
123
124
# File 'lib/mill/site.rb', line 117

def tag_uri
  'tag:%s:' % [
    [
      @site_uri.host.downcase,
      @site_control_date
    ].join(','),
  ]
end

#uploadObject



262
263
264
265
266
267
268
269
270
271
272
# File 'lib/mill/site.rb', line 262

def upload
  raise "site_rsync not defined" unless @site_rsync
  system('rsync',
    '--progress',
    '--verbose',
    '--archive',
    '--exclude=.git',
    '--delete-after',
    @output_dir.to_s,
    @site_rsync)
end