Пример #1
0
    def test_use_filename_as_slug_fallback(self):
        file_metadata = [
            ".. title: Nikola needs more tests!\n",
            ".. date: 2012/09/15 19:52:05\n",
            ".. tags:\n",
            ".. link:\n",
            ".. description:\n",
            "Post content\n",
        ]

        opener_mock = mock.mock_open(read_data=file_metadata)
        opener_mock.return_value.readlines.return_value = file_metadata

        post = dummy()
        post.source_path = "Slugify this"
        post.metadata_path = "Slugify this.meta"

        with mock.patch("nikola.post.codecs.open", opener_mock, create=True):
            meta = get_meta(post, "Slugify this")

        self.assertEqual("Nikola needs more tests!", meta["title"])
        self.assertEqual("slugify-this", meta["slug"])
        self.assertEqual("2012/09/15 19:52:05", meta["date"])
        self.assertFalse("tags" in meta)
        self.assertFalse("link" in meta)
        self.assertFalse("description" in meta)
Пример #2
0
    def test_get_title_from_fname(self):
        file_metadata = [
            ".. slug: write-tests-now\n",
            ".. date: 2012/09/15 19:52:05\n",
            ".. tags:\n",
            ".. link:\n",
            ".. description:\n",
        ]

        opener_mock = mock.mock_open(read_data=file_metadata)
        opener_mock.return_value.readlines.return_value = file_metadata

        post = dummy()
        post.source_path = "file_with_metadata"
        post.metadata_path = "file_with_metadata.meta"

        with mock.patch("nikola.post.codecs.open", opener_mock, create=True):
            meta = get_meta(post, "file_with_metadata")

        self.assertEqual("file_with_metadata", meta["title"])
        self.assertEqual("write-tests-now", meta["slug"])
        self.assertEqual("2012/09/15 19:52:05", meta["date"])
        self.assertFalse("tags" in meta)
        self.assertFalse("link" in meta)
        self.assertFalse("description" in meta)
Пример #3
0
    def test_get_title_from_rest(self):
        file_metadata = [".. slug: write-tests-now\n",
                         ".. date: 2012/09/15 19:52:05\n",
                         ".. tags:\n",
                         ".. link:\n",
                         ".. description:\n",
                         "Post Title\n",
                         "----------\n"]

        opener_mock = mock.mock_open(read_data=file_metadata)
        opener_mock.return_value.readlines.return_value = file_metadata

        post = dummy()
        post.source_path = 'file_with_metadata'
        post.metadata_path = 'file_with_metadata.meta'

        with mock.patch('nikola.post.io.open', opener_mock, create=True):
            meta, nsm = get_meta(post)

        self.assertEqual('Post Title', meta['title'])
        self.assertEqual('write-tests-now', meta['slug'])
        self.assertEqual('2012/09/15 19:52:05', meta['date'])
        self.assertFalse('tags' in meta)
        self.assertFalse('link' in meta)
        self.assertFalse('description' in meta)
        self.assertTrue(nsm)
Пример #4
0
    def test_getting_metadata_from_content(self):
        file_metadata = [".. title: Nikola needs more tests!\n",
                         ".. slug: write-tests-now\n",
                         ".. date: 2012/09/15 19:52:05\n",
                         ".. tags:\n",
                         ".. link:\n",
                         ".. description:\n",
                         "Post content\n"]

        opener_mock = mock.mock_open(read_data=file_metadata)
        opener_mock.return_value.readlines.return_value = file_metadata

        post = dummy()
        post.source_path = 'file_with_metadata'
        post.metadata_path = 'file_with_metadata.meta'

        with mock.patch('nikola.post.codecs.open', opener_mock, create=True):
            meta = get_meta(post)

        self.assertEqual('Nikola needs more tests!', meta['title'])
        self.assertEqual('write-tests-now', meta['slug'])
        self.assertEqual('2012/09/15 19:52:05', meta['date'])
        self.assertFalse('tags' in meta)
        self.assertFalse('link' in meta)
        self.assertFalse('description' in meta)
