mirror of
https://github.com/venthur/blag.git
synced 2025-11-25 20:52:43 +00:00
Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9cdecdccf7 | ||
|
|
499b0dfe11 | ||
|
|
72971408b2 | ||
|
|
6445f31204 | ||
|
|
98b97fbbbd | ||
|
|
df65dee488 | ||
|
|
d227392c79 | ||
|
|
03663a855d |
@@ -289,7 +289,7 @@ def process_markdown(convertibles, input_dir, output_dir,
|
|||||||
articles.append((dst, context))
|
articles.append((dst, context))
|
||||||
result = article_template.render(context)
|
result = article_template.render(context)
|
||||||
else:
|
else:
|
||||||
pages.append((dst, content))
|
pages.append((dst, context))
|
||||||
result = page_template.render(context)
|
result = page_template.render(context)
|
||||||
with open(f'{output_dir}/{dst}', 'w') as fh_dest:
|
with open(f'{output_dir}/{dst}', 'w') as fh_dest:
|
||||||
fh_dest.write(result)
|
fh_dest.write(result)
|
||||||
@@ -392,7 +392,7 @@ def generate_tags(articles, tags_template, tag_template, output_dir):
|
|||||||
# get tags number of occurrences
|
# get tags number of occurrences
|
||||||
all_tags = {}
|
all_tags = {}
|
||||||
for _, context in articles:
|
for _, context in articles:
|
||||||
tags = context.get('tags', None)
|
tags = context.get('tags', [])
|
||||||
for tag in tags:
|
for tag in tags:
|
||||||
all_tags[tag] = all_tags.get(tag, 0) + 1
|
all_tags[tag] = all_tags.get(tag, 0) + 1
|
||||||
# sort by occurrence
|
# sort by occurrence
|
||||||
@@ -405,7 +405,7 @@ def generate_tags(articles, tags_template, tag_template, output_dir):
|
|||||||
# get tags and archive per tag
|
# get tags and archive per tag
|
||||||
all_tags = {}
|
all_tags = {}
|
||||||
for dst, context in articles:
|
for dst, context in articles:
|
||||||
tags = context.get('tags', None)
|
tags = context.get('tags', [])
|
||||||
for tag in tags:
|
for tag in tags:
|
||||||
archive = all_tags.get(tag, [])
|
archive = all_tags.get(tag, [])
|
||||||
entry = context.copy()
|
entry = context.copy()
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
__VERSION__ = '0.0.5'
|
__VERSION__ = '0.0.7'
|
||||||
|
|||||||
4
setup.py
4
setup.py
@@ -17,6 +17,10 @@ setup(
|
|||||||
author='Bastian Venthur',
|
author='Bastian Venthur',
|
||||||
author_email='mail@venthur.de',
|
author_email='mail@venthur.de',
|
||||||
url='https://github.com/venthur/blag',
|
url='https://github.com/venthur/blag',
|
||||||
|
project_urls={
|
||||||
|
'Documentation': 'https://blag.readthedocs.io/',
|
||||||
|
'Source': 'https://github.com/venthur/blag',
|
||||||
|
},
|
||||||
python_requires='>=3.8',
|
python_requires='>=3.8',
|
||||||
package_data={
|
package_data={
|
||||||
'blag': ['templates/*'],
|
'blag': ['templates/*'],
|
||||||
|
|||||||
86
tests/conftest.py
Normal file
86
tests/conftest.py
Normal file
@@ -0,0 +1,86 @@
|
|||||||
|
from tempfile import TemporaryDirectory
|
||||||
|
import os
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from blag import blag
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def environment():
|
||||||
|
site = {
|
||||||
|
'base_url': 'site base_url',
|
||||||
|
'title': 'site title',
|
||||||
|
'description': 'site description',
|
||||||
|
'author': 'site author',
|
||||||
|
}
|
||||||
|
env = blag.environment_factory(globals_=dict(site=site))
|
||||||
|
yield env
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def page_template(environment):
|
||||||
|
yield environment.get_template('page.html')
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def article_template(environment):
|
||||||
|
yield environment.get_template('article.html')
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def archive_template(environment):
|
||||||
|
yield environment.get_template('archive.html')
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def tags_template(environment):
|
||||||
|
yield environment.get_template('tags.html')
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def tag_template(environment):
|
||||||
|
yield environment.get_template('tag.html')
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def cleandir():
|
||||||
|
"""Create a temporary workind directory and cwd.
|
||||||
|
|
||||||
|
"""
|
||||||
|
config = """
|
||||||
|
[main]
|
||||||
|
base_url = https://example.com/
|
||||||
|
title = title
|
||||||
|
description = description
|
||||||
|
author = a. u. thor
|
||||||
|
"""
|
||||||
|
|
||||||
|
with TemporaryDirectory() as dir:
|
||||||
|
for d in 'content', 'build', 'static', 'templates':
|
||||||
|
os.mkdir(f'{dir}/{d}')
|
||||||
|
with open(f'{dir}/config.ini', 'w') as fh:
|
||||||
|
fh.write(config)
|
||||||
|
# change directory
|
||||||
|
old_cwd = os.getcwd()
|
||||||
|
os.chdir(dir)
|
||||||
|
yield dir
|
||||||
|
# and change back afterwards
|
||||||
|
os.chdir(old_cwd)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def args(cleandir):
|
||||||
|
|
||||||
|
class NameSpace:
|
||||||
|
def __init__(self, **kwargs):
|
||||||
|
for name in kwargs:
|
||||||
|
setattr(self, name, kwargs[name])
|
||||||
|
|
||||||
|
args = NameSpace(
|
||||||
|
input_dir='content',
|
||||||
|
output_dir='build',
|
||||||
|
static_dir='static',
|
||||||
|
template_dir='templates',
|
||||||
|
)
|
||||||
|
yield args
|
||||||
@@ -7,19 +7,13 @@ import pytest
|
|||||||
from blag import blag
|
from blag import blag
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
def test_generate_feed(cleandir):
|
||||||
def outdir():
|
|
||||||
with TemporaryDirectory() as dir:
|
|
||||||
yield dir
|
|
||||||
|
|
||||||
|
|
||||||
def test_generate_feed(outdir):
|
|
||||||
articles = []
|
articles = []
|
||||||
blag.generate_feed(articles, outdir, ' ', ' ', ' ', ' ')
|
blag.generate_feed(articles, 'build', ' ', ' ', ' ', ' ')
|
||||||
assert os.path.exists(f'{outdir}/atom.xml')
|
assert os.path.exists('build/atom.xml')
|
||||||
|
|
||||||
|
|
||||||
def test_feed(outdir):
|
def test_feed(cleandir):
|
||||||
articles = [
|
articles = [
|
||||||
[
|
[
|
||||||
'dest1.html',
|
'dest1.html',
|
||||||
@@ -40,9 +34,9 @@ def test_feed(outdir):
|
|||||||
|
|
||||||
]
|
]
|
||||||
|
|
||||||
blag.generate_feed(articles, outdir, 'https://example.com/', 'blog title',
|
blag.generate_feed(articles, 'build', 'https://example.com/',
|
||||||
'blog description', 'blog author')
|
'blog title', 'blog description', 'blog author')
|
||||||
with open(f'{outdir}/atom.xml') as fh:
|
with open('build/atom.xml') as fh:
|
||||||
feed = fh.read()
|
feed = fh.read()
|
||||||
|
|
||||||
assert '<title>blog title</title>' in feed
|
assert '<title>blog title</title>' in feed
|
||||||
@@ -66,7 +60,7 @@ def test_feed(outdir):
|
|||||||
assert '<link href="https://example.com/dest2.html"' in feed
|
assert '<link href="https://example.com/dest2.html"' in feed
|
||||||
|
|
||||||
|
|
||||||
def test_generate_feed_with_description(outdir):
|
def test_generate_feed_with_description(cleandir):
|
||||||
# if a description is provided, it will be used as the summary in
|
# if a description is provided, it will be used as the summary in
|
||||||
# the feed, otherwise we simply use the title of the article
|
# the feed, otherwise we simply use the title of the article
|
||||||
articles = [[
|
articles = [[
|
||||||
@@ -78,9 +72,9 @@ def test_generate_feed_with_description(outdir):
|
|||||||
'content': 'content',
|
'content': 'content',
|
||||||
}
|
}
|
||||||
]]
|
]]
|
||||||
blag.generate_feed(articles, outdir, ' ', ' ', ' ', ' ')
|
blag.generate_feed(articles, 'build', ' ', ' ', ' ', ' ')
|
||||||
|
|
||||||
with open(f'{outdir}/atom.xml') as fh:
|
with open('build/atom.xml') as fh:
|
||||||
feed = fh.read()
|
feed = fh.read()
|
||||||
|
|
||||||
assert '<title>title</title>' in feed
|
assert '<title>title</title>' in feed
|
||||||
@@ -180,3 +174,106 @@ def test_environment_factory():
|
|||||||
env = blag.environment_factory(globals_=globals_)
|
env = blag.environment_factory(globals_=globals_)
|
||||||
assert env.globals['foo'] == 'bar'
|
assert env.globals['foo'] == 'bar'
|
||||||
assert env.globals['test'] == 'me'
|
assert env.globals['test'] == 'me'
|
||||||
|
|
||||||
|
|
||||||
|
def test_process_markdown(cleandir, page_template, article_template):
|
||||||
|
page1 = """\
|
||||||
|
title: some page
|
||||||
|
|
||||||
|
some text
|
||||||
|
foo bar
|
||||||
|
"""
|
||||||
|
|
||||||
|
article1 = """\
|
||||||
|
title: some article1
|
||||||
|
date: 2020-01-01
|
||||||
|
|
||||||
|
some text
|
||||||
|
foo bar
|
||||||
|
"""
|
||||||
|
|
||||||
|
article2 = """\
|
||||||
|
title: some article2
|
||||||
|
date: 2021-01-01
|
||||||
|
|
||||||
|
some text
|
||||||
|
foo bar
|
||||||
|
"""
|
||||||
|
|
||||||
|
convertibles = []
|
||||||
|
for i, txt in enumerate((page1, article1, article2)):
|
||||||
|
i = str(i)
|
||||||
|
with open(f'content/{i}', 'w') as fh:
|
||||||
|
fh.write(txt)
|
||||||
|
convertibles.append([i, i])
|
||||||
|
|
||||||
|
articles, pages = blag.process_markdown(
|
||||||
|
convertibles,
|
||||||
|
'content',
|
||||||
|
'build',
|
||||||
|
page_template,
|
||||||
|
article_template
|
||||||
|
)
|
||||||
|
|
||||||
|
assert isinstance(articles, list)
|
||||||
|
assert len(articles) == 2
|
||||||
|
for dst, context in articles:
|
||||||
|
assert isinstance(dst, str)
|
||||||
|
assert isinstance(context, dict)
|
||||||
|
assert 'content' in context
|
||||||
|
|
||||||
|
assert isinstance(pages, list)
|
||||||
|
assert len(pages) == 1
|
||||||
|
for dst, context in pages:
|
||||||
|
assert isinstance(dst, str)
|
||||||
|
assert isinstance(context, dict)
|
||||||
|
assert 'content' in context
|
||||||
|
|
||||||
|
|
||||||
|
def test_build(args):
|
||||||
|
page1 = """\
|
||||||
|
title: some page
|
||||||
|
|
||||||
|
some text
|
||||||
|
foo bar
|
||||||
|
"""
|
||||||
|
|
||||||
|
article1 = """\
|
||||||
|
title: some article1
|
||||||
|
date: 2020-01-01
|
||||||
|
tags: foo, bar
|
||||||
|
|
||||||
|
some text
|
||||||
|
foo bar
|
||||||
|
"""
|
||||||
|
|
||||||
|
article2 = """\
|
||||||
|
title: some article2
|
||||||
|
date: 2021-01-01
|
||||||
|
tags: baz
|
||||||
|
|
||||||
|
some text
|
||||||
|
foo bar
|
||||||
|
"""
|
||||||
|
|
||||||
|
# write some convertibles
|
||||||
|
convertibles = []
|
||||||
|
for i, txt in enumerate((page1, article1, article2)):
|
||||||
|
i = str(i)
|
||||||
|
with open(f'{args.input_dir}/{i}.md', 'w') as fh:
|
||||||
|
fh.write(txt)
|
||||||
|
convertibles.append([i, i])
|
||||||
|
|
||||||
|
# some static files
|
||||||
|
with open(f'{args.static_dir}/test', 'w') as fh:
|
||||||
|
fh.write('hello')
|
||||||
|
|
||||||
|
os.mkdir(f'{args.input_dir}/testdir')
|
||||||
|
with open(f'{args.input_dir}/testdir/test', 'w') as fh:
|
||||||
|
fh.write('hello')
|
||||||
|
|
||||||
|
blag.build(args)
|
||||||
|
|
||||||
|
|
||||||
|
def test_main(cleandir):
|
||||||
|
blag.main(['build'])
|
||||||
|
|||||||
@@ -1,30 +1,21 @@
|
|||||||
import time
|
import time
|
||||||
|
|
||||||
import pytest
|
|
||||||
|
|
||||||
from tempfile import TemporaryDirectory
|
|
||||||
from blag import devserver
|
from blag import devserver
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
def test_get_last_modified(cleandir):
|
||||||
def tempdir():
|
|
||||||
with TemporaryDirectory() as dir:
|
|
||||||
yield dir
|
|
||||||
|
|
||||||
|
|
||||||
def test_get_last_modified(tempdir):
|
|
||||||
# take initial time
|
# take initial time
|
||||||
t1 = devserver.get_last_modified([tempdir])
|
t1 = devserver.get_last_modified(['content'])
|
||||||
|
|
||||||
# wait a bit, create a file and measure again
|
# wait a bit, create a file and measure again
|
||||||
time.sleep(0.1)
|
time.sleep(0.1)
|
||||||
with open(f'{tempdir}/test', 'w') as fh:
|
with open('content/test', 'w') as fh:
|
||||||
fh.write('boo')
|
fh.write('boo')
|
||||||
t2 = devserver.get_last_modified([tempdir])
|
t2 = devserver.get_last_modified(['content'])
|
||||||
|
|
||||||
# wait a bit and take time again
|
# wait a bit and take time again
|
||||||
time.sleep(0.1)
|
time.sleep(0.1)
|
||||||
t3 = devserver.get_last_modified([tempdir])
|
t3 = devserver.get_last_modified(['content'])
|
||||||
|
|
||||||
assert t2 > t1
|
assert t2 > t1
|
||||||
assert t2 == t3
|
assert t2 == t3
|
||||||
|
|||||||
@@ -1,46 +1,5 @@
|
|||||||
import datetime
|
import datetime
|
||||||
|
|
||||||
import pytest
|
|
||||||
|
|
||||||
from blag import blag
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def environment():
|
|
||||||
site = {
|
|
||||||
'base_url': 'site base_url',
|
|
||||||
'title': 'site title',
|
|
||||||
'description': 'site description',
|
|
||||||
'author': 'site author',
|
|
||||||
}
|
|
||||||
env = blag.environment_factory(globals_=dict(site=site))
|
|
||||||
yield env
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def page_template(environment):
|
|
||||||
yield environment.get_template('page.html')
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def article_template(environment):
|
|
||||||
yield environment.get_template('article.html')
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def archive_template(environment):
|
|
||||||
yield environment.get_template('archive.html')
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def tags_template(environment):
|
|
||||||
yield environment.get_template('tags.html')
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def tag_template(environment):
|
|
||||||
yield environment.get_template('tag.html')
|
|
||||||
|
|
||||||
|
|
||||||
def test_page(page_template):
|
def test_page(page_template):
|
||||||
ctx = {
|
ctx = {
|
||||||
|
|||||||
Reference in New Issue
Block a user