1
0
src/homepage

112 lines
3.2 KiB
Plaintext
Raw Normal View History

2023-02-05 18:02:19 -07:00
#!/bin/sh
2023-10-14 14:43:48 -06:00
# vim: syntax=:ts=8
2024-06-15 22:07:51 -06:00
2023-02-20 08:46:03 -07:00
set -ex
2024-06-15 22:07:51 -06:00
2024-06-15 22:11:37 -06:00
printf '%s: Current working directory is %s.\n' "$0" "$PWD"
2024-06-15 22:07:51 -06:00
<"$0".content python3 -c '
2023-07-16 08:20:48 -06:00
import os, sys
2023-07-19 21:27:46 -06:00
class File:
2023-07-21 10:19:18 -06:00
attributes = []; content = ""; substitutions = dict()
figurative = True; stub = True
def addattribute(self, *args):
for a in args: # sloppy but works
if a == "stub": self.stub = True
elif a == "verbatim": self.stub = False
elif a == "figuratively": self.figurative = True
elif a == "literally": self.figurative = False
2023-07-19 21:27:46 -06:00
def __init__(self, **kwargs):
for key in kwargs:
2023-07-21 10:19:18 -06:00
if key == "attributes": self.addattribute(*kwargs[key])
else: setattr(self, key, kwargs[key])
2023-07-28 12:46:47 -06:00
files = dict()
2024-06-15 22:07:51 -06:00
for part in reversed(sys.stdin.read().split("\n\n\n")[1:]):
2023-07-19 22:46:51 -06:00
name = "." + part.split("\n")[0]
2023-07-19 21:27:46 -06:00
if "\t" in "." + name:
attributes = name.split("\t")[1].split(",")
name = name.split("\t")[0]
2023-07-19 22:46:51 -06:00
else: attributes = []
2023-07-28 12:46:47 -06:00
if len(name) <= 1 or name[1] != "/" or "ignore" in attributes:
continue
2023-07-19 22:46:51 -06:00
content = part.split("\n\n")[0].split("\n")
substitutions = dict()
if(len(content) > 1):
for s in content[1:]:
s = s.split("\t")
if len(s) == 2: substitutions[s[0]] = s[1]
2023-07-21 10:19:18 -06:00
mode = "replace"
2023-07-28 12:46:47 -06:00
for attribute in attributes:
if attribute in ["append", "replace"]:
mode = attribute
2023-07-21 10:19:18 -06:00
attributes = list(set(attributes) ^ {"append", "replace"})
2023-07-19 23:09:34 -06:00
content = part[len("\n".join(content))+2:]
2023-08-05 09:05:36 -06:00
file = File(attributes = attributes, content = content + "\n",
2023-07-19 22:46:51 -06:00
substitutions = substitutions)
2023-07-21 10:19:18 -06:00
if mode == "append":
2023-07-19 21:27:46 -06:00
if not(name in files):
sys.stderr.write(sys.argv[0] + ": " + name + ": "
+ "appending to nothing\n")
else:
2023-07-19 22:46:51 -06:00
file.content = files[name].content + file.content
files[name] = file
2023-07-19 21:27:46 -06:00
for name in files:
2023-07-21 10:19:18 -06:00
if files[name].stub:
2023-07-28 12:46:47 -06:00
p = ""; s = ""; d = name
while True:
2023-08-03 17:15:58 -06:00
d = os.path.dirname(d)
if (p == ""
and os.path.join(d, "Prefix")
in files.keys()):
p = files[os.path.join(d, "Prefix")].content
if (s == ""
and os.path.join(d, "Suffix")
in files.keys()):
s = files[os.path.join(d, "Suffix")].content
2023-08-05 09:05:36 -06:00
if d == "." or (not(p == "") and not(s == "")):
break
2023-07-28 12:46:47 -06:00
files[name].content = p + files[name].content + s
2023-07-21 10:19:18 -06:00
if files[name].figurative:
2023-07-19 21:27:46 -06:00
content = files[name].content
for s in files[name].substitutions:
instances = []
i = 0
while True:
instance = content.find(s, i)
if instance == -1: break
2023-07-19 22:46:51 -06:00
instances += [instance]
2023-07-19 21:27:46 -06:00
i = instance + len(s)
if len(instances) == 0: continue
2023-07-19 22:46:51 -06:00
for i in reversed(instances):
content = (content[:i]
+ files[name].substitutions[s]
2023-07-19 21:27:46 -06:00
+ content[i+len(s):])
files[name].content = content
2023-07-28 12:46:47 -06:00
# TODO error checking
if not(os.path.isdir(os.path.dirname(name))):
os.makedirs(os.path.dirname(name))
2023-07-19 22:46:51 -06:00
with open(name, "w") as fd: fd.write(files[name].content)
2023-08-03 17:15:58 -06:00
d = ""; bucket = "#!/bin/sh\n"
for name in files:
d = name
while True:
if os.path.dirname(d) == ".":
mop = ("rm "
+ "-r " * os.path.isdir(d)
+ name # yeah this sucks
+ "\n"
)
if not(mop in bucket): bucket += mop
break
else:
d = os.path.dirname(d)
if len(bucket.split("\n")) > 2:
2023-07-19 21:27:46 -06:00
with open("./cleanup.sh", "w") as fd:
2023-08-03 17:15:58 -06:00
fd.write(bucket)
2023-07-19 21:27:46 -06:00
'
2024-06-15 22:07:51 -06:00
test -x "$0".local \
&& exec ./"$0".local \
|| test -e "$0".local \
&& exec sh "$0".local \
2023-07-15 21:44:35 -06:00
|| exit 0