atm3-postpackager-list-html
Advanced tools
Comparing version 0.0.1 to 0.0.2
60
index.js
@@ -1,8 +0,12 @@ | ||
module.exports = function(ret, conf, settings, opt) { | ||
var tpl = "<li><a href='{href}'>{title}</a></li>"; | ||
var path = require('path'), | ||
listFilePath = path.join(__dirname, "./list.html"), | ||
content = fis.util.read(listFilePath); | ||
module.exports = function (ret, conf, settings, opt) { | ||
var folders = {}; | ||
fis.util.map(ret.src, function(subpath, file) { | ||
fis.util.map(ret.src, function (subpath, file) { | ||
if (file.isHtmlLike) { | ||
if (!folders[file.subdirname]) { | ||
folders[file.subdirname] = { | ||
var folderObj = folders[file.subdirname]; | ||
if (!folderObj) { | ||
folderObj = folders[file.subdirname] = { | ||
files: [], | ||
@@ -12,28 +16,38 @@ listFile: null | ||
} | ||
var folderObj = folders[file.subdirname]; | ||
if (file.basename === 'list.html') { | ||
if (!!folderObj.listFile) { | ||
return false; | ||
} else if (file.basename === 'list.html') { | ||
folderObj.listFile = file; | ||
} else { | ||
folderObj.files.push(file); | ||
folderObj.dirname = file.dirname; | ||
} | ||
} | ||
}); | ||
fis.util.map(folders, function(_subdirname, folderObj) { | ||
var listFile = folderObj.listFile; | ||
if (!folderObj.listFile) { | ||
return false; | ||
fis.util.map(folders, function (_subdirname, folderObj) { | ||
if (folderObj.files.length > 1) { | ||
var listFile = folderObj.listFile; | ||
if (!listFile) { | ||
fis.file.wrap(folderObj.dirname + "/list.html"); | ||
listFile.setContent(content); | ||
} | ||
generateListFile(listFile, folderObj.files); | ||
fis.log.debug("generate list.html ok"); | ||
} | ||
var body = []; | ||
folderObj.files.forEach(function(file) { | ||
var matches = file.getContent().match(/\<title\>\S*\-(\S*)<\/title>/i); | ||
if (!matches) { | ||
matches = file.getContent().match(/\<title\>(\S*)<\/title>/i) | ||
} | ||
var title = matches ? RegExp.$1 : file.basename; | ||
body.push(tpl.replace('{href}', file.basename).replace('{title}', title)); | ||
}); | ||
listFile.setContent(listFile.getContent().replace('${list}', body.join("\r\n"))); | ||
fis.log.debug("generate list.html ok"); | ||
}); | ||
} | ||
function generateListFile(listFile, files) { | ||
var body = [], | ||
tpl = "<li><a href='{href}'>{title}</a></li>"; | ||
files.forEach(function (file) { | ||
var matches = file.getContent().match(/\<title\>\S*\-(\S*)<\/title>/i); | ||
if (!matches) { | ||
matches = file.getContent().match(/\<title\>(\S*)<\/title>/i) | ||
} | ||
var title = matches ? RegExp.$1 : file.basename; | ||
body.push(tpl.replace('{href}', file.basename).replace('{title}', title)); | ||
}); | ||
listFile.setContent(listFile.getContent().replace('${list}', body.join("\r\n"))); | ||
} |
{ | ||
"name": "atm3-postpackager-list-html", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "atm3 generate list.html", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
3368
4
48