Esempio n. 1
0
def test_process_file_includes(tmpdir):

    source_dir = tmpdir.mkdir('source')
    source_dir.mkdir('ns1')
    target_dir = tmpdir.mkdir('target')

    create_markdown_file(source_dir.join('ns1',
                                         'file1.md'), {'title': 'Page One'},
                         f'<<{source_dir.join("ns1", "file2.md")}>>')

    create_markdown_file(source_dir.join('ns1', 'file2.md'),
                         {'title': 'Page Two'}, 'Included Text')

    create_wiki_config(
        str(source_dir.join('test.cfg')), None, {
            'name': 'ns1',
            'path': f'{source_dir.join("ns1")}',
            'target': str(target_dir)
        })

    wiki = Wiki(source_dir.join('test.cfg'))

    wiki.process_namespaces()

    expect1 = create_markdown_string({'title': 'Page One'},
                                     '''Included Text''')

    assert os.path.exists(target_dir.join('ns1', 'page_one.md'))

    with open(target_dir.join('ns1', 'page_one.md'), 'r',
              encoding='utf8') as fh:
        actual1 = fh.read()

    assert compare_markdown_content(expect1, actual1)
Esempio n. 2
0
def test_process_page_links_broken(tmpdir):
    source_dir = tmpdir.mkdir('source')
    source_dir.mkdir('ns1')
    target_dir = tmpdir.mkdir('target')

    create_markdown_file(source_dir.join('ns1', 'file1.md'),
                         {'title': 'Page One'}, 'A link to [[Page Two]]')

    create_wiki_config(
        str(source_dir.join('test.cfg')), None, {
            'name': 'ns1',
            'path': f'{source_dir.join("ns1")}',
            'target': str(target_dir)
        })

    wiki = Wiki(source_dir.join('test.cfg'))

    wiki.process_namespaces()

    assert len(wiki) == 1
    assert len(wiki.namespaces['ns1']) == 1

    expect1 = create_markdown_string({'title': 'Page One'},
                                     'A link to [Page Two]{.broken}')

    assert os.path.exists(target_dir.join('ns1', 'page_one.md'))

    with open(target_dir.join('ns1', 'page_one.md'), 'r',
              encoding='utf8') as fh:
        actual1 = fh.read()

    assert compare_markdown_content(expect1, actual1)
Esempio n. 3
0
def test_search_index_noise_words_string(tmpdir):

    source_dir = tmpdir.mkdir('source')
    source_dir.mkdir('ns1')
    target_dir = tmpdir.mkdir('target')

    create_markdown_file(source_dir.join('ns1', 'file1.md'),
                         {'title': 'Page One',
                          'alias': 'First Apple',
                          'tags': '[abc]'},
                         'A link to [[Page Two]]')

    create_markdown_file(source_dir.join('ns1', 'file2.md'),
                         {'title': 'Page Two',
                          'alias': 'Second',
                          'tags': '[abc]'},
                         'A link to [[Page One]]')

    create_wiki_config(str(source_dir.join('test.cfg')),
                       None,
                       {'name': 'ns1',
                        'path': f'{source_dir.join("ns1")}',
                        'target': str(target_dir),
                        'search_fields': 'title,alias,tags',
                        'noise_words': 'first, apple'})

    wiki = Wiki(source_dir.join('test.cfg'))

    wiki.process_namespaces()

    assert os.path.exists(target_dir.join('ns1', 'page_one.md'))
    assert os.path.exists(target_dir.join('ns1', '_index.json'))

    expect = {
        "page": [
            ["page_one", "Page One"],
            ["page_two", "Page Two"]
        ],
        "abc": [
            ["page_one", "Page One"],
            ["page_two", "Page Two"]
        ],
        "one": [
            ["page_one", "Page One"]
        ],
        "two": [
            ["page_two", "Page Two"]
        ],
        "second": [
            ["page_two", "Page Two"]
        ]
    }

    with open(target_dir.join('ns1', '_index.json'), 'r', encoding='utf8') as fh:
        index = fh.read()

    actual = json.loads(index)

    # use DeepDiff to compare structures
    assert not deepdiff.DeepDiff(expect, actual, ignore_order=True)
