def test_quotes_in_link_text(): """quotes in link text are tricky.""" test = '""this is a quote in link text"":url' t = Textile() result = t.parse(test) expect = '\t<p><a href="url">“this is a quote in link text”</a></p>' assert result == expect
def test_getRefs(): t = Textile() result = t.getRefs("some text [Google]http://www.google.com") expect = 'some text ' assert result == expect result = t.urlrefs expect = {'Google': 'http://www.google.com'} assert result == expect
def test_table(): t = Textile() result = t.table('(rowclass). |one|two|three|\n|a|b|c|') expect = '\t<table>\n\t\t<tr class="rowclass">\n\t\t\t<td>one</td>\n\t\t\t<td>two</td>\n\t\t\t<td>three</td>\n\t\t</tr>\n\t\t<tr>\n\t\t\t<td>a</td>\n\t\t\t<td>b</td>\n\t\t\t<td>c</td>\n\t\t</tr>\n\t</table>\n\n' assert result == expect t = Textile(lite=True) result = t.table('(lite). |one|two|three|\n|a|b|c|\n| * test\n* test|1|2|') expect = '\t<table>\n\t\t<tr class="lite">\n\t\t\t<td>one</td>\n\t\t\t<td>two</td>\n\t\t\t<td>three</td>\n\t\t</tr>\n\t\t<tr>\n\t\t\t<td>a</td>\n\t\t\t<td>b</td>\n\t\t\t<td>c</td>\n\t\t</tr>\n\t\t<tr>\n\t\t\t<td> * test\n* test</td>\n\t\t\t<td>1</td>\n\t\t\t<td>2</td>\n\t\t</tr>\n\t</table>\n\n' assert result == expect
def test_glyphs(): t = Textile() result = t.glyphs("apostrophe's") expect = 'apostrophe’s' assert result == expect result = t.glyphs("back in '88") expect = 'back in ’88' assert result == expect result = t.glyphs('foo ...') expect = 'foo …' assert result == expect result = t.glyphs('--') expect = '—' assert result == expect result = t.glyphs('FooBar[tm]') expect = 'FooBar™' assert result == expect result = t.glyphs("<p><cite>Cat's Cradle</cite> by Vonnegut</p>") expect = '<p><cite>Cat’s Cradle</cite> by Vonnegut</p>' assert result == expect result = t.glyphs('test"') expect = 'test” ' assert result == expect
def __init__(self, filepath, template): """Class construction""" self.filepath = filepath self.template = template self._tt = Textile() self._parse_file_contents() self._parse_file_path() # Map parsed information to template self._templatize()
class TextileMarkup(AbstractMarkup): """Markup class for Textile language. Inherits :class:`~markups.abstract.AbstractMarkup`. """ name = "Textile" attributes = { common.LANGUAGE_HOME_PAGE: "http://en.wikipedia.org/wiki/Textile_(markup_language)", common.MODULE_HOME_PAGE: "https://github.com/sebix/python-textile", common.SYNTAX_DOCUMENTATION: "http://movabletype.org/documentation/author/textile-2-syntax.html", } file_extensions = (".textile",) default_extension = ".textile" @staticmethod def available(): try: import textile except ImportError: return False return True def __init__(self, filename=None): AbstractMarkup.__init__(self, filename) from textile import Textile self.parser = Textile() def get_document_body(self, text): return self.parser.textile(text)
def test_image(): t = Textile() result = t.image('!/imgs/myphoto.jpg!:http://jsamsa.com') expect = ('<a href="{0}1:url"><img alt="" src="{0}2:url" /></a>'.format( t.uid)) assert result == expect assert t.refCache[1] == 'http://jsamsa.com' assert t.refCache[2] == '/imgs/myphoto.jpg' result = t.image('!</imgs/myphoto.jpg!') expect = '<img align="left" alt="" src="{0}3:url" />'.format(t.uid) assert result == expect assert t.refCache[3] == '/imgs/myphoto.jpg' t = Textile(rel='nofollow') result = t.image('!/imgs/myphoto.jpg!:http://jsamsa.com') expect = ('<a href="{0}1:url" rel="nofollow"><img alt="" src="{0}2:url" ' '/></a>'.format(t.uid)) assert result == expect
def test_span(): t = Textile() result = t.span("hello %(bob)span *strong* and **bold**% goodbye") expect = ('hello <span class="bob">span <strong>strong</strong> and ' '<b>bold</b></span> goodbye') assert result == expect result = t.span('%:http://domain.tld test%') expect = '<span cite="http://domain.tld">test</span>' assert result == expect t = Textile() # cover the partial branch where we exceed the max_span_depth. t.max_span_depth = 2 result = t.span('_-*test*-_') expect = '<em><del>*test*</del></em>' assert result == expect
def test_getRefs(): t = Textile() result = t.getRefs("some text [Google]http://www.google.com") expect = 'some text ' assert result == expect result = t.urlrefs expect = {'Google': 'http://www.google.com'} assert result == expect t2 = Textile() result = t2.getRefs("my ftp [ftp]ftp://example.com") expect = 'my ftp ' assert result == expect result = t2.urlrefs expect = {'ftp': 'ftp://example.com'} assert result == expect
def test_lists(): t = Textile() result = t.textileLists("* one\n* two\n* three") expect = '\t<ul>\n\t\t<li>one</li>\n\t\t<li>two</li>\n\t\t<li>three</li>\n\t</ul>' assert result == expect
def textile_renderer(text): rendered_textile = Textile().textile(text) return rendered_textile
def test_rel_attribute(): t = Textile(rel='nofollow') result = t.parse('"$":http://domain.tld') expect = '\t<p><a href="http://domain.tld" rel="nofollow">domain.tld</a></p>' assert result == expect
def test_urls(): t = Textile() assert t.relURL("http://www.google.com/") == 'http://www.google.com/' result = t.links('fooobar "Google":http://google.com/foobar/ and hello world "flickr":http://flickr.com/photos/jsamsa/ ') expect = 'fooobar {0}2:shelve and hello world {0}4:shelve '.format(t.uid) assert result == expect result = t.links('""Open the door, HAL!"":https://xkcd.com/375/') expect = '{0}6:shelve'.format(t.uid) assert result == expect result = t.links('"$":http://domain.tld/test_[brackets]') expect = '{0}8:shelve'.format(t.uid) assert result == expect result = t.links('<em>"$":http://domain.tld/test_</em>') expect = '<em>{0}10:shelve</em>'.format(t.uid) assert result == expect expect = '"":test' result = t.links(expect) assert result == expect expect = '"$":htt://domain.tld' result = t.links(expect) assert result == expect result = t.shelveURL('') expect = '' assert result == expect result = t.retrieveURLs('{0}2:url'.format(t.uid)) expect = '' assert result == expect result = t.encode_url('http://domain.tld/übermensch') expect = 'http://domain.tld/%C3%BCbermensch' assert result == expect result = t.parse('A link that starts with an h is "handled":/test/ incorrectly.') expect = '\t<p>A link that starts with an h is <a href="/test/">handled</a> incorrectly.</p>' assert result == expect result = t.parse('A link that starts with a space" raises":/test/ an exception.') expect = '\t<p><a href="/test/">A link that starts with a space” raises</a> an exception.</p>' assert result == expect result = t.parse('A link that "contains a\nnewline":/test/ raises an exception.') expect = '\t<p>A link that <a href="/test/">contains a\nnewline</a> raises an exception.</p>' assert result == expect
class Entry: """Entry is the object representing a blog entry""" def __init__(self, filepath, template): """Class construction""" self.filepath = filepath self.template = template self._tt = Textile() self._parse_file_contents() self._parse_file_path() # Map parsed information to template self._templatize() def _templatize(self): d = date.today() current = d.strftime("%Y-%m-%dT%H:%M:%S%z") mappings = ({ '{{ BODY }}': self.htmlbody, '{{ ESCBODY }}': cgi.escape(self.htmlbody), '{{ TITLE }}': self.title, '{{ URL }}': self.relative_url, '{{ DATE }}': self.prettydate, '{{ HOMEURL }}': URL, '{{ DATESTAMP }}': current[:-2] + ':' + current[-2:], '{{ TAG }}': d.strftime('%Y-%m-%d') + ':' + self.relative_url, '{{ PUBLISHED }}': self.year + '-' + self.month + '-' + self.day, }) filehandle = open(self.template) contents = filehandle.read() for key in mappings: contents = contents.replace(key, mappings[key]) self.template_contents = contents def _parse_file_contents(self): filehandle = open(self.filepath) self.title = filehandle.readline().split('Title: ')[1].strip() filehandle.seek(0) body = self._tt.textile(filehandle.read()) body = body.split(self.title + '</p>')[1].strip() self.htmlbody = body[0:] def _parse_file_path(self): entry_dir = self.filepath.split(PWD)[1].split("/")[0] + "/" parts = self.filepath.split(entry_dir)[1].split("/") self.relative_url = "/" self.prettydate = "" self.year = "" self.month = "" self.day = "" self.filename = parts[len(parts) - 1].split(".textile")[0] if entry_dir == "entries/": self.year = parts[0] self.month = parts[1] self.day = parts[2] date = datetime.date(int(self.year), int(self.month), int(self.day)) self.prettydate = date.strftime("%e %B %Y") self.relative_url = os.path.join(self.relative_url, self.year, self.month, self.day) self.relative_url = os.path.join(self.relative_url, self.filename) + ".html" def _createdir(self): outdir = WWWDIR if self.year != "" and self.month != "" and self.day != "": outdir = os.path.join(WWWDIR, self.year, self.month, self.day) if not os.path.isdir(outdir): os.makedirs(outdir) def _createfile(self): outfile = WWWDIR if self.year != "" and self.month != "" and self.day != "": outfile = os.path.join(outfile, self.year, self.month, self.day) outfile = outfile + "/" + self.filename + ".html" writefile(outfile, self.template_contents) def publish(self, filepath=""): """Saves the entry to webroot""" if filepath != "": writefile(filepath, self.template_contents) else: self._createdir() self._createfile()
def __init__(self, filename=None): AbstractMarkup.__init__(self, filename) from textile import Textile self.parser = Textile()
def test_retrieve(): t = Textile() id = t.shelve("foobar") assert t.retrieve(id) == 'foobar'
class Entry: """Entry is the object representing a blog entry""" def __init__(self, filepath, template): """Class construction""" self.filepath = filepath self.template = template self._tt = Textile() self._parse_file_contents() self._parse_file_path() # Map parsed information to template self._templatize() def _templatize(self): d = date.today() current = d.strftime("%Y-%m-%dT%H:%M:%S%z") mappings = ({ '{{ BODY }}': self.htmlbody, '{{ ESCBODY }}': cgi.escape(self.htmlbody), '{{ TITLE }}': self.title, '{{ URL }}': self.relative_url, '{{ DATE }}': self.prettydate, '{{ HOMEURL }}': URL, '{{ DATESTAMP }}': current[:-2] + ':' + current[-2:], '{{ TAG }}': d.strftime('%Y-%m-%d') + ':' + self.relative_url, '{{ PUBLISHED }}': self.year + '-' + self.month + '-' + self.day, }) filehandle = open(self.template) contents = filehandle.read() for key in mappings: contents = contents.replace(key, mappings[key]) self.template_contents = contents def _parse_file_contents(self): filehandle = open(self.filepath) self.title = filehandle.readline().split('Title: ')[1].strip() filehandle.seek(0) body = self._tt.textile(filehandle.read()) body = body.split(self.title + '</p>')[1].strip() self.htmlbody = body[0:] def _parse_file_path(self): entry_dir = self.filepath.split(PWD)[1].split("/")[0] + "/" parts = self.filepath.split(entry_dir)[1].split("/") self.relative_url = "/" self.prettydate = "" self.year = "" self.month = "" self.day = "" self.filename = parts[len(parts)-1].split(".textile")[0] if entry_dir == "entries/": self.year = parts[0] self.month = parts[1] self.day = parts[2] date = datetime.date(int(self.year), int(self.month), int(self.day)) self.prettydate = date.strftime("%e %B %Y") self.relative_url = os.path.join(self.relative_url, self.year, self.month, self.day) self.relative_url = os.path.join(self.relative_url, self.filename) + ".html" def _createdir(self): outdir = WWWDIR if self.year != "" and self.month != "" and self.day != "": outdir = os.path.join(WWWDIR, self.year, self.month, self.day) if not os.path.isdir(outdir): os.makedirs(outdir) def _createfile(self): outfile = WWWDIR if self.year != "" and self.month != "" and self.day != "": outfile = os.path.join(outfile, self.year, self.month, self.day) outfile = outfile + "/" + self.filename + ".html" writefile(outfile, self.template_contents) def publish(self, filepath=""): """Saves the entry to webroot""" if filepath != "": writefile(filepath, self.template_contents) else: self._createdir() self._createfile()