__sssstatic/gen.py
2023-09-12 08:36:35 +02:00

370 lines
10 KiB
Python

#!/usr/bin/env python
import argparse, json, sys, markdown, shutil, json, re, pathlib, shutil
import utils.imgconv
input_dir = None
output_dir = None
style = None
(W, H, FMT) = (None, None, None)
codebase = pathlib.Path(__file__).parent.absolute()
def translate_index_header(txt, dirname):
sections = txt.count('- - -')
if sections == 0:
# not following the normal template -> direct html
txt = txt.replace('data/', dirname + '/') #<--- replace reference to data/
return txt
elif sections == 1:
[text, link] = txt.split('- - -')
text_md = markdown.markdown(text)
link_info = json.loads(link)
dirpath = pathlib.Path(dirname)
img_name = pathlib.Path(link_info['img']).name
img_path = dirpath / img_name
img_resize_path = dirpath / "img-resize" / img_name
picture_el = emit_picture(str(img_path), str(img_resize_path))
table = codebase / "templates/<table>"
with table.open(encoding="utf-8") as fp:
out = fp.read()
out = out.replace('[[text_md]]', text_md);
out = out.replace('[[link_md]]', picture_el);
if link_info['href']:
if link_info['href'] == "data":
href = dirname + "/"
else:
href = link_info['href']
out = "<a href='" + href + "'>" + out + "</a>";
return out
def escape_date(dirname):
return re.sub('^20\d{2}?.', '', dirname)
def emit_picture(src_full, src_resize):
return f'''<picture>
<source media="(max-width: 799px)" srcset="{src_resize}" />
<source media="(min-width: 800px)" srcset="{src_full}" />
<img src="{src_full}" />
</picture>'''
def emit_img(file: pathlib.Path, data_dir: pathlib.Path):
## this is where LOD can be applied
resize_dir = data_dir / "img-resize"
resize_dir.mkdir(parents=True, exist_ok=True)
filepath = data_dir / file
resize_file = utils.imgconv.convert(filepath, (W, H, FMT), resize_dir)
return f'<a href="{file}">' + emit_picture(str(file), "img-resize/" + resize_file.name) + '</a>'
def emit_video_mp4(file: pathlib.Path, data_dir: pathlib.Path):
return f'<video controls><source src="{file}" type="video/mp4"</video>'
def emit_audio(file: pathlib.Path, data_dir: pathlib.Path, full=False):
# an '.audio' file is a json file with a list of audio elements
# which need to be bundles as a type of list in a single <li>
if not file.is_file():
file = data_dir / file;
if file.is_file():
with file.open() as fp:
audio = json.loads(fp.read())
if full:
out = '<sound class="full">\n'
else:
out = "<sound>\n"
for a in audio:
out += "<track>\n"
out += "<info>\n"
out += "<name>" + a["name"] + "</name>\n"
#out += "<duration>" + a["length"] + "</duration>\n"
out += "</info>\n"
out += "<audio controls preload>\n"
# if it is a list of files
if isinstance(a["type"], list):
for t in a["type"]:
out += '<source src="' + a["file"] + '.' + t + '" type="audio/' + t + '">\n'
else:
out += '<source src="' + a["file"] + '" type="audio/' + a["type"] + '">\n'
out += "</audio>\n"
out += "</track>\n"
out += "</sound>\n"
return out
return None
def emit_audio_full(file, data_dir):
return emit_audio(file, data_dir, full=True)
def default(file, data_dir):
return None;
content_map = {
'.png': emit_img,
'.jpg': emit_img,
'.jpeg': emit_img,
'.m4v': emit_video_mp4,
'.mov': emit_video_mp4,
'.mp4': emit_video_mp4,
'.audio': emit_audio,
'.audio_full': emit_audio_full,
'.html': default,
'.txt': default
};
def index_content(dir_name: str, data_dir: pathlib.Path, index_txt: pathlib.Path, desc_txt: pathlib.Path, template: str):
print(" indexing content -- " + dir_name);
# desc_txt is a markdown file containing description
# for the project - no layout applied to it, only md
desc_md = None
if desc_txt.is_file():
with desc_txt.open(encoding="utf-8") as fp:
desc_md = markdown.markdown(fp.read());
# index_txt is a json file containing one thing:
# an array of files names or glob patterns
content_index = None
if index_txt.is_file():
with index_txt.open(encoding="utf-8") as fp:
content_index = json.loads(fp.read());
if desc_md is None and content_index is None:
return;
content = ""
if desc_md:
content += "<li>" + "<desc>" + "\n" + desc_md + "</desc>" + "</li>" + "\n";
if content_index:
files = [];
for i in content_index:
f = data_dir / i
if f.is_file():
files.append(data_dir / i);
else:
files += [pathlib.Path(a.name) for a in list(data_dir.glob(i))]
# sort files?
files.sort()
for j in files:
ext = j.suffix.lower()
if ext in content_map:
element = content_map[ext](j, data_dir);
if element:
content += "<li>" + element + "</li>" + "\n";
# check if there is an html file existing already. if so use that one.
html = template.replace('[[content]]', content).replace('[[dir]]', str(dir_name));
try:
out = data_dir / 'index.html'
with out.open('w', encoding="utf-8") as fp:
fp.write(html);
return out
except:
print('error creating content index output file. aborting...');
return None
def check_dir_exists(dirpath, create=False):
d = pathlib.Path(dirpath)
if d.is_dir():
return d
elif create:
print(f"Directory {dirpath} is not a directory... Create it? [y/n]")
x = input()
if x.lower() == "y":
d.mkdir(parents=True)
return d
return None
if __name__ == '__main__':
p = argparse.ArgumentParser(description="SSSSTATIC generates html files from directory contents")
p.add_argument('--input', "-i", help='input directory');
p.add_argument('--output', "-o", help='output directory');
p.add_argument('--style', "-y", default="+++", help='css style, ico, etc.');
p.add_argument("--settings", "-s", default=".static/settings.json", help="Settings file (default: .static/settings.json")
p.add_argument('--format', '-f', default="1000x1000", help='img output formats (ex. 1000x)');
p.add_argument('--clean', help='cleans the output directory');
args = p.parse_args();
if args.settings == ".static/settings.json":
file_settings = codebase / ".static/settings.json"
else:
file_settings = pathlib.Path(args.settings)
if file_settings.exists() and file_settings.is_file():
with file_settings.open(encoding="utf-8") as fp:
settings = json.load(fp)
input_dir = check_dir_exists(settings['inputdir'])
output_dir = check_dir_exists(settings['outputdir'])
style = settings['style']
if args.input:
inputdir = check_dir_exists(args.input)
if inputdir is not None:
input_dir = inputdir;
else:
sys.exit(f"Input directory {args.input} is not a directory... Aborting.")
if args.output:
outputdir = check_dir_exists(args.output, create=True)
if outputdir is not None:
output_dir = outputdir;
else:
sys.exit(f"Output directory {args.output} is not a directory... Aborting.")
if args.format:
(W, H, FMT) = utils.imgconv.parse_format(args.format)
if not FMT:
sys.exit(f"Format {str(args.format)} is not valid. Aborting.")
if style is not None:
style_dir = codebase / f"styles/{style}"
if not style_dir.is_dir():
style = args.style
if input_dir is None:
sys.exit(f"Error with input directory... Aborting.")
if output_dir is None:
print(f"No output directory.")
print(f"Dry-run test? [y/n]")
x = input()
if x.lower() == "n":
sys.exit(1)
else:
if output_dir.exists() and any(output_dir.iterdir()):
print(f"? {output_dir} exists and is not empty. Erase content? [y/n]")
x = input()
if x.lower() == "y":
try:
shutil.rmtree(output_dir)
except:
print(f"error erasing {output_dir}. Continuing.")
print('1/3 - Configuring');
# main index template
indx_template = codebase / "templates/index_template.html"
with indx_template.open(encoding="utf-8") as fp:
indx_template_str = fp.read()
# images index template
section_indx_template = codebase / "templates/index_apache_template.html"
with section_indx_template.open(encoding="utf-8") as fp:
section_indx_template_str = fp.read()
print('2/3 - Parsing input');
dirs = sorted([d for d in input_dir.iterdir() if d.is_dir()])
content = ''
for d in reversed(dirs):
indx_txt = d / 'index.txt'
if not indx_txt.exists():
print(f"{indx_txt} does not exists. Skipping.")
continue
with indx_txt.open(encoding="utf-8") as fp:
txt = fp.read()
dirname = escape_date(d.name)
content += translate_index_header(txt, dirname);
datadir = d / "data"
if datadir.is_dir():
section_indx_txt = datadir / "index.txt"
section_desc_txt = datadir / "desc.md"
out_index = None
if section_indx_txt.is_file() or section_desc_txt.is_file():
out_index = index_content(dirname, datadir, section_indx_txt, section_desc_txt, section_indx_template_str)
if output_dir is not None:
out_datadir = output_dir / dirname
if out_datadir.exists() and any(out_datadir.iterdir()):
print(f" ? {out_datadir} exists and is not empty. Erase content? [y/n]")
x = input()
if x.lower() == "y":
try:
shutil.rmtree(out_datadir)
except:
print(f"error erasing {out_datadir}. Continuing.")
try:
shutil.copytree(datadir, out_datadir, ignore=shutil.ignore_patterns('.DS_Store'), dirs_exist_ok=True)
except:
print(f"error copying {datadir} to {out_datadir}. Continuing.")
continue
# copy index after (since index.html might be lurking in content dirs)
if out_index:
try:
shutil.copy2(out_index, out_datadir)
except:
print(f"error copying {out_index} to {out_datadir}. Continuing.")
continue
print('3/3 - Generating ouput');
html = indx_template_str.replace('[[content]]', content);
if output_dir is not None:
out = output_dir / "index.html"
out.write_text(html, encoding="utf-8")
# style
style_dir = codebase / f"styles/{style}"
if not style_dir.is_dir():
print(f"{style} does not exists (make sure to add a style to styles/)... going vanilla")
else:
try:
out_style_dir = output_dir / style
shutil.copytree(style_dir, out_style_dir, ignore=shutil.ignore_patterns('.DS_Store'), dirs_exist_ok=True)
except:
print(f"error copying {style_dir} to {out_style_dir}. Continuing.")
# robots.txt?
robots = codebase / "templates/robots.txt"
if robots.is_file():
try:
shutil.copy(robots, output_dir)
except:
print(f"error copying {robots} to {output_dir}. Continuing.")
# .htaccess?
ht = codebase / "templates/.htaccess"
if ht.is_file():
try:
shutil.copy(ht, output_dir)
except:
print(f"error copying {ht} to {output_dir}. Continuing.")
print('done.');