Skip to content

Instantly share code, notes, and snippets.

@felipeasimos
Created June 16, 2023 03:56
Show Gist options
  • Save felipeasimos/dd11e199b6631b0e262fb91b1bb0dd2b to your computer and use it in GitHub Desktop.
Save felipeasimos/dd11e199b6631b0e262fb91b1bb0dd2b to your computer and use it in GitHub Desktop.
#/bin/bash
PAT=""
BROWSER="firefox"
PORT=1357
function get_javascript() {
javascript="
const key = 'numReloads';
let numReloads = localStorage.getItem(key);
numReloads = numReloads ? Number(numReloads) : 0
localStorage.setItem(key, numReloads+1);
if(numReloads & 1) {
window.location.reload();
}
console.log('numReloads: ' + numReloads)
"
javascript="${javascript//'\'/'\\'}" # replace backslash with double blackslash
javascript="${javascript//$'\n'/'\n'}" # replace newlines with '\n'
javascript="${javascript//'"'/'\"'}" # replace quotes with '\"'
echo ${javascript}
}
function get_css() {
css="
html {
font-family: 'Montserrat', sans-serif;
line-height: 25px;
color: #e6edf3;
}
body {
margin: 16px;
padding: 30px;
background-color: #0d1117;
border: 1px solid #30363d;
border-radius: 5px;
}
code {
background-color: #343941;
border-radius: 5px;
padding: 3px;
}
a {
text-decoration: none;
color: #2f81f7
}
blockquote {
border-left: 4px solid #ccc;
border-color: #30363d;
margin: 1.5em 0px;
padding: 0.2em 16px;
}
blockquote p {
display: inline;
color: #707782;
}
details ul li {
list-style-type: none;
}
pre {
background-color: #161b22;
padding: 10px;
}
table {
border-collapse: collapse;
}
td {
border: 1px solid #30363d;
padding: 5px;
}
th {
border: 1px solid #30363d;
padding-inline: 10px;
}
tbody tr:nth-child(even) {
background-color: #161b22;
}
"
echo ${css}
}
function get_head() {
css="$(get_css)"
html_head="
<meta charset='utf-8'/>
<style>
${css}
</style>
"
echo ${html_head}
}
function to_html() {
markdown="$(cat)" # get pipe input
markdown="${markdown//'\'/'\\'}" # replace backslash with double blackslash
markdown="${markdown//$'\n'/'\n'}" # replace newlines with '\n'
markdown="${markdown//'"'/'\"'}" # replace quotes with '\"'
curl -L -s \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${PAT}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/markdown \
-d "{\"text\":\"${markdown}\"}"
}
function get_last_modified_time() {
stat -c %Y "$1"
}
FILE="$1"
function get_html() {
html="$(cat ${FILE} | to_html)"
javascript="$(get_javascript)"
css="$(get_css)"
html_head="$(get_head)"
echo "
<!DOCTYPE html>
<html>
<head>
${html_head}
</head>
<body>
${html}
</body>
<script>
${javascript}
</script>
</html>"
# if [ ${last_modified} == "$(get_last_modified_time ${FILE})" ]; then
# exit 0
# fi
# last_modified="$(get_last_modified_time ${FILE})"
# html=$(cat ${FILE} | to_html)
# if [ ! $? ]; then
# echo ${html}
# exit 1
# fi
# echo ${html}
}
${BROWSER} "http://localhost:${PORT}"
while true; do
echo -e "HTTP/1.1 200 OK\n\n $(get_html)" | nc -l -N -p ${PORT}
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment