Esempio n. 1
0
    def test_get_meta_and_content(self):
        src_file_path = os.path.join(TESTS_ROOT, 'content', 'foo目录',
                                     'foo_page_中文.md')
        generator = PageGenerator(self.config, self.base_path, src_file_path)
        meta, content = generator.get_meta_and_content()
        expected_meta = {
            'date': '2013-10-17 00:03',
            'layout': 'page',
            'title': 'Foo Page 2'
        }
        self.assertEqual(meta, expected_meta)
        self.assertEqual(
            content, '<p>Simiki is a simple wiki '
            'framework, written in Python.</p>')

        # get meta notaion error
        src_file_path = os.path.join(TESTS_ROOT, 'content', 'foo目录',
                                     'foo_page_中文_meta_error_1.md')
        generator = PageGenerator(self.config, self.base_path, src_file_path)
        self.assertRaises(Exception, generator.get_meta_and_content)

        src_file_path = os.path.join(TESTS_ROOT, 'content', 'foo目录',
                                     'foo_page_中文_meta_error_2.md')
        generator = PageGenerator(self.config, self.base_path, src_file_path)
        self.assertRaises(Exception, generator.get_meta_and_content)
Esempio n. 2
0
    def test_get_meta(self):
        src_file_path = os.path.join(TESTS_ROOT, 'content', 'foo目录',
                                     'foo_page_get_meta_yaml_error.md')
        generator = PageGenerator(self.config, self.base_path, src_file_path)
        self.assertRaises(Exception, generator.get_meta_and_content)

        src_file_path = os.path.join(TESTS_ROOT, 'content', 'foo目录',
                                     'foo_page_get_meta_without_title.md')
        generator = PageGenerator(self.config, self.base_path, src_file_path)
        self.assertRaises(Exception, generator.get_meta_and_content)
Esempio n. 3
0
    def test_to_html(self):
        src_file_path = os.path.join(TESTS_ROOT, 'content', 'foo目录',
                                     'foo_page_中文.md')
        generator = PageGenerator(self.config, self.base_path, src_file_path)
        html = generator.to_html().strip()
        expected_output = os.path.join(TESTS_ROOT, 'expected_output.html')
        fd = open(expected_output, "rb")
        expected_html = unicode(fd.read(), "utf-8")
        assert html == expected_html

        # load template error
        src_file_path = os.path.join(TESTS_ROOT, 'content', 'foo目录',
                                     'foo_page_中文.md')
        generator = PageGenerator(self.config, 'wrong_basepath', src_file_path)
        self.assertRaises(Exception, generator.to_html)
Esempio n. 4
0
    def setUp(self):
        self.config_file = os.path.join(
            os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
            "simiki/conf_templates/_config.yml.in")

        configs = parse_configs(self.config_file)
        self.generator = PageGenerator(configs, ".", TEST_INPUT_FILE)
Esempio n. 5
0
    def test_get_layout(self):
        src_file_path = os.path.join(TESTS_ROOT, 'content', 'foo目录',
                                     'foo_page_layout_old_post.md')
        generator = PageGenerator(self.config, self.base_path, src_file_path)
        meta, _ = generator.get_meta_and_content()

        layout = generator.get_layout(meta)
        self.assertEqual(layout, 'page')

        src_file_path = os.path.join(TESTS_ROOT, 'content', 'foo目录',
                                     'foo_page_layout_without_layout.md')
        generator = PageGenerator(self.config, self.base_path, src_file_path)
        meta, _ = generator.get_meta_and_content()

        layout = generator.get_layout(meta)
        self.assertEqual(layout, 'page')
Esempio n. 6
0
 def test_get_category_and_file(self):
     src_file_path = os.path.join(TESTS_ROOT, 'content', 'foo目录',
                                  'foo_page_中文.md')
     generator = PageGenerator(self.config, self.base_path, src_file_path)
     category, filename = generator.get_category_and_file()
     self.assertEqual((category, filename),
                      (u'foo\u76ee\u5f55', u'foo_page_\u4e2d\u6587.md'))
Esempio n. 7
0
 def generate_single_page(self, md_file):
     md_file = md_file.decode('utf8')
     logger.debug("Generate {0}".format(md_file))
     pgen = PageGenerator(self.configs, self.target_path,
                          os.path.realpath(md_file))
     try:
         html = pgen.markdown2html()
     except Exception, e:
         logger.exception("{0}\n{1}".format(str(e), traceback.format_exc()))
         sys.exit(1)
