diff --git a/htmd/cli.py b/htmd/cli.py index 4e3990b..514005b 100644 --- a/htmd/cli.py +++ b/htmd/cli.py @@ -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.') @@ -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: diff --git a/htmd/example_site/config.py b/htmd/example_site/config.py deleted file mode 100644 index 7a3edfc..0000000 --- a/htmd/example_site/config.py +++ /dev/null @@ -1,34 +0,0 @@ -SITE_NAME = 'HTMD' -SITE_URL = '' -SITE_LOGO = '' - -SITE_DESCRIPTION = '' -# @site_username -SITE_TWITTER = '' -# URL of a Facebook page -SITE_FACEBOOK = '' -# Unique Facebook ID used for platforminsights -# https://developers.facebook.com/docs/platforminsights -FACEBOOK_APP_ID = '' - -# Where to look for files -STATIC_FOLDER = 'static' -POSTS_FOLDER = 'posts' -PAGES_FOLDER = 'pages' -BUILD_FOLDER = 'build' - -POSTS_EXTENSION = '.md' - -# How to format HTML -# Only set one to True -# If PRETTY_HTML is True, MINIFY_HTML will not be checked -PRETTY_HTML = False -MINIFY_HTML = False - -# Show author and date at the top of every post? -SHOW_AUTHOR = True -# Author to use if author is not specified in post file -DEFAULT_AUTHOR = '' -# @twitter_username -DEFAULT_AUTHOR_TWITTER = '' -DEFAULT_AUTHOR_FACEBOOK = '' diff --git a/htmd/example_site/config.toml b/htmd/example_site/config.toml new file mode 100644 index 0000000..b27f28a --- /dev/null +++ b/htmd/example_site/config.toml @@ -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 = "" diff --git a/htmd/site.py b/htmd/site.py index 72baa4f..cdf8c3f 100644 --- a/htmd/site.py +++ b/htmd/site.py @@ -1,6 +1,7 @@ import datetime import os import sys +import tomllib from bs4 import BeautifulSoup from feedwerk.atom import AtomFeed @@ -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 @@ -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') ) diff --git a/tests/test_build.py b/tests/test_build.py index fd0cf2a..cd1dec6 100644 --- a/tests/test_build.py +++ b/tests/test_build.py @@ -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) @@ -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) diff --git a/tests/test_start.py b/tests/test_start.py index e329d6c..de434ca 100644 --- a/tests/test_start.py +++ b/tests/test_start.py @@ -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) @@ -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']) diff --git a/tests/test_verify.py b/tests/test_verify.py index 8c364e2..cdc4fa4 100644 --- a/tests/test_verify.py +++ b/tests/test_verify.py @@ -134,26 +134,29 @@ 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 @@ -161,17 +164,17 @@ def test_verify_SITE_NAME_empty(): 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) @@ -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)