Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 40 additions & 14 deletions discount/libmarkdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,29 @@
import os


MKD_NOLINKS = 0x0001
MKD_NOIMAGE = 0x0002
MKD_NOPANTS = 0x0004
MKD_NOHTML = 0x0008
MKD_STRICT = 0x0010
MKD_NO_EXT = 0x0040
MKD_NOHEADER = 0x0100
MKD_TABSTOP = 0x0200
MKD_NOTABLES = 0x0400
MKD_TOC = 0x1000
MKD_1_COMPAT = 0x2000
MKD_AUTOLINK = 0x4000
MKD_SAFELINK = 0x8000
MKD_NOLINKS = 0x00000001
MKD_NOIMAGE = 0x00000002
MKD_NOPANTS = 0x00000004
MKD_NOHTML = 0x00000008
MKD_STRICT = 0x00000010
MKD_TAGTEXT = 0x00000020
MKD_NO_EXT = 0x00000040
MKD_CDATA = 0x00000080
MKD_NOSUPERSCRIPT = 0x00000100
MKD_NORELAXED = 0x00000200
MKD_NOTABLES = 0x00000400
MKD_NOSTRIKETHROUGH = 0x00000800
MKD_TOC = 0x00001000
MKD_1_COMPAT = 0x00002000
MKD_AUTOLINK = 0x00004000
MKD_SAFELINK = 0x00008000
MKD_NOHEADER = 0x00010000
MKD_TABSTOP = 0x00020000
MKD_NODIVQUOTE = 0x00040000
MKD_NOALPHALIST = 0x00080000
MKD_NODLIST = 0x00100000
MKD_EXTRA_FOOTNOTE = 0x00200000
MKD_NOSTYLE = 0x00400000


_so = ctypes.CDLL(
Expand Down Expand Up @@ -136,13 +146,26 @@ class Footnote_STRING(ctypes.Structure):
('alloc', ctypes.c_int),
]

# An empty class with _fields_ assigned later is done because of the
# circular reference in the C struct.
class Escaped(ctypes.Structure):
pass

Escaped._fields_ = [
('text', ctypes.c_char_p),
('up', ctypes.POINTER(Escaped)),
]


class MMIOT(ctypes.Structure):
_fields_ = [
('out', Cstring),
('in_', Cstring),
('Q', Qblock),
('isp', ctypes.c_int),
('reference', ctypes.c_int),
('esc', ctypes.POINTER(Escaped)),
('ref_prefix', ctypes.c_char_p),
('footnotes', ctypes.POINTER(Footnote_STRING)),
('flags', ctypes.c_int),
('base', ctypes.c_char_p),
Expand All @@ -152,12 +175,15 @@ class MMIOT(ctypes.Structure):
class Document(ctypes.Structure):
_fields_ = [
('magic', ctypes.c_int),
('headers', ctypes.POINTER(Line)),
('title', ctypes.POINTER(Line)),
('author', ctypes.POINTER(Line)),
('date', ctypes.POINTER(Line)),
('content', Line_ANCHOR),
('code', ctypes.POINTER(Paragraph)),
('compiled', ctypes.c_int),
('html', ctypes.c_int),
('tabstop', ctypes.c_int),
('ref_prefix', ctypes.c_char_p),
('ctx', ctypes.POINTER(MMIOT)),
('cb', ctypes.c_void_p), # TODO: bind related structures
]
Expand Down
37 changes: 25 additions & 12 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,18 @@
from distutils.core import setup, Extension


DEFAULT_DISCOUNT_VERSION = '1.6.6'
DEFAULT_DISCOUNT_VERSION = 'v2.1.7'


DEFAULT_DISCOUNT_DOWNLOAD_URL = (
'http://github.com/Orc/discount/tarball/v%s'
'http://github.com/Orc/discount/tarball/%s'
) % DEFAULT_DISCOUNT_VERSION


DEFAULT_DISCOUNT_CONFIGURE_OPTS = (
# DL tag extension
'--enable-dl-tag '

# Use pandoc-style header blocks
'--enable-pandoc-header '

# A^B becomes A<sup>B</sup>
'--enable-superscript '

# underscores aren'\''t special in the middle of words
'--relaxed-emphasis '

# Set tabstops to N characters (default is 4)
'--with-tabstops=4 '

Expand Down Expand Up @@ -104,6 +95,28 @@ def build_extension(self, ext):
)
os.chdir(current_dir)

if not os.path.exists(
os.path.join(discount_src_path, 'mktags')):
current_dir = os.getcwd()
os.chdir(discount_src_path)
subprocess.call(
['make', 'blocktags'],
env=os.environ
)
os.chdir(current_dir)

# if not os.path.exists(
# os.path.join(discount_src_path, 'blocktags')):
# current_dir = os.getcwd()
# os.chdir(discount_src_path)
# with open(os.path.join(discount_src_path, 'blocktags'), 'w') as f:
# subprocess.call(
# ['mktags'],
# stdout=f,
# env=os.environ
# )
# os.chdir(current_dir)

ext.sources = [
os.path.join(discount_src_path, s) for s in ext.sources
]
Expand Down Expand Up @@ -158,7 +171,7 @@ def build_extension(self, ext):
'amalloc.c', 'Csio.c', 'css.c', 'docheader.c',
'dumptree.c', 'generate.c', 'markdown.c', 'mkdio.c',
'resource.c', 'toc.c', 'version.c', 'xml.c', 'xmlpage.c',
'basename.c', 'emmatch.c', 'tags.c', 'html5.c',
'basename.c', 'emmatch.c', 'tags.c', 'html5.c', 'setup.c',
],
)
],
Expand Down
80 changes: 37 additions & 43 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ def test_mkd_css(self):
self.assertNotEqual(ret, -1)
self.assertEqual(
sb.value[:ret],
'<style> *{color:red}</style>\n'
'<style>\n *{color:red}\n</style>\n'
)

