示例#1
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())
示例#2
0
文件: tags.py 项目: bronsen/plugins
    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())
示例#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.site.scan_posts()
        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)
        p = Post(inf, self.site.config, outf, False, None, '', self.compiler)
        self.site.post_per_input_file[inf] = p
        p.compile_html(inf, outf, post=p)
        with io.open(outf, 'r', encoding='utf8') as f:
            self.html = f.read()
        os.unlink(inf)
        os.unlink(outf)
        p.write_depfile(outf, p._depfile[outf])
        if os.path.isfile(depf):
            with io.open(depf, 'r', encoding='utf8') as f:
                self.assertEqual(self.deps.strip(), f.read().strip())
            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)
示例#4
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.site.scan_posts()
        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)
        p = Post(inf, self.site.config, outf, False, None, '', self.compiler)
        self.site.post_per_input_file[inf] = p
        p.compile_html(inf, outf, post=p)
        with io.open(outf, 'r', encoding='utf8') as f:
            self.html = f.read()
        os.unlink(inf)
        os.unlink(outf)
        p.write_depfile(outf, p._depfile[outf])
        if os.path.isfile(depf):
            with io.open(depf, 'r', encoding='utf8') as f:
                self.assertEqual(self.deps.strip(), f.read().strip())
            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)