예제 #1
0
def main():
    """ Main method to trigger generation of all pages
    """

    if len(argv) == 3 and argv[1] == '-config':
        config_filename = argv[2]
    else:
        config_filename = '../config/magnetizer.cfg'

    print('Using config ' + config_filename + '...')

    website = Website(config_filename)
    website.wipe()

    Webpage.write_list_pages_from_directory(
        website, website.config.value('source_path'))
    Webpage.write_item_pages_from_directory(
        website, website.config.value('source_path'))

    website.copy_resources()
    website.sitemap.write(website.config.value('output_path'))

    atom = Atom(website)
    atom.write()
def test_three_paginated_list_pages():
    """ Test 3 listing pages, with pagination
    """

    TEST_WEBSITE.wipe()
    _clean_up_test_items_md()
    _generate_test_items_md(10)

    Webpage.write_list_pages_from_directory(
        TEST_WEBSITE, TEST_WEBSITE.config.value('source_path'))

    # There should be an index.html and exactly 2 blog-n.html files
    assert path.isfile(TEST_WEBSITE.config.value('output_path') + 'index.html')
    assert not path.isfile(
        TEST_WEBSITE.config.value('output_path') + 'blog-1.html')
    assert path.isfile(
        TEST_WEBSITE.config.value('output_path') + 'blog-2.html')
    assert path.isfile(
        TEST_WEBSITE.config.value('output_path') + 'blog-3.html')
    assert not path.isfile(
        TEST_WEBSITE.config.value('output_path') + 'blog-4.html')

    with open(TEST_WEBSITE.config.value('output_path') + 'index.html',
              'r') as myfile:
        blog_1_content = myfile.read()

    with open(TEST_WEBSITE.config.value('output_path') + 'blog-2.html',
              'r') as myfile:
        blog_2_content = myfile.read()

    with open(TEST_WEBSITE.config.value('output_path') + 'blog-3.html',
              'r') as myfile:
        blog_3_content = myfile.read()

    assert blog_1_content.count('<article>') == 4
    assert 'Article 10.' in blog_1_content
    assert 'Article 9.' in blog_1_content
    assert 'Article 8.' in blog_1_content
    assert 'Article 7.' in blog_1_content
    assert '<p>Listing page template</p>' in blog_1_content

    assert blog_2_content.count('<article>') == 4
    assert 'Article 6.' in blog_2_content
    assert 'Article 5.' in blog_2_content
    assert 'Article 4.' in blog_2_content
    assert 'Article 3.' in blog_2_content
    assert '<p>Listing page template</p>' in blog_2_content

    assert blog_3_content.count('<article>') == 2
    assert 'Article 2.' in blog_3_content
    assert 'Article 1.' in blog_3_content
    assert '<p>Listing page template</p>' in blog_3_content

    # Page title = "Website Name - Page n"
    assert 'Test website name - test tag & line' in blog_1_content
    assert '<title>Test website name - Page 2</title>' in blog_2_content
    assert '<title>Test website name - Page 3</title>' in blog_3_content

    # First page should have link to older posts but not newer
    assert '<a href="blog-2.html" class="magnetizer-next">Older posts</a>' in blog_1_content
    assert 'class="magnetizer-previous"' not in blog_1_content

    # Middle page should have link to older posts (i.e. homepage) and newer
    assert '<a href="blog-3.html" class="magnetizer-next">Older posts</a>' in blog_2_content
    assert '<a href="/" class="magnetizer-previous">Newer posts</a>' in blog_2_content

    # Last page should have link to newer posts but not older
    assert 'class="magnetizer-next"' not in blog_3_content
    assert '<a href="blog-2.html" class="magnetizer-previous">Newer posts</a>' in blog_3_content

    # Pages should have meta description from config
    assert '<meta name="description" content="Meta \\"description\\" from cfg">' in blog_1_content
    assert '<meta name="description" content="Meta \\"description\\" from cfg">' in blog_2_content
    assert '<meta name="description" content="Meta \\"description\\" from cfg">' in blog_3_content

    # index.html and the blog-n pages should be present in the sitemap
    assert 'https://example.com/' in TEST_WEBSITE.sitemap.pages
    assert not 'https://example.com/blog-1.html' in TEST_WEBSITE.sitemap.pages
    assert 'https://example.com/blog-2.html' in TEST_WEBSITE.sitemap.pages
    assert 'https://example.com/blog-3.html' in TEST_WEBSITE.sitemap.pages
def test_single_list_page():
    """ Test when there is just one listing page, so no pagination etc
    """

    TEST_WEBSITE.wipe()
    _clean_up_test_items_md()
    _generate_test_items_md(4)
    _generate_non_indexable_test_items_md()

    Webpage.write_list_pages_from_directory(
        TEST_WEBSITE, TEST_WEBSITE.config.value('source_path'))

    # There should be an index.html but no blog-n.html files
    assert path.isfile(TEST_WEBSITE.config.value('output_path') + 'index.html')
    assert not path.isfile(
        TEST_WEBSITE.config.value('output_path') + 'blog-1.html')
    assert not path.isfile(
        TEST_WEBSITE.config.value('output_path') + 'blog-2.html')

    with open(TEST_WEBSITE.config.value('output_path') + 'index.html',
              'r') as myfile:
        blog_1_content = myfile.read()

    assert blog_1_content.count('<article>') == 4
    assert 'Article 4.' in blog_1_content
    assert 'Article 3.' in blog_1_content
    assert 'Article 2.' in blog_1_content
    assert 'Article 1.' in blog_1_content

    # Page should use listing page template
    assert '<p>Listing page template</p>' in blog_1_content

    # Index title = "Website Name - Page 1"
    assert 'Test website name - test tag & line' in blog_1_content

    # Don't show article footers on list page
    assert '<footer>footer</footer>' not in blog_1_content

    # Body should have class='magnetizer-listing-page'
    assert "<body class='magnetizer-listing-page'>" in blog_1_content

    # Twitter card should *not* be present
    assert '<meta name="twitter:card" content="summary" />' not in blog_1_content

    # Link to Atom feed should be present
    assert ('<link rel="alternate" type="application/rss+xml" ' +
            'href="https://example.com/atom.xml" />') in blog_1_content

    # No links previous/next page should be present
    assert 'class="magnetizer-pagination"' not in blog_1_content
    assert 'class="magnetizer-previous"' not in blog_1_content
    assert 'class="magnetizer-next"' not in blog_1_content

    # The index page should be present in the sitemap
    assert 'https://example.com/' in TEST_WEBSITE.sitemap.pages

    # Link to CSS should be present
    assert '<link rel="stylesheet" type="text/css" href="test-stylesheet.css' in blog_1_content

    # Meta description from config file should be present
    assert '<meta name="description" content="Meta \\"description\\" from cfg">' in blog_1_content