def test_mkd_generatecss(self):
Expand Down Expand Up @@ -274,7 +274,7 @@ def test_mkd_generatecss(self):
html = out.read()

self.assertEqual(
html, '<style> *{color:red}</style>\n'
html, '<style>\n *{color:red}\n</style>\n'
)

out.close()
Expand Down Expand Up @@ -305,11 +305,10 @@ def test_mkd_toc(self):
self.assertNotEqual(ret, -1)
self.assertEqual(
sb.value[:ret], (
'\n '
'<ul>\n <li><a href="#header-1">header-1</a>\n '
'<ul>\n <li><a href="#header-2">header-2</a>\n '
'<ul>\n <li><a href="#header-3">header-3</a> </li>\n '
'</ul>\n </li>\n </ul>\n </li>\n </ul>\n'
'<ul>\n <li><a href="#header-1">header-1</a>\n '
'<ul>\n <li><a href="#header-2">header-2</a>\n '
'<ul>\n <li><a href="#header-3">header-3</a></li>\n '
'</ul>\n </li>\n </ul>\n </li>\n</ul>\n'
)
)

Expand Down Expand Up @@ -345,11 +344,10 @@ def test_mkd_generatetoc(self):

self.assertEqual(
html, (
'\n '
'<ul>\n <li><a href="#header-1">header-1</a>\n '
'<ul>\n <li><a href="#header-2">header-2</a>\n '
'<ul>\n <li><a href="#header-3">header-3</a> </li>\n '
'</ul>\n </li>\n </ul>\n </li>\n </ul>\n'
'<ul>\n <li><a href="#header-1">header-1</a>\n <ul>\n '
'<li><a href="#header-2">header-2</a>\n <ul>\n '
'<li><a href="#header-3">header-3</a></li>\n </ul>\n '
'</li>\n </ul>\n </li>\n</ul>\n'
)
)

Expand Down Expand Up @@ -393,8 +391,8 @@ def test_mkd_dump(self):

self.assertEqual(
dump, (
'title-----[source]--+--[header, <P>, 1 line]\n'
' `--[markup, <P>, 1 line]\n'
'title-----[source]--+--[header, 1 line]\n'
' `--[markup, 1 line]\n'
)
)

Expand Down Expand Up @@ -700,8 +698,8 @@ def test_MKD_NOPANTS(self):
'a --- b\n'
'a ... b\n'
),
'<p>&ldquo;test&rdquo;\n``test\na &mdash; b\n'
'a &mdash;&ndash; b\na &hellip; b</p>',
'<p>&ldquo;test&rdquo;\n``test\na &ndash; b\n'
'a &mdash; b\na &hellip; b</p>',
0
)

Expand Down Expand Up @@ -787,7 +785,7 @@ def test_MKD_NOTABLES(self):
'a | b\n'
'-- | -\n'
'1 | 2\n',
'<p>a | b\n&mdash; | &ndash;\n1 | 2</p>',
'<p>a | b\n&ndash; | -\n1 | 2</p>',
libmarkdown.MKD_NOTABLES
)

Expand Down Expand Up @@ -828,8 +826,8 @@ def test_MKD_TOC(self):
self._test_flag(
'# header-1\n'
'## header-2\n',
'<h1 id="header-1">header-1</h1>\n\n'
'<h2 id="header-2">header-2</h2>',
'<a name="header-1"></a>\n<h1>header-1</h1>\n\n'
'<a name="header-2"></a>\n<h2>header-2</h2>',
libmarkdown.MKD_TOC
)

