From ae73db13d4edcd717ed82c8f91a17b423d9cd50d Mon Sep 17 00:00:00 2001 From: Augusto Gunsch Date: Sat, 15 Jan 2022 16:55:28 -0300 Subject: [PATCH] Improve code --- .gitignore | 4 +- generate.py | 135 +++++++++++++++++++++++++------------------ index.html | 14 ----- templates/dir.html | 18 ------ templates/file.html | 4 +- templates/index.html | 16 +++++ 6 files changed, 100 insertions(+), 91 deletions(-) delete mode 100644 index.html delete mode 100644 templates/dir.html create mode 100644 templates/index.html diff --git a/.gitignore b/.gitignore index 695016d..7e4951f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ venv/ -input_files/ -Files/ +input/ +out/ diff --git a/generate.py b/generate.py index a107f58..a44c7d9 100755 --- a/generate.py +++ b/generate.py @@ -1,80 +1,105 @@ #!python import os +import re import shutil import pdfkit from markdown2 import markdown -input_ = 'input_files' +input_ = 'input' +outroot = 'out' output = 'Files' templates = 'templates' - -def render_template(template, **kwargs): - expanded = template[:] - - for var, val in kwargs.items(): - expanded = expanded.replace('${%s}' % var, val) - - return expanded +shutil.rmtree(outroot, ignore_errors=True) +os.mkdir(outroot) +shutil.copy(templates + '/stylesheet.css', outroot + '/stylesheet.css') with open(templates + '/file.html', 'r') as template: file_template = template.read() -with open(templates + '/dir.html', 'r') as template: - dir_template = template.read() +with open(templates + '/index.html', 'r') as template: + 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 += """ + + """ + + pdfkit.from_string(content, self.outdir + '/' + self.pdf) + + def write(self): + self.write_html() + self.write_pdf() + + +toc = '' - new_file = render_template(file_template, - title=pretty_name, - path=outroot.replace('_', ' ') + '/' + pretty_name, - pdf=pdf, - content=content) - content += '' - - pdfkit.from_string(content, outroot + '/' + pdf) - - f.write(new_file) - - index_html = '' - - with open(outroot + '/index.html', 'w') as f: - pretty_outroot = outroot.replace('_', ' ') - - f.write(render_template(dir_template, - title=pretty_outroot, - path=pretty_outroot, - content=index_html)) +with open(outroot + '/index.html', 'w') as f: + f.write(render_template(index_template, + toc=toc)) diff --git a/index.html b/index.html deleted file mode 100644 index 1671bda..0000000 --- a/index.html +++ /dev/null @@ -1,14 +0,0 @@ - - - - - - Cabinet - - -
-

Cabinet file manager

- Explore -
- - diff --git a/templates/dir.html b/templates/dir.html deleted file mode 100644 index 318b850..0000000 --- a/templates/dir.html +++ /dev/null @@ -1,18 +0,0 @@ - - - - - - ${title} - - -
-

Cabinet file manager

- ← Back - ${path} -
-
- ${content} -
- - diff --git a/templates/file.html b/templates/file.html index 93cbbd5..d50afd7 100644 --- a/templates/file.html +++ b/templates/file.html @@ -2,14 +2,14 @@ - + ${title}

Cabinet file manager

- ← Back + ← Back ${path} ↓ PDF
diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..67d7b2a --- /dev/null +++ b/templates/index.html @@ -0,0 +1,16 @@ + + + + + + Cabinet + + +
+

Cabinet file manager

+
+ + ${toc} + + +