Esempio n. 4
0
def test_multiple_namespaces_aliases(tmpdir):
    # two pages in same namespace, still need a wiki

    source_dir = tmpdir.mkdir('source')
    source_dir.mkdir('ns1')
    source_dir.mkdir('ns2')
    target_dir = tmpdir.mkdir('target')

    create_markdown_file(source_dir.join('ns1',
                                         'file1.md'), {'title': 'Page One'},
                         'A link to [[2nd Page|Y:Page Two]]')

    create_markdown_file(source_dir.join('ns2', 'file2.md'),
                         {'title': 'Page Two'}, 'A link to [[X:Page One]]')

    create_wiki_config(
        str(source_dir.join('test.cfg')), None, {
            'name': 'ns1',
            'alias': 'X',
            'path': f'{source_dir.join("ns1")}',
            'target': str(target_dir)
        }, {
            'name': 'ns2',
            'alias': 'Y',
            'path': f'{source_dir.join("ns2")}',
            'target': str(target_dir)
        })

    wiki = Wiki(source_dir.join('test.cfg'))

    wiki.process_namespaces()

    assert len(wiki) == 2
    assert len(wiki.namespaces['ns1']) == 1
    assert len(wiki.namespaces['ns2']) == 1

    expect1 = create_markdown_string(
        {'title': 'Page One'}, 'A link to [2nd Page](../ns2/page_two.html)')

    assert os.path.exists(target_dir.join('ns1', 'page_one.md'))

    with open(target_dir.join('ns1', 'page_one.md'), 'r',
              encoding='utf8') as fh:
        actual1 = fh.read()

    assert compare_markdown_content(expect1, actual1)

    expect2 = create_markdown_string(
        {'title': 'Page Two'}, 'A link to [Page One](../ns1/page_one.html)')

    assert os.path.exists(target_dir.join('ns2', 'page_two.md'))

    with open(target_dir.join('ns2', 'page_two.md'), 'r',
              encoding='utf8') as fh:
        actual2 = fh.read()

    assert compare_markdown_content(expect2, actual2)
Esempio n. 5
0
def test_search_index_prefix(tmpdir):

    source_dir = tmpdir.mkdir('source')
    source_dir.mkdir('ns1')
    target_dir = tmpdir.mkdir('target')

    create_markdown_file(source_dir.join('ns1', 'file1.md'),
                         {'title': 'Page One',
                          'tags': '[abc]'},
                         'A link to [[Page Two]]')

    create_markdown_file(source_dir.join('ns1', 'file2.md'),
                         {'title': 'Page Two',
                          'tags': '[abc]'},
                         'A link to [[Page One]]')

    create_wiki_config(str(source_dir.join('test.cfg')),
                       None,
                       {'name': 'ns1',
                        'path': f'{source_dir.join("ns1")}',
                        'target': str(target_dir),
                        'search_fields': 'title,alias,tags,summary,keywords',
                        'search_prefix': 'var MW = MW || {};\nMW.searchIndex = '})

    wiki = Wiki(source_dir.join('test.cfg'))

    wiki.process_namespaces()

    assert os.path.exists(target_dir.join('ns1', 'page_one.md'))
    assert os.path.exists(target_dir.join('ns1', '_index.json'))

    expect = {
        "page": [
            ["page_one", "Page One"],
            ["page_two", "Page Two"]
        ],
        "abc": [
            ["page_one", "Page One"],
            ["page_two", "Page Two"]
        ],
        "one": [
            ["page_one", "Page One"]
        ],
        "two": [
            ["page_two", "Page Two"]
        ]
    }

    with open(target_dir.join('ns1', '_index.json'), 'r', encoding='utf8') as fh:
        index = fh.read()

    assert index.startswith('var MW = MW || {};\nMW.searchIndex =')

    actual = json.loads('{' + index.split('{', 2)[2])

    # use DeepDiff to compare structures
    assert not deepdiff.DeepDiff(expect, actual, ignore_order=True)
