First attempt at adding a GIST URL shortener.

With reference to #19
dev/git-series/gccdum
Matt Godbolt 7 years ago
parent 191410c653
commit 1e810d3a65

@ -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"),

@ -1 +1,2 @@
language=C++
language=C++
clientURLShortener=gist

@ -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

@ -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;
}

@ -8,6 +8,7 @@ body {
*/
#permalink {
cursor: text;
width: 300px;
}
.sideBySide {

@ -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);

@ -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>

@ -0,0 +1,9 @@
function shortenURL(url, done) {
makeGist(function (msg) {
done(document.location.origin + "#g=" + msg.id);
},
function (failure) {
done("Failed: " + failure);
}
);
}
Loading…
Cancel
Save