Skip to content

Commit 3e698b8

Browse files
authored
Use mime types (#166), close #144
* Get resource type by mime or filename * Remove determining of resource type by html markup * Fix extra coma
1 parent c10a4d2 commit 3e698b8

File tree

17 files changed

+82
-210
lines changed

17 files changed

+82
-210
lines changed

lib/config/resource-ext-by-type.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
var types = require('./resource-types');
2+
var defaultExtensions = {};
3+
4+
// should contain same data as ./resource-type-by-ext
5+
defaultExtensions[types.html] = [ '.html', '.htm' ];
6+
defaultExtensions[types.css] = [ '.css' ];
7+
8+
module.exports = defaultExtensions;

lib/config/resource-extensions-by-type.js

Lines changed: 0 additions & 14 deletions
This file was deleted.

lib/config/resource-type-by-ext.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
var types = require('./resource-types');
2+
3+
// should contain same data as ./resource-ext-by-type
4+
module.exports = {
5+
'.html': types.html,
6+
'.htm': types.html,
7+
'.css': types.css
8+
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
var types = require('./resource-types');
2+
3+
module.exports = {
4+
'text/html': types.html,
5+
'text/css': types.css
6+
};

lib/config/resource-types-by-tag.js

Lines changed: 0 additions & 14 deletions
This file was deleted.

lib/config/resource-types.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
var types = {
22
css: 'css',
3-
html: 'html',
4-
other: 'other'
3+
html: 'html'
54
};
65

76
module.exports = types;

lib/filename-generator/by-site-structure.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ var _ = require('lodash');
22
var path = require('path');
33
var utils = require('../utils');
44
var resourceTypes = require('../config/resource-types');
5-
var resourceTypeExtensions = require('../config/resource-extensions-by-type');
5+
var resourceTypeExtensions = require('../config/resource-ext-by-type');
66

77
module.exports = function generateFilename (resource, options) {
88
var resourceUrl = resource.getUrl();
@@ -11,7 +11,7 @@ module.exports = function generateFilename (resource, options) {
1111

1212
// If we have HTML from 'http://example.com/path' => set 'path/index.html' as filepath
1313
if (resource.isHtml()) {
14-
var htmlExtensions = resourceTypeExtensions[resourceTypes.html].possibleExtensions;
14+
var htmlExtensions = resourceTypeExtensions[resourceTypes.html];
1515
var resourceHasHtmlExtension = _.includes(htmlExtensions, extension);
1616
// add index.html only if filepath has ext != html '/path/test.com' => '/path/test.com/index.html'
1717
if (!resourceHasHtmlExtension) {

lib/filename-generator/by-type.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
var _ = require('lodash');
22
var path = require('path');
33
var utils = require('../utils.js');
4-
var typeExtensions = require('../config/resource-extensions-by-type');
4+
var typeExtensions = require('../config/resource-ext-by-type');
55

66
module.exports = function generateFilename (resource, options, occupiedFileNames) {
77
var occupiedNames = getSubDirectoryNames(options).concat(occupiedFileNames);
@@ -33,7 +33,7 @@ function getFilenameForResource (resource, options) {
3333
var extension = utils.getFilenameExtension(filename);
3434

3535
if (!extension && typeExtensions[resourceType]) {
36-
extension = typeExtensions[resourceType].defaultExtension;
36+
extension = typeExtensions[resourceType][0];
3737
filename += extension;
3838
}
3939

lib/request.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,18 @@ var Promise = require('bluebird');
33
var request = require('request');
44
var get = Promise.promisify(request.get);
55

6+
function getMimeType (contentType) {
7+
return contentType ? contentType.split(';')[0] : null;
8+
}
9+
610
function makeRequest (options, url) {
711
var requestOptions = _.clone(options);
812
requestOptions.url = url;
913

1014
return get(requestOptions).then(function handleResponse (data) {
1115
return {
1216
url: data.request.href,
17+
mimeType: getMimeType(data.headers['content-type']),
1318
body: data.body
1419
};
1520
});

lib/resource-handler/html/index.js

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ HtmlResourceHandler.prototype.handle = function handle (resource) {
1919
});
2020
};
2121

22-
HtmlResourceHandler.prototype.loadResourcesForRule = function loadResourcesForRule ($, resource, rule) {
22+
HtmlResourceHandler.prototype.loadResourcesForRule = function loadResourcesForRule ($, parentResource, rule) {
2323
var self = this;
2424
var promises = $(rule.selector).map(function loadForElement () {
2525
var el = new HtmlSourceElement($(this), rule);
2626
var pathContainer = el.getPathContainer();
2727
if (!pathContainer) {
2828
return Promise.resolve();
2929
}
30-
return self.handleChildrenPaths(pathContainer, resource, createHtmlData(el)).then(el.setData.bind(el));
30+
return self.handleChildrenPaths(pathContainer, parentResource).then(el.setData.bind(el));
3131
}).get();
3232

3333
return utils.waitAllFulfilled(promises);
@@ -51,16 +51,4 @@ function loadTextToCheerio (text) {
5151
});
5252
}
5353

54-
/**
55-
* @param {HtmlSourceElement} htmlSourceEl
56-
* @returns {HtmlData}
57-
*/
58-
function createHtmlData (htmlSourceEl) {
59-
return {
60-
tagName: htmlSourceEl.el[0].name,
61-
attributeName: htmlSourceEl.rule.attr,
62-
attributeValue: htmlSourceEl.el.attr(htmlSourceEl.rule.attr)
63-
};
64-
}
65-
6654
module.exports = HtmlResourceHandler;

0 commit comments

Comments
 (0)