Skip to content

Commit

Permalink
Improve formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Siecje committed Dec 13, 2023
1 parent 08d8f6d commit eb44fb4
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 74 deletions.
51 changes: 13 additions & 38 deletions htmd/cli.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import importlib
from importlib.resources import as_file, files
import os
import sys

import click

from .utils import (
combine_and_minify_css,
combine_and_minify_js,
copy_file,
copy_missing_templates,
copy_site_file,
create_directory,
)

Expand All @@ -32,43 +30,19 @@ def start(all_templates):
if all_templates:
copy_missing_templates()
else:
with as_file(files('htmd.example_site.templates') / '_layout.html') as file:
copy_file(
file,
os.path.join('templates/', '_layout.html')
)
copy_site_file('templates', '_layout.html')

create_directory('static/')
with as_file(files('htmd.example_site.static') / '_reset.css') as file:
copy_file(
file,
os.path.join('static/', '_reset.css')
)
with as_file(files('htmd.example_site.static') / 'style.css') as file:
copy_file(
file,
os.path.join('static/', 'style.css'),
)
copy_site_file('static', '_reset.css')
copy_site_file('static', 'style.css')

create_directory('pages/')
with as_file(files('htmd.example_site.pages') / 'about.html') as file:
copy_file(
file,
os.path.join('pages/', 'about.html'),
)
copy_site_file('pages', 'about.html')

create_directory('posts/')
with as_file(files('htmd.example_site.posts') / 'example.md') as file:
copy_file(
file,
os.path.join('posts/', 'example.md'),
)

with as_file(files('htmd.example_site') / 'config.toml') as file:
copy_file(
file,
'config.toml',
)
copy_site_file('posts', 'example.md')

copy_site_file('', 'config.toml')
click.echo('Add the site name and edit settings in config.toml')


Expand All @@ -83,7 +57,7 @@ def verify():

correct = True
for post in site.posts:
for item in ['author', 'title', 'published']:
for item in ['author', 'published', 'title']:
if item not in post.meta:
correct = False
msg = f'Post "{post.path}" does not have field {item}.'
Expand All @@ -107,9 +81,10 @@ def verify():
app = site.app
site_name = app.config.get('SITE_NAME')
if not site_name:
click.echo(click.style('[site] name is not set in config.toml.', fg='red'))

# SITE_NAME is not required
# SITE_NAME is not required
message = '[site] name is not set in config.toml.'
click.echo(click.style(message, fg='yellow'))

if not correct:
sys.exit(1)

Expand Down
64 changes: 39 additions & 25 deletions htmd/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,25 +52,33 @@ def get_project_dir():
print('Can not find config.toml')
sys.exit(1)

# Flask configs are flat, our toml file is not
app.config['SITE_NAME'] = htmd_config.get('site', {}).get('name', '')
app.config['SITE_URL'] = htmd_config.get('site', {}).get('url', '')
app.config['SITE_LOGO'] = htmd_config.get('site', {}).get('logo', '')
app.config['SITE_DESCRIPTION'] = htmd_config.get('site', {}).get('description', '')
app.config['SITE_TWITTER'] = htmd_config.get('site', {}).get('twitter', '')
app.config['SITE_FACEBOOK'] = htmd_config.get('site', {}).get('facebook', '')
app.config['FACEBOOK_APP_ID'] = htmd_config.get('site', {}).get('facebook_app_id', '')
app.config['STATIC_FOLDER'] = htmd_config.get('folders', {}).get('static', 'static')
app.config['POSTS_FOLDER'] = htmd_config.get('folders', {}).get('posts', 'posts')
app.config['PAGES_FOLDER'] = htmd_config.get('folders', {}).get('pages', 'pages')
app.config['BUILD_FOLDER'] = htmd_config.get('folders', {}).get('build', 'build')
app.config['POSTS_EXTENSION'] = htmd_config.get('posts', {}).get('extension', '.md')
app.config['PRETTY_HTML'] = htmd_config.get('html', {}).get('pretty', False)
app.config['MINIFY_HTML'] = htmd_config.get('html', {}).get('minify', False)
app.config['SHOW_AUTHOR'] = htmd_config.get('author', {}).get('show', True)
app.config['DEFAULT_AUTHOR'] = htmd_config.get('author', {}).get('default_name', '')
app.config['DEFAULT_AUTHOR_TWITTER'] = htmd_config.get('author', {}).get('default_twitter', '')
app.config['DEFAULT_AUTHOR_FACEBOOK'] = htmd_config.get('author', {}).get('default_facebook', '')
# Flask configs are flat, config.toml is not
# Define the configuration keys and their default values
config_keys = {
'SITE_NAME': ['site', 'name', ''],
'SITE_URL': ['site', 'url', ''],
'SITE_LOGO': ['site', 'logo', ''],
'SITE_DESCRIPTION': ['site', 'description', ''],
'SITE_TWITTER': ['site', 'twitter', ''],
'SITE_FACEBOOK': ['site', 'facebook', ''],
'FACEBOOK_APP_ID': ['site', 'facebook_app_id', ''],
'STATIC_FOLDER': ['folders', 'static', 'static'],
'POSTS_FOLDER': ['folders', 'posts', 'posts'],
'PAGES_FOLDER': ['folders', 'pages', 'pages'],
'BUILD_FOLDER': ['folders', 'build', 'build'],
'POSTS_EXTENSION': ['posts', 'extension', '.md'],
'PRETTY_HTML': ['html', 'pretty', False],
'MINIFY_HTML': ['html', 'minify', False],
'SHOW_AUTHOR': ['author', 'show', True],
'DEFAULT_AUTHOR': ['author', 'default_name', ''],
'DEFAULT_AUTHOR_TWITTER': ['author', 'default_twitter', ''],
'DEFAULT_AUTHOR_FACEBOOK': ['author', 'default_facebook', ''],
}

