Skip to content

Commit

Permalink
Use toml for the config file
Browse files Browse the repository at this point in the history
Fixes #17
  • Loading branch information
Siecje committed Dec 8, 2023
1 parent b1c21e3 commit bc928be
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 69 deletions.
8 changes: 4 additions & 4 deletions htmd/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ def start(all_templates):
os.path.join('posts/', 'example.md'),
)

with as_file(files('htmd.example_site') / 'config.py') as file:
with as_file(files('htmd.example_site') / 'config.toml') as file:
copy_file(
file,
'config.py',
'config.toml',
)
click.echo('Add the site name and edit settings in config.py')
click.echo('Add the site name and edit settings in config.toml')


@cli.command('verify', short_help='Verify posts formatting is correct.')
Expand Down Expand Up @@ -107,7 +107,7 @@ 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.py.', fg='red'))
click.echo(click.style('[site] name is not set in config.toml.', fg='red'))

# SITE_NAME is not required
if not correct:
Expand Down
34 changes: 0 additions & 34 deletions htmd/example_site/config.py

This file was deleted.

39 changes: 39 additions & 0 deletions htmd/example_site/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
[site]
description = ""
# URL of a Facebook page
facebook = ""
# Unique Facebook ID used for platforminsights
# https://developers.facebook.com/docs/platforminsights
facebook_app_id = ""
logo = ""
name = "htmd"
# @twitter_username
twitter = "@"
url = ""

# Where to look for files
[folders]
static = "static"
posts = "posts"
pages = "pages"
build = "build"

[posts]
extension = ".md"

[html]
# How to format HTML
# Only set one to True
# If pretty is true, minify will not be checked
pretty = false
minify = false

[author]
# Show author and date at the top of every post?
show = true
# Author to use if author is not specified in the post file
default_name = ""
# @twitter_username
default_twitter = ""
# facebook URL
default_facebook = ""
31 changes: 27 additions & 4 deletions htmd/site.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import datetime
import os
import sys
import tomllib

from bs4 import BeautifulSoup
from feedwerk.atom import AtomFeed
Expand All @@ -19,7 +20,7 @@ def get_project_dir():
current_directory = os.getcwd()

while True:
file_path = os.path.join(current_directory, 'config.py')
file_path = os.path.join(current_directory, 'config.toml')

if os.path.isfile(file_path):
return current_directory
Expand All @@ -45,12 +46,34 @@ def get_project_dir():


try:
app.config.from_pyfile(os.path.join(project_dir, 'config.py'))
with open(os.path.join(project_dir, 'config.toml'), 'rb') as config_file:
htmd_config = tomllib.load(config_file)
except IOError:
print('Can not find config.py')
print('Can not find config.toml')
sys.exit(1)

# To avoid full paths in config.py
# 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', '')


# To avoid full paths in config.toml
app.config['FLATPAGES_ROOT'] = os.path.join(
project_dir, app.config.get('POSTS_FOLDER')
)
Expand Down
20 changes: 10 additions & 10 deletions tests/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,17 @@ def test_build_css_minify_no_css_files():
assert re.search(SUCCESS_REGEX, result.output)


def test_build_PRETTY_HTML_True():
def test_build_html_pretty_true():
runner = CliRunner()
with runner.isolated_filesystem():
result = runner.invoke(start)
with open('config.py', 'r') as config_file:
with open('config.toml', 'r') as config_file:
lines = config_file.readlines()

with open('config.py', 'w') as config_file:
with open('config.toml', 'w') as config_file:
for line in lines:
if 'PRETTY_HTML' in line:
config_file.write('PRETTY_HTML = True\n')
if 'pretty =' in line:
config_file.write('pretty = true\n')
else:
config_file.write(line)

Expand All @@ -118,17 +118,17 @@ def test_build_PRETTY_HTML_True():
assert re.search(SUCCESS_REGEX, result.output)


def test_build_MINIFY_HTML_True():
def test_build_html_minify_true():
runner = CliRunner()
with runner.isolated_filesystem():
result = runner.invoke(start)
with open('config.py', 'r') as config_file:
with open('config.toml', 'r') as config_file:
lines = config_file.readlines()

with open('config.py', 'w') as config_file:
with open('config.toml', 'w') as config_file:
for line in lines:
if 'MINIFY_HTML' in line:
config_file.write('MINIFY_HTML = True\n')
if 'minify =' in line:
config_file.write('minify = true\n')
else:
config_file.write(line)

Expand Down
8 changes: 4 additions & 4 deletions tests/test_start.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ def test_start():
'pages/about.html was created.\n'
'posts/ was created.\n'
'posts/example.md was created.\n'
'config.py was created.\n'
'Add the site name and edit settings in config.py\n'
'config.toml was created.\n'
'Add the site name and edit settings in config.toml\n'
)
with runner.isolated_filesystem():
result = runner.invoke(start)
Expand Down Expand Up @@ -48,8 +48,8 @@ def test_start_all_templates():
'pages/about.html was created.\n'
'posts/ was created.\n'
'posts/example.md was created.\n'
'config.py was created.\n'
'Add the site name and edit settings in config.py\n'
'config.toml was created.\n'
'Add the site name and edit settings in config.toml\n'
)
with runner.isolated_filesystem():
result = runner.invoke(start, ['--all-templates'])
Expand Down
29 changes: 16 additions & 13 deletions tests/test_verify.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,44 +134,47 @@ def test_verify_published_invalid_day():
assert result.output == expected_output


def test_verify_SITE_NAME_empty():
def test_verify_site_name_empty():
expected_output = (
'All posts are correctly formatted.\n'
'SITE_NAME is not set in config.py.\n'
'[site] name is not set in config.toml.\n'
)
runner = CliRunner()
with runner.isolated_filesystem():
runner.invoke(start)

with open('config.py', 'r') as post:
with open('config.toml', 'r') as post:
lines = post.readlines()
with open('config.py', 'w') as post:
with open('config.toml', 'w') as post:
seen = False
for line in lines:
if 'SITE_NAME' in line:
post.write("SITE_NAME = ''\n")
if 'name' in line and not seen:
# [site] name is the first name
seen = True
post.write("name = ''\n")
else:
post.write(line)

result = runner.invoke(verify)
# SITE_NAME isn't required
# [site] name isn't required
assert result.exit_code == 0
assert result.output == expected_output


def test_verify_SITE_NAME_missing():
expected_output = (
'All posts are correctly formatted.\n'
'SITE_NAME is not set in config.py.\n'
'[site] name is not set in config.toml.\n'
)
runner = CliRunner()
with runner.isolated_filesystem():
runner.invoke(start)

with open('config.py', 'r') as post:
with open('config.toml', 'r') as post:
lines = post.readlines()
with open('config.py', 'w') as post:
with open('config.toml', 'w') as post:
for line in lines:
if 'SITE_NAME' not in line:
if 'name' not in line:
post.write(line)

result = runner.invoke(verify)
Expand All @@ -181,12 +184,12 @@ def test_verify_SITE_NAME_missing():


def test_verify_no_config():
expected_output = 'Can not find config.py\n'
expected_output = 'Can not find config.toml\n'
runner = CliRunner()
with runner.isolated_filesystem():
runner.invoke(start)

os.remove('config.py')
os.remove('config.toml')

result = runner.invoke(verify)

Expand Down

0 comments on commit bc928be

Please sign in to comment.