Skip to content

Commit

Permalink
First attempt at adding a GIST URL shortener.
Browse files Browse the repository at this point in the history
With reference to #19
  • Loading branch information
mattgodbolt committed Mar 24, 2016
1 parent 191410c commit 1e810d3
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 22 deletions.
3 changes: 2 additions & 1 deletion app.js
Expand Up @@ -101,11 +101,12 @@ function clientOptionsHandler(compilers, fileSources) {
google_analytics_enabled: gccProps('clientGoogleAnalyticsEnabled', false),
sharing_enabled: gccProps('clientSharingEnabled', true),
github_ribbon_enabled: gccProps('clientGitHubRibbonEnabled', true),
urlshortener: gccProps('clientURLShortener', 'google'),
urlshortener: gccProps('clientURLShortener', 'gist'),
gapiKey: gccProps('google-api-key', 'AIzaSyAaz35KJv8DA0ABoime0fEIh32NmbyYbcQ'),
defaultSource: gccProps('defaultSource', ''),
language: language,
compilers: compilers,
sourceExtension: compilerProps('compileFilename').split('.', 2)[1],
defaultCompiler: compilerProps('defaultCompiler', ''),
compileOptions: compilerProps("options"),
supportsBinary: !!compilerProps("supportsBinary"),
Expand Down
3 changes: 2 additions & 1 deletion etc/config/gcc-explorer.lud-ldnmg01.properties
@@ -1 +1,2 @@
language=C++
language=C++
clientURLShortener=gist
2 changes: 0 additions & 2 deletions etc/config/gcc-explorer.wud-mgodbolt01.properties
@@ -1,7 +1,5 @@
# Default settings for GCC Explorer.
port=10240
compileTimeoutMs=5000
compilers=/home/mgodbolt/.fighome/runtime/gcc/4.9.2-1/bin/g++
clientSharingEnabled=false
tempDirCleanupSecs=10

Expand Down
8 changes: 6 additions & 2 deletions static/compiler.js
Expand Up @@ -327,12 +327,16 @@ function Compiler(domRoot, origFilters, windowLocalPrefix, onChangeCallback, lan
return cppEditor.getValue();
}

function serialiseState() {
function serialiseState(compress) {
var state = {
sourcez: LZString.compressToBase64(cppEditor.getValue()),
compiler: domRoot.find('.compiler').val(),
options: domRoot.find('.compiler_options').val()
};
if (compress) {
state.sourcez = LZString.compressToBase64(cppEditor.getValue());
} else {
state.source = cppEditor.getValue();
}
return state;
}

Expand Down
1 change: 1 addition & 0 deletions static/gcc.css
Expand Up @@ -8,6 +8,7 @@ body {
*/
#permalink {
cursor: text;
width: 300px;
}

.sideBySide {
Expand Down
103 changes: 88 additions & 15 deletions static/gcc.js
Expand Up @@ -156,34 +156,107 @@ function togglePermalink() {
}

function serialiseState() {
var state = {
return encodeURIComponent(JSON.stringify(getState(true)));
}

function getState(compress) {
return {
version: 3,
filterAsm: getAsmFilters(),
compilers: $.map(allCompilers, function (compiler) {
return compiler.serialiseState();
return compiler.serialiseState(compress);
})
};
return encodeURIComponent(JSON.stringify(state));
}

function toGist(state) {
files = {};
function nameFor(compiler) {
var addNum = 0;
var name, add;
for (; ;) {
add = addNum ? addNum.toString() : "";
name = compiler + add + '.' + OPTIONS.sourceExtension;
if (files[name] === undefined) return name;
addNum++;
}
};
state.compilers.forEach(function (s) {
var name = nameFor(s.compiler);
files[name] = {
content: s.source,
language: OPTIONS.language
};
s.source = name;
});
files['state.json'] = {content: JSON.stringify(state)};
return JSON.stringify({
description: "Compiler Explorer automatically generated files",
'public': false,
files: files
});
}

function makeGist(onDone, onFail) {
var req = $.ajax('https://api.github.com/gists', {
type: 'POST',
//accepts: 'application/vnd.github.v3+json',
dataType: 'json',
contentType: 'application/json',
data: toGist(getState())
});
req.done(function (msg) {
onDone(msg);
});
req.fail(function (jqXHR, textStatus) {
onFail(textStatus + " (" + jqXHR.statusText + ")");
});
}

function fromGist(msg) {
var state = JSON.parse(msg.files['state.json'].content);
state.compilers.forEach(function (s) {
s.source = msg.files[s.source].content;
});
return state;
}
function loadGist(gist) {
var req = $.ajax('https://api.github.com/gists/' + gist);
req.done(function (msg) {
loadState(fromGist(msg));
});
req.fail(function (jqXHR, textStatus) {
alert("Unable to load gist: " + textStatus + " (" + jqXHR.statusText + ")");
});
}

function deserialiseState(state) {
if (state.substr(0, 2) == "g=") {
loadGist(state.substr(2));
return;
}
try {
state = $.parseJSON(decodeURIComponent(state));
switch (state.version) {
case 1:
state.filterAsm = {};
/* falls through */
case 2:
state.compilers = [state];
/* falls through */
case 3:
break;
default:
return false;
}
} catch (ignored) {
return false;
}
return loadState(state);
}

function loadState(state) {
if (!state || state['version'] === undefined) return false;
switch (state.version) {
case 1:
state.filterAsm = {};
/* falls through */
case 2:
state.compilers = [state];
/* falls through */
case 3:
break;
default:
return false;
}
setFilterUi(state.filterAsm);
for (var i = 0; i < Math.min(allCompilers.length, state.compilers.length); i++) {
allCompilers[i].setFilters(state.filterAsm);
Expand Down
2 changes: 1 addition & 1 deletion static/index.html
Expand Up @@ -104,7 +104,7 @@
<button class="btn permalink">Permalink</button>
</div>
<div class="collapse permalink-collapse">
<label>Permalink: <input class="input-medium input-uneditable" type="text" placeholder="Loading" readonly id="permalink"></input></label>
<label>Permalink: <input class="input-uneditable" type="text" placeholder="Loading" readonly id="permalink"></input></label>
</div>
</form>
</div>
Expand Down
9 changes: 9 additions & 0 deletions static/urlshorten-gist.js
@@ -0,0 +1,9 @@
function shortenURL(url, done) {
makeGist(function (msg) {
done(document.location.origin + "#g=" + msg.id);
},
function (failure) {
done("Failed: " + failure);
}
);
}

0 comments on commit 1e810d3

Please sign in to comment.