Beispiel #1
0
 def setUp(self):
     conf = {}
     if self.extra_plugins_dirs is not None:
         conf['EXTRA_PLUGINS_DIRS'] = self.extra_plugins_dirs
     self.site = Nikola(**conf)
     self.site.init_plugins()
     self.compiler = self.site.compilers['rest']
     return super(ReSTExtensionTestCase, self).setUp()
Beispiel #2
0
class ReSTExtensionTestCase(BaseTestCase):
    """ Base class for testing ReST extensions """

    sample = 'foo'
    deps = None
    extra_plugins_dirs = None

    def setUp(self):
        conf ={}
        if self.extra_plugins_dirs is not None:
            conf['EXTRA_PLUGINS_DIRS'] = self.extra_plugins_dirs
        self.site = Nikola(**conf)
        self.site.init_plugins()
        self.compiler = self.site.compilers['rest']
        return super(ReSTExtensionTestCase, self).setUp()

    def basic_test(self):
        """ Parse cls.sample into a HTML document tree """
        self.setHtmlFromRst(self.sample)

    def setHtmlFromRst(self, rst):
        """ Create html output from rst string """
        tmpdir = tempfile.mkdtemp()
        inf = os.path.join(tmpdir, 'inf')
        outf = os.path.join(tmpdir, 'outf')
        depf = os.path.join(tmpdir, 'outf.dep')
        with io.open(inf, 'w+', encoding='utf8') as f:
            f.write(rst)
        self.html = self.compiler.compile_html(inf, outf)
        with io.open(outf, 'r', encoding='utf8') as f:
            self.html = f.read()
        os.unlink(inf)
        os.unlink(outf)
        if os.path.isfile(depf):
            with io.open(depf, 'r', encoding='utf8') as f:
                self.assertEqual(self.deps, f.read())
            os.unlink(depf)
        else:
            self.assertEqual(self.deps, None)
        os.rmdir(tmpdir)
        self.html_doc = html.parse(StringIO(self.html))

    def assertHTMLContains(self, element, attributes=None, text=None):
        """ Test if HTML document includes an element with the given
        attributes and text content

        """
        try:
            tag = next(self.html_doc.iter(element))
        except StopIteration:
            raise Exception("<{0}> not in {1}".format(element, self.html))
        else:
            if attributes:
                arg_attrs = set(attributes.items())
                tag_attrs = set(tag.items())
                self.assertTrue(arg_attrs.issubset(tag_attrs))
            if text:
                self.assertIn(text, tag.text)
Beispiel #3
0
class ReSTExtensionTestCase(BaseTestCase):
    """ Base class for testing ReST extensions """

    sample = 'foo'
    deps = None
    extra_plugins_dirs = None

    def setUp(self):
        conf = {}
        if self.extra_plugins_dirs is not None:
            conf['EXTRA_PLUGINS_DIRS'] = self.extra_plugins_dirs
        self.site = Nikola(**conf)
        self.site.init_plugins()
        self.compiler = self.site.compilers['rest']
        return super(ReSTExtensionTestCase, self).setUp()

    def basic_test(self):
        """ Parse cls.sample into a HTML document tree """
        self.setHtmlFromRst(self.sample)

    def setHtmlFromRst(self, rst):
        """ Create html output from rst string """
        tmpdir = tempfile.mkdtemp()
        inf = os.path.join(tmpdir, 'inf')
        outf = os.path.join(tmpdir, 'outf')
        depf = os.path.join(tmpdir, 'outf.dep')
        with io.open(inf, 'w+', encoding='utf8') as f:
            f.write(rst)
        self.html = self.compiler.compile_html(inf, outf)
        with io.open(outf, 'r', encoding='utf8') as f:
            self.html = f.read()
        os.unlink(inf)
        os.unlink(outf)
        if os.path.isfile(depf):
            with io.open(depf, 'r', encoding='utf8') as f:
                self.assertEqual(self.deps, f.read())
            os.unlink(depf)
        else:
            self.assertEqual(self.deps, None)
        os.rmdir(tmpdir)
        self.html_doc = html.parse(StringIO(self.html))

    def assertHTMLContains(self, element, attributes=None, text=None):
        """ Test if HTML document includes an element with the given
        attributes and text content

        """
        try:
            tag = next(self.html_doc.iter(element))
        except StopIteration:
            raise Exception("<{0}> not in {1}".format(element, self.html))
        else:
            if attributes:
                arg_attrs = set(attributes.items())
                tag_attrs = set(tag.items())
                self.assertTrue(arg_attrs.issubset(tag_attrs))
            if text:
                self.assertIn(text, tag.text)
Beispiel #4
0
 def setUp(self):
     conf ={}
     if self.extra_plugins_dirs is not None:
         conf['EXTRA_PLUGINS_DIRS'] = self.extra_plugins_dirs
     self.site = Nikola(**conf)
     self.site.init_plugins()
     self.compiler = self.site.compilers['rest']
     return super(ReSTExtensionTestCase, self).setUp()