Esempio n. 6
0
def test_process_tags_directive_and_three(tmpdir):
    """Test AND
    """

    source_dir = tmpdir.mkdir('source')
    source_dir.mkdir('ns1')
    target_dir = tmpdir.mkdir('target')

    create_markdown_file(source_dir.join('ns1', 'file1.md'), {
        'title': 'Page One',
        'tags': '[abc]'
    }, '{{abc +def +ghi}}')

    create_markdown_file(source_dir.join('ns1', 'file2.md'), {
        'title': 'Page Two',
        'tags': '[abc, def]'
    }, 'Text')

    create_markdown_file(source_dir.join('ns1', 'file3.md'), {
        'title': 'Page Three',
        'tags': '[xyz]'
    }, '{{xyz}}')

    create_markdown_file(source_dir.join('ns1', 'file4.md'), {
        'title': 'Page Four',
        'tags': '[abc, def, ghi]'
    }, 'Text')

    create_wiki_config(
        str(source_dir.join('test.cfg')), None, {
            'name': 'ns1',
            'path': f'{source_dir.join("ns1")}',
            'target': str(target_dir)
        })

    wiki = Wiki(source_dir.join('test.cfg'))

    wiki.process_namespaces()

    # assert page 4 only one with all three tags
    expect1 = create_markdown_string({
        'title': 'Page One',
        'tags': '[abc]'
    }, '''[Page Four](page_four.html)

''')

    assert os.path.exists(target_dir.join('ns1', 'page_one.md'))

    with open(target_dir.join('ns1', 'page_one.md'), 'r',
              encoding='utf8') as fh:
        actual1 = fh.read()

    assert compare_markdown_content(expect1, actual1)
Esempio n. 7
0
def test_process_tags_directive_or(tmpdir):
    """Test OR
    """

    source_dir = tmpdir.mkdir('source')
    source_dir.mkdir('ns1')
    target_dir = tmpdir.mkdir('target')

    create_markdown_file(source_dir.join('ns1', 'file1.md'), {
        'title': 'Page One',
        'tags': '[abc]'
    }, '{{abc def}}')

    create_markdown_file(source_dir.join('ns1', 'file2.md'), {
        'title': 'Page Two',
        'tags': '[def]'
    }, 'Text')

    create_markdown_file(source_dir.join('ns1', 'file3.md'), {
        'title': 'Page Three',
        'tags': '[xyz]'
    }, '{{xyz}}')

    create_wiki_config(
        str(source_dir.join('test.cfg')), None, {
            'name': 'ns1',
            'path': f'{source_dir.join("ns1")}',
            'target': str(target_dir)
        })

    wiki = Wiki(source_dir.join('test.cfg'))

    wiki.process_namespaces()

    # assert page 3 not in list as it is not tagged 'abc'
    expect1 = create_markdown_string({
        'title': 'Page One',
        'tags': '[abc]'
    }, '''[Page One](page_one.html)

[Page Two](page_two.html)

''')

    assert os.path.exists(target_dir.join('ns1', 'page_one.md'))

    with open(target_dir.join('ns1', 'page_one.md'), 'r',
              encoding='utf8') as fh:
        actual1 = fh.read()

    assert compare_markdown_content(expect1, actual1)
