1
0
mirror of https://github.com/venthur/blag.git synced 2025-11-25 20:52:43 +00:00

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

@@ -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.',
)
@@ -89,22 +90,26 @@ def parse_args(args: list[str] | None = None) -> argparse.Namespace:
)
build_parser.set_defaults(func=build)
build_parser.add_argument(
'-i', '--input-dir',
'-i',
'--input-dir',
default='content',
help='Input directory (default: content)',
)
build_parser.add_argument(
'-o', '--output-dir',
'-o',
'--output-dir',
default='build',
help='Ouptut directory (default: build)',
)
build_parser.add_argument(
'-t', '--template-dir',
'-t',
'--template-dir',
default='templates',
help='Template directory (default: templates)',
)
build_parser.add_argument(
'-s', '--static-dir',
'-s',
'--static-dir',
default='static',
help='Static directory (default: static)',
)
@@ -121,22 +126,26 @@ def parse_args(args: list[str] | None = None) -> argparse.Namespace:
)
serve_parser.set_defaults(func=serve)
serve_parser.add_argument(
'-i', '--input-dir',
'-i',
'--input-dir',
default='content',
help='Input directory (default: content)',
)
serve_parser.add_argument(
'-o', '--output-dir',
'-o',
'--output-dir',
default='build',
help='Ouptut directory (default: build)',
)
serve_parser.add_argument(
'-t', '--template-dir',
'-t',
'--template-dir',
default='templates',
help='Template directory (default: templates)',
)
serve_parser.add_argument(
'-s', '--static-dir',
'-s',
'--static-dir',
default='static',
help='Static directory (default: static)',
)
@@ -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'],

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

