Пример #1
0
    def test_context_lifecycle(self):
        project_dir, output_dir = get_dirs({
            'input/p1/blog_post.md': [
                "foo.jpg"
            ],
            "input/bogus.jpg": "",
            'bangfile.py': [
                "from bang import event",
                "from bang.plugins import feed",
                "host = 'example.com'",
                "name = 'example site'",
                "",
                "scheme = 'https'",
                "@event.bind('context.web')",
                "def feed_context_handler(event_name, config):",
                "    config.scheme = ''",
                "",
            ]
        })

        s = Site(project_dir, output_dir)
        s.output()

        r = output_dir.file_contents("feed.rss")
        self.assertTrue("<link>https://example.com" in r)

        post_dir = output_dir / "p1"
        r = post_dir.file_contents("index.html")
        self.assertTrue('src="//example.com' in r)
Пример #2
0
def console_watch(args, project_dir, output_dir):
    ret_code = 0
    logger.info("running watch")
    d = Directory(project_dir, '.git')
    if d.exists():
        try:
            git_path = subprocess.check_output(['which', 'git']).strip()
            output = subprocess.check_output(
                [git_path, "pull", "origin", "master"],
                stderr=subprocess.STDOUT,
                cwd=str(project_dir)
            )
            if (output.find("Updating") >= 0) or not output_dir.exists():
                # there are new changes, let's recompile the project
                s = Site(project_dir, output_dir)
                s.output()

            elif output.find("Already up-to-date"):
                # nothing has changed, so don't recompile
                pass
            else:
                raise RuntimeError(output)

        except subprocess.CalledProcessError as e:
            raise

    else:
        ret_code = 1

    logger.info("watch done")
    return ret_code
Пример #3
0
def console_compile(args, project_dir, output_dir):
    start = time.time()

    regex = args.regex
    s = Site(project_dir, output_dir)

    if regex:
        logger.info("Compiling directories matching {} in {} to {}".format(
            regex,
            s.input_dir,
            s.output_dir
        ))
    else:
        logger.info("Compiling directory {} to {}".format(s.input_dir, s.output_dir))

    s.output(regex)

    stop = time.time()
    multiplier = 1000.00
    rnd = 2
    elapsed = round(abs(stop - start) * float(multiplier), rnd)
    total = "{:.1f} ms".format(elapsed)

    logger.info("Compile done in {}".format(total))
    return 0
Пример #4
0
    def test_unicode_output(self):
        project_dir, output_dir = get_dirs({
            'input/aux/index.md': testdata.get_unicode_words(),
        })

        s = Site(project_dir, output_dir)
        s.output()

        self.assertTrue(os.path.isfile(os.path.join(str(output_dir), 'aux', 'index.html')))
Пример #5
0
    def test_private_post(self):
        project_dir, output_dir = get_dirs({
            'input/_foo/post1.md': testdata.get_unicode_words(),
            'input/_foo/fake.jpg': "",
            'input/_bar/other/something.jpg': "",
        })

        s = Site(project_dir, output_dir)

        s.output()
        self.assertIsNone(s.posts.first_post)
        self.assertIsNone(s.others.first_post)
Пример #6
0
    def test_drafts(self):
        project_dir, output_dir = get_dirs({
            'input/_draft/foo.md': testdata.get_words(),
            'input/notdraft/_bar.md': testdata.get_words(),
        })

        s = Site(project_dir, output_dir)
        s.output()

        self.assertFalse(os.path.isfile(os.path.join(str(output_dir), '_draft', 'index.html')))
        self.assertFalse(os.path.isfile(os.path.join(str(output_dir), 'notdraft', 'index.html')))
        self.assertEqual(0, len(s.posts))
Пример #7
0
    def test_regex_compile(self):
        project_dir, output_dir = get_dirs({
            'input/foo/post1.md': testdata.get_unicode_words(),
            'input/foo2/post2.md': testdata.get_unicode_words(),
            'input/bar/post3.md': testdata.get_unicode_words(),
            'input/bar/fake.jpg': "",
        })

        s = Site(project_dir, output_dir)

        s.output(r"bar")
        count = 0
        for p in s.posts:
            if p.output_dir.exists():
                count += 1
        self.assertEqual(1, count)

        s.output(r"bar")
        count = 0
        for p in s.posts:
            if p.output_dir.exists():
                count += 1
        self.assertEqual(1, count)

        s.output()
        count = 0
        for p in s.posts:
            if p.output_dir.exists():
                count += 1
        self.assertEqual(3, count)
Пример #8
0
def get_post(post_files, name=""):

    # clear the environment
    for k, v in os.environ.items():
        if k.startswith('BANG_'):
            del os.environ[k]
    sys.modules.pop("bangfile_module", None)

    if not name:
        name = testdata.get_ascii(16)

    di = {
        'bangfile.py': "\n".join([
            "host = 'example.com'",
            "name = 'example site'",
            ""
        ])
    }

    # replace any project files if they are present
    for rp in di.keys():
        if rp in post_files:
            di[rp] = post_files.pop(rp)

    for basename, file_contents in post_files.items():
        fp = os.path.join('input', name, basename)
        di[fp] = file_contents

    project_dir, output_dir = get_dirs(di)

#     d = Directory(project_dir.input_dir, name)
#     d.ancestor_dir = project_dir.input_dir
#     tmpl = Template(project_dir.template_dir)
#     p = Post(d, output_dir, tmpl, Config(project_dir))

    s = Site(project_dir, output_dir)
    s.output()

    #pout.v(s, len(s.posts), len(s.auxs))

    return s.posts.first_post if len(s.posts) else s.auxs.first_post
Пример #9
0
    def test_sitemap(self):
        from bang.plugins import sitemap
        project_dir, output_dir = get_dirs({
            'input/1/one.md': u'1. {}'.format(testdata.get_unicode_words()),
            'input/2/two.md': u'2. {}'.format(testdata.get_unicode_words()),
            'input/3/three.md': u'3. {}'.format(testdata.get_unicode_words()),
            'bangfile.py': "\n".join([
                "host = 'example.com'",
                ""
            ])
        })
        s = Site(project_dir, output_dir)

        s.output()
        p = os.path.join(str(s.output_dir), 'sitemap.xml')
        self.assertTrue(os.path.isfile(p))

        body = get_body(p)
        self.assertTrue('example.com/1' in body)
        self.assertTrue('example.com/2' in body)
        self.assertTrue('example.com/3' in body)