Esempio n. 8
0
    def generate_page(_file):
        pg = PageGenerator(_site_config, _base_path)
        html = pg.to_html(_file)
        # ignore draft
        if not html:
            return None

        output_fname = YAPatternMatchingEventHandler.get_ofile(_file)
        write_file(output_fname, html)
        logging.debug('Regenerating: {0}'.format(_file))
Esempio n. 9
0
 def generate_page(_file):
     pg = PageGenerator(_site_config, _base_path, _file)
     html = pg.to_html()
     category, filename = os.path.split(_file)
     category = os.path.relpath(category, _site_config['source'])
     output_fname = os.path.join(
         _base_path, _site_config['destination'], category,
         '{0}.html'.format(os.path.splitext(filename)[0]))
     write_file(output_fname, html)
     logging.debug('Regenerating: {0}'.format(_file))
Esempio n. 10
0
    def generate_tags(self):
        g = PageGenerator(self.config, self.target_path)

        for root, dirs, files in os.walk(self.config["source"]):
            files = [f for f in files if not f.startswith(".")]
            dirs[:] = [d for d in dirs if not d.startswith(".")]
            for filename in files:
                if not filename.endswith(self.config["default_ext"]):
                    continue
                md_file = os.path.join(root, filename)

                g.src_file = md_file
                meta, _ = g.get_meta_and_content(do_render=False)
                _tags = meta.get('tag') or []  # if None
                for t in _tags:
                    self.tags.setdefault(t, []).append(meta)
Esempio n. 11
0
    def test_to_html(self):
        src_file_path = os.path.join(self.wiki_path, 'content', 'foo目录',
                                     'foo_page_中文.md')
        generator = PageGenerator(self.config, self.wiki_path, src_file_path)
        html = generator.to_html().strip()
        expected_output = os.path.join(self.wiki_path, 'expected_output.html')
        fd = open(expected_output, "rb")
        year = datetime.date.today().year
        expected_html = unicode(fd.read(), "utf-8") % year
        assert html == expected_html

        # load template error
        src_file_path = os.path.join(self.wiki_path, 'content', 'foo目录',
                                     'foo_page_中文.md')
        self.assertRaises(Exception, PageGenerator, self.config,
                          'wrong_basepath', src_file_path)
Esempio n. 12
0
    def setUp(self):
        self.wiki_path = os.path.join(test_path, 'mywiki_for_generator')

        os.chdir(self.wiki_path)

        self.config_file = os.path.join(base_path, 'simiki', 'conf_templates',
                                        '_config.yml.in')

        self.config = parse_config(self.config_file)

        s_themes_path = os.path.join(base_path, 'simiki', 'themes')
        self.d_themes_path = os.path.join('./', 'themes')
        if os.path.exists(self.d_themes_path):
            shutil.rmtree(self.d_themes_path)
        copytree(s_themes_path, self.d_themes_path)

        self.generator = PageGenerator(self.config, self.wiki_path)
Esempio n. 13
0
    def generate_single_page(self, md_file):
        md_file = md_file.decode('utf8')
        logger.debug("Generate {}".format(md_file))
        pgen = PageGenerator(self.configs, os.getcwd(), osp.realpath(md_file))
        html = pgen.markdown2html()

        def get_ofile():
            scategory, fname = osp.split(md_file)
            ofname = "{}.html".format(osp.splitext(fname)[0])
            category = osp.relpath(scategory, self.configs["source"])
            ocategory = osp.join(os.getcwd(), self.configs["destination"],
                                 category)
            ofile = osp.join(ocategory, ofname)
            return ofile

        ofile = get_ofile()
        write_file(html, ofile)
        meta_data, _ = pgen.get_metadata_and_content()
        return meta_data
Esempio n. 14
0
    def generate_single_page(self, md_file):
        logger.debug("Generate: {0}".format(md_file))
        page_generator = PageGenerator(self.config, self.target_path,
                                       os.path.realpath(md_file))
        html = page_generator.to_html()

        # ignore draft
        if not html:
            return None

        category, filename = os.path.split(md_file)
        category = os.path.relpath(category, self.config['source'])
        output_file = os.path.join(
            self.target_path, self.config['destination'], category,
            '{0}.html'.format(os.path.splitext(filename)[0]))

        write_file(output_file, html)
        meta = page_generator.meta
        return meta
