Re-formatted the code

This commit is contained in:
Bastian Venthur
2022-09-01 12:41:25 +02:00
parent 7b6b219cdf
commit 87d619cc1c
9 changed files with 167 additions and 135 deletions

View File

@@ -1 +1 @@
from blag.version import __VERSION__ as __VERSION__ # noqa
from blag.version import __VERSION__ as __VERSION__ # noqa

View File

@@ -30,8 +30,8 @@ from blag.quickstart import quickstart
logger = logging.getLogger(__name__)
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s %(levelname)s %(name)s %(message)s',
level=logging.INFO,
format='%(asctime)s %(levelname)s %(name)s %(message)s',
)
@@ -72,10 +72,11 @@ def parse_args(args: list[str] | None = None) -> argparse.Namespace:
parser.add_argument(
'--version',
action='version',
version='%(prog)s '+__VERSION__,
version='%(prog)s ' + __VERSION__,
)
parser.add_argument(
'-v', '--verbose',
'-v',
'--verbose',
action='store_true',
help='Verbose output.',
)
@@ -84,61 +85,69 @@ def parse_args(args: list[str] | None = None) -> argparse.Namespace:
commands.required = True
build_parser = commands.add_parser(
'build',
help='Build website.',
'build',
help='Build website.',
)
build_parser.set_defaults(func=build)
build_parser.add_argument(
'-i', '--input-dir',
default='content',
help='Input directory (default: content)',
'-i',
'--input-dir',
default='content',
help='Input directory (default: content)',
)
build_parser.add_argument(
'-o', '--output-dir',
default='build',
help='Ouptut directory (default: build)',
'-o',
'--output-dir',
default='build',
help='Ouptut directory (default: build)',
)
build_parser.add_argument(
'-t', '--template-dir',
default='templates',
help='Template directory (default: templates)',
'-t',
'--template-dir',
default='templates',
help='Template directory (default: templates)',
)
build_parser.add_argument(
'-s', '--static-dir',
default='static',
help='Static directory (default: static)',
'-s',
'--static-dir',
default='static',
help='Static directory (default: static)',
)
quickstart_parser = commands.add_parser(
'quickstart',
help="Quickstart blag, creating necessary configuration.",
'quickstart',
help="Quickstart blag, creating necessary configuration.",
)
quickstart_parser.set_defaults(func=quickstart)
serve_parser = commands.add_parser(
'serve',
help="Start development server.",
'serve',
help="Start development server.",
)
serve_parser.set_defaults(func=serve)
serve_parser.add_argument(
'-i', '--input-dir',
default='content',
help='Input directory (default: content)',
'-i',
'--input-dir',
default='content',
help='Input directory (default: content)',
)
serve_parser.add_argument(
'-o', '--output-dir',
default='build',
help='Ouptut directory (default: build)',
'-o',
'--output-dir',
default='build',
help='Ouptut directory (default: build)',
)
serve_parser.add_argument(
'-t', '--template-dir',
default='templates',
help='Template directory (default: templates)',
'-t',
'--template-dir',
default='templates',
help='Template directory (default: templates)',
)
serve_parser.add_argument(
'-s', '--static-dir',
default='static',
help='Static directory (default: static)',
'-s',
'--static-dir',
default='static',
help='Static directory (default: static)',
)
return parser.parse_args(args)
@@ -224,8 +233,9 @@ def build(args: argparse.Namespace) -> None:
convertibles = []
for root, dirnames, filenames in os.walk(args.input_dir):
for filename in filenames:
rel_src = os.path.relpath(f'{root}/{filename}',
start=args.input_dir)
rel_src = os.path.relpath(
f'{root}/{filename}', start=args.input_dir
)
# all non-markdown files are just copied over, the markdown
# files are converted to html
if rel_src.endswith('.md'):
@@ -233,8 +243,10 @@ def build(args: argparse.Namespace) -> None:
rel_dst = rel_dst[:-3] + '.html'
convertibles.append((rel_src, rel_dst))
else:
shutil.copy(f'{args.input_dir}/{rel_src}',
f'{args.output_dir}/{rel_src}')
shutil.copy(
f'{args.input_dir}/{rel_src}',
f'{args.output_dir}/{rel_src}',
)
for dirname in dirnames:
# all directories are copied into the output directory
path = os.path.relpath(f'{root}/{dirname}', start=args.input_dir)
@@ -264,7 +276,8 @@ def build(args: argparse.Namespace) -> None:
)
generate_feed(
articles, args.output_dir,
articles,
args.output_dir,
base_url=config['base_url'],
blog_title=config['title'],
blog_description=config['description'],
@@ -364,10 +377,10 @@ def generate_feed(
"""
logger.info('Generating Atom feed.')
feed = feedgenerator.Atom1Feed(
link=base_url,
title=blog_title,
description=blog_description,
feed_url=base_url + 'atom.xml',
link=base_url,
title=blog_title,
description=blog_description,
feed_url=base_url + 'atom.xml',
)
for dst, context in articles:

View File

@@ -91,8 +91,10 @@ def serve(args: argparse.Namespace) -> None:
contains the input-, template- and static dir
"""
httpd = HTTPServer(('', 8000), partial(SimpleHTTPRequestHandler,
directory=args.output_dir))
httpd = HTTPServer(
('', 8000),
partial(SimpleHTTPRequestHandler, directory=args.output_dir),
)
proc = multiprocessing.Process(target=autoreload, args=(args,))
proc.start()
logger.info("\n\n Devserver Started -- visit http://localhost:8000\n")

View File

@@ -33,8 +33,11 @@ def markdown_factory() -> Markdown:
"""
md = Markdown(
extensions=[
'meta', 'fenced_code', 'codehilite', 'smarty',
MarkdownLinkExtension()
'meta',
'fenced_code',
'codehilite',
'smarty',
MarkdownLinkExtension(),
],
output_format='html',
)
@@ -91,9 +94,7 @@ def convert_markdown(
class MarkdownLinkTreeprocessor(Treeprocessor):
"""Converts relative links to .md files to .html
"""
"""Converts relative links to .md files to .html"""
def run(self, root: Element) -> Element:
for element in root.iter():
@@ -112,7 +113,7 @@ class MarkdownLinkTreeprocessor(Treeprocessor):
logger.debug(
f'{url}: {scheme=} {netloc=} {path=} {query=} {fragment=}'
)
if (scheme or netloc or not path):
if scheme or netloc or not path:
return url
if path.endswith('.md'):
path = path[:-3] + '.html'
@@ -122,10 +123,11 @@ class MarkdownLinkTreeprocessor(Treeprocessor):
class MarkdownLinkExtension(Extension):
"""markdown.extension that converts relative .md- to .html-links.
"""markdown.extension that converts relative .md- to .html-links."""
"""
def extendMarkdown(self, md: Markdown) -> None:
md.treeprocessors.register(
MarkdownLinkTreeprocessor(md), 'mdlink', 0,
MarkdownLinkTreeprocessor(md),
'mdlink',
0,
)

View File

@@ -64,10 +64,10 @@ def quickstart(args: argparse.Namespace | None) -> None:
config = configparser.ConfigParser()
config['main'] = {
'base_url': base_url,
'title': title,
'description': description,
'author': author,
'base_url': base_url,
'title': title,
'description': description,
'author': author,
}
with open('config.ini', 'w') as fh:
config.write(fh)