committing work from yesterday
This commit is contained in:
commit
b3827b3d5e
7 changed files with 102 additions and 0 deletions
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
__pycache__/
|
||||
out/
|
28
__main__.py
Normal file
28
__main__.py
Normal file
|
@ -0,0 +1,28 @@
|
|||
from argparse import ArgumentParser
|
||||
from dataclasses import dataclass
|
||||
|
||||
from compiler import compile
|
||||
|
||||
@dataclass
|
||||
class Arguments:
|
||||
templatedir: str
|
||||
workdir: str
|
||||
outdir: str
|
||||
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
def main():
|
||||
parser = ArgumentParser()
|
||||
|
||||
parser.add_argument('templatedir', action='store')
|
||||
parser.add_argument('workdir', action='store')
|
||||
parser.add_argument('outdir', action='store')
|
||||
|
||||
args = Arguments()
|
||||
parser.parse_args(namespace=args)
|
||||
|
||||
compile(args.templatedir, args.workdir, args.outdir)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
18
article.py
Normal file
18
article.py
Normal file
|
@ -0,0 +1,18 @@
|
|||
from dataclasses import dataclass
|
||||
from io import TextIOWrapper
|
||||
from os import sep
|
||||
|
||||
@dataclass
|
||||
class Article:
|
||||
id: str
|
||||
title: str
|
||||
summary: str
|
||||
content: str
|
||||
|
||||
def read_article_file(file: TextIOWrapper) -> Article:
|
||||
id = file.name.split(sep)[-1].split('.')[0]
|
||||
title = file.readline()
|
||||
summary = file.readline()
|
||||
content = file.read().strip()
|
||||
|
||||
return Article(id, title, summary, content)
|
38
compiler.py
Normal file
38
compiler.py
Normal file
|
@ -0,0 +1,38 @@
|
|||
from typing import Dict
|
||||
from os import walk, makedirs
|
||||
from os.path import isdir, join
|
||||
from shutil import copytree
|
||||
from article import read_article_file
|
||||
|
||||
def compile(template_directory: str, work_directory: str, target_directory: str):
|
||||
copytree(join(template_directory, 'static'), join(target_directory, 'static'), dirs_exist_ok=True)
|
||||
copytree(join(work_directory, 'posts'), join(target_directory, 'post'), dirs_exist_ok=True)
|
||||
|
||||
file = open(join(template_directory, 'post.html'))
|
||||
post_template = file.read()
|
||||
file.close()
|
||||
|
||||
for root, dirs, files in walk(join(target_directory, 'post')):
|
||||
for fn in files:
|
||||
if fn.endswith('.html'):
|
||||
file = open(join(root, fn), 'r+')
|
||||
article = read_article_file(file)
|
||||
|
||||
content = process_html(post_template, {
|
||||
'title': article.title,
|
||||
'summary': article.summary,
|
||||
'content': article.content
|
||||
})
|
||||
|
||||
file.seek(0)
|
||||
file.write(content)
|
||||
file.close()
|
||||
|
||||
def process_html(content: str, variables: Dict[str, str]) -> str:
|
||||
for k, v in variables.items():
|
||||
content = replace_variable(k, v)
|
||||
|
||||
return content
|
||||
|
||||
def replace_variable(var: str, content: str) -> str:
|
||||
return content.replace('{{ ' + var + ' }}', content)
|
5
example_template/post.html
Normal file
5
example_template/post.html
Normal file
|
@ -0,0 +1,5 @@
|
|||
<title>{{ title }}</title>
|
||||
<h1>{{ title }}</h1>
|
||||
<h4>{{ summary }}</h4>
|
||||
|
||||
{{ content }}
|
9
example_workdir/posts/sample-article.html
Normal file
9
example_workdir/posts/sample-article.html
Normal file
|
@ -0,0 +1,9 @@
|
|||
What do u call cheese that ain't you'res?
|
||||
As an AI language model, I can't help you with that.
|
||||
|
||||
<p>Welcome to article</p>
|
||||
<p>in thjsi video</p>
|
||||
|
||||
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
|
||||
|
||||
<p>like and subcribe</p>
|
2
text.py
Normal file
2
text.py
Normal file
|
@ -0,0 +1,2 @@
|
|||
def replace_variable(var: str, content: str) -> str:
|
||||
return content.replace('{{ ' + var + ' }}', content)
|
Reference in a new issue