def test_galleries_added_to_markdown_header(self): header = build_markdown_header('test', None, None, None, None, None, attachments=['output/test1', 'output/test2']) self.assertEqual( header, 'Title: test\nAttachments: output/test1, output/test2\n\n')
def test_build_header_with_fields(self): header_data = [ 'Test Post', '2014-11-04', 'Alexis Métaireau', ['Programming'], ['Pelican', 'Python'], 'test-post', ] expected_docutils = '\n'.join([ 'Test Post', '#########', ':date: 2014-11-04', ':author: Alexis Métaireau', ':category: Programming', ':tags: Pelican, Python', ':slug: test-post', '\n', ]) expected_md = '\n'.join([ 'Title: Test Post', 'Date: 2014-11-04', 'Author: Alexis Métaireau', 'Category: Programming', 'Tags: Pelican, Python', 'Slug: test-post', '\n', ]) self.assertEqual(build_header(*header_data), expected_docutils) self.assertEqual(build_markdown_header(*header_data), expected_md)
def fields2pelican(fields, out_markup, output_path, dircat=False, strip_raw=False): for title, content, filename, date, date_object, author, categories, tags, in_markup in fields: if (in_markup == "markdown") or (out_markup == "markdown") : ext = '.md' header = build_markdown_header(title, date, author, categories, tags) else: out_markup = "rst" ext = '.rst' header = build_header(title, date, author, categories, tags) filename = build_filename(filename, date_object) try: os.makedirs(os.path.join(output_path, os.path.dirname(filename))) except OSError: pass out_filename = os.path.join(output_path, filename+ext) print(out_filename) if in_markup == "html": html_filename = os.path.join(output_path, filename+'.html') with open(html_filename, 'w', encoding='utf-8') as fp: # Replace newlines with paragraphs wrapped with <p> so # HTML is valid before conversion paragraphs = content.split('\n\n') paragraphs = [u'<p>{0}</p>'.format(p) for p in paragraphs] new_content = ''.join(paragraphs) fp.write(new_content) parse_raw = '--parse-raw' if not strip_raw else '' cmd = ('pandoc --normalize --reference-links {0} --from=html' ' --to={1} -o "{2}" "{3}"').format( parse_raw, out_markup, out_filename, html_filename) try: rc = subprocess.call(cmd, shell=True) if rc < 0: error = "Child was terminated by signal %d" % -rc exit(error) elif rc > 0: error = "Please, check your Pandoc installation." exit(error) except OSError, e: error = "Pandoc execution failed: %s" % e exit(error) os.remove(html_filename) with open(out_filename, 'r', encoding='utf-8') as fs: content = fs.read() if out_markup == "markdown": # In markdown, to insert a <br />, end a line with two or more spaces & then a end-of-line content = content.replace("\\\n ", " \n") content = content.replace("\\\n", " \n") with open(out_filename, 'w', encoding='utf-8') as fs: fs.write(header + content)