Beispiel #5
0
    def _execute(self, options, args):
        """Manage the tags on the site."""

        try:
            import conf

        except ImportError:
            LOGGER.error("No configuration found, cannot run the console.")

        else:
            _reload(conf)
            nikola = Nikola(**conf.__dict__)
            nikola.scan_posts()

            if len(options['add']) > 0 and len(args) > 0:
                add_tags(nikola, options['add'], args, options['dry-run'])

            elif options['list']:
                list_tags(nikola, options['list_sorting'])

            elif options['merge'].count(',') > 0 and len(args) > 0:
                merge_tags(nikola, options['merge'], args, options['dry-run'])

            elif len(options['remove']) > 0 and len(args) > 0:
                remove_tags(nikola, options['remove'], args,
                            options['dry-run'])

            elif len(options['search']) > 0:
                search_tags(nikola, options['search'])

            elif options['tag'] and len(args) > 0:
                tagger = _AutoTag(nikola)
                for post in args:
                    tags = ','.join(tagger.tag(post))
                    add_tags(nikola, tags, [post], options['dry-run'])

            elif options['sort'] and len(args) > 0:
                sort_tags(nikola, args, options['dry-run'])

            else:
                print(self.help())
Beispiel #6
0
    def _execute(self, options, args):
        """Manage the tags on the site."""

        try:
            import conf

        except ImportError:
            LOGGER.error("No configuration found, cannot run the console.")

        else:
            _reload(conf)
            nikola = Nikola(**conf.__dict__)
            nikola.scan_posts()

            if len(options['add']) > 0 and len(args) > 0:
                add_tags(nikola, options['add'], args, options['dry-run'])

            elif options['list']:
                list_tags(nikola, options['list_sorting'])

            elif options['merge'].count(',') > 0 and len(args) > 0:
                merge_tags(nikola, options['merge'], args, options['dry-run'])

            elif len(options['remove']) > 0 and len(args) > 0:
                remove_tags(nikola, options['remove'], args, options['dry-run'])

            elif len(options['search']) > 0:
                search_tags(nikola, options['search'])

            elif options['tag'] and len(args) > 0:
                tagger = _AutoTag(nikola)
                for post in args:
                    tags = ','.join(tagger.tag(post))
                    add_tags(nikola, tags, [post], options['dry-run'])

            elif options['sort'] and len(args) > 0:
                sort_tags(nikola, args, options['dry-run'])

            else:
                print(self.help())
Beispiel #7
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Please don't edit this file unless you really know what you are doing.
# The configuration is now in conf.py

from doit.reporter import ExecutedOnlyReporter

from nikola.nikola import Nikola

import conf

DOIT_CONFIG = {
    'reporter': ExecutedOnlyReporter,
    'default_tasks': ['render_site'],
}
SITE = Nikola(**conf.__dict__)


def task_render_site():
    return SITE.gen_tasks()
Beispiel #8
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Please don't edit this file unless you really know what you are doing.
# The configuration is now in conf.py

import os

from doit.reporter import ExecutedOnlyReporter

from nikola.nikola import Nikola

import conf

DOIT_CONFIG = {
    'reporter': ExecutedOnlyReporter,
    'default_tasks': ['render_site'],
}
site = Nikola(**conf.__dict__)


def task_render_site():
    return site.gen_tasks()


if __name__ == "__main__":
    _nikola_main()
Beispiel #9
0
def rss_feed_content(blog_url, config, default_locale):
    default_post = {
        "title": "post title",
        "slug": "awesome_article",
        "date": "2012-10-01 22:41",
        "author": None,
        "tags": "tags",
        "link": "link",
        "description": "description",
        "enclosure": "http://www.example.org/foo.mp3",
        "enclosure_length": "5",
    }
    meta_mock = mock.Mock(return_value=(defaultdict(str, default_post), None))
    with mock.patch("nikola.post.get_meta", meta_mock):
        with mock.patch("nikola.nikola.utils.os.path.isdir",
                        mock.Mock(return_value=True)):
            with mock.patch("nikola.nikola.Post.text",
                            mock.Mock(return_value="some long text")):
                example_post = Post(
                    "source.file",
                    config,
                    "blog_folder",
                    True,
                    {"en": ""},
                    "post.tmpl",
                    FakeCompiler(),
                )

                filename = "testfeed.rss"
                opener_mock = mock.mock_open()

                with mock.patch("nikola.nikola.io.open",
                                opener_mock,
                                create=True):
                    Nikola().generic_rss_renderer(
                        default_locale,
                        "blog_title",
                        blog_url,
                        "blog_description",
                        [
                            example_post,
                        ],
                        filename,
                        True,
                        False,
                    )

                opener_mock.assert_called_once_with(filename,
                                                    "w+",
                                                    encoding="utf-8")

                # Python 3 / unicode strings workaround
                # lxml will complain if the encoding is specified in the
                # xml when running with unicode strings.
                # We do not include this in our content.
                file_content = [
                    call[1][0] for call in opener_mock.mock_calls[2:-1]
                ][0]
                splitted_content = file_content.split("\n")
                # encoding_declaration = splitted_content[0]
                content_without_encoding_declaration = splitted_content[1:]
                yield "\n".join(content_without_encoding_declaration)
Beispiel #10
0
 def set_site(self, site: Nikola) -> None:
     site.register_shortcode(self.name, self.generate_index)