Пример #5
0
def test_nikola_meta_markdown(metadata_extractors_by, meta_twofile):
    twofile_number, twofile_expected, twofile_unexpected = meta_twofile
    twofile = twofile_number == 2

    source_filename = "f-markdown-{0}-nikola.md".format(twofile_number)
    metadata_filename = "f-markdown-{0}-nikola.meta".format(twofile_number)
    title = 'T: Markdown, {0}, Nikola'.format(twofile_number)
    slug = "s-markdown-{0}-nikola".format(twofile_number)
    source_path = os.path.abspath(
        os.path.join(os.path.dirname(__file__), 'data', 'metadata_extractors',
                     source_filename))
    metadata_path = os.path.abspath(
        os.path.join(os.path.dirname(__file__), 'data', 'metadata_extractors',
                     metadata_filename))
    post = FakePost(source_path, metadata_path, {}, None,
                    metadata_extractors_by)

    assert os.path.exists(source_path)
    if twofile:
        assert os.path.exists(metadata_path)

    meta, extractor = get_meta(post, None)
    assert extractor is metadata_extractors_by['name']['nikola']

    assert meta['title'] == title
    assert meta['slug'] == slug
    assert twofile_expected in meta['tags']
    assert twofile_unexpected not in meta['tags']
    assert 'meta' in meta['tags']
    assert 'Nikola' in meta['tags']
    assert 'Markdown' in meta['tags']
    assert meta['date'] == '2017-07-01 00:00:00 UTC'
Пример #6
0
def test_nikola_meta_markdown(metadata_extractors_by, testfiledir, filecount,
                              expected, unexpected):
    is_two_files = filecount == 2

    source_filename = "f-markdown-{0}-nikola.md".format(filecount)
    metadata_filename = "f-markdown-{0}-nikola.meta".format(filecount)
    source_path = os.path.join(testfiledir, source_filename)
    metadata_path = os.path.join(testfiledir, metadata_filename)
    post = FakePost(source_path, metadata_path, {}, None,
                    metadata_extractors_by)

    assert os.path.exists(source_path)
    if is_two_files:
        assert os.path.exists(metadata_path)

    meta, extractor = get_meta(post, None)
    assert extractor is metadata_extractors_by["name"]["nikola"]

    assert meta["title"] == "T: Markdown, {0}, Nikola".format(filecount)
    assert meta["slug"] == "s-markdown-{0}-nikola".format(filecount)
    assert expected in meta["tags"]
    assert unexpected not in meta["tags"]
    assert "meta" in meta["tags"]
    assert "Nikola" in meta["tags"]
    assert "Markdown" in meta["tags"]
    assert meta["date"] == "2017-07-01 00:00:00 UTC"
def test_compiler_metadata(metadata_extractors_by, compiler_data):
    compiler_cls, compiler_ext, compiler_lc, compiler_name = compiler_data
    source_filename = "f-{0}-1-compiler.{1}".format(compiler_lc, compiler_ext)
    metadata_filename = "f-{0}-1-compiler.meta".format(compiler_lc)
    title = 'T: {0}, 1, compiler'.format(compiler_name)
    slug = "s-{0}-1-compiler".format(compiler_lc)
    source_path = os.path.abspath(os.path.join(os.path.dirname(__file__), 'data', 'metadata_extractors', source_filename))
    metadata_path = os.path.abspath(os.path.join(os.path.dirname(__file__), 'data', 'metadata_extractors', metadata_filename))

    config = {'USE_REST_DOCINFO_METADATA': True, 'MARKDOWN_EXTENSIONS': ['markdown.extensions.meta']}
    site = FakeSite()
    site.config.update(config)
    compiler_obj = compiler_cls()
    compiler_obj.set_site(site)

    post = FakePost(source_path, metadata_path, config, compiler_obj, metadata_extractors_by)

    class FakeBorg():
        current_lang = 'en'

        def __call__(self):
            return self

    with mock.patch('nikola.plugins.compile.' + compiler_lc + '.LocaleBorg', FakeBorg):
        meta, extractor = get_meta(post, None)

    assert meta['title'] == title
    assert meta['slug'] == slug
    assert 'meta' in meta['tags']
    assert 'onefile' in meta['tags']
    assert 'compiler' in meta['tags']
    assert compiler_name in meta['tags']
    assert meta['date'] == '2017-07-01 00:00:00 UTC'
