def test_dirpage_directive_for_page_kind(self): silent_f2p = mute(True)(fields2pelican) test_post = filter(lambda p: p[0].startswith("Empty Page"), self.posts) with temporary_folder() as temp: fname = list(silent_f2p(test_post, 'markdown', temp, dirpage=True))[0] self.assertTrue(fname.endswith('pages%sempty.md' % os.path.sep))
def test_creates_gzip_file(self): # A file matching the input filename with a .gz extension is created. # The plugin walks over the output content after the finalized signal # so it is safe to assume that the file exists (otherwise walk would # not report it). Therefore, create a dummy file to use. with temporary_folder() as tempdir: _, a_html_filename = tempfile.mkstemp(suffix='.html', dir=tempdir) gzip_cache.create_gzip_file(a_html_filename) self.assertTrue(os.path.exists(a_html_filename + '.gz'))
def test_download_attachments(self): real_file = os.path.join(CUR_DIR, 'content/article.rst') good_url = path_to_file_url(real_file) bad_url = 'http://localhost:1/not_a_file.txt' silent_da = mute()(download_attachments) with temporary_folder() as temp: locations = list(silent_da(temp, [good_url, bad_url])) self.assertEqual(1, len(locations)) directory = locations[0] self.assertTrue(directory.endswith(os.path.join('content', 'article.rst')), directory)
def test_download_attachments(self): real_file = os.path.join(CUR_DIR, 'content/article.rst') good_url = 'file://' + real_file bad_url = 'http://localhost:1/not_a_file.txt' silent_da = mute()(download_attachments) with temporary_folder() as temp: #locations = download_attachments(temp, [good_url, bad_url]) locations = list(silent_da(temp, [good_url, bad_url])) self.assertTrue(len(locations) == 1) directory = locations[0] self.assertTrue(directory.endswith('content/article.rst'))
def test_code_in_list(self): def r(f): with open(f) as infile: return infile.read() silent_f2p = mute(True)(fields2pelican) test_post = filter(lambda p: p[0].startswith("Code in List"), self.posts) with temporary_folder() as temp: md = [r(f) for f in silent_f2p(test_post, 'markdown', temp)][0] sample_line = re.search(r'- This is a code sample', md).group(0) code_line = re.search(r'\s+a = \[1, 2, 3\]', md).group(0) self.assertTrue(sample_line.rindex('This') < code_line.rindex('a'))
def test_dont_use_smart_quotes(self): def r(f): with open(f, encoding='utf-8') as infile: return infile.read() silent_f2p = mute(True)(fields2pelican) test_post = filter(lambda p: p[0].startswith("Post with raw data"), self.posts) with temporary_folder() as temp: md = [r(f) for f in silent_f2p(test_post, 'markdown', temp)][0] escaped_quotes = re.search(r'\\[\'"“”‘’]', md) self.assertFalse(escaped_quotes)
def test_dont_use_smart_quotes(self): def r(f): with open(f, encoding='utf-8') as infile: return infile.read() silent_f2p = mute(True)(fields2pelican) test_post = filter( lambda p: p[0].startswith("Post with raw data"), self.posts) with temporary_folder() as temp: md = [r(f) for f in silent_f2p(test_post, 'markdown', temp)][0] escaped_quotes = re.search(r'\\[\'"“”‘’]', md) self.assertFalse(escaped_quotes)
def test_download_attachments(self): real_file = os.path.join(CUR_DIR, 'content/article.rst') good_url = path_to_file_url(real_file) bad_url = 'http://localhost:1/not_a_file.txt' silent_da = mute()(download_attachments) with temporary_folder() as temp: locations = list(silent_da(temp, [good_url, bad_url])) self.assertEqual(1, len(locations)) directory = locations[0] self.assertTrue( directory.endswith(os.path.join('content', 'article.rst')), directory)
def test_code_in_list(self): def r(f): with open(f, encoding='utf-8') as infile: return infile.read() silent_f2p = mute(True)(fields2pelican) test_post = filter( lambda p: p[0].startswith("Code in List"), self.posts) with temporary_folder() as temp: md = [r(f) for f in silent_f2p(test_post, 'markdown', temp)][0] sample_line = re.search(r'- This is a code sample', md).group(0) code_line = re.search(r'\s+a = \[1, 2, 3\]', md).group(0) self.assertTrue(sample_line.rindex('This') < code_line.rindex('a'))
def test_preserve_verbatim_formatting(self): def r(f): with open(f) as infile: return infile.read() silent_f2p = mute(True)(fields2pelican) test_post = filter(lambda p: p[0].startswith("Code in List"), self.posts) with temporary_folder() as temp: md = [r(f) for f in silent_f2p(test_post, 'markdown', temp)][0] self.assertTrue(re.search(r'\s+a = \[1, 2, 3\]', md)) self.assertTrue(re.search(r'\s+b = \[4, 5, 6\]', md)) for_line = re.search(r'\s+for i in zip\(a, b\):', md).group(0) print_line = re.search(r'\s+print i', md).group(0) self.assertTrue(for_line.rindex('for') < print_line.rindex('print'))
def test_wp_custpost_true_dirpage_false(self): # pages should only be put in their own directory when dirpage = True silent_f2p = mute(True)(fields2pelican) test_posts = [] for post in self.custposts: # check post kind if post[8] == 'page': test_posts.append(post) with temporary_folder() as temp: fnames = list(silent_f2p(test_posts, 'markdown', temp, wp_custpost=True, dirpage=False)) index = 0 for post in test_posts: name = post[2] name += '.md' filename = os.path.join('pages', name) out_name = fnames[index] self.assertFalse(out_name.endswith(filename))
def test_dircat(self): silent_f2p = mute(True)(fields2pelican) test_posts = [] for post in self.posts: # check post kind if len(post[5]) > 0: # Has a category test_posts.append(post) with temporary_folder() as temp: fnames = list(silent_f2p(test_posts, 'markdown', temp, dircat=True)) index = 0 for post in test_posts: name = post[2] category = slugify(post[5][0]) name += '.md' filename = os.path.join(category, name) out_name = fnames[index] self.assertTrue(out_name.endswith(filename)) index += 1
def test_can_toggle_raw_html_code_parsing(self): def r(f): with open(f) as infile: return infile.read() silent_f2p = mute(True)(fields2pelican) with temporary_folder() as temp: rst_files = (r(f) for f in silent_f2p(self.posts, "markdown", temp)) self.assertTrue(any("<iframe" in rst for rst in rst_files)) rst_files = (r(f) for f in silent_f2p(self.posts, "markdown", temp, strip_raw=True)) self.assertFalse(any("<iframe" in rst for rst in rst_files)) # no effect in rst rst_files = (r(f) for f in silent_f2p(self.posts, "rst", temp)) self.assertFalse(any("<iframe" in rst for rst in rst_files)) rst_files = (r(f) for f in silent_f2p(self.posts, "rst", temp, strip_raw=True)) self.assertFalse(any("<iframe" in rst for rst in rst_files))
def test_can_toggle_raw_html_code_parsing(self): posts = list(self.posts) r = lambda f: open(f).read() silent_f2p = mute(True)(fields2pelican) with temporary_folder() as temp: rst_files = (r(f) for f in silent_f2p(posts, 'markdown', temp)) self.assertTrue(any('<iframe' in rst for rst in rst_files)) rst_files = (r(f) for f in silent_f2p(posts, 'markdown', temp, strip_raw=True)) self.assertFalse(any('<iframe' in rst for rst in rst_files)) # no effect in rst rst_files = (r(f) for f in silent_f2p(posts, 'rst', temp)) self.assertFalse(any('<iframe' in rst for rst in rst_files)) rst_files = (r(f) for f in silent_f2p(posts, 'rst', temp, strip_raw=True)) self.assertFalse(any('<iframe' in rst for rst in rst_files))
def test_can_toggle_raw_html_code_parsing(self): def r(f): with open(f) as infile: return infile.read() silent_f2p = mute(True)(fields2pelican) with temporary_folder() as temp: rst_files = (r(f) for f in silent_f2p(self.posts, 'markdown', temp)) self.assertTrue(any('<iframe' in rst for rst in rst_files)) rst_files = (r(f) for f in silent_f2p(self.posts, 'markdown', temp, strip_raw=True)) self.assertFalse(any('<iframe' in rst for rst in rst_files)) # no effect in rst rst_files = (r(f) for f in silent_f2p(self.posts, 'rst', temp)) self.assertFalse(any('<iframe' in rst for rst in rst_files)) rst_files = (r(f) for f in silent_f2p(self.posts, 'rst', temp, strip_raw=True)) self.assertFalse(any('<iframe' in rst for rst in rst_files))
def test_custom_posts_put_in_own_dir(self): silent_f2p = mute(True)(fields2pelican) test_posts = [] for post in self.custposts: # check post kind if post[8] == 'article' or post[8] == 'page': pass else: test_posts.append(post) with temporary_folder() as temp: fnames = list(silent_f2p(test_posts, 'markdown', temp, wp_custpost = True)) index = 0 for post in test_posts: name = post[2] kind = post[8] name += '.md' filename = os.path.join(kind, name) out_name = fnames[index] self.assertTrue(out_name.endswith(filename)) index += 1
def test_transforms(self): settings = get_settings(IMAGE_PROCESS=self.transforms) def test_transform(d, i, tmpdir): path, name = os.path.split(i) destination = os.path.join(tmpdir, d, name) image = (i, destination, settings["IMAGE_PROCESS"][d]) process_image(image, settings) transformed = Image.open(destination) expected_path = os.path.join(path, "results", d, name) expected = Image.open(expected_path) img_diff = ImageChops.difference(transformed, expected).getbbox() self.assertEqual(img_diff, None) with temporary_folder() as tmpdir: [test_transform(d, i, tmpdir) for d in self.transforms for i in TEST_IMAGES]
def test_custom_posts_put_in_own_dir(self): silent_f2p = mute(True)(fields2pelican) test_posts = [] for post in self.custposts: # check post kind if post[7] == 'article' or post[7] == 'page': pass else: test_posts.append(post) with temporary_folder() as temp: fnames = list(silent_f2p(test_posts, 'markdown', temp, wp_custpost = True)) index = 0 for post in test_posts: name = post[2] kind = post[7] name += '.md' filename = os.path.join(kind, name) out_name = fnames[index] self.assertTrue(out_name.endswith(filename)) index += 1
def test_dircat(self): silent_f2p = mute(True)(fields2pelican) test_posts = [] for post in self.posts: # check post kind if len(post[5]) > 0: # Has a category test_posts.append(post) with temporary_folder() as temp: fnames = list(silent_f2p(test_posts, 'markdown', temp, dircat=True)) subs = DEFAULT_CONFIG['SLUG_REGEX_SUBSTITUTIONS'] index = 0 for post in test_posts: name = post[2] category = slugify(post[5][0], regex_subs=subs, preserve_case=True) name += '.md' filename = os.path.join(category, name) out_name = fnames[index] self.assertTrue(out_name.endswith(filename)) index += 1
def test_transforms(self): settings = get_settings(IMAGE_PROCESS = self.transforms) p = Pelican(settings) del images[:] with temporary_folder() as tmpdir: for d in self.transforms: for i in TEST_IMAGES: _, name = os.path.split(i) destination = os.path.join(tmpdir, d, name) images.append((i, destination, settings['IMAGE_PROCESS'][d])) process_images(p) for i in images: transformed = Image.open(i[1]) path, name = os.path.split(i[0]) expected_path = os.path.join(path, 'results', i[2], name) expected = Image.open(expected_path) self.assertEqual(ImageChops.difference(transformed, expected).getbbox(), None)
def test_transforms(self): settings = get_settings(IMAGE_PROCESS=self.transforms) def test_transform(d, i, tmpdir): path, name = os.path.split(i) destination = os.path.join(tmpdir, d, name) image = (i, destination, settings['IMAGE_PROCESS'][d]) process_image(image, settings) transformed = Image.open(destination) expected_path = os.path.join(path, 'results', d, name) expected = Image.open(expected_path) img_diff = ImageChops.difference(transformed, expected).getbbox() self.assertEqual(img_diff, None) with temporary_folder() as tmpdir: [test_transform(d, i, tmpdir) for d in self.transforms for i in TEST_IMAGES]
def test_custom_posts_put_in_own_dir_and_catagory_sub_dir(self): silent_f2p = mute(True)(fields2pelican) test_posts = [] for post in self.custposts: # check post kind if post[8] == 'article' or post[8] == 'page': pass else: test_posts.append(post) with temporary_folder() as temp: fnames = list(silent_f2p(test_posts, 'markdown', temp, wp_custpost=True, dircat=True)) subs = DEFAULT_CONFIG['SLUG_REGEX_SUBSTITUTIONS'] index = 0 for post in test_posts: name = post[2] kind = post[8] category = slugify(post[5][0], regex_subs=subs, preserve_case=True) name += '.md' filename = os.path.join(kind, category, name) out_name = fnames[index] self.assertTrue(out_name.endswith(filename)) index += 1
def test_transforms(self): settings = get_settings(IMAGE_PROCESS=self.transforms) p = Pelican(settings) del images[:] with temporary_folder() as tmpdir: for d in self.transforms: for i in TEST_IMAGES: _, name = os.path.split(i) destination = os.path.join(tmpdir, d, name) images.append( (i, destination, settings['IMAGE_PROCESS'][d])) process_images(p) for i in images: transformed = Image.open(i[1]) path, name = os.path.split(i[0]) expected_path = os.path.join(path, 'results', i[2], name) expected = Image.open(expected_path) self.assertEqual( ImageChops.difference(transformed, expected).getbbox(), None)