Commit 84101f21 authored by Dave Lane's avatar Dave Lane
Browse files

working latiniser plus other url fixes, commented out a lot of debugging

parent bf3359d5
......@@ -98,16 +98,16 @@ const doc = `Usage:
`;
opt = docopt(doc, {exit: false});
console.log(
/*console.log(
`========================= ${new Date().toJSON()} =========================`
);
console.log(opt);
);*/
//console.log(opt);
// work out the base_url for Wiki pages
base_url = getBaseURL(opt);
//
// deal with other command line options
if (opt["--theme"]) {
theme = require("./themes/" + opt["--theme"]);
theme = require("./themes/" + opt["--theme"]);
}
if (opt["--iframes"]) {
iframes = require("./extras/iframes.js");
......@@ -181,9 +181,9 @@ if (opt["--wpurl"]) {
opt["--urlprefix"] = "";
// split into hostname and URI within host
let wpURLparts = opt["--wpurl"].replace(/^(https?:)?\/\//, "").split("/", 2);
console.log("============ wpURLparts ==============");
console.log(wpURLparts);
console.log("============ /wpURLparts ==============");
//console.log("============ wpURLparts ==============");
//console.log(wpURLparts);
//console.log("============ /wpURLparts ==============");
if (wpURLparts.length === 2 && wpURLparts[1]) {
// remove any trailing slash
opt["--urlprefix"] = "/" + wpURLparts[1].replace(/\/$/, "");
......@@ -198,7 +198,7 @@ if (opt["--wpurl"]) {
function setMetaName(head, name, content) {
var meta = '<meta name="' + name + '" content="' + content + '">';
console.log('setting new meta: ', meta);
//console.log('setting new meta: ', meta);
head.append(meta);
}
......@@ -235,10 +235,10 @@ function getTimestamp() {
function getBaseURL(opt) {
let u = uri.parse(opt['OUTLINE']);
console.log('u: ', u);
//console.log('u: ', u);
base_url = u.scheme + '://' + u.host;
if (u.port) {
console.log("adding port: ", u.port);
//console.log("adding port: ", u.port);
base_url += ':' + u.port;
}
return base_url;
......@@ -247,7 +247,7 @@ function getBaseURL(opt) {
function pageToURL(p) {
val = opt["--urlprefix"] +
p.replace(opt.PAGEPREFIX, "").replace("?", "").replace("%3F", "");
console.log('converting '+p+' into '+val);
//console.log('converting '+p+' into '+val);
return (val);
}
......@@ -267,11 +267,23 @@ function slugify(u) {
.replace(/[ _]/g, "-")
.replace(/[()?&,;:'/]/g, "")
.replace(/’/g, "")
.toLowerCase()
.toLowerCase();
}
// apply extra fixes for latinised stuff...
function fixurl(u) {
return u
.replace(/[«»– ]/g, "")
.replace(/ /g, "")
.replace(/--/g, "-");
}
String.prototype.latinise = latinise;
String.prototype.slugify = function() {
return this.replace(/[\W\S]/g, function(x) { return slugify(x); })
result = this.replace(/[\W\S]/g, function(x) { return slugify(x); });
// result.replace(/-[«-»]-/g, "-");
/*result.replace(/-«-/g, "-");
result.replace(/-»-/g, "-"); */
return result;
}
// step through pages array looking for prev/next non-blank page
......@@ -297,7 +309,7 @@ function getNext(pi, pages) {
// source: https://www.geeksforgeeks.org/how-to-create-hash-from-string-in-javascript/
function stringToHash(string) {
var hash = md5(string).toString();
console.log('hash for '+string+' = '+hash+'...');
//console.log('hash for '+string+' = '+hash+'...');
return hash;
}
......@@ -395,7 +407,7 @@ function processPage(pi) {
$(this).attr("href", urls[orig]);
} else if (typeof orig === 'string' && orig.indexOf('#') != -1) {
// if the # is the first character, we need to use the
console.log('Found a # anchor! - '+orig);
//console.log('Found a # anchor! - '+orig);
if (orig.indexOf('#') == 0) {
//console.log('URI starts with a #!');
}
......@@ -403,7 +415,7 @@ function processPage(pi) {
//console.log('orig "'+orig+'" isn\'t listed in the urls array');
// other WikiEducator relative pages that are not in the outline
if (orig && orig.indexOf("//") === -1) {
console.log(orig+' is another relative wikieducator page, so we\'re giving it a url based on '+base_url+'.');
//console.log(orig+' is another relative wikieducator page, so we\'re giving it a url based on '+base_url+'.');
//$(this).attr("href", "//wikieducator.org" + orig);
//$(this).attr("href", "//TEST" + orig);
$(this).attr("href", base_url + orig);
......@@ -412,7 +424,7 @@ function processPage(pi) {
});
$('.weVimeo-wrapper').each(function(i, e) {
console.log("******* Checking for Vimeo video! ********");
//console.log("******* Checking for Vimeo video! ********");
var wrapper = $(this),
wrapper_classes = wrapper.attr("class"),
align = 'none', align_class = 'none', url, id, args = '', width, w_unit, height, h_unit,
......@@ -428,7 +440,7 @@ function processPage(pi) {
align = 'center';
align_class = 'center';
}
console.log('align=' + align + ', align_class=' + align_class + '...');
//console.log('align=' + align + ', align_class=' + align_class + '...');
wrapper.find('iframe').each(function(i, e) {
var $this = $(this);
url = $this.attr('src'),
......@@ -443,11 +455,11 @@ function processPage(pi) {
id = url.split('?')[0].split('video/')[1],
args = url.split('?')[1].replace('&amp;', '&');
start = args.split('#t=')[1].replace('s','');
console.log('++++++++++++++++++++++++start = ', start);
//console.log('++++++++++++++++++++++++start = ', start);
args = args.split('#t=')[0] + '&#t=' + start + 's';
console.log('url = ', url);
console.log('id = ', id);
console.log('width = ' + width + ' units = ' + w_unit + ', height = ' + height + ' units = ' + h_unit + '...');
//console.log('url = ', url);
//console.log('id = ', id);
//console.log('width = ' + width + ' units = ' + w_unit + ', height = ' + height + ' units = ' + h_unit + '...');
});
vimeoinvocation = '[arve class="weVimeo-responsive" url="https://vimeo.com/' + id + '" parameters="' + args + '" align="' + align + '" maxwidth="' + width + '"]';
// disabling for the time being
......@@ -506,7 +518,7 @@ function processPage(pi) {
// get video details
if (wrapper.find("a").attr("href").includes("youtube.com")) {
url = wrapper.find("a").attr("href");
console.log('checking for start in url = ', url);
//console.log('checking for start in url = ', url);
var qstring = url.split("?")[1],
qvars = qstring.split('&'),
qparam, i;
......@@ -515,14 +527,14 @@ function processPage(pi) {
qparam = qvars[i].split('=');
if (qparam[0] === 't') {
start = qparam[1];
console.log('found start! = ', start);
//console.log('found start! = ', start);
}
}
console.log('qstring = ', qstring);
//console.log('qstring = ', qstring);
} else {
url = 'https://youtube.com/watch?v=' + id;
}
console.log('url = ', url);
//console.log('url = ', url);
var opts = ' class="weYouTube-responsive" controls="yes" disable_links="true" allowfullscreen="yes"';
opts += ' url="' + url + '"';
opts += ' maxwidth="' + width +'"';
......@@ -530,7 +542,7 @@ function processPage(pi) {
if (yttitle != "") { opts += ' title="' + yttitle + '"'; }
if (align != "none") { opts += ' align="' + align + '"'; }
ytinvocation = '[arve' + opts + ']';
console.log('initial ytinvocation = ', ytinvocation);
//console.log('initial ytinvocation = ', ytinvocation);
});
// assign the new content...
//ytinvocation = '<div class="' + wrapper_classes + '"' + style + '>' + ytinvocation + '</div>';
......@@ -571,7 +583,7 @@ function processPage(pi) {
// make stylesheet & Javascript references point to WikiEducator
page_header.find('link[rel="stylesheet"]').each(function() {
var href = $(this).attr("href");
console.log('************updating link stylesheet href: ', href);
//console.log('************updating link stylesheet href: ', href);
if (
href &&
href.length >= 2 &&
......@@ -579,7 +591,7 @@ function processPage(pi) {
href.charAt(1) !== "/"
) {
//$(this).attr("href", "https://wikieducator.org" + href);
console.log('************updating link stylesheet href: '+href+' with: '+base_url + href);
//console.log('************updating link stylesheet href: '+href+' with: '+base_url + href);
$(this).attr("href", base_url + href);
}
});
......@@ -628,15 +640,15 @@ function processPage(pi) {
</script>`); */
/* if (opt['--h5p']) {
console.log('!!!!!!!!!!!!!!!!!!!!!!!!!! adding H5P Resize script!!!!');
//console.log('!!!!!!!!!!!!!!!!!!!!!!!!!! adding H5P Resize script!!!!');
page_header.append(getH5PResizer());
}*/
// compile the WEnotes IDs
/* console.log('|||||||||||| setting page IDs for '+base_url+' - '+url+'...');
/* //console.log('|||||||||||| setting page IDs for '+base_url+' - '+url+'...');
var site_id = stringToHash(base_url);
var path_id = stringToHash(url);
console.log('|||||||||||| hashes: base_id = '+site_id+', path_id = '+path_id);
//console.log('|||||||||||| hashes: base_id = '+site_id+', path_id = '+path_id);
var wenotes_ids = "<script type='text/javascript' id='wenotes-ids'> \
var WEnotesSite = '`+base_url+`'; \
var WEnotesPath = '`+url+`'; \
......@@ -644,7 +656,7 @@ function processPage(pi) {
var WEnotesPathID = '`+path_id+`'; \
var WEnotesIDs = { site: '`+base_url+`', path: '`+url+`', site_id: '`+site_id+`', path_id: '`+path_id+`' }; \
</script>";
console.log('|||||||||||||<<<<<<<<<<<<<< wenotes-ids ', wenotes_ids); */
//console.log('|||||||||||||<<<<<<<<<<<<<< wenotes-ids ', wenotes_ids); */
//theme.header($, opt, wenotes_ids);
......@@ -655,16 +667,16 @@ function processPage(pi) {
theme.wrap($);
if (opt["--iframes"]) {
console.log("+++++");
//console.log("+++++");
iframes.process($, opt);
console.log("+++++");
//console.log("+++++");
}
if (opt["--h5p"]) {
console.log("+++++");
//console.log("+++++");
h5p.process($, opt);
//addH5PResizer(page_header);
console.log("+++++");
//console.log("+++++");
}
if (opt["--prevnext"]) {
......@@ -673,10 +685,16 @@ function processPage(pi) {
var pp = getPrev(pi, pages);
if (pp >= 0) {
pl = urls[pages[pp]];
console.log("previous page's link = "+pl);
pl = fixurl(pl.latinise());
console.log(" fixed link = "+pl);
}
var np = getNext(pi, pages);
if (np < pages.length) {
nl = urls[pages[np]];
console.log("next page's link = "+nl);
nl = fixurl(nl.latinise());
console.log(" fixed link = "+nl);
}
if (opt["--previous"] && opt["--next"]) {
pt = opt["--previous"];
......@@ -777,15 +795,15 @@ function processPage(pi) {
}
// add per page identifier
console.log('|||||||||||| setting page IDs for '+base_url+' - '+url+'...');
//console.log('|||||||||||| setting page IDs for '+base_url+' - '+url+'...');
var site_id = stringToHash(base_url);
var path_id = stringToHash(url);
console.log('|||||||||||| hashes: base_id = '+site_id+', path_id = '+path_id);
//console.log('|||||||||||| hashes: base_id = '+site_id+', path_id = '+path_id);
// create a shortcode to insert...
var shortcode = '[WEnotesIDs WEnotesSite="'+base_url+'" WEnotesPath="'+url+'" WEnotesSiteID="'+site_id+'" WEnotesPathID="'+path_id+'"]';
// insert the WEnotesIds short code
$('#content').append('<div id="wenote-ids">'+shortcode+'</div>');
console.log('|||||||||||| shortcode: '+shortcode);
//console.log('|||||||||||| shortcode: '+shortcode);
// save the page: to Wordpress or to filesystem
if (opt["--wpurl"]) {
......@@ -830,34 +848,34 @@ function processPage(pi) {
}
//console.log('page_desc', page_desc);
if (opt["--debugout"]) {
console.log("######");
console.log(page);
console.log("######");
//console.log("######");
//console.log(page);
//console.log("######");
}
try {
//console.log("---- data1 ----");
////console.log("---- data1 ----");
//console.log(data);
//console.log("---- data2 ----");
wp.newPost(page_desc, function(err, data) {
if (err) {
console.log('...error...')
console.log(page_desc, pi, pages[pi], err);
//console.log('...error...')
//console.log(page_desc, pi, pages[pi], err);
process.exit(9);
} else {
console.log('...no error, saving in WP...')
console.log(pi, "wp saved:", data);
//console.log('...no error, saving in WP...')
//console.log(pi, "wp saved:", data);
outline[pi].wpID = data;
}
console.log("processing page...")
console.log( arguments );
//console.log("processing page...")
//console.log( arguments );
setImmediate(processPages);
});
} catch (e) {
console.log(pi, "!!!error writing to wp");
console.log( arguments );
console.log("------1");
console.log(outline[pi]);
console.log("------2");
//console.log(pi, "!!!error writing to wp");
//console.log( arguments );
//console.log("------1");
//console.log(outline[pi]);
//console.log("------2");
//console.log("page_desc", page_desc);
setImmediate(processPages);
}
......@@ -867,8 +885,8 @@ function processPage(pi) {
pagename = pages[pi];
filename = courseURLToFile(pagename);
console.log("pagename:", pagename);
console.log(" filename:", filename);
//console.log("pagename:", pagename);
//console.log(" filename:", filename);
mkdirp.sync(filename);
fs.writeFileSync(filename + "/index.html", page);
setImmediate(processPages);
......@@ -877,24 +895,24 @@ function processPage(pi) {
}
function oeruMenuCreate() {
console.log('*********** start oeruMenuCreate **************');
//console.log('*********** start oeruMenuCreate **************');
// find out the methods this WordPress install supports
wp.listMethods(function(err, methods) {
if (err) {
console.log(`Unable to retrieve list of XMLRPC methods ${err}`);
//console.log(`Unable to retrieve list of XMLRPC methods ${err}`);
process.exit(10);
} else {
console.log('got XMLRPC Methods');
//console.log('got XMLRPC Methods');
// if oeru_course.menu_create is supported, invoke it
if (methods.indexOf("oeru_course.menu_create") > -1) {
console.log('trying to create a menu... ');
//console.log('trying to create a menu... ');
wp.authenticatedCall("oeru_course.menu_create", function(error, data) {
console.log('completed XMLRPC call... ');
//console.log('completed XMLRPC call... ');
if (error) {
console.log(`Menu creation failed: ${error}`);
//console.log(`Menu creation failed: ${error}`);
process.exit(11);
} else {
console.log("Menu created");
//console.log("Menu created");
}
});
}
......@@ -903,8 +921,8 @@ function oeruMenuCreate() {
}
function processPages() {
console.log(`---- processPages(pi=${pi}) ----`);
console.log('outline[0]: ', outline[0]);
/*//console.log(`---- processPages(pi=${pi}) ----`);
//console.log('outline[0]: ', outline[0]);*/
if (pi >= pages.length) {
wp.authenticatedCall(
"wp.setOptions",
......@@ -915,10 +933,10 @@ function processPages() {
},
function(err, a) {
if (err) {
console.log("theme options setting error", err);
//console.log("theme options setting error", err);
process.exit(9);
} else {
console.log("theme options set", a);
//console.log("theme options set", a);
if (opt["--wpmenu"]) {
oeruMenuCreate();
}
......@@ -1004,7 +1022,7 @@ function getOutline() {
info.base_url = base_url;
info.text = text;
info.wkpage = wkpage;
info.url = url;
info.url = url.latinise();
info.depth = depth;
info.hasPage = hasPage;
info.hasChildren = $sub.length > 0;
......@@ -1022,24 +1040,24 @@ function getOutline() {
}
if (errors) {
console.log("error reading outline: ", errors);
//console.log("error reading outline: ", errors);
return;
}
$nav = $("#mw-content-text>ul");
readOutline($nav, 0, 0, [], "");
console.log("==== Outline ====", outline.length);
//console.log("==== Outline ====", outline.length);
outline.forEach(function(v, i) {
console.log(v);
//console.log(v);
});
console.log("==== URLs ====");
console.log(urls);
//console.log("==== URLs ====");
//console.log(urls);
console.log("==== Pages ====", pages.length);
//console.log("==== Pages ====", pages.length);
pages.forEach(function(v, i) {
console.log(i, v);
//console.log(i, v);
});
if (opt["--wpurl"]) {
......@@ -1060,13 +1078,13 @@ function getOutline() {
var post;
if (posts.length > 0) {
post = posts.pop();
console.log("post id", post.id);
//console.log("post id", post.id);
wp.deletePost(post.id, function(err, data) {
if (err) {
console.log(err);
//console.log(err);
process.exit(8);
} else {
console.log("deleted:", post.id);
//console.log("deleted:", post.id);
setImmediate(deletePages);
}
});
......@@ -1074,13 +1092,13 @@ function getOutline() {
setImmediate(processPages);
}
}
console.log('before getPosts');
//console.log('before getPosts');
wp.getPosts({ post_type: "page", number: 200 }, ["post_id"], function(
err,
data
) {
if (err) {
console.log(err);
//console.log(err);
process.exit(8);
} else {
posts = data;
......
console.log('in h5p.js!');
//console.log('in h5p.js!');
module.exports = {
version: "0.0.3",
process: function($, opt) {
console.log('in h5p.js module!');
//console.log('in h5p.js module!');
var count = 0,
h5pid,
host,
......@@ -20,13 +20,13 @@ module.exports = {
h5pid = host = subdir = subsite = height = width = style = style_string = classes = id = "";
code = "";
count += 1;
console.log('WEH5P '+count+' ...');
//console.log('WEH5P '+count+' ...');
$(this).find("div").each(function() {
console.log('found class = '+$(this).attr('class'));
//console.log('found class = '+$(this).attr('class'));
switch($(this).attr('class')) {
case 'h5pid':
h5pid = $(this).text();
console.log('h5pid = '+h5pid);
//console.log('h5pid = '+h5pid);
break;
case 'host':
host = $(this).text();
......@@ -40,34 +40,34 @@ module.exports = {
break;
case 'subsite':
subsite = $(this).text();
console.log('subsite = '+subsite);
//console.log('subsite = '+subsite);
break;
case 'subdir':
subdir = $(this).text();
console.log('subdir = '+subdir);
//console.log('subdir = '+subdir);
break;
case 'height':
height = $(this).text();
console.log('height = '+height);
// console.log('height = '+height);
style_string += ' height:'+height+';';
break;
case 'width':
width = $(this).text();
console.log('width = '+width);
//console.log('width = '+width);
style_string += ' width:'+width+';';
break;
case 'style':
style = $(this).text();
console.log('style = '+style);
//console.log('style = '+style);
style_string += ' '+style+';';
break;
case 'classes':
classes = $(this).text();
console.log('classes = '+classes);
//console.log('classes = '+classes);
break;
case 'id':
id = $(this).text();
console.log('id = '+id);
//console.log('id = '+id);
break;
}
});
......@@ -104,7 +104,7 @@ module.exports = {
code += '<script src="https://h5p.oeru.org/modules/contrib/h5p/vendor/h5p/h5p-core/js/h5p-resizer.js" charset="UTF-8"></script>';
//code += '<script class="WEH5P" src="'+script_url+'/wp-content/plugins/h5p/h5p-php-library/js/h5p-resizer.js" charset="UTF-8">&nbsp;</script>';
//code = '<!-- Escape editor -->'+code+'<!-- Un-Escape editor -->';
console.log('code = '+code);
//console.log('code = '+code);
$(this).replaceWith(code);
});
}
......
console.log('in iframe.js!');
//console.log('in iframe.js!');
module.exports = {
version: "0.1.0",
process: function($, opt) {
console.log('in iframe.js module!');
//console.log('in iframe.js module!');
var ifcount = 0,
iframe,
ifurl,
......@@ -17,39 +17,39 @@ module.exports = {
$(".WEiFrame").each(function() {
iframe = ifurl = ifheight = ifwidth = ifstyle = ifstyle_string = ifclasses = ifid = "";
ifcount += 1;
console.log('WEiFrame '+ifcount+' ...');
//console.log('WEiFrame '+ifcount+' ...');
$(this).find("div").each(function() {
ifclass = $(this).attr('class');
//console.log('found '+ifclass+'='+$(this).text());
switch($(this).attr('class')) {
case 'url':
ifurl = $(this).text();
console.log('url = '+ifurl);
//console.log('url = '+ifurl);
iframe += ' src="'+ifurl+'"';
break;
case 'height':
ifheight = $(this).text();
console.log('height = '+ifheight);
//console.log('height = '+ifheight);
ifstyle_string += ' height:'+ifheight+';';
break;
case 'width':
ifwidth = $(this).text();
console.log('width = '+ifwidth);
//console.log('width = '+ifwidth);
ifstyle_string += ' width:'+ifwidth+';';
break;
case 'style':
ifstyle = $(this).text();
console.log('style = '+ifstyle);
//console.log('style = '+ifstyle);
ifstyle_string += ' '+ifstyle;
break;
case 'classes':
ifclasses = $(this).text();
console.log('classes = '+ifclasses);
//console.log('classes = '+ifclasses);
iframe += ' class="'+ifclasses+'"';
break;
case 'id':
ifid = $(this).text();
console.log('id = '+ifid);
//console.log('id = '+ifid);
iframe += ' id="'+ifid+'"';
break;
}
......@@ -60,7 +60,7 @@ module.exports = {
//iframe = '<div class="WEiFrame"><iframe frameborder="0" '+iframe+' /></div>';
iframe = '<div class="WEiFrame"><iframe '+iframe+' /></div>';
iframe = '<!-- Escape editor -->'+iframe+'<!-- Un-Escape editor -->';
console.log(ifcount+'. '+iframe);
//console.log(ifcount+'. '+iframe);
$(this).replaceWith(iframe);
});
}
......
......@@ -828,15 +828,12 @@ module.exports = function latinise() {
"":"r",
"":"u",
"":"v",
"":"x