Пример #8
0
    def test_getting_metadata_from_content(self):
        file_metadata = [".. title: Nikola needs more tests!\n",
                         ".. slug: write-tests-now\n",
                         ".. date: 2012/09/15 19:52:05\n",
                         ".. tags:\n",
                         ".. link:\n",
                         ".. description:\n",
                         "Post content\n"]

        opener_mock = mock.mock_open(read_data=file_metadata)
        opener_mock.return_value.readlines.return_value = file_metadata

        post = dummy()
        post.source_path = 'file_with_metadata'
        post.metadata_path = 'file_with_metadata.meta'

        with mock.patch('nikola.post.codecs.open', opener_mock, create=True):
            meta = get_meta(post)

        self.assertEqual('Nikola needs more tests!', meta['title'])
        self.assertEqual('write-tests-now', meta['slug'])
        self.assertEqual('2012/09/15 19:52:05', meta['date'])
        self.assertFalse('tags' in meta)
        self.assertFalse('link' in meta)
        self.assertFalse('description' in meta)
def test_builtin_extractors_rest(metadata_extractors_by, meta_twofile, meta_format):
    twofile_number, twofile_expected, twofile_unexpected = meta_twofile
    twofile = twofile_number == 2
    format_lc, format_friendly = meta_format

    source_filename = "f-rest-{0}-{1}.rst".format(twofile_number, format_lc)
    metadata_filename = "f-rest-{0}-{1}.meta".format(twofile_number, format_lc)
    title = 'T: reST, {0}, {1}'.format(twofile_number, format_friendly)
    slug = "s-rest-{0}-{1}".format(twofile_number, format_lc)
    source_path = os.path.abspath(os.path.join(os.path.dirname(__file__), 'data', 'metadata_extractors', source_filename))
    metadata_path = os.path.abspath(os.path.join(os.path.dirname(__file__), 'data', 'metadata_extractors', metadata_filename))
    post = FakePost(source_path, metadata_path, {}, None, metadata_extractors_by)

    assert os.path.exists(source_path)
    if twofile:
        assert os.path.exists(metadata_path)

    meta, extractor = get_meta(post, None)

    assert meta
    if twofile:
        assert extractor is None
    else:
        assert extractor is metadata_extractors_by['name'][format_lc]

    assert meta['title'] == title
    assert meta['slug'] == slug
    assert twofile_expected in meta['tags']
    assert twofile_unexpected not in meta['tags']
    assert 'meta' in meta['tags']
    assert format_friendly in meta['tags']
    assert 'reST' in meta['tags']
    assert meta['date'] == '2017-07-01 00:00:00 UTC'
def test_nikola_meta_markdown(metadata_extractors_by, meta_twofile):
    twofile_number, twofile_expected, twofile_unexpected = meta_twofile
    twofile = twofile_number == 2

    source_filename = "f-markdown-{0}-nikola.md".format(twofile_number)
    metadata_filename = "f-markdown-{0}-nikola.meta".format(twofile_number)
    title = 'T: Markdown, {0}, Nikola'.format(twofile_number)
    slug = "s-markdown-{0}-nikola".format(twofile_number)
    source_path = os.path.abspath(os.path.join(os.path.dirname(__file__), 'data', 'metadata_extractors', source_filename))
    metadata_path = os.path.abspath(os.path.join(os.path.dirname(__file__), 'data', 'metadata_extractors', metadata_filename))
    post = FakePost(source_path, metadata_path, {}, None, metadata_extractors_by)

    assert os.path.exists(source_path)
    if twofile:
        assert os.path.exists(metadata_path)

    meta, extractor = get_meta(post, None)
    if twofile:
        assert extractor is None
    else:
        assert extractor is metadata_extractors_by['name']['nikola']

    assert meta['title'] == title
    assert meta['slug'] == slug
    assert twofile_expected in meta['tags']
    assert twofile_unexpected not in meta['tags']
    assert 'meta' in meta['tags']
    assert 'Nikola' in meta['tags']
    assert 'Markdown' in meta['tags']
    assert meta['date'] == '2017-07-01 00:00:00 UTC'
