<commit_before>from datetime import datetime
from pathlib import Path

import pytest
from PIL import Image
from pytest_toolbox import gettree, mktree
from pytest_toolbox.comparison import RegexStr

from harrier.build import FileData
from harrier.config import Mode
from harrier.main import build
from harrier.render import json_filter, paginate_filter


def test_build_multi_part(tmpdir):
    mktree(tmpdir, {
        'pages': {
            'multipart_list.md': (
                '---\n'
                'uri: /list_md.html\n'
                'template: list.jinja\n'
                '---\n'
                'part 1\n'
                '--- . ---\n'
                'part **2**\n'
                '---.---\n'
                'this is part *3*\n'
            ),
            'multipart_dict.md': (
                '---\n'
                'uri: /dict_md.html\n'
                'template: dict.jinja\n'
                '---\n'
                'the main **section**\n'
                '--- other ---\n'
                'part *2*\n'
            ),
            'multipart_list.html': (
                '---\n'
                'uri: /list_html.html\n'
                'template: list.jinja\n'
                '---\n'
                'part 1\n'
                '--- . ---\n'
                'part 2\n'
                '---.---\n'
                'this is part 3\n'
            ),
            'multipart_dict.html': (
                '---\n'
                'uri: /dict_html.html\n'
                'template: dict.jinja\n'
                '---\n'
                'the main section\n'
                '--- other ---\n'
                'part 2\n'
            ),
        },
        'theme': {
            'templates/': {
                'list.jinja': (
                    '{% for v in content %}\n'
                    '  {{ v.content }}\n'
                    '{% endfor %}\n'
                ),
                'dict.jinja': (
                    '{{ content.main.content }}\n'
                    '{{ content.other.content }}\n'
                ),
            },
        },
    })
    build(tmpdir, mode=Mode.production)
    assert gettree(tmpdir.join('dist')) == {
        'list_md.html': (
            '\n'
            '  <p>part 1</p>\n'
            '\n\n'
            '  <p>part <strong>2</strong></p>\n'
            '\n\n'
            '  <p>this is part <em>3</em></p>\n'
        ),
        'dict_md.html': (
            '<p>the main <strong>section</strong></p>\n'
            '\n'
            '<p>part <em>2</em></p>\n'
        ),
        'list_html.html': (
            '\n'
            '  part 1\n'
            '\n'
            '  part 2\n'
            '\n'
            '  this is part 3\n'
        ),
        'dict_html.html': (
            'the main section\n'
            'part 2\n'
        ),
    }


def test_ignore_no_template(tmpdir):
    mktree(tmpdir, {
        'pages': {
            'ignore_this.md': 'this file is ignored',
            'normal.md': 'hello this is normal',
            'no_template.md': 'this should be passed through as-is',
            'normal_but_no_output.md': (
                '---\n'
                'output: false\n'
                '---\n'
                'hello this is normal\n'
            )
        },
        'theme': {
            'templates/foobar.jinja': 'rendered {{ content }}',
        },
        'harrier.yml': (
            'default_template: "foobar.jinja"\n'
            'ignore:\n'
            '- "**/ignore*"\n'
            'defaults:\n'
            '  "/no_temp*":\n'
            '    pass_through: true\n'
        )
    })
    build(tmpdir, mode=Mode.production)
    assert gettree(tmpdir.join('dist')) == {
        'no_template': {
            'index.html': 'this should be passed through as-is',
        },
        'normal': {
            'index.html': 'rendered <p>hello this is normal</p>\n',
        },

    }


def test_inline_css_prod(tmpdir):
    mktree(tmpdir, {
        'pages': {
            'foobar.html': '{{inline_css("theme/main.css")}}'
        },
        'theme': {
            'sass/main.scss': 'body {width: 10px + 10px;}',
        },
    })
    build(tmpdir, mode=Mode.production)
    assert gettree(tmpdir.join('dist')) == {
        'foobar': {
            'index.html': (
                'body{width:20px}\n'
            ),
        },
        'theme': {
            'main.a1ac3a7.css': 'body{width:20px}\n',
        },
    }


