Skip to content

Commit

Permalink
generator.js Replaced by Promise() .
Browse files Browse the repository at this point in the history
  • Loading branch information
twpl committed Feb 6, 2017
1 parent de1c301 commit 0e9f9be
Show file tree
Hide file tree
Showing 25 changed files with 1,188 additions and 494 deletions.
71 changes: 71 additions & 0 deletions lib/filter/amp-validate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@

'use strict';

var Promise = require('bluebird');
var pathFn = require('path');
var assign = require('object-assign');
var amphtmlValidator = require('amphtml-validator').getInstance();
var lg = require('../log.js');
var util = require('../util.js');
var validPassCnt = 0;
var validErrorCnt = 0;

//------------------------------------
// AMP HTML Validate
//------------------------------------
module.exports.amp_validate = function(result){
if(result.tempData.isCacheUse)return Promise.resolve(result);

lg.setConfig(result.config);

return new Promise(function(resolve , reject){

if(result.config.generator_amp.warningLog){
amphtmlValidator.then(function (validator) {
var validate_result = validator.validateString(result.data);
if( validate_result.status === 'PASS'){
validPassCnt++;
process.stdout.write('[hexo-generator-amp] Plugin is currently AMP validating now ... '+validPassCnt+' pages newly PASSED \u001b[32m✓\u001b[0m . \r');
}else{
validErrorCnt++;
if(validErrorCnt == 1){
//(validate_result.errors.length <= 1 ? "an error." : validate_result.errors.length + " errors.")
// result.post.path + "amp/#development=1 (source: "+ result.post.source
lg.log("amp error", "\u001b[7;49;31mThe AMP Validator found error.\u001b[0m" + "\u001b[31m Please check the template files ( ./"+ result.config.generator_amp.templateDir +"/ ) , and the post's file.\u001b[0m \n");

if(validate_result.errors.length > 0){
console.log("\u001b[31m" + util.spacePadding(validate_result.errors.length, 3) + " AMP Validation Error" + (validate_result.errors.length == 1 ? "" : "s") + "\u001b[31m : " + result.post.path + "amp/#development=1 (source: "+ result.post.source +")\u001b[0m");
console.log(" \n \u001b[31mThe following is error message about AMP validation. \n ----------------------------------------");
}
for (var ii = 0; ii < validate_result.errors.length; ii++) {
var error = validate_result.errors[ii];
var msg = '';
msg += ' line ' + error.line + ', col ' + error.col + ': ' + error.message;
if (error.specUrl !== null && error.specUrl != "") {
msg += ' (see ' + error.specUrl + ')';
}
((error.severity === 'ERROR') ? console.error : console.warn)(msg);
}
if(validate_result.errors.length > 0)
console.log(" ----------------------------------------\u001b[0m\n");
}else{
if(validErrorCnt == 2){
lg.log("amp error", "\u001b[7;49;31mThe AMP Validator found error.\u001b[0m \u001b[31mFor the other pages , check the error message displayed in Chrome Devtools . \u001b[0m\n");
}
console.log("\u001b[31m" + util.spacePadding(validate_result.errors.length, 3) + " AMP Validation Error" + (validate_result.errors.length == 1 ? " " : "s") + "\u001b[31m : " + result.post.path + "amp/#development=1 (source: "+ result.post.source +")\u001b[0m");
}



}
resolve( result );

});
}else{

resolve( result );

}

});
};
28 changes: 28 additions & 0 deletions lib/filter/check-cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

'use strict';

var Promise = require('bluebird');
var pathFn = require('path');
var assign = require('object-assign');
var cache = require('../cache.js');

//------------------------------------
// check the Cache file
//------------------------------------
module.exports.checkCache = function(result){
return new Promise(function(resolve , reject){
var cachedData = cache.getCache( result.post , result.config );
process.stdout.write('[hexo-generator-amp] Plugin is currently checking the cache now ... \r');
if(cachedData){
resolve(assign( result , {
path : pathFn.join( result.post.path , "amp/index.html" ),
data : cachedData.xml,
tempData: assign( result.tempData , { isCacheUse : true } )
}));
}else{
resolve(assign( result , {
path : pathFn.join( result.post.path , "amp/index.html" )
}));
}
});
};
52 changes: 52 additions & 0 deletions lib/filter/figure-escape.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@

