Class: Mill

Inherits:
Object
  • Object
show all
Defined in:
lib/mill/resources/feed.rb,
lib/mill.rb,
lib/mill/version.rb,
lib/mill/resource.rb,
lib/mill/navigator.rb,
lib/mill/file_types.rb,
lib/mill/resources/text.rb,
lib/mill/resources/image.rb,
lib/mill/resources/robots.rb,
lib/mill/resources/generic.rb,
lib/mill/resources/sitemap.rb,
lib/mill/resources/redirect.rb

Overview

Defined Under Namespace

Classes: Navigator, Resource

Constant Summary collapse

DefaultResourceClasses =
[
  Resource::Text,
  Resource::Image,
  Resource::Generic,
]
SchemasDir =
Path.new(__FILE__).dirname / 'mill' / 'schemas'
DefaultSchemaTypes =
{
  feed: SchemasDir / 'atom.xsd',
  sitemap: SchemasDir / 'sitemap.xsd',
}
VERSION =
'0.1'
FileTypes =
{
  text: %w{
    text/plain
    text/html
  },
  image: %w{
    image/gif
    image/jpeg
    image/png
    image/tiff
    image/vnd.microsoft.icon
    image/x-icon
  },
  generic: %w{
    text/css
    application/font-sfnt
    application/x-font-opentype
    application/x-font-otf
    application/x-font-truetype
    application/x-font-ttf
    application/javascript
    application/x-javascript
    text/javascript
    application/pdf
  },
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Mill

Returns a new instance of Mill.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/mill.rb', line 64

def initialize(params={})
  @resource_classes = {}
  @resources = []
  @resources_by_uri = {}
  @schema_types = {}
  @schemas = {}
  @shorten_uris = true
  @input_file_type_order = [:generic, :image, :text]
  @link_elem_attrs = %w{
    img/@src
    script/@src
    a/@href
    link/@href
    stylesheet/@href
  }
  params.each { |k, v| send("#{k}=", v) }
end

Instance Attribute Details

#beta_ssh_locationObject

Returns the value of attribute beta_ssh_location.



40
41
42
# File 'lib/mill.rb', line 40

def beta_ssh_location
  @beta_ssh_location
end

#feed_resourceObject

Returns the value of attribute feed_resource.



36
37
38
# File 'lib/mill.rb', line 36

def feed_resource
  @feed_resource
end

#input_dirObject

Returns the value of attribute input_dir.



30
31
32
# File 'lib/mill.rb', line 30

def input_dir
  @input_dir
end

#input_file_type_orderObject

Returns the value of attribute input_file_type_order.



48
49
50
# File 'lib/mill.rb', line 48

def input_file_type_order
  @input_file_type_order
end

Returns the value of attribute link_elem_attrs.



49
50
51
# File 'lib/mill.rb', line 49

def link_elem_attrs
  @link_elem_attrs
end

Returns the value of attribute navigator.



43
44
45
# File 'lib/mill.rb', line 43

def navigator
  @navigator
end

Returns the value of attribute navigator_items.



44
45
46
# File 'lib/mill.rb', line 44

def navigator_items
  @navigator_items
end

#output_dirObject

Returns the value of attribute output_dir.



31
32
33
# File 'lib/mill.rb', line 31

def output_dir
  @output_dir
end

#redirectsObject

Returns the value of attribute redirects.



47
48
49
# File 'lib/mill.rb', line 47

def redirects
  @redirects
end

#resource_classesObject

Returns the value of attribute resource_classes.



45
46
47
# File 'lib/mill.rb', line 45

def resource_classes
  @resource_classes
end

#resourcesObject

Returns the value of attribute resources.



41
42
43
# File 'lib/mill.rb', line 41

def resources
  @resources
end

#robots_resourceObject

Returns the value of attribute robots_resource.



38
39
40
# File 'lib/mill.rb', line 38

def robots_resource
  @robots_resource
end

#schema_typesObject

Returns the value of attribute schema_types.



46
47
48
# File 'lib/mill.rb', line 46

def schema_types
  @schema_types
end

#shorten_urisObject

Returns the value of attribute shorten_uris.



42
43
44
# File 'lib/mill.rb', line 42

def shorten_uris
  @shorten_uris
end

#site_control_dateObject

Returns the value of attribute site_control_date.



35
36
37
# File 'lib/mill.rb', line 35

def site_control_date
  @site_control_date
end

#site_emailObject

Returns the value of attribute site_email.



34
35
36
# File 'lib/mill.rb', line 34

def site_email
  @site_email
end

#site_titleObject

Returns the value of attribute site_title.



32
33
34
# File 'lib/mill.rb', line 32

def site_title
  @site_title
end

#site_uriObject

Returns the value of attribute site_uri.



33
34
35
# File 'lib/mill.rb', line 33

def site_uri
  @site_uri
end

#sitemap_resourceObject

Returns the value of attribute sitemap_resource.



37
38
39
# File 'lib/mill.rb', line 37

def sitemap_resource
  @sitemap_resource
end

#ssh_locationObject

Returns the value of attribute ssh_location.



39
40
41
# File 'lib/mill.rb', line 39

def ssh_location
  @ssh_location
end

Instance Method Details

#add_resource(resource) ⇒ Object



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

def add_resource(resource)
  resource.mill = self
  begin
    # ;;warn "loading #{resource.class.type} resource #{resource.uri} as #{resource.class}"
    resource.load
  rescue => e
    warn "Failed to load resource #{resource.uri} (#{resource.class}): #{e}"
    raise
  end
  @resources << resource
end

#buildObject



201
202
203
204
205
206
207
208
209
210
211
# File 'lib/mill.rb', line 201

def build
  warn "building #{@resources.length} resources..."
  @resources.each do |resource|
    begin
      resource.build
    rescue => e
      warn "Failed to build resource #{resource.uri}: #{e}"
      raise
    end
  end
end

#cleanObject



179
180
181
182
# File 'lib/mill.rb', line 179

def clean
  @output_dir.rmtree if @output_dir.exist?
  @output_dir.mkpath
end

#feed_author_emailObject



171
172
173
# File 'lib/mill.rb', line 171

def feed_author_email
  @site_email
end

#feed_author_nameObject



163
164
165
# File 'lib/mill.rb', line 163

def feed_author_name
  @site_title
end

#feed_author_uriObject



167
168
169
# File 'lib/mill.rb', line 167

def feed_author_uri
  @site_uri
end

#feed_generatorObject



153
154
155
156
157
158
159
160
161
# File 'lib/mill.rb', line 153

def feed_generator
  [
    'Mill',
    {
      uri: Addressable::URI.parse('http://github.com/jslabovitz/mill'),
      version: Mill::VERSION,
    }
  ]
end

#file_type(file) ⇒ Object



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

def file_type(file)
  if file.directory? || file.basename.to_s[0] == '.'
    return :ignore
  else
    MIME::Types.of(file.to_s).each do |mime_type|
      if (type = @file_types[mime_type.content_type])
        return type
      end
    end
  end
  nil
end

#find_resource(uri) ⇒ Object



131
132
133
134
135
136
137
138
139
# File 'lib/mill.rb', line 131

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



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

def home_resource
  find_resource('/') or raise "Can't find home"
end

#loadObject



184
185
186
187
188
189
190
191
# File 'lib/mill.rb', line 184

def load
  warn "loading resources..."
  build_file_types
  build_resource_classes
  build_schemas
  load_files
  load_others
end

#load_othersObject



193
194
195
196
197
198
199
# File 'lib/mill.rb', line 193

def load_others
  make_redirects
  make_feed
  make_sitemap
  make_robots
  make_navigator
end

#public_resourcesObject



175
176
177
# File 'lib/mill.rb', line 175

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

#publish(mode = :final) ⇒ Object



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/mill.rb', line 213

def publish(mode=:final)
  location = case mode
             when :final
               @ssh_location
             when :beta
               @beta_ssh_location
             else
               raise "Unknown publish mode: #{mode.inspect}"
             end
  raise "Must specify SSH location" unless location
  system('rsync',
    # '--dry-run',
    '--archive',
    '--delete-after',
    '--progress',
    # '--verbose',
    @output_dir.to_s + '/',
    location,
  )
end

#schema_for_type(type) ⇒ Object



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

def schema_for_type(type)
  @schemas[type]
end

#serverObject



234
235
236
237
238
239
# File 'lib/mill.rb', line 234

def server
  SimpleServer.run!(
    root: @output_dir,
    multihosting: false,
  )
end

#tag_uriObject



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

def tag_uri
  "tag:#{@site_uri.host.downcase},#{@site_control_date}:"
end

#update_resource(resource) ⇒ Object



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

def update_resource(resource)
  @resources_by_uri[resource.uri] = resource
end