Esempio n. 8
0
def test_process_tags_directive_list_tags(tmpdir):
    """Test LIST TAGS
    """

    source_dir = tmpdir.mkdir('source')
    source_dir.mkdir('ns1')
    target_dir = tmpdir.mkdir('target')

    create_markdown_file(source_dir.join('ns1', 'file1.md'), {
        'title': 'Page One',
        'tags': '[abc]'
    }, '{{@}}')

    create_markdown_file(source_dir.join('ns1', 'file2.md'), {
        'title': 'Page Two',
        'tags': '[abc, def]'
    }, 'Text')

    create_markdown_file(source_dir.join('ns1', 'file3.md'), {
        'title': 'Page Three',
        'tags': '[xyz]'
    }, '{{xyz}}')

    create_wiki_config(
        str(source_dir.join('test.cfg')), None, {
            'name': 'ns1',
            'path': f'{source_dir.join("ns1")}',
            'target': str(target_dir)
        })

    wiki = Wiki(source_dir.join('test.cfg'))

    wiki.process_namespaces()

    # assert all pages are counted
    expect1 = create_markdown_string({
        'title': 'Page One',
        'tags': '[abc]'
    }, '''[abc]{.tag}

[def]{.tag}

[xyz]{.tag}''')

    assert os.path.exists(target_dir.join('ns1', 'page_one.md'))

    with open(target_dir.join('ns1', 'page_one.md'), 'r',
              encoding='utf8') as fh:
        actual1 = fh.read()

    assert compare_markdown_content(expect1, actual1)
Esempio n. 9
0
def test_process_tags_directive_list_all(tmpdir):
    """Test LIST ALL
    """

    source_dir = tmpdir.mkdir('source')
    source_dir.mkdir('ns1')
    target_dir = tmpdir.mkdir('target')

    create_markdown_file(source_dir.join('ns1', 'file1.md'), {
        'title': 'Page One',
        'tags': '[abc]'
    }, '{{*}}')

    create_markdown_file(source_dir.join('ns1', 'file2.md'), {
        'title': 'Page Two',
        'tags': '[def]'
    }, 'Text')

    create_markdown_file(source_dir.join('ns1', 'file3.md'), {
        'title': 'Page Three',
        'tags': '[xyz]'
    }, '{{xyz}}')

    create_wiki_config(
        str(source_dir.join('test.cfg')), None, {
            'name': 'ns1',
            'path': f'{source_dir.join("ns1")}',
            'target': str(target_dir)
        })

    wiki = Wiki(source_dir.join('test.cfg'))

    wiki.process_namespaces()

    # assert all pages are listed. NOTE: list is alphabetical (Three before Two!)
    expect1 = create_markdown_string({
        'title': 'Page One',
        'tags': '[abc]'
    }, '''[Page One](page_one.html)

[Page Three](page_three.html)

[Page Two](page_two.html)''')

    assert os.path.exists(target_dir.join('ns1', 'page_one.md'))

    with open(target_dir.join('ns1', 'page_one.md'), 'r',
              encoding='utf8') as fh:
        actual1 = fh.read()

    assert compare_markdown_content(expect1, actual1)
Esempio n. 10
0
def test_process_tags_directive_not_and(tmpdir):
    """Test NOT
    """

    source_dir = tmpdir.mkdir('source')
    source_dir.mkdir('ns1')
    target_dir = tmpdir.mkdir('target')

    create_markdown_file(source_dir.join('ns1', 'file1.md'), {
        'title': 'Page One',
        'tags': '[abc]'
    }, '{{abc +def -ghi}}')

    create_markdown_file(source_dir.join('ns1', 'file2.md'), {
        'title': 'Page Two',
        'tags': '[abc, def]'
    }, 'Text')

    create_markdown_file(source_dir.join('ns1', 'file3.md'), {
        'title': 'Page Three',
        'tags': '[abc, def, ghi, jkl]'
    }, 'Text')

    create_wiki_config(
        str(source_dir.join('test.cfg')), None, {
            'name': 'ns1',
            'path': f'{source_dir.join("ns1")}',
            'target': str(target_dir)
        })

    wiki = Wiki(source_dir.join('test.cfg'))

    wiki.process_namespaces()

    # assert page 4 not in list, has def but also ghi
    expect1 = create_markdown_string({
        'title': 'Page One',
        'tags': '[abc]'
    }, '''[Page Two](page_two.html)

''')

    assert os.path.exists(target_dir.join('ns1', 'page_one.md'))

    with open(target_dir.join('ns1', 'page_one.md'), 'r',
              encoding='utf8') as fh:
        actual1 = fh.read()

    assert compare_markdown_content(expect1, actual1)
