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)
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'))
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) ) try: html = page_generator.to_html() except Exception as e: logger.exception('{0}: {1}'.format(md_file, unicode(e))) 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.config["source"]) ocategory = os.path.join( self.target_path, self.config["destination"], category ) ofile = os.path.join(ocategory, ofname) return ofile ofile = get_ofile() write_file(html, ofile) meta = page_generator.meta return meta
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
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') )
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)
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))
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))
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)
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))
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)
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)
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)
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)
def setUp(self): self.config_file = osp.join( osp.dirname(osp.dirname(osp.abspath(__file__))), "simiki/conf_templates/_config.yml.in" ) configs = parse_configs(self.config_file) self.generator = PageGenerator(configs, TESTS_ROOT, TEST_INPUT_FILE)
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)
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)
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
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
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')
class TestPageGenerator(unittest.TestCase): 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) def test_get_category_and_file(self): category_name, filename = self.generator.get_category_and_file() self.assertEqual((category_name, filename), (u'..', u'test_input.md'))
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
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
class TestPageGenerator(unittest.TestCase): 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) def test_get_category_and_file(self): category_name, filename = self.generator.get_category_and_file() self.assertEqual((category_name, filename), (u'..', u'test_input.md'))
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
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)
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')
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)
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')
class TestPageGenerator(unittest.TestCase): def setUp(self): self.config_file = osp.join( osp.dirname(osp.dirname(osp.abspath(__file__))), "simiki/conf_templates/_config.yml.in" ) configs = parse_configs(self.config_file) self.generator = PageGenerator(configs, TESTS_ROOT, TEST_INPUT_FILE) # def test_get_category_and_file(self): # category_name, filename = self.generator.get_category_and_file() # self.assertEqual((category_name, filename), (u'sample', u'hellosimiki.md')) def test_get_layout(self): layout = self.generator.get_layout() self.assertEqual(layout, "page")
class TestPageGenerator(unittest.TestCase): def setUp(self): self.config_file = osp.join( osp.dirname(osp.dirname(osp.abspath(__file__))), "simiki/conf_templates/_config.yml.in") configs = parse_configs(self.config_file) self.generator = PageGenerator(configs, TESTS_ROOT, TEST_INPUT_FILE) # def test_get_category_and_file(self): # category_name, filename = self.generator.get_category_and_file() # self.assertEqual((category_name, filename), (u'sample', u'hellosimiki.md')) def test_get_layout(self): layout = self.generator.get_layout() self.assertEqual(layout, "page")
def setUp(self): self.default_config = get_default_config() 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", self.default_config["themes_dir"]) self.d_themes_path = os.path.join("./", self.default_config["themes_dir"]) 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)
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
class TestPageGenerator(unittest.TestCase): 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, os.path.dirname(__file__), TEST_INPUT_FILE) def test_get_category_and_file(self): category_name, filename = self.generator.get_category_and_file() self.assertEqual((category_name, filename), (u'linux', u'test_input.md')) def test_get_metadata_and_content(self): metadata, content = self.generator.get_metadata_and_content() expected_metadata = { 'date': '2013-11-11 11:11', 'tag': 'test, wiki', 'layout': 'page', 'title': 'Test Generator' } assert metadata == expected_metadata expected_content_list = [ '\n', '# Simiki #\n', '\n', ('Simiki is a simple static site generator ' 'for wiki. Powered by Python.\n') ] assert content == "".join(expected_content_list) def test_get_template_vars(self): template_vars = self.generator.get_template_vars() expected_template_vars = { u'site': { 'index': False, 'description': '', 'title': '', 'url': '', 'default_ext': 'md', 'destination': 'output', 'themes_dir': 'themes', 'theme': 'simple', 'pygments': True, 'source': 'content', 'keywords': '', 'debug': False, 'author': '', 'root': '' }, u'page': { u'category': u'linux', u'content': (u'<h1 id="simiki">' 'Simiki</h1>\n<p>Simiki is a simple static site generator for ' 'wiki. Powered by Python.</p>'), 'tag': 'test, wiki', 'layout': 'page', 'title': 'Test Generator', 'date': '2013-11-11 11:11' } } assert template_vars == expected_template_vars def test_markdown2html(self): html = self.generator.markdown2html().strip() fd = open(EXPECTED_OUTPUT, "rb") expected_html = unicode(fd.read(), "utf-8") assert html == expected_html
class TestPageGenerator(unittest.TestCase): 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) def test_get_category_and_file(self): src_file = os.path.join(self.wiki_path, 'content', 'foo目录', 'foo_page_中文.md') self.generator.src_file = src_file category, filename = self.generator.get_category_and_file() self.assertEqual((category, filename), (u'foo\u76ee\u5f55', u'foo_page_\u4e2d\u6587.md')) def test_get_meta_and_content(self): src_file = os.path.join(self.wiki_path, 'content', 'foo目录', 'foo_page_中文.md') self.generator.src_file = src_file meta, content = self.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 = os.path.join(self.wiki_path, 'content', 'foo目录', 'foo_page_中文_meta_error_1.md') self.generator.src_file = src_file self.assertRaises(Exception, self.generator.get_meta_and_content) src_file = os.path.join(self.wiki_path, 'content', 'foo目录', 'foo_page_中文_meta_error_2.md') self.generator.src_file = src_file self.assertRaises(Exception, self.generator.get_meta_and_content) def test_get_template_vars(self): src_file = os.path.join(self.wiki_path, 'content', 'foo目录', 'foo_page_中文.md') self.generator.src_file = src_file meta, content = self.generator.get_meta_and_content() template_vars = self.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>', u'filename': u'foo_page_\u4e2d\u6587.html', u'date': '2013-10-17 00:03', u'layout': 'page', u'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 def test_to_html(self): src_file = os.path.join(self.wiki_path, 'content', 'foo目录', 'foo_page_中文.md') html = self.generator.to_html(src_file).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 = os.path.join(self.wiki_path, 'content', 'foo目录', 'foo_page_中文.md') self.assertRaises(Exception, PageGenerator, self.config, 'wrong_basepath', src_file) def test_get_layout(self): src_file = os.path.join(self.wiki_path, 'content', 'foo目录', 'foo_page_layout_old_post.md') self.generator.src_file = src_file meta, _ = self.generator.get_meta_and_content() layout = self.generator.get_layout(meta) self.assertEqual(layout, 'page') src_file = os.path.join(self.wiki_path, 'content', 'foo目录', 'foo_page_layout_without_layout.md') self.generator.src_file = src_file meta, _ = self.generator.get_meta_and_content() layout = self.generator.get_layout(meta) self.assertEqual(layout, 'page') def test_get_meta(self): src_file = os.path.join(self.wiki_path, 'content', 'foo目录', 'foo_page_get_meta_yaml_error.md') self.generator.src_file = src_file self.assertRaises(Exception, self.generator.get_meta_and_content) src_file = os.path.join(self.wiki_path, 'content', 'foo目录', 'foo_page_get_meta_without_title.md') self.generator.src_file = src_file self.assertRaises(Exception, self.generator.get_meta_and_content) def tearDown(self): if os.path.exists(self.d_themes_path): shutil.rmtree(self.d_themes_path)
class TestPageGenerator(unittest.TestCase): 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) def test_get_category_and_file(self): src_file = os.path.join(self.wiki_path, 'content', 'foo目录', 'foo_page_中文.md') self.generator.src_file = src_file category, filename = self.generator.get_category_and_file() self.assertEqual( (category, filename), (u'foo\u76ee\u5f55', u'foo_page_\u4e2d\u6587.md') ) def test_get_meta_and_content(self): src_file = os.path.join(self.wiki_path, 'content', 'foo目录', 'foo_page_中文.md') self.generator.src_file = src_file meta, content = self.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 = os.path.join(self.wiki_path, 'content', 'foo目录', 'foo_page_中文_meta_error_1.md') self.generator.src_file = src_file self.assertRaises(Exception, self.generator.get_meta_and_content) src_file = os.path.join(self.wiki_path, 'content', 'foo目录', 'foo_page_中文_meta_error_2.md') self.generator.src_file = src_file self.assertRaises(Exception, self.generator.get_meta_and_content) def test_get_template_vars(self): src_file = os.path.join(self.wiki_path, 'content', 'foo目录', 'foo_page_中文.md') self.generator.src_file = src_file meta, content = self.generator.get_meta_and_content() template_vars = self.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>', u'filename': u'foo_page_\u4e2d\u6587.html', u'date': '2013-10-17 00:03', u'layout': 'page', u'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 def test_to_html(self): src_file = os.path.join(self.wiki_path, 'content', 'foo目录', 'foo_page_中文.md') html = self.generator.to_html(src_file).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 = os.path.join(self.wiki_path, 'content', 'foo目录', 'foo_page_中文.md') self.assertRaises(Exception, PageGenerator, self.config, 'wrong_basepath', src_file) def test_get_layout(self): src_file = os.path.join(self.wiki_path, 'content', 'foo目录', 'foo_page_layout_old_post.md') self.generator.src_file = src_file meta, _ = self.generator.get_meta_and_content() layout = self.generator.get_layout(meta) self.assertEqual(layout, 'page') src_file = os.path.join(self.wiki_path, 'content', 'foo目录', 'foo_page_layout_without_layout.md') self.generator.src_file = src_file meta, _ = self.generator.get_meta_and_content() layout = self.generator.get_layout(meta) self.assertEqual(layout, 'page') def test_get_meta(self): src_file = os.path.join(self.wiki_path, 'content', 'foo目录', 'foo_page_get_meta_yaml_error.md') self.generator.src_file = src_file self.assertRaises(Exception, self.generator.get_meta_and_content) src_file = os.path.join(self.wiki_path, 'content', 'foo目录', 'foo_page_get_meta_without_title.md') self.generator.src_file = src_file self.assertRaises(Exception, self.generator.get_meta_and_content) def tearDown(self): if os.path.exists(self.d_themes_path): shutil.rmtree(self.d_themes_path)
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(), md_file) html = pgen.markdown2html() pgen.output_to_file(html)
class TestPageGenerator(unittest.TestCase): def setUp(self): self.default_config = get_default_config() 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", self.default_config["themes_dir"]) self.d_themes_path = os.path.join("./", self.default_config["themes_dir"]) 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) def test_get_category_and_file(self): src_file = os.path.join(self.wiki_path, "content", "foo目录", "foo_page_中文.md") self.generator.src_file = src_file category, filename = self.generator.get_category_and_file() self.assertEqual((category, filename), ("foo\u76ee\u5f55", "foo_page_\u4e2d\u6587.md")) def test_get_meta_and_content(self): src_file = os.path.join(self.wiki_path, "content", "foo目录", "foo_page_中文.md") self.generator.src_file = src_file meta, content = self.generator.get_meta_and_content() expected_meta = { "date": "2013-10-17 00:03", "layout": "page", "title": "Foo Page 2", "category": "foo目录", "filename": "foo_page_中文.html", } 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 = os.path.join(self.wiki_path, "content", "foo目录", "foo_page_中文_meta_error_1.md") self.generator.src_file = src_file self.assertRaises(Exception, self.generator.get_meta_and_content) src_file = os.path.join(self.wiki_path, "content", "foo目录", "foo_page_中文_meta_error_2.md") self.generator.src_file = src_file self.assertRaises(Exception, self.generator.get_meta_and_content) def test_get_template_vars(self): src_file = os.path.join(self.wiki_path, "content", "foo目录", "foo_page_中文.md") self.generator.src_file = src_file meta, content = self.generator.get_meta_and_content() template_vars = self.generator.get_template_vars(meta, content) expected_template_vars = { "page": { "category": "foo\u76ee\u5f55", "content": "<p>Simiki is a simple wiki " "framework, written in Python.</p>", "filename": "foo_page_\u4e2d\u6587.html", "date": "2013-10-17 00:03", "layout": "page", "relation": [], "title": "Foo Page 2", }, "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 def test_to_html(self): src_file = os.path.join(self.wiki_path, "content", "foo目录", "foo_page_中文.md") html = self.generator.to_html(src_file).strip() # trip page updated and site generated paragraph html = re.sub('(?sm)\\n\s*<span class="updated">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 == expected_html # 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) def test_get_layout(self): src_file = os.path.join(self.wiki_path, "content", "foo目录", "foo_page_layout_old_post.md") self.generator.src_file = src_file meta, _ = self.generator.get_meta_and_content() layout = self.generator.get_layout(meta) self.assertEqual(layout, "page") src_file = os.path.join(self.wiki_path, "content", "foo目录", "foo_page_layout_without_layout.md") self.generator.src_file = src_file meta, _ = self.generator.get_meta_and_content() layout = self.generator.get_layout(meta) self.assertEqual(layout, "page") def test_get_meta(self): src_file = os.path.join(self.wiki_path, "content", "foo目录", "foo_page_get_meta_yaml_error.md") self.generator.src_file = src_file self.assertRaises(Exception, self.generator.get_meta_and_content) src_file = os.path.join(self.wiki_path, "content", "foo目录", "foo_page_get_meta_without_title.md") self.generator.src_file = src_file self.assertRaises(Exception, self.generator.get_meta_and_content) def tearDown(self): if os.path.exists(self.d_themes_path): shutil.rmtree(self.d_themes_path)
def generate_single_page(self, md_file): logger.debug("Generate {}".format(md_file)) pgen = PageGenerator(self.configs, md_file) html = pgen.mdown2html() pgen.output_to_file(html)