'use strict';

var Promise = require('bluebird');
var assign = require('object-assign');

//------------------------------------
// figure escape
//------------------------------------
module.exports.filter_figureEscape = function(result){
if(result.tempData.isCacheUse)return Promise.resolve(result);
return new Promise(function(resolve , reject){
// figure tag
var updateObj;
var replaceStr = result.post.content;
var figureEscArr = [];
var figureMatch = replaceStr.match(/\<figure\s.*?\<\/figure\>/g);
if(figureMatch){
for(var i = 0; i < figureMatch.length; i++){
replaceStr = replaceStr.replace(figureMatch[i],"<!--figure"+i+"-->");
figureEscArr.push(figureMatch[i]);
}

updateObj = assign(
result ,
{
post : assign(
result.post ,
{
content : replaceStr
}
)
} ,
{
tempData : assign(
result.tempData ,
{
figureEscArr : figureEscArr
}
)
}
);

process.stdout.write('[hexo-generator-amp] Plugin is currently escaping <figure> now ... \r');

}else{
updateObj = result;
}

resolve( updateObj );
});
};
45 changes: 45 additions & 0 deletions lib/filter/figure-restoration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@

'use strict';

var Promise = require('bluebird');
var assign = require('object-assign');


//------------------------------------
// figure restoration
//------------------------------------
module.exports.filter_figureRestoration = function(result){
if(result.tempData.isCacheUse)return Promise.resolve(result);
return new Promise(function(resolve , reject){

var updateObj;
var replaceStr = result.post.content;
var figureEscArr = result.tempData.figureEscArr;
var figureMatch = replaceStr.match(/\<\!\-\-figure[0-9]+\-\-\>/g);
if(figureMatch){
for(var i=0; i<figureMatch.length; i++){
var figureID = figureMatch[i].match(/[0-9]+/);
replaceStr = replaceStr.replace( figureMatch[i] , figureEscArr[Number(figureID[0])] );
}

updateObj = assign(
result ,
{
post : assign(
result.post ,
{
content : replaceStr
}
)
}
);

process.stdout.write('[hexo-generator-amp] Plugin is currently restorating <figure> now ... \r');

}else{
updateObj = result;
}

resolve(result);
});
};
55 changes: 55 additions & 0 deletions lib/filter/google-adsense.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@

'use strict';

var Promise = require('bluebird');
var assign = require('object-assign');