Пример #11
0
    def test_get_title_from_rest(self):
        file_metadata = ".. slug: write-tests-now\n"\
                        ".. date: 2012/09/15 19:52:05\n"\
                        ".. tags:\n"\
                        ".. link:\n"\
                        ".. description:\n\n"\
                        "Post Title\n"\
                        "----------\n"

        opener_mock = mock.mock_open(read_data=file_metadata)

        post = dummy()
        post.source_path = 'file_with_metadata'
        post.metadata_path = 'file_with_metadata.meta'

        with mock.patch('nikola.post.io.open', opener_mock, create=True):
            meta, nsm = get_meta(post)

        self.assertEqual('Post Title', meta['title'])
        self.assertEqual('write-tests-now', meta['slug'])
        self.assertEqual('2012/09/15 19:52:05', meta['date'])
        self.assertFalse('tags' in meta)
        self.assertFalse('link' in meta)
        self.assertFalse('description' in meta)
        self.assertTrue(nsm)
Пример #12
0
    def test_use_filename_as_slug_fallback(self):
        file_metadata = [".. title: Nikola needs more tests!\n",
                         ".. date: 2012/09/15 19:52:05\n",
                         ".. tags:\n",
                         ".. link:\n",
                         ".. description:\n",
                         "Post content\n"]

        opener_mock = mock.mock_open(read_data=file_metadata)
        opener_mock.return_value.readlines.return_value = file_metadata

        post = dummy()
        post.source_path = 'Slugify this'
        post.metadata_path = 'Slugify this.meta'

        with mock.patch('nikola.post.io.open', opener_mock, create=True):
            meta, nsm = get_meta(post, 'Slugify this')

        self.assertEqual('Nikola needs more tests!', meta['title'])
        self.assertEqual('slugify-this', meta['slug'])
        self.assertEqual('2012/09/15 19:52:05', meta['date'])
        self.assertFalse('tags' in meta)
        self.assertFalse('link' in meta)
        self.assertFalse('description' in meta)
        self.assertTrue(nsm)
Пример #13
0
    def test_get_meta_slug_only_from_filename(self):
        post = dummy()
        post.source_path = "some/path/the_slug.md"
        post.metadata_path = "some/path/the_slug.meta"
        with mock.patch("nikola.post.codecs.open", create=True):
            meta = get_meta(post)

        self.assertEqual("the_slug", meta["slug"])
Пример #14
0
 def validate(post):
     meta = get_meta(post, 'fr')
     has_reference = len(meta[0]['references']) > 0
     has_description = len(post.description()) > 0
     if has_reference & has_description:
         return '\033[92m Valid \033[0m'
     else:
         return '\033[91m Invalid \033[0m'
Пример #15
0
    def test_get_meta_slug_only_from_filename(self):
        post = dummy()
        post.source_path = 'some/path/the_slug.md'
        post.metadata_path = 'some/path/the_slug.meta'
        with mock.patch('nikola.post.io.open', create=True):
            meta, _ = get_meta(post)

        self.assertEqual('the_slug', meta['slug'])
Пример #16
0
    def test_get_meta_slug_only_from_filename(self):
        post = dummy()
        post.source_path = 'some/path/the_slug.md'
        post.metadata_path = 'some/path/the_slug.meta'
        with mock.patch('nikola.post.io.open', create=True):
            meta = get_meta(post)

        self.assertEqual('the_slug', meta['slug'])
Пример #17
0
    def test_get_meta_slug_only_from_filename(self):
        dummy_opener_mock = mock.mock_open(read_data="No metadata in the file!")
        post = dummy()
        post.source_path = 'some/path/the_slug.md'
        post.metadata_path = 'some/path/the_slug.meta'
        with mock.patch('nikola.post.io.open', dummy_opener_mock, create=True):
            meta = get_meta(post, None)[0]

        self.assertEqual('the_slug', meta['slug'])
