From 1684517611fef6139dd4d89f7788c4f8807c16fd Mon Sep 17 00:00:00 2001 From: keorn Date: Sun, 13 Dec 2015 17:47:22 +0000 Subject: [PATCH] Add viewer support for IPFS files. --- .../gorilla-repl-client/js-viewer/ipfs.js | 13 +++++++++++ .../js-viewer/main-viewer.js | 23 ++++++++++++++----- resources/gorilla-repl-client/view.html | 1 + 3 files changed, 31 insertions(+), 6 deletions(-) create mode 100644 resources/gorilla-repl-client/js-viewer/ipfs.js diff --git a/resources/gorilla-repl-client/js-viewer/ipfs.js b/resources/gorilla-repl-client/js-viewer/ipfs.js new file mode 100644 index 000000000..072f95850 --- /dev/null +++ b/resources/gorilla-repl-client/js-viewer/ipfs.js @@ -0,0 +1,13 @@ +/* + * This file is part of gorilla-repl. Copyright (C) 2014-, Jony Hudson. + * + * gorilla-repl is licenced to you under the MIT licence. See the file LICENCE.txt for full details. + */ + +var getFromIpfs = function (hash, callback) { + $.get("http://gateway.ipfs.io/ipfs/" + hash, callback); +}; + +var getFromIpns = function (hash, callback) { + $.get("http://gateway.ipfs.io/ipns/" + hash, callback); +}; diff --git a/resources/gorilla-repl-client/js-viewer/main-viewer.js b/resources/gorilla-repl-client/js-viewer/main-viewer.js index 126ddbdc6..1208b390e 100644 --- a/resources/gorilla-repl-client/js-viewer/main-viewer.js +++ b/resources/gorilla-repl-client/js-viewer/main-viewer.js @@ -27,15 +27,14 @@ var app = function () { self.copyBoxVisible(false); }; - self.start = function (worksheetData, sourceURL, worksheetName, source) { + self.start = function (worksheetData, sourceURL, worksheetName, host) { var ws = worksheet(); ws.segments = ko.observableArray(worksheetParser.parse(worksheetData)); self.worksheet(ws); self.sourceURL(sourceURL); self.filename(worksheetName); - self.source(source); - self.host((source.toLowerCase() === "bitbucket") ? "Bitbucket" : "GitHub"); + self.host(host); // wire up the UI ko.applyBindings(self, document.getElementById("document")); @@ -64,14 +63,14 @@ $(function () { var repo = getParameterByName("repo"); var path = getParameterByName("path"); getFromGithub(user, repo, path, function (data) { - viewer.start(data, "https://github.com/" + user + "/" + repo, path, source); + viewer.start(data, "https://github.com/" + user + "/" + repo, path, "GitHub"); }); return; case "gist": var id = getParameterByName("id"); var filename = getParameterByName("filename"); getFromGist(id, filename, function (data) { - viewer.start(data, "https://gist.github.com/" + id, filename, source); + viewer.start(data, "https://gist.github.com/" + id, filename, "GitHub"); }); return; case "bitbucket": @@ -80,7 +79,19 @@ $(function () { var path = getParameterByName("path"); var revision = getParameterByName("revision") || "HEAD"; getFromBitbucket(user, repo, path, revision, function (data) { - viewer.start(data, "https://bitbucket.org/" + user + "/" + repo, path, source); + viewer.start(data, "https://bitbucket.org/" + user + "/" + repo, path, "Bitbucket"); + }); + return; + case "ipfs": + var hash = getParameterByName("hash"); + getFromIpfs(hash, function(data) { + viewer.start(data, "http://gateway.ipfs.io/ipfs/" + hash, hash, "IPFS"); + }); + return; + case "ipns": + var hash = getParameterByName("hash"); + getFromIpns(hash, function(data) { + viewer.start(data, "http://gateway.ipfs.io/ipns/" + hash, hash, "IPFS"); }); return; case "test": diff --git a/resources/gorilla-repl-client/view.html b/resources/gorilla-repl-client/view.html index 612025f19..1b4e9e2af 100644 --- a/resources/gorilla-repl-client/view.html +++ b/resources/gorilla-repl-client/view.html @@ -40,6 +40,7 @@ + Gorilla REPL viewer