m15o.net notes/quine

NAME

quine - self-reproducing program

DESCRIPTION

A quine is a program that outputs its own source code.

The state of a program changes as it runs. Sometimes the program does too. A save file then depends on the exact program that wrote it, and no other version is guaranteed to open it. A quine removes the dependency by keeping code and data in one self-contained file.

EXAMPLES

A simple HTML quine

Here's an example of how I usually structure quines:

<textarea></textarea>
<script type="application/json">{"text":""}</script>
<script>
$ = s => document.querySelector(s), src = $("script").textContent, page = document.documentElement.outerHTML,
save = () => page.replace(src, () => JSON.stringify({ text: $("textarea").value }).replace(/<\//g, "<\\/"))
$("textarea").value = JSON.parse(src).text
</script>

The state gets its own script tag. Saving is replacing the previous state by the new one, directly taken by looking up the value of the textarea.

REF

bin/wombat, srv/moka