Skip to content

Instantly share code, notes, and snippets.

@strellic
Last active March 1, 2021 01:59
Show Gist options
  • Save strellic/8e55f273d8e8d9873d5a49fce1866546 to your computer and use it in GitHub Desktop.
Save strellic/8e55f273d8e8d9873d5a49fce1866546 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<title>ocf.io shorturls</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta2/dist/css/bootstrap.min.css" rel="stylesheet" crossorigin="anonymous">
</head>
<body>
<div class="m-3">
<h1>ocf.io short link translations</h1>
<h5><a href="https://www.ocf.berkeley.edu/docs/">Documentation</a></h5>
<hr />
<table class="table">
<thead>
<tr>
<th scope="col">Short (ocf.io)</th>
<th scope="col">Long</th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
<script type="text/javascript">
let fixup = (item) => {
// this function tries to simplify regexes to try and make them as human-readable as possible
// yes, i know this is scuffed
// but it only took like an hour so ¯\_(ツ)_/¯
let [r,u] = item;
// remove regex prefix / postfix
if(r.startsWith("^")) r = r.slice(1);
if(r.endsWith("$")) r = r.slice(0, -1);
r = r.replace(/\?(\:)*/g, ""); // remove negative lookbacks
r = r.replace(/\{.*?\}|[\+\?]|\.[\+\?\*]/g, "*"); // remove quantifiers
r = r.replace(/\(?!.*?\)/g, ""); // remove negative capture groups
r = r.replace(/\[\^.*?\]/g, ""); // remove negative brackets
r = r.replace(/\(|\)/g, ""); // remove parens
u = u.replace(/\$\d/g, "*"); // replace $ in url
return [r, u];
};
let add = (item) => {
let tr = document.createElement("tr");
let short = document.createElement("td");
let long = document.createElement("td");
let longA = document.createElement("a");
short.innerHTML = item[0];
longA.href = item[1].replace(/\*/g, ""); // remove * from href
longA.innerHTML = item[1];
long.appendChild(longA);
tr.appendChild(short);
tr.appendChild(long);
document.querySelector("tbody").appendChild(tr);
}
let init = async () => {
// fetch shorturl.pp
let pp = await (await fetch("https://raw.githubusercontent.com/ocf/puppet/master/modules/ocf_www/manifests/site/shorturl.pp")).text();
// extract all lines with 'rewrite_rule'
let rewrites = pp.split("\n").filter(l => l.includes("rewrite_rule")).map(l => l.trim().replace(/\\/g, ""));
// extract regex and url from each line
rewrites = rewrites.map(r => r.split(" => '")[1].split(" [")[0].split("$ "));
// remove ' from beginning of regex
rewrites = rewrites.map(r => [r[0].slice(1), r[1]]);
// make sure url begins with http
rewrites = rewrites.filter(r => r[1].startsWith("http"));
for(let rewrite of rewrites) {
add(fixup(rewrite));
}
};
window.onload = init;
</script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta2/dist/js/bootstrap.bundle.min.js" crossorigin="anonymous"></script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment