#!/usr/bin/env python
import unittest
import sys
if __name__ == "__main__":
# Hack because we can't use from . import w/e if we're __main__
import os.path
sys.path = ([os.path.dirname(os.path.dirname(os.path.realpath(__file__)))]
+ sys.path)
from redbeans.parser import Parser
from redbeans.formats import Format
from redbeans.html import HTMLFormat
from redbeans.latex import LaTeXFormat
from redbeans.tokens import *
if '-v' in sys.argv:
import redbeans.creole
redbeans.creole.debug = True
class TestCreole(unittest.TestCase):
def error_func(self):
self.error = True
def assertMakes(self, mf, str, format, correct, expect_error=False):
if '-v' in sys.argv:
print >>sys.stderr,"...................... New Test.................."
self.error = False
parser = Parser(format, mf, error_func=self.error_func)
ev = ''
for s in parser.iparse(str):
if '-v' in sys.argv: print >>sys.stderr, repr(s)
ev += s
self.assertEqual(correct, ev)
self.assertEqual(expect_error, self.error)
def testBasics(self):
self.assertMakes(None,'foo',Format,'foo')
self.assertMakes(None, "foo **bar** baz was a parrot.", Format,
"foo *bar* baz was a parrot.")
self.assertMakes(None, "foo '[[GoogleFish|Google Fish]]' baz was a parrot.",HTMLFormat,u"""
""")
self.assertMakes(None, "foo '[[GoogleFish]]' baz was a parrot.",
Format,
u"foo \u2018GoogleFish@\u2019 baz was a parrot.")
self.assertMakes(None,'{{wombat.png}}',Format,'[wombat.png]')
self.assertMakes(None,'{{wombat.png|A wombat}}',Format,'[A wombat]')
self.assertMakes(None,'[[Foo]] [[Bar]] [[Baz]]',HTMLFormat,'''
''')
def mf(name, arglist=None, content=None):
if name == 'bar':
yield Text(u"Foo")
elif name == 'fish':
yield Entity(MACRO, ('bar', None))
self.assertMakes(mf,'''<>''',Format,'''Foo''')
self.assertMakes(mf,'''<>''',Format,'''Foo''')
self.assertMakes(mf, '''<><>''', Format,
'''Foo''')
def testBrokenMacros(self):
# Macros
def mf(name, arglist, content=None):
if name == 'fish':
yield Text(u"Carp")
else:
yield Text(u'???' + name)
self.assertMakes(mf,'<', Format, 'Carp')
self.assertMakes(mf,'<\nCheese', Format, 'Carp Cheese')
self.assertMakes(mf,'<Cheese<>', Format, u"???fish>Cheese<Cheese<>
* <><><>
<>
* <><><>
<>''', Format, ' * Name')
def testContentMacros(self):
def mf(name, arglist=None, content=None):
if name == 'par':
yield Entity(ENV_BREAK)
elif name == 'bold':
yield Start(BOLD)
for t in content:
yield t
yield End(BOLD)
elif name == 'great':
yield Start(BOLD)
for t in content:
yield t
yield End(BOLD)
elif name == 'token':
yield Text(u':')
elif name == 'let':
for t in content:
yield t
self.assertMakes(mf,'''<>Bob George<>''',HTMLFormat,'''
''')
self.assertMakes(mf, '''<>
<> is equivalent to :
<>''', Format, ": is equivalent to :")
self.assertMakes(mf, '''<>
<> is equivalent to :
<>''', Format, ": is equivalent to :")
self.assertMakes(mf, '''<>
<>
To flip over.
<>
<>
To go on this Quest.
<>
''', LaTeXFormat, ":\n\n To flip over.\n\n\n\n:\n\n To go on this Quest.")
self.assertMakes(mf, '''<>
== <> ==
= <> =
=== <> ===
= <> =
<>''', HTMLFormat, '''
')
def testHeadings(self):
# Headings
self.assertMakes(None,'= Head =',HTMLFormat,'
Head
')
self.assertMakes(None,'== Head',Format,'== Head ==')
self.assertMakes(None,'=== Head ===',Format,'=== Head ===')
self.assertMakes(None,'= Head ===',Format,'= Head =')
self.assertMakes(None,'Foo\n== Head',HTMLFormat,'
')
self.assertMakes(None,'''Paragraph.
= Head ===
Wine is great.''',Format,'Paragraph.\n\n= Head =\n\nWine is great.')
self.assertMakes(None,'''* Listless
** List listers.
== Head ==
Wine is great.''',Format,' * Listless\n * List listers.\n\n== Head ==\n\nWine is great.')
self.assertMakes(None,'''* Listless
** List listers.
== Head ==
Wine is great.''',HTMLFormat,'''
Listless
List listers.
Head
Wine is great.
''')
def testTables(self):
self.assertMakes(None,'|= Test |= Table |\n|Here|Contents|',Format,'''\t* Test *\t* Table *\n\tHere\tContents''')
self.assertMakes(None,'|= Test |= Table |\n|Here|Contents|',HTMLFormat,'''
Test
Table
Here
Contents
''')
self.assertMakes(None,'| Test | Table |\n|Here|Contents|\n|Three| LineS |',HTMLFormat,'''
Test
Table
Here
Contents
Three
LineS
''')
def mf(name, arglist=None, content=None):
if name == 'mesa':
yield Start(TABLE_ROW)
yield Start(TABLE_HEADING)
yield Text(u' Foo ')
yield End(TABLE_HEADING)
yield Start(TABLE_HEADING)
yield Text(u' Bar')
yield End(TABLE_HEADING)
yield End(TABLE_ROW)
yield Start(TABLE_ROW)
yield Start(TABLE_CELL)
yield Text(u'Baz')
yield End(TABLE_CELL)
yield Start(TABLE_CELL)
yield Text(u'Quux')
yield End(TABLE_CELL)
yield End(TABLE_ROW)
elif name == 'table':
yield Start(TABLE)
for t in content:
yield t
yield End(TABLE)
self.assertMakes(mf,'''<>''',HTMLFormat,'''
''')
self.assertMakes(None,'''This is to test that things like the foll
|= Title |= More Title|
| Body | Elephant |''',HTMLFormat,'''
This is to test that things like the foll
Title
More Title
Body
Elephant
''')
def mf(name, arglist=None,content=None):
if name == 'repeat':
lst = list(content)
for x in xrange(3):
for t in lst:
yield t
self.assertMakes(mf,'''|=Title|=Row
<>
|Foo|Bar<>
|Last|Row''', HTMLFormat,'''
''')
def testDynamicLists(self):
def mf(name, arglist=None, content=None):
if name == 'tree':
for x in [1,2,2,3,2,3,4,1,2,1,2,3,3,1]:
yield Start(UNORDERED_ITEM, x)
yield Text(u'content')
yield End(UNORDERED_ITEM, x)
self.assertMakes(mf,'''Tree:
<>''',HTMLFormat,'''
Tree:
content
content
content
content
content
content
content
content
content
content
content
content
content
content
''')
def testBlockquote(self):
# Blockquote
self.assertMakes(None,''': Line
:: More indented
: Less''',HTMLFormat,'''
Line
More indented
Less
''')
self.assertMakes(None,'''Para
> Line
>> More indented
>> Second
> Less''',HTMLFormat,'''
''')
self.assertMakes(None, '''~Xavid''', Format, '~Xavid')
self.assertMakes(None, '''~the''', Format, '~the')
def testFullparse(self):
def mf(name, arglist=None, content=None):
if name == 'block':
for t in content:
yield t
yield Entity(ENV_BREAK, True)
self.assertMakes(mf,'''
<>
* Fleas
* Swiss
<>
* Trees
''',HTMLFormat,'''
\n\n')
# self.assertMakes(None,'\n; Test: Desc\n;Two: Desc', LaTeXFormat,
# r'''\begin{descripton}
#\item[Test] Desc
#\item[Two] Desc
#\end{description}
#''')
def testTeXMarkup(self):
def mf(name, arglist=None, content=[]):
if name == 'textbf':
yield Start(BOLD)
for t in content:
yield t
yield End(BOLD)
elif name == 'emph':
yield Start(ITALIC)
for t in content:
yield t
yield End(ITALIC)
elif name == 'center':
yield Start(CENTER)
for t in content:
yield t
yield End(CENTER)
elif name == 'cenquote':
yield Start(CENTER)
closed = False
for t in content:
if (t.op == ENTITY and t.style == MACRO
and t.arg[0] == 'break'):
yield End(CENTER)
yield Start(RIGHT)
closed = True
else:
yield t
if closed:
yield End(RIGHT)
else:
yield End(CENTER)
elif name == "'":
lc = list(content)
assert len(lc) == 1 and lc[0].op == TEXT, lc
char = lc[0].arg
assert len(char) == 1
if char == 'e':
yield Text(u'\u00E9')
else:
yield Text(char)
elif name == "Dude":
yield Text(u'Bob')
elif name == "start":
yield Text(u'1615')
elif name == "end":
yield Text(u'1618')
else:
assert False, name
self.assertMakes(mf, 'foo', Format, 'foo')
self.assertMakes(mf, r"foo \textbf{fish} baz was a parrot.",HTMLFormat,"""
foo fish baz was a parrot.
""")
self.assertMakes(mf, r"foo \textbf{\emph{fish}} baz was a parrot.",HTMLFormat,"""
')
self.assertMakes(mf, '...', Format, u'\u2026')
self.assertMakes(mf, "Dr. Alex ``Bulldozer'' Anderson, Esq.", Format,
u"Dr. Alex \u201CBulldozer\u201D Anderson, Esq.")
self.assertMakes(mf, "\cenquote{``Dude.''}{Mike}", HTMLFormat,
u'
\u201cDude.\u201d
Mike
')
self.assertMakes(mf, 'Tammy & Rachel', LaTeXFormat, 'Tammy \\& Rachel')
self.assertMakes(mf, '[[Wherever|Tammy & Rachel]]', LaTeXFormat,
'\\href{Wherever}{Tammy \\& Rachel}')
self.assertMakes(mf, '---', Format, u'\u2014')
self.assertMakes(mf, '--', Format, u'\u2013')
self.assertMakes(mf, "Foo---bar.", Format, u"Foo\u2014bar.")
self.assertMakes(mf, "Foo -- bar.", Format, u"Foo\u2014bar.")
self.assertMakes(mf, "7--9", Format, u"7\u20139")
self.assertMakes(mf, "7-9", Format, u"7\u20139")
self.assertMakes(mf, "Foo--\n\nbar.", Format, u"Foo\u2014\n\nbar.")
self.assertMakes(mf, '--strike--', Format, u'\u2014strike\u2014')
self.assertMakes(mf, "The party is from 7--9 or 8--10.", Format,
u'The party is from 7\u20139 or 8\u201310.')
self.assertMakes(mf, "5-story pagoda", Format,
u'5-story pagoda')
self.assertMakes(mf, '\start--\end', Format, u'1615\u20131618')
self.assertMakes(mf, '\Dude--\Dude', Format, u'Bob\u2013Bob')
self.assertMakes(mf, "\cenquote{``Dude.''}{-- Mike}", HTMLFormat,
u'
\u201cDude.\u201d
\u2014Mike
')
self.assertMakes(mf, r'Pok\'{e}mon', Format, u'Pok\u00E9mon')
self.assertMakes(mf, r'Pok\'emon', Format, u'Pok\u00E9mon')
self.assertMakes(mf, r"\Dude's sandwich", Format,
u'Bob\u2019s sandwich')
self.assertMakes(mf, r"I like \Dude.", Format,
u'I like Bob.')
self.assertMakes(mf, r"\Dude{Likes \Actors.}\Dude{}\Dude{}", Format,
u'BobBobBob')
self.assertMakes(mf, r"\Dude{\first}", Format,
u'Bob')
self.assertMakes(mf, r"\textbf{{{otron}}}", HTMLFormat,
u'
')
self.assertMakes(mf, r"\Dude~\Dude", Format, u'Bob\Dude')
self.assertMakes(mf, r"<>I like \Dude<>", Format,
u'*I like Bob*')
self.assertMakes(mf, "* \emph{**\Dude**: \Dude, \Dude}", Format,
u' * /*Bob*: Bob, Bob/')
self.assertMakes(mf, r'''Foo
\Dude
Bar''', LaTeXFormat, u'''Foo
Bob
Bar''')
# This syntax is standard to use with \LARGE, etc.
self.assertMakes(mf, r'''{\textbf Foo}''', Format, '*Foo*')
self.assertMakes(mf, r'''\textbf{{\emph Foo}}''', Format, '*/Foo/*')
self.assertMakes(mf, r'''\Dude\Dude''', Format, 'BobBob')
self.assertMakes(mf, r'''== Foo ==
\Dude
== Foo ==''', HTMLFormat, '
Foo
\n\n
Bob
\n\n
Foo
')
def testTeXMacros(self):
def mf(name, arglist=None, content=None):
if name == 'count':
if content is not None:
count = 1
for t in content:
if (t.op == ENTITY and t.style == MACRO
and t.arg[0] == 'break'):
count += 1
else:
count = 0
if arglist is not None:
yield Text(unicode(len(arglist)))
yield Text(u',')
yield Text(unicode(count))
elif name == 'fish':
yield Text(u"Trout")
elif name.startswith('fish.'):
yield Text(u"Fishy %s!" % name[len('fish.'):])
else:
assert False, name
self.assertMakes(mf, r'''\count{foo}{bar}{baz}''', Format, u'3')
self.assertMakes(mf, r'''\count[foo][bar][baz]''', Format, u'3,0')
self.assertMakes(mf, r'''\count[foo bar baz]''', Format, u'1,0')
self.assertMakes(mf, r'''\count[foo][bar][baz]{fush}''', Format, u'3,1')
self.assertMakes(mf, r'''\count[foo]{bar}[baz]{fush}''', Format, u'1,1[baz]{fush}')
self.assertMakes(mf, r'''\fish.tree''', Format, u'Fishy tree!')
self.assertMakes(mf, r'''\fish.tree house''', Format,
u'Fishy tree! house')
self.assertMakes(mf, r'''\fish. tree house''', Format,
u'Trout. tree house')
self.assertMakes(mf, r'''\fish.tree.house''', Format,
u'Fishy tree.house!')
def test_arglists(self):
def mf(name, arglist=None, content=None):
if name == 'fish':
for a in arglist:
assert isinstance(a, unicode), arglist
yield Text(a)
yield Text(u'.')
elif name == 'element':
yield Text(u'Metal')
if arglist is not None:
yield Text(unicode(len(arglist)))
else:
assert False, name
self.assertMakes(mf, r'''\fish[foo bar][baz]''', Format,
'foo bar.baz.')
self.assertMakes(mf, r'''<>''', Format,
'i.range(2, 4)."test this".')
# Args aren't auto-evaled.
self.assertMakes(mf, r'''\fish[Maladama][\element]''', Format,
r'Maladama.\element.')
self.assertMakes(mf, r'''\fish[Maladama][\element[2]]''', Format,
'Maladama.\element[2].')
self.assertMakes(mf, r'''\fish[Maladama][\element["2"]]''', Format,
'Maladama.\element["2"].')
def testQuotes(self):
self.assertMakes(None, '"Why?"', Format, u'\u201CWhy?\u201D')
self.assertMakes(None, "'Why?'", Format, u'\u2018Why?\u2019')
self.assertMakes(None, 'He asked, "Why?"', Format,
u'He asked, \u201CWhy?\u201D')
self.assertMakes(None, 'He asked "Why?"', Format,
u'He asked \u201CWhy?\u201D')
self.assertMakes(None, '"Why?" he asked.', Format,
u'\u201CWhy?\u201D he asked.')
self.assertMakes(None, '"He told me \'No,\'" she said.', Format,
u'\u201CHe told me \u2018No,\u2019\u201D she said.')
self.assertMakes(None, '"He told me \'No,\' " she said.', Format,
u'\u201CHe told me \u2018No,\u2019 \u201D she said.')
self.assertMakes(None, '"\'No,\' he told me," she said.', Format,
u'\u201C\u2018No,\u2019 he told me,\u201D she said.')
self.assertMakes(None, '" \'No,\' he told me," she said.', Format,
u'\u201C \u2018No,\u2019 he told me,\u201D she said.')
self.assertMakes(None, 'Hank the-"Big"-Man Whistleblower', Format,
u'Hank the-\u201CBig\u201D-Man Whistleblower')
self.assertMakes(None, '("Why?")', Format, u'(\u201CWhy?\u201D)')
self.assertMakes(None, '**"Why?"**', Format, u'*\u201CWhy?\u201D*')
self.assertMakes(None, '"**Why?**"', Format, u'\u201C*Why?*\u201D')
def mf(name, arglist=None, content=None):
if name == 'break':
return
for t in content:
yield t
self.assertMakes(mf, r'\cenquote{"Why?"}', Format,
u'\u201CWhy?\u201D')
self.assertMakes(mf, r'\cenquote{"Why?"}{"Why not?}"', Format,
u'\u201CWhy?\u201D\u201CWhy not?\u201D')
def testCodeblock(self):
self.assertMakes(None, """{{{
foo:
bar
}}}""", HTMLFormat, """
foo:
bar
""")
def testNoindent(self):
def mf(name, arglist=None, content=None):
if name == 'noindent':
yield Entity(NOINDENT)
# TODO(xavid): The space before Foo is dumb, but not worth worrying
# about right now.
self.assertMakes(mf, '\\noindent\nFoo\n', LaTeXFormat,
u'\\noindent{} Foo')
self.assertMakes(mf, '\\noindent\nFoo\n', HTMLFormat,
u'
\nFoo
')
def testSpacing(self):
self.assertMakes(None, 'Foo Bar', Format, 'Foo Bar')
self.assertMakes(None, 'Foo\n Bar', Format, 'Foo Bar')
self.assertMakes(None, 'Foo\n\n----\n\nBar', Format,
'Foo\n\n' + 70*'-' + '\n\nBar')
self.assertMakes(None, 'Foo\\\\\nBar', Format, 'Foo\nBar')
self.assertMakes(None, 'Foo\\\\Bar', Format, 'Foo\nBar')
def mf(name, arglist, content=None):
if name == 'fish':
yield Text(u"Carp")
elif name == 'c':
for t in content:
yield t
else:
yield Text(u'???' + name)
self.assertMakes(mf, '<>\\\\Bar', Format, 'Carp\nBar')
self.assertMakes(mf, '\\c{foo\\\\bar}', Format, 'foo\nbar')
def testLinks(self):
self.assertMakes(None, '[[http://xavid.us/|Xavidland]]',
LaTeXFormat, r'\href{http://xavid.us/}{Xavidland}')
self.assertMakes(None, '[[http://xavid.us/?__arg=true|Xavidland]]',
LaTeXFormat,
r'\href{http://xavid.us/?\_\_arg=true}{Xavidland}')
self.assertMakes(None, '[[http://xavid.us/~xavid/|Xavidland]]',
LaTeXFormat,
r'\href{http://xavid.us/\~{}xavid/}{Xavidland}')
self.assertMakes(None, 'http://xavid.us/\n\nFoo', LaTeXFormat,
r'''\href{http://xavid.us/}{http://xavid.us/}
Foo''')
self.assertMakes(None, '~http://xavid.us/', LaTeXFormat,
r'http://xavid.us/')
self.assertMakes(None, 'http://xavid.us/?__arg=true', LaTeXFormat,
r'\href{http://xavid.us/?\_\_arg=true}{http://xavid.us/?\_\_arg=true}')
self.assertMakes(None, '"http://xavid.us/">', LaTeXFormat,
u'''\u201c\\href{http://xavid.us/}{http://xavid.us/}\u201c$>$''')
self.assertMakes(None, '(http://xavid.us/).', LaTeXFormat,
u'''(\\href{http://xavid.us/}{http://xavid.us/}).''')
self.assertMakes(None, '[http://xavid.us/].', LaTeXFormat,
u'''{[}\\href{http://xavid.us/}{http://xavid.us/}{]}.''')
def mf(name, arglist, content=None):
if name == 'subpage':
yield Text(u"carp")
# TODO(xavid): this should magically do the right thing
self.assertMakes(mf, 'http://xavid.us/<>', LaTeXFormat,
r'\href{http://xavid.us/}{http://xavid.us/}carp')
self.assertMakes(mf, 'http://xavid.us/\subpage', LaTeXFormat,
r'\href{http://xavid.us/}{http://xavid.us/}carp')
if __name__ == "__main__":
unittest.main()