# Update app.config using the configuration keys
for flask_key, (table, key, default) in config_keys.items():
app.config[flask_key] = htmd_config.get(table, {}).get(key, default)



# To avoid full paths in config.toml
Expand Down Expand Up @@ -354,19 +362,25 @@ def page_not_found(e):
@freezer.register_generator
def year_view():
for post in posts:
yield {'year': post.meta.get('published').year}
yield {
'year': post.meta.get('published').year
}


@freezer.register_generator
def month_view():
for post in posts:
yield {'year': post.meta.get('published').year,
'month': post.meta.get('published').strftime('%m')}
yield {
'month': post.meta.get('published').strftime('%m'),
'year': post.meta.get('published').year,
}


@freezer.register_generator
def day_view():
for post in posts:
yield {'year': post.meta.get('published').year,
'month': post.meta.get('published').strftime('%m'),
'day': post.meta.get('published').strftime('%d')}
yield {
'day': post.meta.get('published').strftime('%d'),
'month': post.meta.get('published').strftime('%m'),
'year': post.meta.get('published').year,
}
32 changes: 21 additions & 11 deletions htmd/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import importlib
from importlib.resources import as_file, files
import os
import shutil
Expand All @@ -13,20 +12,11 @@ def create_directory(name):
os.mkdir(name)
except FileExistsError:
msg = f'{name} already exists and was not created.'
click.echo(click.style(msg, fg='red'))
click.echo(click.style(msg, fg='yellow'))
else:
click.echo(click.style(f'{name} was created.', fg='green'))


def copy_file(source, destination):
if os.path.exists(destination) is False:
shutil.copyfile(source, destination)
click.echo(click.style(f'{destination} was created.', fg='green'))
else:
msg = f'{destination} already exists and was not created.'
click.echo(click.style(msg, fg='red'))


def combine_and_minify_css(static_folder):
# Combine and minify all .css files in the static folder
css_files = sorted(
Expand Down Expand Up @@ -77,6 +67,14 @@ def combine_and_minify_js(static_folder):
with open(os.path.join(static_folder, 'combined.min.js'), 'w') as master:
master.write(jsmin(combined))

def copy_file(source, destination):
if os.path.exists(destination) is False:
shutil.copyfile(source, destination)
click.echo(click.style(f'{destination} was created.', fg='green'))
else:
msg = f'{destination} already exists and was not created.'
click.echo(click.style(msg, fg='yellow'))


def copy_missing_templates():
template_dir = files('htmd.example_site') / 'templates'
Expand All @@ -93,3 +91,15 @@ def copy_missing_templates():
template_file,
os.path.join('templates/', file_name)
)


def copy_site_file(directory, filename):
if directory == '':
anchor = 'htmd.example_site'
else:
anchor = f'htmd.example_site.{directory}'
source_path = files(anchor) / filename
destination_path = os.path.join(directory, filename)

with as_file(source_path) as file:
copy_file(file, destination_path)

0 comments on commit eb44fb4

Please sign in to comment.