mirror of
https://github.com/alvierahman90/gronk.git
synced 2024-11-22 07:19:53 +00:00
clean up code
This commit is contained in:
parent
9c6b2247a7
commit
444c777135
92
gronk.py
92
gronk.py
@ -16,7 +16,6 @@ import regex as re
|
|||||||
import pprint
|
import pprint
|
||||||
|
|
||||||
import frontmatter
|
import frontmatter
|
||||||
import git
|
|
||||||
import jinja2
|
import jinja2
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
@ -39,7 +38,6 @@ JINJA_TEMPLATE_ARTICLE = JINJA_ENV.get_template("article.html")
|
|||||||
JINJA_TEMPLATE_PERMALINK = JINJA_ENV.get_template("permalink.html")
|
JINJA_TEMPLATE_PERMALINK = JINJA_ENV.get_template("permalink.html")
|
||||||
|
|
||||||
LICENSE = None
|
LICENSE = None
|
||||||
GIT_REPO = None
|
|
||||||
FILEMAP = None
|
FILEMAP = None
|
||||||
|
|
||||||
|
|
||||||
@ -232,8 +230,8 @@ def update_required(src_filepath, output_filepath):
|
|||||||
check if file requires an update,
|
check if file requires an update,
|
||||||
return boolean
|
return boolean
|
||||||
"""
|
"""
|
||||||
return not output_filepath.exists() or src_filepath.stat().st_mtime > output_filepath.stat().st_mtimeme()
|
return not output_filepath.exists() or src_filepath.stat(
|
||||||
|
).st_mtime > output_filepath.stat().st_mtimeme()
|
||||||
|
|
||||||
|
|
||||||
def get_args():
|
def get_args():
|
||||||
@ -242,8 +240,13 @@ def get_args():
|
|||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument('notes', type=Path)
|
parser.add_argument('notes', type=Path)
|
||||||
parser.add_argument('-o', '--output-dir', type=Path, default='web')
|
parser.add_argument('-o', '--output-dir', type=Path, default='web')
|
||||||
parser.add_argument('-F', '--force', action="store_true",
|
parser.add_argument(
|
||||||
help="Generate new output html even if source file was modified before output html")
|
'-F',
|
||||||
|
'--force',
|
||||||
|
action="store_true",
|
||||||
|
help=
|
||||||
|
"Generate new output html even if source file was modified before output html"
|
||||||
|
)
|
||||||
return parser.parse_args()
|
return parser.parse_args()
|
||||||
|
|
||||||
|
|
||||||
@ -273,18 +276,11 @@ def render_plaintext_file(input_filepath):
|
|||||||
return list of tuple of output filepath, empty dict
|
return list of tuple of output filepath, empty dict
|
||||||
"""
|
"""
|
||||||
|
|
||||||
with open(input_filepath, encoding='utf-8') as file_pointer:
|
raw_content = input_filepath.read_text()
|
||||||
raw_content = file_pointer.read()
|
|
||||||
|
|
||||||
properties = FILEMAP.get(input_filepath)
|
properties = FILEMAP.get(input_filepath)
|
||||||
|
html = JINJA_TEMPLATE_TEXTARTICLE.render(license=LICENSE, **properties)
|
||||||
html = JINJA_TEMPLATE_TEXTARTICLE.render(license = LICENSE, **properties)
|
properties['dst_path']['raw'].write_text(raw_content)
|
||||||
|
properties['dst_path']['html'].write_text(html)
|
||||||
with open(properties['dst_path']['raw'], "w+", encoding='utf-8') as file_pointer:
|
|
||||||
file_pointer.write(raw_content)
|
|
||||||
|
|
||||||
with open(properties['dst_path']['html'], "w+", encoding='utf-8') as file_pointer:
|
|
||||||
file_pointer.write(html)
|
|
||||||
|
|
||||||
|
|
||||||
def render_generic_file(input_filepath):
|
def render_generic_file(input_filepath):
|
||||||
@ -331,38 +327,28 @@ def render_markdown(content):
|
|||||||
'standalone': False,
|
'standalone': False,
|
||||||
}
|
}
|
||||||
|
|
||||||
headers = {
|
headers = {'Accept': 'application/json'}
|
||||||
'Accept': 'application/json'
|
|
||||||
}
|
|
||||||
|
|
||||||
response = requests.post(
|
response = requests.post(PANDOC_SERVER_URL,
|
||||||
PANDOC_SERVER_URL,
|
|
||||||
headers=headers,
|
headers=headers,
|
||||||
json=post_body,
|
json=post_body,
|
||||||
timeout=PANDOC_TIMEOUT
|
timeout=PANDOC_TIMEOUT)
|
||||||
)
|
|
||||||
|
|
||||||
response = response.json()
|
response = response.json()
|
||||||
|
|
||||||
|
|
||||||
# TODO look at response['messages'] and log them maybe?
|
# TODO look at response['messages'] and log them maybe?
|
||||||
# https://github.com/jgm/pandoc/blob/main/doc/pandoc-server.md#response
|
# https://github.com/jgm/pandoc/blob/main/doc/pandoc-server.md#response
|
||||||
|
|
||||||
return response['output']
|
return response['output']
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def process_home_index(args, notes_git_head_sha1=None):
|
def process_home_index(args, notes_git_head_sha1=None):
|
||||||
"""
|
"""
|
||||||
create home index.html in output_dir
|
create home index.html in output_dir
|
||||||
"""
|
"""
|
||||||
|
|
||||||
post = {
|
post = {'title': 'gronk', 'content': ''}
|
||||||
'title': 'gronk',
|
|
||||||
'content': ''
|
|
||||||
}
|
|
||||||
custom_content_file = args.notes.joinpath('index.md')
|
custom_content_file = args.notes.joinpath('index.md')
|
||||||
print(f'{custom_content_file=}')
|
|
||||||
if custom_content_file.is_file():
|
if custom_content_file.is_file():
|
||||||
fmpost = frontmatter.loads(custom_content_file.read_text()).to_dict()
|
fmpost = frontmatter.loads(custom_content_file.read_text()).to_dict()
|
||||||
for key, val in fmpost.items():
|
for key, val in fmpost.items():
|
||||||
@ -371,11 +357,10 @@ def process_home_index(args, notes_git_head_sha1=None):
|
|||||||
post['content'] = render_markdown(post['content'])
|
post['content'] = render_markdown(post['content'])
|
||||||
|
|
||||||
html = JINJA_TEMPLATE_HOME_INDEX.render(
|
html = JINJA_TEMPLATE_HOME_INDEX.render(
|
||||||
gronk_commit = GRONK_COMMIT,
|
gronk_commit=GRONK_COMMIT,
|
||||||
search_data = FILEMAP.to_search_data(),
|
search_data=FILEMAP.to_search_data(),
|
||||||
notes_git_head_sha1 = notes_git_head_sha1,
|
notes_git_head_sha1=notes_git_head_sha1,
|
||||||
post=post
|
post=post)
|
||||||
)
|
|
||||||
|
|
||||||
args.output_dir.joinpath('index.html').write_text(html)
|
args.output_dir.joinpath('index.html').write_text(html)
|
||||||
|
|
||||||
@ -398,36 +383,47 @@ def generate_tag_browser(output_dir) :
|
|||||||
|
|
||||||
tags[tag].append(post)
|
tags[tag].append(post)
|
||||||
|
|
||||||
|
|
||||||
for tag, index_entries in tags.items():
|
for tag, index_entries in tags.items():
|
||||||
output_file = output_dir.joinpath(tag, 'index.html')
|
output_file = output_dir.joinpath(tag, 'index.html')
|
||||||
output_file.parent.mkdir(exist_ok=True, parents=True)
|
output_file.parent.mkdir(exist_ok=True, parents=True)
|
||||||
output_file.write_text(JINJA_TEMPLATE_INDEX.render(
|
output_file.write_text(
|
||||||
|
JINJA_TEMPLATE_INDEX.render(
|
||||||
|
gronk_commit=GRONK_COMMIT,
|
||||||
automatic_index=True,
|
automatic_index=True,
|
||||||
search_bar=True,
|
search_bar=True,
|
||||||
title=tag,
|
title=tag,
|
||||||
index_entries = index_entries
|
index_entries=[{
|
||||||
|
'title': entry.get('title', ''),
|
||||||
|
'is_dir': entry.get('is_dir', False),
|
||||||
|
'path': str(entry.get('path', Path(''))),
|
||||||
|
} for entry in index_entries],
|
||||||
))
|
))
|
||||||
|
|
||||||
output_file = output_dir.joinpath('index.html')
|
output_file = output_dir.joinpath('index.html')
|
||||||
output_file.parent.mkdir(exist_ok=True, parents=True)
|
output_file.parent.mkdir(exist_ok=True, parents=True)
|
||||||
output_file.write_text(JINJA_TEMPLATE_INDEX.render(
|
output_file.write_text(
|
||||||
automatic_index=True,
|
JINJA_TEMPLATE_INDEX.render(automatic_index=True,
|
||||||
|
gronk_commit=GRONK_COMMIT,
|
||||||
search_bar=True,
|
search_bar=True,
|
||||||
title='tags',
|
title='tags',
|
||||||
index_entries = [{ 'path': tag, 'title': tag, 'is_dir': False, } for tag in tags.keys()]
|
index_entries=[{
|
||||||
))
|
'path': tag,
|
||||||
|
'title': tag,
|
||||||
|
'is_dir': False,
|
||||||
|
} for tag in tags.keys()]))
|
||||||
|
|
||||||
|
|
||||||
def main(args):
|
def main(args):
|
||||||
""" Entry point for script """
|
""" Entry point for script """
|
||||||
|
|
||||||
global LICENSE
|
global LICENSE
|
||||||
global GIT_REPO
|
|
||||||
global FILEMAP
|
global FILEMAP
|
||||||
|
|
||||||
FILEMAP = FileMap(args.notes, args.output_dir.joinpath('notes'))
|
FILEMAP = FileMap(args.notes, args.output_dir.joinpath('notes'))
|
||||||
|
|
||||||
|
# TODO have some sort of 'site rebuild in progress - come back in a minute
|
||||||
|
# or two!' or auto checking/refreshing page for when site is being built
|
||||||
|
|
||||||
if args.output_dir.is_file():
|
if args.output_dir.is_file():
|
||||||
print(f"Output directory ({args.output_dir}) cannot be a file.")
|
print(f"Output directory ({args.output_dir}) cannot be a file.")
|
||||||
|
|
||||||
@ -436,15 +432,11 @@ def main(args):
|
|||||||
# attempt to get licensing information
|
# attempt to get licensing information
|
||||||
license_path = args.notes.joinpath("LICENSE")
|
license_path = args.notes.joinpath("LICENSE")
|
||||||
if license_path.exists():
|
if license_path.exists():
|
||||||
with open(license_path, encoding='utf-8') as file_pointer:
|
LICENSE = license_path.read_text()
|
||||||
LICENSE = file_pointer.read()
|
|
||||||
|
|
||||||
# create git.Repo object if notes dir is a git repo
|
|
||||||
# TODO git commit log integration
|
# TODO git commit log integration
|
||||||
if '.git' in args.notes.iterdir():
|
|
||||||
GIT_REPO = git.Repo(args.notes)
|
|
||||||
|
|
||||||
for root_str, subdirectories, files in os.walk(args.notes):
|
for root_str, _, files in os.walk(args.notes):
|
||||||
root = Path(root_str)
|
root = Path(root_str)
|
||||||
if '.git' in root.parts:
|
if '.git' in root.parts:
|
||||||
continue
|
continue
|
||||||
|
Loading…
Reference in New Issue
Block a user