//------------------------------------
// google Adsense to <amp-ad>
//------------------------------------
module.exports.filter_googleAdsense = function(result){
if(result.tempData.isCacheUse)return Promise.resolve(result);
return new Promise(function(resolve , reject){

var replaceStr = result.post.content;
var config = result.config;

replaceStr = replaceStr.replace(/\<[^\s\>]*ins[^\S\>]*class="adsbygoogle"[^\S\>]*style="([^"\\]*(?:\\.[^"\\]*)*)"[^\>]*data-ad-client="([^"\\]*(?:\\.[^"\\]*)*)"[^\>]*data-ad-slot="([^"\\]*(?:\\.[^"\\]*)*)"[^\<]*\<\/ins\>/g, function ($1, $2, $3){

var adWidth;
var adHeight;
if(arguments[1]){
var matches = /width:([0-9]*)/g.exec(arguments[1]);
adWidth = matches ? matches[1] : config.generator_amp.substituteGoogle_adsense.width;

matches = /height:([0-9]*)/g.exec(arguments[1]);
adHeight = matches ? matches[1] : config.generator_amp.substituteGoogle_adsense.height;
}else{
adWidth = config.generator_amp.substituteGoogle_adsense.width;
adHeight = config.generator_amp.substituteGoogle_adsense.height;
}

var adClient = arguments[2] ? arguments[2] : config.generator_amp.substituteGoogle_adsense.data_ad_client;
var adSlot = arguments[3] ? arguments[3] : config.generator_amp.substituteGoogle_adsense.data_ad_slot;

return "<amp-ad width=\"" + adWidth + "\" height=\"" + adHeight + "\" type=\"adsense\" data-ad-client=\"" + adClient + "\" data-ad-slot=\"" + adSlot + "\"></amp-ad>";
});

var updateObj = assign(
result ,
{
post : assign(
result.post ,
{
content : replaceStr
}
)
}
);

process.stdout.write('[hexo-generator-amp] Plugin is currently replacing Google Ad ... \r');

resolve(result);

});
};
109 changes: 109 additions & 0 deletions lib/filter/img.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@

'use strict';

var Promise = require('bluebird');
var assign = require('object-assign');
var gs = require('../imageSize.js');
var lg = require('../log.js');

//------------------------------------
// <img> to <amp-img>
//------------------------------------
module.exports.filter_img = function(result){

if(result.tempData.isCacheUse)return Promise.resolve(result);

lg.setConfig(result.config);
gs.setConfig(result.config);

return new Promise(function(resolve , reject){

var updateObj;
var absolutePathReg = /^[a-zA-Z0-9]*?\:\/\//;
var replaceStr = result.post.content;
var post = result.post;
var imgMatch = replaceStr.match(/\<img\s.*?\>/g);
var imgSrc;
var imgWidth;
var imgHeight;

if(imgMatch){
for(var i=0; i<imgMatch.length; i++){

imgSrc = "";
imgWidth = "";
imgHeight = "";

var imgDataMatch = imgMatch[i].match(/data\-original\=\".*?\"/g);
if(imgDataMatch && imgDataMatch.length > 0){
imgSrc = imgDataMatch[0].replace("data-original=","src=");
}else{
imgDataMatch = imgMatch[i].match(/src\=\".*?\"/g);
if(imgDataMatch){
imgSrc = imgDataMatch[0];
}
}

imgDataMatch = imgMatch[i].match(/width\=\".*?\"/g);
if(imgDataMatch){
imgWidth = imgDataMatch[0];
}

imgDataMatch = imgMatch[i].match(/(height|data\-height)\=\".*?\"/g);
if(imgDataMatch){
imgHeight = imgDataMatch[0].replace("data-height","height");
}

if(imgSrc == "" || imgSrc =='src=""'){
lg.log("error", "<img> should contain image src attribute." , post.source);
}else{
if(imgWidth == "" || imgHeight == ""){
var imgPath = imgSrc.replace(/^src\=\"/,"").replace(/\"$/,"");
if(absolutePathReg.test(imgPath)){
//External image file
lg.log("warn", "<img> should contain image file and width height attribute. img path: "+imgPath , post.source);
}else{
//Local image files
var gsSizeInfo = gs.getSizeInfo(imgPath , post);
if(gsSizeInfo){
imgWidth = 'width="' + gsSizeInfo.w + '"';
imgHeight = 'height="' + gsSizeInfo.h + '"';
}else{
reject();
}

}
}
}

// console.log("imgSrc:"+imgSrc + " imgWidth:"+imgWidth + " imgHeight:"+imgHeight);
if(imgSrc != "" && imgWidth != "" && imgHeight != ""){
replaceStr = replaceStr.replace(imgMatch[i], '<p><amp-img '+ imgSrc +' '+ imgWidth +' '+ imgHeight +' layout="responsive"></amp-img></p>');
}else{
//coment out img
replaceStr = replaceStr.replace(imgMatch[i], "<!-- This img tag couldn't replace. : "+ imgMatch[i] +"-->");
}

}

updateObj = assign(
result ,
{
post : assign(
result.post ,
{
content : replaceStr
}
)
}
);

process.stdout.write('[hexo-generator-amp] Plugin is currently replacing <img> now ... \r');

}else{
updateObj = result;
}

resolve(result);
});
};
Loading

0 comments on commit 0e9f9be

Please sign in to comment.