Skip to content

Instantly share code, notes, and snippets.

@ronw23
Last active November 7, 2018 18:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ronw23/da9ef4577bebcbb1025f6c3f734d1807 to your computer and use it in GitHub Desktop.
Save ronw23/da9ef4577bebcbb1025f6c3f734d1807 to your computer and use it in GitHub Desktop.
var jq = document.createElement('script');
jq.src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js";
document.getElementsByTagName('head')[0].appendChild(jq);
jq = document.createElement('script');
jq.src = "https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js";
document.getElementsByTagName('head')[0].appendChild(jq);
$(document.head).append("<link href='http://fed.wiki.org/style/style.css' rel='stylesheet' type='text/css' media='screen'>");
$(document.head).append("<link href='http://fed.wiki.org/theme/style.css' rel='stylesheet' type='text/css' media='screen'>");
$(document.head).append("<link href='http://fed.wiki.org/style/print.css' rel='stylesheet' type='text/css' media='print'>");
var asSlug = function (name) {
return name.replace(/\s/g, '-').replace(/[^A-Za-z0-9-]/g, '').toLowerCase();
};
var pluginBase = 'https://federated-wiki-plugins.hashbase.io/'
window.plugins = {
paragraph: {
emit: (div, item) => {
return div.append("<p>" + wiki.resolveLinks(item.text)+ "</p>")
}
},
image: {
emit: (div, item) => {
item.text || (item.text = item.caption);
return div.append(`<img class=thumbnail src="${item.url}"> <p>${wiki.resolveLinks(item.text)}</p>`);
}
}
}
resolve = {};
resolve.resolutionContext = [];
resolve.escape = escape = function (string) {
return string.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
};
resolve.resolveFrom = function (addition, callback) {
resolve.resolutionContext.push(addition);
try {
return callback();
} finally {
resolve.resolutionContext.pop();
}
};
resolve.resolveLinks = function (string, sanitize) {
var external, internal, stash, stashed, unstash;
if (sanitize == null) {
sanitize = escape;
}
stashed = [];
stash = function (text) {
var here;
here = stashed.length;
stashed.push(text);
return "〖" + here + "〗";
};
unstash = function (match, digits) {
return stashed[+digits];
};
internal = function (match, name) {
var slug;
slug = asSlug(name);
if (slug.length) {
return stash("<a class=\"internal\" href=\"/" + slug + ".html\" data-page-name=\"" + slug + "\" title=\"" + (resolve.resolutionContext.join(' => ')) + "\">" + (escape(name)) + "</a>");
} else {
return match;
}
};
external = function (match, href, protocol, rest) {
return stash("<a class=\"external\" target=\"_blank\" href=\"" + href + "\" title=\"" + href + "\" rel=\"nofollow\">" + (escape(rest)) + " <img src=\"/images/external-link-ltr-icon.png\"></a>");
};
string = string.replace(/〖(\d+)〗/g, "〖 $1 〗").replace(/\[\[([^\]]+)\]\]/gi, internal).replace(/\[((http|https|ftp):.*?) (.*?)\]/gi, external);
return sanitize(string).replace(/〖(\d+)〗/g, unstash);
};
wiki = {
resolveLinks: resolve.resolveLinks,
pluginRoutes: {},
getScript: $.getScript,
getPlugin: async (name) => {
if (name != 'paragraph' && name != 'activity' && !window.plugins[name]) {
return await fetch(`${pluginBase}${name}/client/${name}.js`, {
mode: 'cors'
})
.then(res => res.text())
.then(eval)
.then(() => wiki.pluginRoutes[name] = `${pluginBase}/${name}/`)
.then(() => window.plugins[name])
} else if (name == 'activity' && !window.plugins[name]) {
return await fetch(`https://wiki.dbbs.co/plugins/activity/activity.js`, {
mode: 'cors'
})
.then(res => res.text())
.then(eval)
.then(() => window.plugins[name])
}
return window.plugins[name]
}
}
var page_url = 'https://wiki.dbbs.co/wiki-as-flat-json-database.json'
page_url = 'http://ward.fed.wiki.org/identifiable-neighborhood.json'
document.body.innerHTML = "";
//<section class='main'>
$(document.body).append("<div class='page'><div class='paper'><div class='story'>")
fetch(page_url)
.then(res => res.json())
.then(page => {
window.page = page;
})
.then(await Promise.all(page.story.map(item => wiki.getPlugin(item.type))))
.then(() => {
page.story.forEach(item => {
var div = $(`<div class="item ${item.type}">`)
window.plugins[item.type].emit(div, item)
$("div.story").append(div)
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment