Пример #1
0
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
Пример #2
0
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()
Пример #3
0
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()
Пример #4
0
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()
Пример #5
0
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()
Пример #6
0
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
Пример #7
0
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
Пример #8
0
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()
Пример #9
0
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}')