Expand Down Expand Up @@ -978,19 +976,18 @@ def test_input_string_get_html_css(self):

style = md.get_html_css()

self.assertEqual(style, '<style> *{color:red}</style>\n')
self.assertEqual(style, '<style>\n *{color:red}\n</style>\n')

def test_input_string_get_html_toc(self):
md = Markdown('# header-1\n## header-2\n### header-3')
html = md.get_html_toc()

self.assertEqual(
html, (
'\n '
'<ul>\n <li><a href="#header-1">header-1</a>\n '
'<ul>\n <li><a href="#header-2">header-2</a>\n '
'<ul>\n <li><a href="#header-3">header-3</a> </li>\n '
'</ul>\n </li>\n </ul>\n </li>\n </ul>\n'
'<ul>\n <li><a href="#header-1">header-1</a>\n <ul>\n '
'<li><a href="#header-2">header-2</a>\n <ul>\n '
'<li><a href="#header-3">header-3</a></li>\n </ul>\n '
'</li>\n </ul>\n </li>\n</ul>\n'
)
)

Expand All @@ -1011,7 +1008,7 @@ def test_input_string_write_html_css(self):
style = o.read()
o.close()

self.assertEqual(style, '<style> *{color:red}</style>\n')
self.assertEqual(style, '<style>\n *{color:red}\n</style>\n')

def test_input_string_write_html_toc(self):
o = tempfile.TemporaryFile('r+w')
Expand All @@ -1024,11 +1021,10 @@ def test_input_string_write_html_toc(self):

self.assertEqual(
html, (
'\n '
'<ul>\n <li><a href="#header-1">header-1</a>\n '
'<ul>\n <li><a href="#header-2">header-2</a>\n '
'<ul>\n <li><a href="#header-3">header-3</a> </li>\n '
'</ul>\n </li>\n </ul>\n </li>\n </ul>\n'
'<ul>\n <li><a href="#header-1">header-1</a>\n <ul>\n '
'<li><a href="#header-2">header-2</a>\n <ul>\n '
'<li><a href="#header-3">header-3</a></li>\n </ul>\n '
'</li>\n </ul>\n </li>\n</ul>\n'
)
)

Expand Down Expand Up @@ -1138,7 +1134,7 @@ def test_input_file_get_html_css(self):

style = md.get_html_css()

self.assertEqual(style, '<style> *{color:red}</style>\n')
self.assertEqual(style, '<style>\n *{color:red}\n</style>\n')

def test_input_file_get_html_toc(self):
i = tempfile.TemporaryFile('r+w')
Expand All @@ -1150,11 +1146,10 @@ def test_input_file_get_html_toc(self):

self.assertEqual(
html, (
'\n '
'<ul>\n <li><a href="#header-1">header-1</a>\n '
'<ul>\n <li><a href="#header-2">header-2</a>\n '
'<ul>\n <li><a href="#header-3">header-3</a> </li>\n '
'</ul>\n </li>\n </ul>\n </li>\n </ul>\n'
'<ul>\n <li><a href="#header-1">header-1</a>\n <ul>\n '
'<li><a href="#header-2">header-2</a>\n <ul>\n '
'<li><a href="#header-3">header-3</a></li>\n </ul>\n '
'</li>\n </ul>\n </li>\n</ul>\n'
)
)

Expand Down Expand Up @@ -1182,7 +1177,7 @@ def test_input_file_write_html_css(self):
o.close()
i.close()

self.assertEqual(style, '<style> *{color:red}</style>\n')
self.assertEqual(style, '<style>\n *{color:red}\n</style>\n')

def test_input_file_write_html_toc(self):
i = tempfile.TemporaryFile('r+w')
Expand All @@ -1200,11 +1195,10 @@ def test_input_file_write_html_toc(self):

self.assertEqual(
html, (
'\n '
'<ul>\n <li><a href="#header-1">header-1</a>\n '
'<ul>\n <li><a href="#header-2">header-2</a>\n '
'<ul>\n <li><a href="#header-3">header-3</a> </li>\n '
'</ul>\n </li>\n </ul>\n </li>\n </ul>\n'
'<ul>\n <li><a href="#header-1">header-1</a>\n <ul>\n '
'<li><a href="#header-2">header-2</a>\n <ul>\n '
'<li><a href="#header-3">header-3</a></li>\n </ul>\n '
'</li>\n </ul>\n </li>\n</ul>\n'
)
)

Expand Down