def test_inline_css_dev(tmpdir):
    mktree(tmpdir, {
        'pages': {
            'foo.html': '{{inline_css("theme/main.css")}}',
            'bar.html': "{{ url('theme/main.css') }}",
        },
        'theme': {
            'sass/main.scss': 'body {width: 10px + 10px;}',
        },
    })
    build(tmpdir, mode=Mode.development)
    mtime = tmpdir.join('theme/sass/main.scss').stat().mtime
    assert gettree(tmpdir.join('dist')) == {
        'foo': {
            'index.html': (
                'body {\n'
                '  width: 20px; }\n'
                '\n'
                '/*# sourceMappingURL=/theme/main.css.map */\n'
            ),
        },
        'bar': {
            'index.html': f'/theme/main.css?t={mtime:0.0f}\n',
        },
        'theme': {
            'main.css.map': RegexStr('{.*'),
            'main.css': (
                'body {\n'
                '  width: 20px; }\n'
                '\n'
                '/*# sourceMappingURL=main.css.map */'
            ),
            '.src': {
                'main.scss': 'body {width: 10px + 10px;}',
            },
        },<commit_msg>fix test<commit_after>from datetime import datetime
from pathlib import Path

import pytest
from PIL import Image
from pytest_toolbox import gettree, mktree
from pytest_toolbox.comparison import RegexStr

from harrier.build import FileData
from harrier.config import Mode
from harrier.main import build
from harrier.render import json_filter, paginate_filter


def test_build_multi_part(tmpdir):
    mktree(tmpdir, {
        'pages': {
            'multipart_list.md': (
                '---\n'
                'uri: /list_md.html\n'
                'template: list.jinja\n'
                '---\n'
                'part 1\n'
                '--- . ---\n'
                'part **2**\n'
                '---.---\n'
                'this is part *3*\n'
            ),
            'multipart_dict.md': (
                '---\n'
                'uri: /dict_md.html\n'
                'template: dict.jinja\n'
                '---\n'
                'the main **section**\n'
                '--- other ---\n'
                'part *2*\n'
            ),
            'multipart_list.html': (
                '---\n'
                'uri: /list_html.html\n'
                'template: list.jinja\n'
                '---\n'
                'part 1\n'
                '--- . ---\n'
                'part 2\n'
                '---.---\n'
                'this is part 3\n'
            ),
            'multipart_dict.html': (
                '---\n'
                'uri: /dict_html.html\n'
                'template: dict.jinja\n'
                '---\n'
                'the main section\n'
                '--- other ---\n'
                'part 2\n'
            ),
        },
        'theme': {
            'templates/': {
                'list.jinja': (
                    '{% for v in content %}\n'
                    '  {{ v.content }}\n'
                    '{% endfor %}\n'
                ),
                'dict.jinja': (
                    '{{ content.main.content }}\n'
                    '{{ content.other.content }}\n'
                ),
            },
        },
    })
    build(tmpdir, mode=Mode.production)
    assert gettree(tmpdir.join('dist')) == {
        'list_md.html': (
            '\n'
            '  <p>part 1</p>\n'
            '\n\n'
            '  <p>part <strong>2</strong></p>\n'
            '\n\n'
            '  <p>this is part <em>3</em></p>\n'
        ),
        'dict_md.html': (
            '<p>the main <strong>section</strong></p>\n'
            '\n'
            '<p>part <em>2</em></p>\n'
        ),
        'list_html.html': (
            '\n'
            '  part 1\n'
            '\n'
            '  part 2\n'
            '\n'
            '  this is part 3\n'
        ),
        'dict_html.html': (
            'the main section\n'
            'part 2\n'
        ),
    }


def test_ignore_no_template(tmpdir):
    mktree(tmpdir, {
        'pages': {
            'ignore_this.md': 'this file is ignored',
            'normal.md': 'hello this is normal',
            'no_template.md': 'this should be passed through as-is',
            'normal_but_no_output.md': (
                '---\n'
                'output: false\n'
                '---\n'
                'hello this is normal\n'
            )
        },
        'theme': {
            'templates/foobar.jinja': 'rendered {{ content }}',
        },
        'harrier.yml': (
            'default_template: "foobar.jinja"\n'
            'ignore:\n'
            '- "**/ignore*"\n'
            'defaults:\n'
            '  "/no_temp*":\n'
            '    pass_through: true\n'
        )
    })
    build(tmpdir, mode=Mode.production)
    assert gettree(tmpdir.join('dist')) == {
        'no_template': {
            'index.html': 'this should be passed through as-is',
        },
        'normal': {
            'index.html': 'rendered <p>hello this is normal</p>\n',
        },

    }


def test_inline_css_prod(tmpdir):
    mktree(tmpdir, {
        'pages': {
            'foobar.html': '{{inline_css("theme/main.css")}}'
        },
        'theme': {
            'sass/main.scss': 'body {width: 10px + 10px;}',
        },
    })
    build(tmpdir, mode=Mode.production)
    assert gettree(tmpdir.join('dist')) == {
        'foobar': {
            'index.html': (
                'body{width:20px}\n'
            ),
        },
        'theme': {
            'main.a1ac3a7.css': 'body{width:20px}\n',
        },
    }


def test_inline_css_dev(tmpdir):
    mktree(tmpdir, {
        'pages': {
            'foo.html': '{{inline_css("theme/main.css")}}',
            'bar.html': "{{ url('theme/main.css') }}",
        },
        'theme': {
            'sass/main.scss': 'body {width: 10px + 10px;}',
        },
    })
    build(tmpdir, mode=Mode.development)
    assert gettree(tmpdir.join('dist')) == {
        'foo': {
            'index.html': (
                'body {\n'
                '  width: 20px; }\n'
                '\n'
                '/*# sourceMappingURL=/theme/main.css.map */\n'
            ),
        },
        'bar': {
            'index.html': RegexStr(r'/theme/main.css\?t=\d+\n'),
        },
        'theme': {
            'main.css.map': RegexStr('{.*'),
            'main.css': (
                'body {\n'
                '  width: 20px; }\n'
                '\n'
                '/*# sourceMappingURL=main.css.map */'
            ),
            '.src': {
                'main.scss': 'body {width: 10px + 10px;}',
            },
        },
{'fraction_non_alphanumeric': 0.17209979967219086, 'fraction_numerical': 0.0072846476051721, 'mean_word_length': 1.0536649214659686, 'pattern_counts': {'":': 2, '<': 43, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4699290', 'n_tokens_mistral': 3655, 'n_tokens_neox': 3472, 'n_words': 595}