Esempio n. 11
0
def test_process_page_links_metadata_list(tmpdir):
    source_dir = tmpdir.mkdir('source')
    source_dir.mkdir('ns1')
    target_dir = tmpdir.mkdir('target')

    create_markdown_file(source_dir.join('ns1', 'file1.md'), {
        'title': 'Page One',
        'tags': '[Page Two, Page Three]'
    }, 'Text')

    create_markdown_file(source_dir.join('ns1', 'file2.md'),
                         {'title': 'Page Two'}, 'Text')

    create_markdown_file(source_dir.join('ns1', 'file3.md'),
                         {'title': 'Page Three'}, 'Text')

    create_wiki_config(
        str(source_dir.join('test.cfg')), None, {
            'name': 'ns1',
            'path': f'{source_dir.join("ns1")}',
            'target': str(target_dir),
            'meta_fields': 'tags'
        })

    wiki = Wiki(source_dir.join('test.cfg'))

    wiki.process_namespaces()

    assert len(wiki) == 1
    assert len(wiki.namespaces['ns1']) == 3

    expect1 = create_markdown_string(
        {
            'title': 'Page One',
            'tags':
            ['[Page Two](page_two.html)', '[Page Three](page_three.html)']
        }, 'Text')

    assert os.path.exists(target_dir.join('ns1', 'page_one.md'))

    with open(target_dir.join('ns1', 'page_one.md'), 'r',
              encoding='utf8') as fh:
        actual1 = fh.read()

    assert compare_markdown_content(expect1, actual1)
Esempio n. 12
0
def test_process_tags_directive(tmpdir):
    """Test that the directive '{{tag}}' produces a list of pages with that tag,
    and that pages without those tags are not included in the list
    """

    source_dir = tmpdir.mkdir('source')
    source_dir.mkdir('ns1')
    target_dir = tmpdir.mkdir('target')

    create_markdown_file(source_dir.join('ns1', 'file1.md'), {
        'title': 'Page One',
        'tags': '[abc]'
    }, '{{abc}}')

    create_markdown_file(source_dir.join('ns1', 'file2.md'), {
        'title': 'Page Two',
        'tags': '[abc]'
    }, 'Text')

    create_markdown_file(source_dir.join('ns1', 'file3.md'), {
        'title': 'Page Three',
        'tags': '[xyz]'
    }, '{{xyz}}')

    create_wiki_config(
        str(source_dir.join('test.cfg')), None, {
            'name': 'ns1',
            'path': f'{source_dir.join("ns1")}',
            'target': str(target_dir)
        })

    wiki = Wiki(source_dir.join('test.cfg'))

    wiki.process_namespaces()

    # assert page 3 not in list as it is not tagged 'abc'
    expect1 = create_markdown_string({
        'title': 'Page One',
        'tags': '[abc]'
    }, '''[Page One](page_one.html)

[Page Two](page_two.html)

''')

    assert os.path.exists(target_dir.join('ns1', 'page_one.md'))

    with open(target_dir.join('ns1', 'page_one.md'), 'r',
              encoding='utf8') as fh:
        actual1 = fh.read()

    # assert page 3 only contains itself, as only it is tagged 'xyz'
    assert compare_markdown_content(expect1, actual1)

    expect3 = create_markdown_string({
        'title': 'Page Three',
        'tags': '[xyz]'
    }, '''[Page Three](page_three.html)

''')

    assert os.path.exists(target_dir.join('ns1', 'page_three.md'))

    with open(target_dir.join('ns1', 'page_three.md'), 'r',
              encoding='utf8') as fh:
        actual3 = fh.read()

    assert compare_markdown_content(expect3, actual3)