Пример #18
0
def test_get_meta_slug_only_from_filename(post):
    post.source_path = "some/path/the_slug.md"
    post.metadata_path = "some/path/the_slug.meta"

    no_metadata_opener = mock.mock_open(read_data="No metadata in the file!")
    with mock.patch("nikola.post.io.open", no_metadata_opener, create=True):
        meta = get_meta(post, None)[0]

    assert "the_slug" == meta["slug"]
Пример #19
0
    def test_get_meta_slug_only_from_filename(self):
        dummy_opener_mock = mock.mock_open(
            read_data="No metadata in the file!")
        post = dummy()
        post.source_path = 'some/path/the_slug.md'
        post.metadata_path = 'some/path/the_slug.meta'
        with mock.patch('nikola.post.io.open', dummy_opener_mock, create=True):
            meta = get_meta(post, None)[0]

        self.assertEqual('the_slug', meta['slug'])
Пример #20
0
    def test_extracting_metadata_from_filename(self):
        post = dummy()
        post.source_path = "2013-01-23-the_slug-dubdubtitle.md"
        post.metadata_path = "2013-01-23-the_slug-dubdubtitle.meta"
        with mock.patch("nikola.post.codecs.open", create=True):
            meta = get_meta(post, "(?P<date>\d{4}-\d{2}-\d{2})-(?P<slug>.*)-(?P<title>.*)\.md")

        self.assertEqual("dubdubtitle", meta["title"])
        self.assertEqual("the_slug", meta["slug"])
        self.assertEqual("2013-01-23", meta["date"])
Пример #21
0
    def test_extracting_metadata_from_filename(self):
        post = dummy()
        post.source_path = '2013-01-23-the_slug-dubdubtitle.md'
        post.metadata_path = '2013-01-23-the_slug-dubdubtitle.meta'
        with mock.patch('nikola.post.codecs.open', create=True):
            meta = get_meta(post,
                            '(?P<date>\d{4}-\d{2}-\d{2})-(?P<slug>.*)-(?P<title>.*)\.md')

        self.assertEqual('dubdubtitle', meta['title'])
        self.assertEqual('the_slug', meta['slug'])
        self.assertEqual('2013-01-23', meta['date'])
Пример #22
0
    def test_extracting_metadata_from_filename(self):
        post = dummy()
        post.source_path = '2013-01-23-the_slug-dub_dub_title.md'
        post.metadata_path = '2013-01-23-the_slug-dub_dub_title.meta'
        post.config['FILE_METADATA_REGEXP'] = r'(?P<date>\d{4}-\d{2}-\d{2})-(?P<slug>.*)-(?P<title>.*)\.md'
        for unslugify, title in ((True, 'Dub dub title'), (False, 'dub_dub_title')):
            post.config['FILE_METADATA_UNSLUGIFY_TITLES'] = unslugify
            with mock.patch('nikola.post.io.open', create=True):
                meta = get_meta(post, None)[0]

            self.assertEqual(title, meta['title'])
            self.assertEqual('the_slug', meta['slug'])
            self.assertEqual('2013-01-23', meta['date'])
Пример #23
0
    def test_extracting_metadata_from_filename(self):
        post = dummy()
        post.source_path = '2013-01-23-the_slug-dub_dub_title.md'
        post.metadata_path = '2013-01-23-the_slug-dub_dub_title.meta'
        post.config[
            'FILE_METADATA_REGEXP'] = r'(?P<date>\d{4}-\d{2}-\d{2})-(?P<slug>.*)-(?P<title>.*)\.md'
        for unslugify, title in ((True, 'Dub dub title'), (False,
                                                           'dub_dub_title')):
            post.config['FILE_METADATA_UNSLUGIFY_TITLES'] = unslugify
            with mock.patch('nikola.post.io.open', create=True):
                meta = get_meta(post, None)[0]

            self.assertEqual(title, meta['title'])
            self.assertEqual('the_slug', meta['slug'])
            self.assertEqual('2013-01-23', meta['date'])
