Skip to content

Instantly share code, notes, and snippets.

@examinedliving
Created August 24, 2013 13:06
Show Gist options
  • Save examinedliving/6328030 to your computer and use it in GitHub Desktop.
Save examinedliving/6328030 to your computer and use it in GitHub Desktop.
jQuery.Parser - specific use jQuery.plugin
(function($) {
$.parser = function(site, args) {
var dfr = $.Deferred(),
result = {
getPages: function() {
var $this = this,
get = $.get('../' + site + '.lsth');
$.extend($this, {
site: site
});
get.then(function(data) {
var arr = [];
arr = data.split(/\r\n/);
$.extend($this, {
files: arr
});
return dfr.resolve(arr).then(function(x) {
return $this.getHTML(x);
});
});
},
getHTML: function(data) {
var $this = this,
defer = $.Deferred(),
arr = [];
$.extend($this, {
responses: {}
});
$.each(data, function(i, a) {
var url = '../' + a,
rand,
promise;
var key = a.split('/').pop();
key = key.replace('.html', '').replace(/-/g, '_');
if (typeof $this.responses[key] !== "undefined") {
rand = Math.round(Math.random() * 2109);
rand = rand + Math.round(Math.random() * 47);
key = key + '_' + rand;
}
$this.responses[key] = {
path: url
};
promise = defer.promise($this.getContent(url, key));
arr.push[promise];
});
$.when.apply($, arr).done(function(data) {
console.log('Complete');
defer.resolve();
}).fail(function(data) {
// console.log(data);
});
return defer.promise();
},
getContent: function(path, key) {
var $this = this,
load = $.Deferred(),
complete = $.Deferred();
$.ajax({
url: path,
accept: 'html',
dataType: 'html',
islocal: true
}).then(function(responseText, status, xhr) {
var $html, $revisedHTML, $content, dp, docTypeObj, xs, content, tmp, currentObject = $this.responses[key];
setTimeout(function() {
dp = new DOMParser();
xs = new XMLSerializer();
$html = $.trim(responseText);
currentObject.html = $html;
docTypeObj = dtp.getDoctypeStr($.trim(responseText));
currentObject.dtp = docTypeObj;
$revisedHTML = dp.parseFromString($.trim(responseText), 'text/html');
try {
$content = $($revisedHTML).find('#content').get()[0].innerHTML;
currentObject.content = $content;
$($revisedHTML).find('#content').replaceWith('<!-- @content -->');
} catch (err) {
currentObject.error = {
errorVal: err,
undefinedContent: typeof($content) === "undefined" ? true : false
}
}
try {
if (typeof currentObject.dtp['attr'] !== "undefined") {
for (var i = 0, $c = currentObject.dtp.attr; i < $c.length; i++) {
$($revisedHTML).find('html').attr($c[i][0], $c[i][1]);
}
}
tmp = xs.serializeToString($revisedHTML);
tmp = tmp.replace('<!DOCTYPE html>', '');
$revisedHTML = currentObject.dtp.docType + tmp;
currentObject.revisedHTML = $revisedHTML;
} catch (err) {
$.extend(currentObject, {
error: err
});
}
$.extend($this.responses[key], currentObject);
load.resolve(currentObject);
}, 2000);
});
return load;
},
build: function() {
var $this = this,
tmp,
url = 'js/utils/FileWriter.php';
$this.failure = [];
$this.success = [];
$responses = $this.responses;
$.each($responses, function($key, $object) {
var path = $object.path,
postObject,
$content = $object.content,
fname = path.split('/').pop();
$res = $object.revisedHTML;
path = path.substr(3);
tmp = path.split('/');
tmp.pop();
path = tmp.join('/') + '/';
fname = fname.substr(0, fname.lastIndexOf('.'));
postObject = {
"res": $res,
"name": fname,
"content": $content,
"path": path
}
$.ajax({
url: url,
type: 'post',
data: postObject,
dataType: 'text html',
}).then(function(response, status, xhr) {
console.log("File name " + fname + ".html is complete");
$this.success.push(fname);
}, function(response, status) {
console.log("File name " + fname + ".html has failed");
$this.failure.push(fname);
});
})
},
flag: "incomplete",
checkFlag: function() {
var $this = this;
if ($this.flag === "complete") {
return true;
}
return false;
}
};
return result;
};
}(jQuery));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment