Skip to content

Commit

Permalink
Merge pull request #11 from jasedit/feat/implement_convert_from
Browse files Browse the repository at this point in the history
Feat/implement convert from
  • Loading branch information
jasedit authored Feb 21, 2017
2 parents d5d64fb + ead61fb commit 02e9d7c
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 3 deletions.
11 changes: 10 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <http://fletcher.github.io/MultiMarkdown-5/transclusion>`_ capabilities of MultiMarkdown.
Conversion can be performed with the `Transclusion <http://fletcher.github.io/MultiMarkdown-5/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
Expand Down
17 changes: 17 additions & 0 deletions pymmd/mmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
30 changes: 29 additions & 1 deletion test/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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()
1 change: 1 addition & 0 deletions test/test_files/section2.mmd
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This file needs to be included as well.
12 changes: 12 additions & 0 deletions test/test_files/test_doc.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
</head>
<body>
<h1 id="introduction">Introduction</h1>

<p>Here is some introductory text.</p>

</body>
</html>
3 changes: 3 additions & 0 deletions test/test_files/test_doc.mmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Introduction

Here is some introductory text.
5 changes: 5 additions & 0 deletions test/test_files/test_doc.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

\part{Introduction}
\label{introduction}

Here is some introductory text.
14 changes: 14 additions & 0 deletions test/test_files/transclusion.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
</head>
<body>
<h1 id="transclusiontest">Transclusion Test</h1>

<p>This document tests Transclusion.</p>

<p>This file needs to be included as well.</p>

</body>
</html>
5 changes: 5 additions & 0 deletions test/test_files/transclusion.mmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Transclusion Test

This document tests Transclusion.

{{section2.mmd}}

0 comments on commit 02e9d7c

Please sign in to comment.