Пример #24
0
def test_extracting_metadata_from_filename(post, unslugify, expected_title):
    post.source_path = "2013-01-23-the_slug-dub_dub_title.md"
    post.metadata_path = "2013-01-23-the_slug-dub_dub_title.meta"

    post.config[
        "FILE_METADATA_REGEXP"] = r"(?P<date>\d{4}-\d{2}-\d{2})-(?P<slug>.*)-(?P<title>.*)\.md"
    post.config["FILE_METADATA_UNSLUGIFY_TITLES"] = unslugify

    no_metadata_opener = mock.mock_open(read_data="No metadata in the file!")
    with mock.patch("nikola.post.io.open", no_metadata_opener, create=True):
        meta = get_meta(post, None)[0]

    assert expected_title == meta["title"]
    assert "the_slug" == meta["slug"]
    assert "2013-01-23" == meta["date"]
Пример #25
0
def test_compiler_metadata(metadata_extractors_by, compiler_data):
    compiler_cls, compiler_ext, compiler_lc, compiler_name = compiler_data
    source_filename = "f-{0}-1-compiler.{1}".format(compiler_lc, compiler_ext)
    metadata_filename = "f-{0}-1-compiler.meta".format(compiler_lc)
    title = 'T: {0}, 1, compiler'.format(compiler_name)
    slug = "s-{0}-1-compiler".format(compiler_lc)
    source_path = os.path.abspath(
        os.path.join(os.path.dirname(__file__), 'data', 'metadata_extractors',
                     source_filename))
    metadata_path = os.path.abspath(
        os.path.join(os.path.dirname(__file__), 'data', 'metadata_extractors',
                     metadata_filename))

    config = {
        'USE_REST_DOCINFO_METADATA': True,
        'MARKDOWN_EXTENSIONS': ['markdown.extensions.meta']
    }
    site = FakeSite()
    site.config.update(config)
    compiler_obj = compiler_cls()
    compiler_obj.set_site(site)

    post = FakePost(source_path, metadata_path, config, compiler_obj,
                    metadata_extractors_by)

    class FakeBorg():
        current_lang = 'en'

        def __call__(self):
            return self

    with mock.patch('nikola.plugins.compile.' + compiler_lc + '.LocaleBorg',
                    FakeBorg):
        meta, extractor = get_meta(post, None)

    assert meta['title'] == title
    assert meta['slug'] == slug
    assert 'meta' in meta['tags']
    assert 'onefile' in meta['tags']
    assert 'compiler' in meta['tags']
    assert compiler_name in meta['tags']
    assert meta['date'] == '2017-07-01 00:00:00 UTC'
    def _execute(self, options, args):
        """Announce today's post on Discord using webhooks"""
        self.site.scan_posts()
        today_post = [
            x for x in self.site.timeline
            if x.date.date() == datetime.today().date()
        ]
        if len(today_post) > 0:
            meta = get_meta(today_post[0], 'fr')
            content = list([today_post[0].title()])
            content.append(today_post[0].description())
            [content.append(x) for x in meta[0]['references']]
            content.append(
                'archives: https://www.mad-scientists.net/la-minute-culturelle/'
            )
            content_str = "\n".join(content)

            webhook = DiscordWebhook(url=options['discord-webhook-url'],
                                     content=content_str)
            response = webhook.execute()
        return 0
Пример #27
0
def test_get_title_from_fname(post):
    post.source_path = "file_with_metadata"
    post.metadata_path = "file_with_metadata.meta"

    file_content = """\
.. slug: write-tests-now
.. date: 2012/09/15 19:52:05
.. tags:
.. link:
.. description:
"""
    opener_mock = mock.mock_open(read_data=file_content)
    with mock.patch("nikola.post.io.open", opener_mock, create=True):
        meta = get_meta(post, None)[0]

    assert "file_with_metadata" == meta["title"]
    assert "write-tests-now" == meta["slug"]
    assert "2012/09/15 19:52:05" == meta["date"]
    assert "tags" not in meta
    assert "link" not in meta
    assert "description" not in meta
