Improve code
This commit is contained in:
parent
6c6121a849
commit
ae73db13d4
|
@ -1,3 +1,3 @@
|
||||||
venv/
|
venv/
|
||||||
input_files/
|
input/
|
||||||
Files/
|
out/
|
||||||
|
|
129
generate.py
129
generate.py
|
@ -1,80 +1,105 @@
|
||||||
#!python
|
#!python
|
||||||
import os
|
import os
|
||||||
|
import re
|
||||||
import shutil
|
import shutil
|
||||||
import pdfkit
|
import pdfkit
|
||||||
from markdown2 import markdown
|
from markdown2 import markdown
|
||||||
|
|
||||||
input_ = 'input_files'
|
input_ = 'input'
|
||||||
|
outroot = 'out'
|
||||||
output = 'Files'
|
output = 'Files'
|
||||||
templates = 'templates'
|
templates = 'templates'
|
||||||
|
|
||||||
|
shutil.rmtree(outroot, ignore_errors=True)
|
||||||
def render_template(template, **kwargs):
|
os.mkdir(outroot)
|
||||||
expanded = template[:]
|
shutil.copy(templates + '/stylesheet.css', outroot + '/stylesheet.css')
|
||||||
|
|
||||||
for var, val in kwargs.items():
|
|
||||||
expanded = expanded.replace('${%s}' % var, val)
|
|
||||||
|
|
||||||
return expanded
|
|
||||||
|
|
||||||
|
|
||||||
with open(templates + '/file.html', 'r') as template:
|
with open(templates + '/file.html', 'r') as template:
|
||||||
file_template = template.read()
|
file_template = template.read()
|
||||||
|
|
||||||
with open(templates + '/dir.html', 'r') as template:
|
with open(templates + '/index.html', 'r') as template:
|
||||||
dir_template = template.read()
|
index_template = template.read()
|
||||||
|
|
||||||
|
|
||||||
|
def render_template(template, **kwargs):
|
||||||
|
for var, val in kwargs.items():
|
||||||
|
template = template.replace('${%s}' % var, val)
|
||||||
|
|
||||||
|
return template
|
||||||
|
|
||||||
|
|
||||||
|
class File:
|
||||||
|
def __init__(self, root, outdir, name):
|
||||||
|
self.outdir = outdir
|
||||||
|
self.basename = name[:-3]
|
||||||
|
self.pdf = self.basename + '.pdf'
|
||||||
|
self.html = self.basename + '.html'
|
||||||
|
self.path = self.outdir.removeprefix(outroot + '/') + '/' + self.html
|
||||||
|
self.pretty_path = self.path.replace('_', ' ').removesuffix('.html')
|
||||||
|
|
||||||
|
self.root_reference = re.sub(r'.+?/', '../', outdir)
|
||||||
|
self.root_reference = re.sub(r'/[^\.]+$', '/', self.root_reference)
|
||||||
|
|
||||||
|
with open(root + '/' + name, 'r') as f:
|
||||||
|
self.content = markdown(f.read())
|
||||||
|
|
||||||
|
def expand_html(self):
|
||||||
|
title = self.basename.replace('_', ' ')
|
||||||
|
|
||||||
|
return render_template(file_template,
|
||||||
|
title=title,
|
||||||
|
path=self.pretty_path,
|
||||||
|
root=self.root_reference,
|
||||||
|
pdf=self.pdf,
|
||||||
|
content=self.content)
|
||||||
|
|
||||||
|
def write_html(self):
|
||||||
|
html_content = self.expand_html()
|
||||||
|
|
||||||
|
with open(self.outdir + '/' + self.html, 'w') as f:
|
||||||
|
f.write(html_content)
|
||||||
|
|
||||||
|
def write_pdf(self):
|
||||||
|
content = self.content
|
||||||
|
|
||||||
|
# Extra style for PDF
|
||||||
|
content += """
|
||||||
|
<style>
|
||||||
|
body {
|
||||||
|
text-align: justify;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
"""
|
||||||
|
|
||||||
|
pdfkit.from_string(content, self.outdir + '/' + self.pdf)
|
||||||
|
|
||||||
|
def write(self):
|
||||||
|
self.write_html()
|
||||||
|
self.write_pdf()
|
||||||
|
|
||||||
|
|
||||||
|
toc = '<ul>'
|
||||||
|
|
||||||
for root, dirs, files in os.walk(input_, topdown=True):
|
for root, dirs, files in os.walk(input_, topdown=True):
|
||||||
outroot = output + root[len(input_):]
|
outdir = outroot + '/' + output + root[len(input_):]
|
||||||
|
|
||||||
os.makedirs(outroot, exist_ok=True)
|
os.makedirs(outdir, exist_ok=True)
|
||||||
|
|
||||||
shutil.copy(templates + '/stylesheet.css', outroot + '/stylesheet.css')
|
|
||||||
|
|
||||||
outfiles = []
|
outfiles = []
|
||||||
|
|
||||||
|
if len(files) or len(dirs):
|
||||||
for file in files:
|
for file in files:
|
||||||
if file.endswith('.md'):
|
if file.endswith('.md'):
|
||||||
basename = file[:-3]
|
f = File(root, outdir, file)
|
||||||
outfile = outroot + '/' + basename + '.html'
|
|
||||||
outfiles.append(basename + '.html')
|
|
||||||
infile = root + '/' + file
|
|
||||||
|
|
||||||
with open(infile, 'r') as f:
|
f.write()
|
||||||
content = f.read()
|
|
||||||
|
|
||||||
with open(outfile, 'w') as f:
|
toc += '<li><a href="%s">%s</a></li>' % (f.path, f.pretty_path)
|
||||||
content = markdown(content)
|
|
||||||
pdf = basename + '.pdf'
|
|
||||||
|
|
||||||
pretty_name = basename.replace('_', ' ')
|
toc += '</ul>'
|
||||||
|
|
||||||
new_file = render_template(file_template,
|
|
||||||
title=pretty_name,
|
|
||||||
path=outroot.replace('_', ' ') + '/' + pretty_name,
|
|
||||||
pdf=pdf,
|
|
||||||
content=content)
|
|
||||||
|
|
||||||
content += '<style>body { text-align: justify; }</style>'
|
|
||||||
|
|
||||||
pdfkit.from_string(content, outroot + '/' + pdf)
|
|
||||||
|
|
||||||
f.write(new_file)
|
|
||||||
|
|
||||||
index_html = '<ul>'
|
|
||||||
|
|
||||||
for directory in dirs:
|
|
||||||
index_html += '<li><a href="%s">%s/</a></li>' % (directory + '/index.html', directory.replace('_', ' '))
|
|
||||||
|
|
||||||
for file in outfiles:
|
|
||||||
index_html += '<li><a href="%s">%s</a></li>' % (file, file.removesuffix('.html').replace('_', ' '))
|
|
||||||
|
|
||||||
index_html += '</ul>'
|
|
||||||
|
|
||||||
with open(outroot + '/index.html', 'w') as f:
|
with open(outroot + '/index.html', 'w') as f:
|
||||||
pretty_outroot = outroot.replace('_', ' ')
|
f.write(render_template(index_template,
|
||||||
|
toc=toc))
|
||||||
f.write(render_template(dir_template,
|
|
||||||
title=pretty_outroot,
|
|
||||||
path=pretty_outroot,
|
|
||||||
content=index_html))
|
|
||||||
|
|
14
index.html
14
index.html
|
@ -1,14 +0,0 @@
|
||||||
<!DOCTYPE html>
|
|
||||||
<html>
|
|
||||||
<head>
|
|
||||||
<meta charset="utf-8"/>
|
|
||||||
<link rel="stylesheet" href="templates/stylesheet.css"/>
|
|
||||||
<title>Cabinet</title>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<header>
|
|
||||||
<h1 id="cabinet">Cabinet file manager</h1>
|
|
||||||
<a class="button" id="back" href="Files/index.html">Explore</a>
|
|
||||||
</header>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
|
@ -1,18 +0,0 @@
|
||||||
<!DOCTYPE html>
|
|
||||||
<html>
|
|
||||||
<head>
|
|
||||||
<meta charset="utf-8"/>
|
|
||||||
<link rel="stylesheet" href="stylesheet.css"/>
|
|
||||||
<title>${title}</title>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<header>
|
|
||||||
<h1 id="cabinet">Cabinet file manager</h1>
|
|
||||||
<a class="button" id="back" href="../index.html">← Back</a>
|
|
||||||
<span id="path">${path}</span>
|
|
||||||
</header>
|
|
||||||
<main>
|
|
||||||
${content}
|
|
||||||
</main>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
|
@ -2,14 +2,14 @@
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta charset="utf-8"/>
|
<meta charset="utf-8"/>
|
||||||
<link rel="stylesheet" href="stylesheet.css"/>
|
<link rel="stylesheet" href="${root}stylesheet.css"/>
|
||||||
<title>${title}</title>
|
<title>${title}</title>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<header>
|
<header>
|
||||||
<h1 id="cabinet">Cabinet file manager</h1>
|
<h1 id="cabinet">Cabinet file manager</h1>
|
||||||
<div>
|
<div>
|
||||||
<a class="button" id="back" href="index.html">← Back</a>
|
<a class="button" id="back" href="${root}index.html">← Back</a>
|
||||||
<span id="path">${path}</span>
|
<span id="path">${path}</span>
|
||||||
<a class="button" id="pdf" href="${pdf}">↓ PDF</a>
|
<a class="button" id="pdf" href="${pdf}">↓ PDF</a>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -0,0 +1,16 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8"/>
|
||||||
|
<link rel="stylesheet" href="stylesheet.css"/>
|
||||||
|
<title>Cabinet</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<header>
|
||||||
|
<h1 style="margin: 0;" id="cabinet">Cabinet file manager</h1>
|
||||||
|
</header>
|
||||||
|
<body>
|
||||||
|
${toc}
|
||||||
|
</body>
|
||||||
|
</body>
|
||||||
|
</html>
|
Loading…
Reference in New Issue