diff --git a/md_pdf/build/content.py b/md_pdf/build/content.py index 27e03a3..4286747 100644 --- a/md_pdf/build/content.py +++ b/md_pdf/build/content.py @@ -19,7 +19,7 @@ def fix_references_title(content, config): return soup.prettify() -def add_base_tag(doc, config): +def make_images_relative(doc, config): logger.debug("Adding Base Tag...") soup = BeautifulSoup(doc, 'html.parser') for img in soup.findAll('img'): @@ -32,6 +32,8 @@ def add_base_tag(doc, config): def add_body_class(doc, config): logger.debug("Adding Body Class...") soup = BeautifulSoup(doc, 'html.parser') + if not soup.body: + return doc soup.body['class'] = 'content' return soup.prettify() @@ -47,7 +49,7 @@ def parse_template(doc, config): parsed_doc = doc for parser in [ fix_references_title, - add_base_tag, + make_images_relative, add_body_class, ]: parsed_doc = parser(parsed_doc, config) diff --git a/tests/__init__.py b/tests/__init__.py index 5fe62ed..7679ddc 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -1,6 +1,7 @@ import unittest import os from md_pdf.consts import TEMPLATES_DIR, STATIC_DIR +from bs4 import BeautifulSoup class BaseTestCase(unittest.TestCase): @@ -16,22 +17,25 @@ class BaseTestCase(unittest.TestCase): } - def removeFile(self, file): + def parse_html(self, html): + return BeautifulSoup(html, 'html.parser') + + def remove_file(self, file): try: os.remove(file) except OSError: pass - def deleteTemplates(self): + def delete_templates(self): for template in [ 'header.html', 'footer.html', 'cover.html', 'toc.xsl', ]: - self.removeFile(os.path.join(TEMPLATES_DIR, template)) + self.remove_file(os.path.join(TEMPLATES_DIR, template)) def tearDown(self): - self.deleteTemplates() - self.removeFile(os.path.join(STATIC_DIR, 'style.css')) + self.delete_templates() + self.remove_file(os.path.join(STATIC_DIR, 'style.css')) diff --git a/tests/test_parser.py b/tests/test_parser.py new file mode 100644 index 0000000..6182204 --- /dev/null +++ b/tests/test_parser.py @@ -0,0 +1,57 @@ +from tests import BaseTestCase +from md_pdf.build import content +import os + + +class FixReferencesTitleTestCase(BaseTestCase): + def test_adds_reference_title(self): + html = '
' + output = content.fix_references_title(html, self.BASE_VALID_CONFIG) + self.assertIn('references-title', output) + self.assertIn('References', output) + + def test_doesnt_modify_if_no_references(self): + html = 'test text' + output = content.fix_references_title(html, self.BASE_VALID_CONFIG) + self.assertNotIn('references-title', output) + self.assertNotIn('References', output) + + +class RelativeImageTestCase(BaseTestCase): + def test_makes_image_relative(self): + html = '' + output = self.parse_html(content.make_images_relative(html, self.BASE_VALID_CONFIG)) + self.assertEqual(output.find('img').attrs['src'], os.path.abspath('test-files/test-image.png')) + + def test_leaves_remote_images(self): + html = '' + output = self.parse_html(content.make_images_relative(html, self.BASE_VALID_CONFIG)) + self.assertEqual(output.find('img').attrs['src'], 'http://example.com/image.png') + + +class AddBodyClassTestCase(BaseTestCase): + def test_adds_class(self): + html = '' + output = self.parse_html(content.add_body_class(html, self.BASE_VALID_CONFIG)) + self.assertEqual(output.body.attrs['class'], ['content']) + + def test_doesnt_change(self): + html = 'test content' + output = content.add_body_class(html, self.BASE_VALID_CONFIG) + self.assertEqual(output, html) + + +class RenderTemplateTestCase(BaseTestCase): + def test_renders_template(self): + html = 'test {{ test }}' + output = content.render_template(html, dict(self.BASE_VALID_CONFIG, **{ + 'test': 'content' + })) + self.assertEqual(output, 'test content') + + def test_changes_nothing(self): + html = 'test test' + output = content.render_template(html, dict(self.BASE_VALID_CONFIG, **{ + 'test': 'content' + })) + self.assertEqual(output, html)