Пример #28
0
    def test_get_title_from_fname(self):
        file_metadata = ".. slug: write-tests-now\n"\
                        ".. date: 2012/09/15 19:52:05\n"\
                        ".. tags:\n"\
                        ".. link:\n"\
                        ".. description:\n"

        opener_mock = mock.mock_open(read_data=file_metadata)

        post = dummy()
        post.source_path = 'file_with_metadata'
        post.metadata_path = 'file_with_metadata.meta'

        with mock.patch('nikola.post.io.open', opener_mock, create=True):
            meta = get_meta(post, None)[0]

        self.assertEqual('file_with_metadata', meta['title'])
        self.assertEqual('write-tests-now', meta['slug'])
        self.assertEqual('2012/09/15 19:52:05', meta['date'])
        self.assertFalse('tags' in meta)
        self.assertFalse('link' in meta)
        self.assertFalse('description' in meta)
Пример #29
0
def test_builtin_extractors_rest(metadata_extractors_by, meta_twofile,
                                 meta_format):
    twofile_number, twofile_expected, twofile_unexpected = meta_twofile
    twofile = twofile_number == 2
    format_lc, format_friendly = meta_format

    source_filename = "f-rest-{0}-{1}.rst".format(twofile_number, format_lc)
    metadata_filename = "f-rest-{0}-{1}.meta".format(twofile_number, format_lc)
    title = 'T: reST, {0}, {1}'.format(twofile_number, format_friendly)
    slug = "s-rest-{0}-{1}".format(twofile_number, format_lc)
    source_path = os.path.abspath(
        os.path.join(os.path.dirname(__file__), 'data', 'metadata_extractors',
                     source_filename))
    metadata_path = os.path.abspath(
        os.path.join(os.path.dirname(__file__), 'data', 'metadata_extractors',
                     metadata_filename))
    post = FakePost(source_path, metadata_path, {}, None,
                    metadata_extractors_by)

    assert os.path.exists(source_path)
    if twofile:
        assert os.path.exists(metadata_path)

    meta, extractor = get_meta(post, None)

    assert meta
    if twofile:
        assert extractor is None
    else:
        assert extractor is metadata_extractors_by['name'][format_lc]

    assert meta['title'] == title
    assert meta['slug'] == slug
    assert twofile_expected in meta['tags']
    assert twofile_unexpected not in meta['tags']
    assert 'meta' in meta['tags']
    assert format_friendly in meta['tags']
    assert 'reST' in meta['tags']
    assert meta['date'] == '2017-07-01 00:00:00 UTC'
Пример #30
0
def test_compiler_metadata(metadata_extractors_by, testfiledir, compiler,
                           fileextension, compiler_lc, name):
    source_filename = "f-{0}-1-compiler.{1}".format(compiler_lc, fileextension)
    metadata_filename = "f-{0}-1-compiler.meta".format(compiler_lc)
    title = "T: {0}, 1, compiler".format(name)
    slug = "s-{0}-1-compiler".format(compiler_lc)
    source_path = os.path.join(testfiledir, source_filename)
    metadata_path = os.path.join(testfiledir, metadata_filename)

    config = {
        "USE_REST_DOCINFO_METADATA": True,
        "MARKDOWN_EXTENSIONS": ["markdown.extensions.meta"],
    }
    site = FakeSite()
    site.config.update(config)
    compiler_obj = compiler()
    compiler_obj.set_site(site)

    post = FakePost(source_path, metadata_path, config, compiler_obj,
                    metadata_extractors_by)

    class FakeBorg:
        current_lang = "en"

        def __call__(self):
            return self

    with mock.patch("nikola.plugins.compile." + compiler_lc + ".LocaleBorg",
                    FakeBorg):
        meta, extractor = get_meta(post, None)

    assert meta["title"] == title
    assert meta["slug"] == slug
    assert "meta" in meta["tags"]
    assert "onefile" in meta["tags"]
    assert "compiler" in meta["tags"]
    assert name in meta["tags"]
    assert meta["date"] == "2017-07-01 00:00:00 UTC"
    def _execute(self, options, args):
        """Announce today's post on Mastodon"""
        self.site.scan_posts()
        today_post = [
            x for x in self.site.timeline
            if x.date.date() == datetime.today().date()
        ]
        if len(today_post) > 0:
            meta = get_meta(today_post[0], 'fr')
            content = list([today_post[0].title()])
            content.append(today_post[0].description())
            [content.append(x) for x in meta[0]['references']]
            content.insert(0, '#laminuteculturelle')
            content.append(
                'archives: https://www.mad-scientists.net/la-minute-culturelle/'
            )
            content_str = "\n".join(content)

            mastodon = Mastodon(access_token=options['mastodon-token'],
                                api_base_url=options['mastodon-node'])
            mastodon.toot(content_str)

        return 0