Esempio n. 15
0
    def generate_catalog():
        pg = PageGenerator(_site_config, _base_path)
        pages = {}

        for root, dirs, files in os.walk(_site_config["source"]):
            files = [f for f in files if not f.startswith(".")]
            dirs[:] = [d for d in dirs if not d.startswith(".")]
            for filename in files:
                if not filename.endswith(_site_config["default_ext"]):
                    continue
                md_file = os.path.join(root, filename)
                pg.src_file = md_file
                meta, _ = pg.get_meta_and_content(do_render=False)
                pages[md_file] = meta

        cg = CatalogGenerator(_site_config, _base_path, pages)
        html = cg.generate_catalog_html()
        ofile = os.path.join(_base_path, _site_config['destination'],
                             "index.html")
        write_file(ofile, html)
        logging.debug('Regenerating catalog')
Esempio n. 16
0
 def generate_multiple_pages(self, md_files):
     _pages = {}
     _page_count = 0
     _draft_count = 0
     page_generator = PageGenerator(self.config, self.target_path,
                                    self.tags)
     for _f in md_files:
         try:
             page_meta = self.generate_single_page(page_generator, _f)
             if page_meta:
                 _pages[_f] = page_meta
                 _page_count += 1
                 if page_meta.get('draft'):
                     _draft_count += 1
             else:
                 # XXX suppose page as draft if page_meta is None, this may
                 # cause error in the future
                 _draft_count += 1
         except Exception:
             page_meta = None
             logger.exception('{0} failed to generate:'.format(_f))
     return _pages, _page_count, _draft_count
Esempio n. 17
0
    def test_get_template_vars(self):
        src_file_path = os.path.join(TESTS_ROOT, 'content', 'foo目录',
                                     'foo_page_中文.md')
        generator = PageGenerator(self.config, self.base_path, src_file_path)
        meta, content = generator.get_meta_and_content()
        template_vars = generator.get_template_vars(meta, content)
        expected_template_vars = {
            u'page': {
                u'category': u'foo\u76ee\u5f55',
                u'content': u'<p>Simiki is a simple wiki '
                'framework, written in Python.</p>',
                'date': '2013-10-17 00:03',
                'layout': 'page',
                'title': 'Foo Page 2'
            },
            u'site': get_default_config()
        }

        expected_template_vars['site'].update({'root': ''})
        template_vars['site'].pop('time')
        expected_template_vars['site'].pop('time')
        assert template_vars == expected_template_vars
Esempio n. 18
0
    def generate_single_page(self, md_file):
        logger.debug("Generate {0}".format(md_file))
        pgen = PageGenerator(self.configs, self.target_path,
                             os.path.realpath(md_file))
        try:
            html = pgen.markdown2html()
        except Exception as e:
            logger.exception("{0}\n{1}".format(str(e), traceback.format_exc()))
            sys.exit(1)

        def get_ofile():
            scategory, fname = os.path.split(md_file)
            ofname = "{0}.html".format(os.path.splitext(fname)[0])
            category = os.path.relpath(scategory, self.configs["source"])
            ocategory = os.path.join(self.target_path,
                                     self.configs["destination"], category)
            ofile = os.path.join(ocategory, ofname)
            return ofile

        ofile = get_ofile()
        write_file(html, ofile)
        meta_data, _ = pgen.get_metadata_and_content()
        return meta_data
Esempio n. 19
0
    def test_to_html(self):
        src_file = os.path.join(self.wiki_path, 'content', 'foo目录',
                                'foo_page_中文.md')
        html_generator_config = self.config
        html_generator_config["markdown_ext"] = {"wikilinks": None}
        html_generator_generator = PageGenerator(html_generator_config,
                                                 self.wiki_path)
        html = html_generator_generator.to_html(src_file).strip()
        # trip page updated and site generated paragraph
        html = re.sub(
            '(?sm)\\n\s*<span class="updated">Page Updated.*?<\/span>',
            '', html)
        html = re.sub('(?m)^\s*<p>Site Generated .*?<\/p>$\n', '', html)
        expected_output = os.path.join(self.wiki_path, 'expected_output.html')
        fd = open(expected_output, "rb")
        year = datetime.date.today().year
        expected_html = unicode(fd.read(), "utf-8") % year
        assert html.rstrip() == expected_html.rstrip()

        # load template error
        src_file = os.path.join(self.wiki_path, 'content', 'foo目录',
                                'foo_page_中文.md')
        self.assertRaises(Exception, PageGenerator, self.config,
                          'wrong_basepath', src_file)