def assert_site_render(src_location, content, tmp_path): with Path(src_location).open() as f: src_content = f.read() test_out = tmp_path / 'out' site = Site(url='https://example.org/') site.add(content) site.generate(test_out) assert (test_out / src_location).exists() assert (test_out / src_location).read_text() == src_content
def test_include_glob(tmp_path: Path): test_out = tmp_path / 'out' site = Site(url='https://example.org/') site.add('resources/glob/**/*.html') site.generate(test_out) result = test_out / 'resources' / 'glob' assert (result / 'a.html').exists() assert (result / 'b.html').exists() assert (result / 'dir' / '1.html').exists()
def test_render_jinja_file(tmp_path: Path): src_location = 'resources/jinja/file.html' out_location = 'jinja/file.html' test_out = tmp_path / 'out' site = Site(url='https://example.org/') site.add(out_location, jinja(src_location)) site.generate(test_out) assert (test_out / out_location).exists() with open('expected/jinja/file.html') as expected: assert (test_out / out_location).read_text() == expected.read()
def test_render_jinja(tmp_path: Path): src_location = 'resources/jinja/title.html' out_location = 'title.html' test_out = tmp_path / 'out' site = Site(url='https://example.org/') site.add(out_location, jinja(src_location, title='99 reasons lightweight rules')) site.generate(test_out) assert (test_out / out_location).exists() with open('expected/jinja/params.html') as expected: assert (test_out / out_location).read_text() == expected.read()
def test_render_markdown(tmp_path: Path): src_location = 'resources/md/collection/post-1.md' out_location = 'md/plain.html' test_out = tmp_path / 'out' site = Site(url='https://example.org/') site.add(out_location, markdown(src_location, template('templates/md/plain.html'))) site.generate(test_out) assert (test_out / out_location).exists() with open('expected/md/plain.html') as expected: assert (test_out / out_location).read_text() == expected.read()
def test_include_file(tmp_path: Path): src_location = 'resources/test.html' src_path = Path(src_location) with src_path.open() as f: src_content = f.read() test_out = tmp_path / 'out' site = Site(url='https://example.org/') site.add(src_location) site.generate(test_out) assert (test_out / src_location).exists() assert (test_out / src_location).read_text() == src_content
def test_include_dir_under_different_name(tmp_path: Path): src_location = 'resources/test_nested/test2/test3/test.html' src_path = Path(src_location) with src_path.open() as f: src_content = f.read() test_out = tmp_path / 'out' site = Site(url='https://example.org/') site.add('successful_test', 'resources/test_nested') site.generate(test_out) assert (test_out / 'successful_test/test2/test3/test.html').exists() assert (test_out / 'successful_test/test2/test3/test.html').read_text() == src_content
def test_lazy_params(tmp_path: Path): src_location = 'resources/jinja/lazy.html' out_location = 'lazy.html' test_out = tmp_path / 'out' site = Site(url='https://example.org/') site.add( out_location, jinja(src_location, lazy=from_ctx(lambda ctx: f'Hello there! {ctx.tasks[0].path}'))) site.generate(test_out) assert (test_out / out_location).exists() with open('expected/jinja/lazy.html') as expected: assert (test_out / out_location).read_text() == expected.read()
def build_jinja_file(url): site = Site(url=url) site.add('index', jinja('index')) return site
def quickstart(location: str, title: Optional[str]): path = Path(location) path.mkdir(parents=True, exist_ok=True) abs_out = os.path.abspath(path) if not title: title = Path(abs_out).name title_slug = slugify_title(title) template_location = Path(__file__).parent / 'project-template' with directory(template_location), custom_jinja_tags(): site = Site(url="https://example.com/", title=title) [site.add(str(p), jinja(p)) for p in paths('_templates_/**/*.html')] [site.add(str(p), jinja(p)) for p in paths('*.html')] site.add('website.py', jinja('website.py.j2', title_slug=title_slug)) site.add('requirements.txt', jinja('requirements.txt.j2', version=lw_version())) site.add('posts') [ site.add(str(p), jinja(p)) for p in paths('styles/**/*css') if p.name != 'attributes.scss' ] site.add('styles/attributes.scss', jinja('styles/attributes.scss', accent=Color.bright())) site.add('js') site.add('img') site.generate(abs_out) website_file = os.path.join(abs_out, 'website.py') os.chmod(website_file, stat.S_IRWXU | stat.S_IRGRP | stat.S_IROTH) # -rwxr--r-- logger.info(f' Project initialized in: {abs_out}')
def test_site_include_duplicate(): site = Site(url='https://example.org/') site.add('page', 'resources/test.html') with pytest.raises(IncludedDuplicate): site.add('page', 'site/index.html')
def test_absolute_includes_not_allowed(): site = Site('https://example.org/') with pytest.raises(AbsolutePathIncluded): site.add('/etc')
def test_include_not_found_under_different_name(tmp_path: Path): site = Site(url='https://example.org/') with pytest.raises(FileNotFoundError): site.add('t.html', str(uuid4()))
def test_include_not_found(tmp_path: Path): site = Site(url='https://example.org/') with pytest.raises(FileNotFoundError): site.add(str(uuid4()))