From a4fed648051a6b847d04932a3b77a678d17caa0f Mon Sep 17 00:00:00 2001 From: Jason Ziglar Date: Tue, 21 Feb 2017 09:08:08 -0500 Subject: [PATCH 1/4] Add convert_from function --- pymmd/mmd.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/pymmd/mmd.py b/pymmd/mmd.py index a4642f9..c94dac3 100644 --- a/pymmd/mmd.py +++ b/pymmd/mmd.py @@ -126,6 +126,23 @@ def convert(source, ext=COMPLETE, fmt=HTML, dname=None): src = source.encode('utf-8') return _MMD_LIB.markdown_to_string(src, ext, fmt).decode('utf-8') +def convert_from(fname, ext=COMPLETE, fmt=HTML): + """ + Reads in a file and performs MultiMarkdown conversion, with transclusion ocurring based on the + file directory. Returns the converted string. + + Keyword arguments: + fname -- Filename of document to convert + ext -- extension bitfield to pass to conversion process + fmt -- flag indicating output format to use + """ + + dname = os.path.abspath(os.path.dirname(fname)) + with open(fname, 'r') as fp: + src = fp.read() + + return convert(src, ext, fmt, dname) + def manifest(txt, dname): """Extracts file manifest for a body of text with the given directory.""" _, files = _expand_source(txt, dname, HTML) From c081673ea30685751d1e68ccff237cb5b992af5f Mon Sep 17 00:00:00 2001 From: Jason Ziglar Date: Tue, 21 Feb 2017 09:08:35 -0500 Subject: [PATCH 2/4] Update README to include transclusion in convert() function --- README.rst | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/README.rst b/README.rst index 3bb814c..40be339 100644 --- a/README.rst +++ b/README.rst @@ -59,7 +59,16 @@ Converting a string of MultiMarkdown directly to various outputs: #Generate a snippet html_snippet = pymmd.convert(data, ext=pymmd.SNIPPET) -Files can also be converted directly, which enables the `Transclusion `_ capabilities of MultiMarkdown. +Conversion can be performed with the `Transclusion `_ capabilities of MultiMarkdown, either by specifying the directory name: + +.. code:: python + import pymmd + + with open('./document.mmd') as fp: + src = fp.read() + output = pymmd.convert(src, dname='.') + +Files can also be converted directly from file: .. code:: python From 28565aba6eb2b1be93e03248bd3e3ea365c2456b Mon Sep 17 00:00:00 2001 From: Jason Ziglar Date: Tue, 21 Feb 2017 09:08:53 -0500 Subject: [PATCH 3/4] Add conversion tests --- test/test.py | 30 +++++++++++++++++++++++++++++- test/test_files/section2.mmd | 1 + test/test_files/test_doc.html | 12 ++++++++++++ test/test_files/test_doc.mmd | 3 +++ test/test_files/test_doc.tex | 5 +++++ test/test_files/transclusion.html | 14 ++++++++++++++ test/test_files/transclusion.mmd | 5 +++++ 7 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 test/test_files/section2.mmd create mode 100644 test/test_files/test_doc.html create mode 100644 test/test_files/test_doc.mmd create mode 100644 test/test_files/test_doc.tex create mode 100644 test/test_files/transclusion.html create mode 100644 test/test_files/transclusion.mmd diff --git a/test/test.py b/test/test.py index 7455a34..caad05b 100644 --- a/test/test.py +++ b/test/test.py @@ -2,13 +2,21 @@ # -*- coding: utf-8 -*- """Unit testing for pymmd""" +import os import unittest import textwrap import pymmd -class TestBasicMMD(unittest.TestCase): +class TestPyMMD(unittest.TestCase): """Test Basic MMD operations.""" + + @classmethod + def setUp(cls): + """Set up test configurations.""" + here = os.path.abspath(os.path.dirname(__file__)) + cls.test_dir = os.path.join(here, 'test_files') + def test_valid(self): """Test that pymmd loads the MMD library.""" self.assertTrue(pymmd.valid_mmd()) @@ -52,5 +60,25 @@ def text_empty_metadata(self): self.assertEqual(pymmd.keys(base_txt), []) self.assertEqual(pymmd.value(base_txt, 'title'), '') + def test_convert(self): + """Test conversion function""" + with open(os.path.join(self.test_dir, 'test_doc.mmd')) as fp: + src_doc = fp.read() + + with open(os.path.join(self.test_dir, 'test_doc.html')) as fp: + html_doc = fp.read() + with open(os.path.join(self.test_dir, 'test_doc.tex')) as fp: + tex_doc = fp.read() + + self.assertEqual(pymmd.convert(src_doc), html_doc) + self.assertEqual(pymmd.convert(src_doc, fmt=pymmd.LATEX), tex_doc) + + def test_convert_from(self): + """Test convert_from function""" + with open(os.path.join(self.test_dir, 'transclusion.html')) as fp: + html_res = fp.read() + + self.assertEqual(pymmd.convert_from(os.path.join(self.test_dir,'transclusion.mmd')), html_res) + if __name__ == '__main__': unittest.main() diff --git a/test/test_files/section2.mmd b/test/test_files/section2.mmd new file mode 100644 index 0000000..069191d --- /dev/null +++ b/test/test_files/section2.mmd @@ -0,0 +1 @@ +This file needs to be included as well. \ No newline at end of file diff --git a/test/test_files/test_doc.html b/test/test_files/test_doc.html new file mode 100644 index 0000000..e2065d8 --- /dev/null +++ b/test/test_files/test_doc.html @@ -0,0 +1,12 @@ + + + + + + +

Introduction

+ +

Here is some introductory text.

+ + + \ No newline at end of file diff --git a/test/test_files/test_doc.mmd b/test/test_files/test_doc.mmd new file mode 100644 index 0000000..b023011 --- /dev/null +++ b/test/test_files/test_doc.mmd @@ -0,0 +1,3 @@ +# Introduction + +Here is some introductory text. \ No newline at end of file diff --git a/test/test_files/test_doc.tex b/test/test_files/test_doc.tex new file mode 100644 index 0000000..1ad7e90 --- /dev/null +++ b/test/test_files/test_doc.tex @@ -0,0 +1,5 @@ + +\part{Introduction} +\label{introduction} + +Here is some introductory text. \ No newline at end of file diff --git a/test/test_files/transclusion.html b/test/test_files/transclusion.html new file mode 100644 index 0000000..5c8f988 --- /dev/null +++ b/test/test_files/transclusion.html @@ -0,0 +1,14 @@ + + + + + + +

Transclusion Test

+ +

This document tests Transclusion.

+ +

This file needs to be included as well.

+ + + \ No newline at end of file diff --git a/test/test_files/transclusion.mmd b/test/test_files/transclusion.mmd new file mode 100644 index 0000000..3a67ef2 --- /dev/null +++ b/test/test_files/transclusion.mmd @@ -0,0 +1,5 @@ +# Transclusion Test + +This document tests Transclusion. + +{{section2.mmd}} \ No newline at end of file From ead61fb129f12143344afa2c5d62419983cd83a9 Mon Sep 17 00:00:00 2001 From: Jason Ziglar Date: Tue, 21 Feb 2017 09:09:30 -0500 Subject: [PATCH 4/4] Increment to 0.4.0 to release convert_from function --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index d82503e..eef4133 100644 --- a/setup.py +++ b/setup.py @@ -35,7 +35,7 @@ def is_pure(self): setup( name='pymmd', - version='0.3.0', + version='0.4.0', description='Python wrapper for the MultiMarkdown library.', long_description=long_description, license='MIT',