Пример #32
0
def test_use_filename_as_slug_fallback(post):
    post.source_path = "Slugify this"
    post.metadata_path = "Slugify this.meta"

    file_content = """\
.. title: Nikola needs more tests!
.. date: 2012/09/15 19:52:05
.. tags:
.. link:
.. description:

Post content
"""
    opener_mock = mock.mock_open(read_data=file_content)
    with mock.patch("nikola.post.io.open", opener_mock, create=True):
        meta = get_meta(post, None)[0]

    assert "Nikola needs more tests!" == meta["title"]
    assert "slugify-this" == meta["slug"]
    assert "2012/09/15 19:52:05" == meta["date"]
    assert "tags" not in meta
    assert "link" not in meta
    assert "description" not in meta
Пример #33
0
    def test_get_title_from_fname(self):
        file_metadata = [".. slug: write-tests-now\n",
                         ".. date: 2012/09/15 19:52:05\n",
                         ".. tags:\n",
                         ".. link:\n",
                         ".. description:\n"]

        opener_mock = mock.mock_open(read_data=file_metadata)
        opener_mock.return_value.readlines.return_value = file_metadata

        post = dummy()
        post.source_path = 'file_with_metadata'
        post.metadata_path = 'file_with_metadata.meta'

        with mock.patch('nikola.post.io.open', opener_mock, create=True):
            meta, nsm = get_meta(post, 'file_with_metadata')

        self.assertEqual('file_with_metadata', meta['title'])
        self.assertEqual('write-tests-now', meta['slug'])
        self.assertEqual('2012/09/15 19:52:05', meta['date'])
        self.assertFalse('tags' in meta)
        self.assertFalse('link' in meta)
        self.assertFalse('description' in meta)
        self.assertTrue(nsm)
Пример #34
0
def test_builtin_extractors_rest(
    metadata_extractors_by,
    testfiledir,
    filecount,
    expected,
    unexpected,
    format_lc,
    format_friendly,
):
    is_two_files = filecount == 2

    source_filename = "f-rest-{0}-{1}.rst".format(filecount, format_lc)
    metadata_filename = "f-rest-{0}-{1}.meta".format(filecount, format_lc)
    source_path = os.path.join(testfiledir, source_filename)
    metadata_path = os.path.join(testfiledir, metadata_filename)
    post = FakePost(source_path, metadata_path, {}, None,
                    metadata_extractors_by)

    assert os.path.exists(source_path)
    if is_two_files:
        assert os.path.exists(metadata_path)

    meta, extractor = get_meta(post, None)

    assert meta
    assert extractor is metadata_extractors_by["name"][format_lc]

    assert meta["title"] == "T: reST, {0}, {1}".format(filecount,
                                                       format_friendly)
    assert meta["slug"] == "s-rest-{0}-{1}".format(filecount, format_lc)
    assert expected in meta["tags"]
    assert unexpected not in meta["tags"]
    assert "meta" in meta["tags"]
    assert format_friendly in meta["tags"]
    assert "reST" in meta["tags"]
    assert meta["date"] == "2017-07-01 00:00:00 UTC"