@@ -50,9 +50,7 @@ def tag_template(environment: Environment) -> Iterator[Template]:
@pytest.fixture
def cleandir() -> Iterator[str]:
"""Create a temporary workind directory and cwd.
"""
"""Create a temporary workind directory and cwd."""
config = """
[main]
base_url = https://example.com/

View File

@@ -28,7 +28,7 @@ def test_feed(cleandir: str) -> None:
'title': 'title1',
'date': datetime(2019, 6, 6),
'content': 'content1',
}
},
),
(
'dest2.html',
@@ -36,12 +36,18 @@ def test_feed(cleandir: str) -> None:
'title': 'title2',
'date': datetime(1980, 5, 9),
'content': 'content2',
}
},
),
]
blag.generate_feed(articles, 'build', 'https://example.com/',
'blog title', 'blog description', 'blog author')
blag.generate_feed(
articles,
'build',
'https://example.com/',
'blog title',
'blog description',
'blog author',
)
with open('build/atom.xml') as fh:
feed = fh.read()
@@ -69,15 +75,17 @@ def test_feed(cleandir: str) -> None:
def test_generate_feed_with_description(cleandir: str) -> None:
# if a description is provided, it will be used as the summary in
# the feed, otherwise we simply use the title of the article
articles: list[tuple[str, dict[str, Any]]] = [(
articles: list[tuple[str, dict[str, Any]]] = [
(
'dest.html',
{
'title': 'title',
'description': 'description',
'date': datetime(2019, 6, 6),
'content': 'content',
}
)]
},
)
]
blag.generate_feed(articles, 'build', ' ', ' ', ' ', ' ')
with open('build/atom.xml') as fh:
@@ -144,10 +152,9 @@ author = a. u. thor
# a missing required config causes a sys.exit
for x in 'base_url', 'title', 'description', 'author':
config2 = '\n'.join([line
for line
in config.splitlines()
if not line.startswith(x)])
config2 = '\n'.join(
[line for line in config.splitlines() if not line.startswith(x)]
)
with TemporaryDirectory() as dir:
configfile = f'{dir}/config.ini'
with open(configfile, 'w') as fh:
@@ -173,10 +180,7 @@ author = a. u. thor
def test_environment_factory() -> None:
globals_: dict[str, object] = {
'foo': 'bar',
'test': 'me'
}
globals_: dict[str, object] = {'foo': 'bar', 'test': 'me'}
env = blag.environment_factory(globals_=globals_)
assert env.globals['foo'] == 'bar'
assert env.globals['test'] == 'me'
@@ -217,11 +221,7 @@ foo bar
convertibles.append((str(i), str(i)))
articles, pages = blag.process_markdown(
convertibles,
'content',
'build',
page_template,
article_template
convertibles, 'content', 'build', page_template, article_template
)
assert isinstance(articles, list)

View File

@@ -32,9 +32,11 @@ def test_autoreload_builds_immediately(args: Namespace) -> None:
with open('content/test.md', 'w') as fh:
fh.write('boo')
t = threading.Thread(target=devserver.autoreload,
args=(args, ),
daemon=True,)
t = threading.Thread(
target=devserver.autoreload,
args=(args,),
daemon=True,
)
t0 = devserver.get_last_modified(['build'])
t.start()
# try for 5 seconds...
@@ -47,11 +49,15 @@ def test_autoreload_builds_immediately(args: Namespace) -> None:
assert t1 > t0
@pytest.mark.filterwarnings("ignore::pytest.PytestUnhandledThreadExceptionWarning") # noqa
@pytest.mark.filterwarnings(
"ignore::pytest.PytestUnhandledThreadExceptionWarning"
)
def test_autoreload(args: Namespace) -> None:
t = threading.Thread(target=devserver.autoreload,
args=(args, ),
daemon=True,)
t = threading.Thread(
target=devserver.autoreload,
args=(args,),
daemon=True,
)
t.start()
t0 = devserver.get_last_modified(['build'])

View File

@@ -9,7 +9,9 @@ import markdown
from blag.markdown import convert_markdown, markdown_factory
@pytest.mark.parametrize("input_, expected", [
@pytest.mark.parametrize(
"input_, expected",
[
# inline
('[test](test.md)', 'test.html'),
('[test](test.md "test")', 'test.html'),
@@ -20,7 +22,7 @@ from blag.markdown import convert_markdown, markdown_factory
('[test](/a/test.md)', '/a/test.html'),
('[test](/a/test.md "test")', '/a/test.html'),
# reference
('[test][]\n[test]: test.md ''', 'test.html'),
('[test][]\n[test]: test.md ' '', 'test.html'),
('[test][]\n[test]: test.md "test"', 'test.html'),
('[test][]\n[test]: a/test.md', 'a/test.html'),
('[test][]\n[test]: a/test.md "test"', 'a/test.html'),
@@ -28,35 +30,44 @@ from blag.markdown import convert_markdown, markdown_factory
('[test][]\n[test]: /test.md "test"', '/test.html'),
('[test][]\n[test]: /a/test.md', '/a/test.html'),
('[test][]\n[test]: /a/test.md "test"', '/a/test.html'),
])
],
)
def test_convert_markdown_links(input_: str, expected: str) -> None:
md = markdown_factory()
html, _ = convert_markdown(md, input_)
assert expected in html
@pytest.mark.parametrize("input_, expected", [
@pytest.mark.parametrize(
"input_, expected",
[
# scheme
('[test](https://)', 'https://'),
# netloc
('[test](//test.md)', '//test.md'),
# no path
('[test]()', ''),
])
],
)
def test_dont_convert_normal_links(input_: str, expected: str) -> None:
md = markdown_factory()
html, _ = convert_markdown(md, input_)
assert expected in html
@pytest.mark.parametrize("input_, expected", [
@pytest.mark.parametrize(
"input_, expected",
[
('foo: bar', {'foo': 'bar'}),
('foo: those are several words', {'foo': 'those are several words'}),
('tags: this, is, a, test\n', {'tags': ['this', 'is', 'a', 'test']}),
('tags: this, IS, a, test', {'tags': ['this', 'is', 'a', 'test']}),
('date: 2020-01-01 12:10', {'date':
datetime(2020, 1, 1, 12, 10).astimezone()}),
])
(
'date: 2020-01-01 12:10',
{'date': datetime(2020, 1, 1, 12, 10).astimezone()},
),
],
)
def test_convert_metadata(input_: str, expected: dict[str, Any]) -> None:
md = markdown_factory()
_, meta = convert_markdown(md, input_)