def process(page): menucfg = page.config.get('menu', 'menu') if menucfg: if 'menu' in page.headers: tmp = template.get_template(page.headers['menu']) page.template.replace('menu', tmp) elif page.config.has_option('templates', 'menu'): tmp = template.get_template(page.config.get('templates', 'menu')) page.template.replace('menu', tmp) split = menucfg.split(',') page.template.repeat('menulinks', len(split)) x = 0 for item in split: (id, link) = item.split('=') title = '%s-title' % id titlecfg = page.config.get('menu', title) if titlecfg: page.template.set_value('menulink', titlecfg, x) else: page.template.set_value('menulink', id, x) page.template.set_attribute('menulink', 'id', id, x) page.template.set_attribute('menulink', 'href', link, x) x += 1 return page
def process(page): # include the head if 'foot' in page.headers: tmp = template.get_template(page.headers['foot']) page.template.replace('foot', tmp) elif page.config.has_option('templates', 'foot'): tmp = template.get_template(page.config.get('templates', 'foot')) page.template.replace('foot', tmp) return page
def setUp(self): template.get_template('rss_test.xml') mock_open = Mock(return_value = MagicMock(spec=file_spec)) mock_open.return_value.read.return_value = 'this is a test' self.orig_open = __main__.__builtins__.open __main__.__builtins__.open = mock_open mock_stat = Mock(return_value = MagicMock(st_mtime = 100)) self.orig_os_stat = os.stat os.stat = mock_stat
def test_repeat4(self): pg = template.get_template('repeat3.xhtml') chunk = [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'] length = len(chunk) pg.repeat('image', 20) print(str(pg))
def new_index_page(page_to_copy, page_num, count, total_posts, posts_per_page, path): index_page = Page() index_page.copy(page_to_copy) index_page.url = '/' + path + '/index-%s' % count index_page.template = template.get_template('post.html') apply_filter('page-head', index_page) apply_filter('page-meta', index_page) apply_filter('page-menu', index_page) apply_filter('page-foot', index_page) total_pages = math.ceil(total_posts / posts_per_page) - 1 if page_num > 0: index_page.template.set_value('prevpage', '<< Newer posts') if page_num - 1 == 0: index_page.template.set_attribute('prevpage', 'href', 'index.html') else: index_page.template.set_attribute('prevpage', 'href', 'index-%s.html' % (page_num - 1)) if page_num < total_pages: index_page.template.set_value('nextpage', 'Older posts >>') index_page.template.set_attribute('nextpage', 'href', 'index-%s.html' % (page_num + 1)) if page_num > 0 and page_num < total_pages: index_page.template.set_value('pagelinksep', ' | ') index_page.template.repeat('posts', min(posts_per_page, total_posts - count)) return index_page
def test_hiding3(self): tmp = template.get_template('hiding3.xhtml') tmp.set_value('title', 'Hiding Xhtml Page', '*') tmp2 = template.get_template('hiding-include.xhtml') tmp.replace('replaced-element', tmp2) tmp.set_value('not-hidden', 'Not hidden content') tmp.hide('hidden-element') out = str(tmp) print(out) et = etree.fromstring(out) self.assert_(et.find('body/span') == None, 'span should have been removed')
def run(times): ## call benchmark function for x in range(0, times): tmp = template.get_template('benchmark.xhtml') tmp.repeat('data', len(data)) for y in range(0, len(data)): d = data[y] tmp.set_attribute('clazz', 'class', d.clazz, y) tmp.set_value('id', str(d.id), y) tmp.set_attribute('symbol', 'href', '/stock/%s' % d.symbol, y) tmp.set_value('symbol', d.symbol, y) tmp.set_attribute('url', 'href', d.url, y) tmp.set_value('name', d.name, y) tmp.set_value('price', d.price, y) if float(d.change) < 0: tmp.hide('azchange', y) tmp.hide('azratio', y) tmp.set_value('bzchange', d.change, y) tmp.set_value('bzratio', d.ratio, y) else: tmp.hide('bzchange', y) tmp.hide('bzratio', y) tmp.set_value('azchange', d.change, y) tmp.set_value('azratio', d.ratio, y) str(tmp)
def process_path(path, output_path, pages): sorted_posts = sorted(filter_pages(path, pages.values()), key=lambda x: x.url[len(path)+1:len(path)+11], reverse=True) total_posts = min(len(sorted_posts), 20) if total_posts == 0: return tmp = template.get_template('rss.xml') if not tmp: return page = sorted_posts[0] tmp.set_value('channel-title', page.config.get('feed', 'title')) tmp.set_value('channel-link', page.config.get('feed', 'link')) tmp.set_value('channel-description', page.config.get('feed', 'description')) tmp.set_value('generator', 'SiteBaker') last_build = time.strftime('%a, %d %b %Y %H:%M:%S %Z') # temporary hack last_build = last_build.replace('BST', 'GMT') tmp.set_value('lastbuild', last_build) max_modified = sorted_posts[0].last_modified tmp.repeat('items', total_posts) for x in range(0, total_posts): page = sorted_posts[x] max_modified = max(page.last_modified, max_modified) html_content = page.get_html_content() link = page.get_permalink() tmp.set_value('title', page.headers['title'], x) d = time.strftime('%a, %d %b %Y', time.strptime(page.get_posted_date(), '%d %b, %Y')) t = page.last_modified.strftime(' %H:%M:%S %Z').replace('BST', 'GMT') tmp.set_value('pubdate', d + t, x) tmp.set_value('description', '<![CDATA[%s]]>' % html_content, x) tmp.set_value('link', link + '.html', x) tmp.set_value('guid', link, x) if path.startswith('/'): path = path[1:] feed_file = os.path.join(output_path, path, 'feed.xml') if os.path.exists(feed_file): statbuf = os.stat(feed_file) last_modified = datetime.datetime.fromtimestamp(statbuf.st_mtime) else: last_modified = None if not last_modified or max_modified >= last_modified: out = str(tmp) f = open(feed_file, 'w+') f.write(out) f.close()
def test_hiding(self): tmp = template.get_template('hiding.xhtml') tmp.set_value('title', 'Hiding Xhtml Page', '*') tmp.hide('hidden-element') out = str(tmp) print(out) et = etree.fromstring(out) self.assert_(et.find('body/div') == None, 'div should have been removed')
def __init__(self, kernel=None, path=None, output_path=None, url=None, config=None): self.kernel = kernel self.url = url self.output_url = None self.output_path = output_path self.full_content = None self.fmt_last_modified = None if path: name = os.path.basename(path)[0:-5] self.full_content = open(path).read() statbuf = os.stat(path) self.last_modified = datetime.datetime.fromtimestamp(statbuf.st_mtime) self.fmt_last_modified = self.last_modified.strftime('%d %b, %Y') self.headers = {} self.config = config if self.full_content: pos = self.full_content.find('\n\n') if pos >= 0: tmp = self.full_content[0:pos] for line in tmp.split('\n'): kvpos = line.find(':') if kvpos > 0: key = line[0:kvpos].strip() val = line[kvpos+1:].strip() self.headers[key] = val if len(self.headers): self.content = self.full_content[pos:] else: self.content = self.full_content template_name = None if 'template' in self.headers: template_name = self.headers['template'] elif self.config: template_name = self.config.get('templates', name) if not template_name and self.config: template_name = self.config.get('templates', 'default') if template_name: self.template = template.get_template(template_name) if 'posted-on' in self.headers: self.fmt_last_modified = self.headers['posted-on'] # default to html content (TODO: probably should handle this better) if url is not None: self.filename = url[1:] + '.html' self.full_output_path = os.path.join(output_path, self.filename) else: self.filename = None self.full_output_path = None
def test_replace_eid(self): tmp = template.get_template('replace.xhtml') tmp.replace('title', '<h2>replaced title</h2>') out = str(tmp) print(out) et = etree.fromstring(out) h2 = et.findall('body/h2') self.assert_(len(h2) == 1)
def test_replace_rid(self): tmp = template.get_template('replace.xhtml') tmp.replace('list-item', '<p>replaced list item</p>') out = str(tmp) print(out) et = etree.fromstring(out) p = et.findall('body/ul/p') self.assert_(len(p) == 1)
def test_include(self): tmp = template.get_template('include1.xhtml') tmp2 = template.get_template('include2.xhtml') tmp.set_value('title', 'Page Title') tmp.replace('include-content', tmp2) tmp.set_value('para1', 'First paragraph of text') tmp.set_value('para2', 'Second paragraph of text') out = str(tmp) print(out) et = etree.fromstring(out) self.assert_(et.find('body/h1').text == 'Page Title', 'incorrect heading') p = et.findall('body/div/p') self.assert_(len(p) == 2, 'should be 2 paragraphs') self.assert_(p[0].text == 'First paragraph of text', 'paragraph text incorrect') self.assert_(p[1].text == 'Second paragraph of text', 'paragraph text incorrect')
def setUp(self): tmp = template.get_template("basic_test.html") mock_open = Mock(return_value=MagicMock(spec=file_spec)) mock_open.return_value.read.return_value = "this is a test" self.orig_open = __main__.__builtins__.open __main__.__builtins__.open = mock_open mock_stat = Mock(return_value=MagicMock(st_mtime=100)) self.orig_os_stat = os.stat os.stat = mock_stat
def test_basic_twice(self): tmp = template.get_template('basic.xhtml') tmp.set_value('title', 'Basic Xhtml Page', '*') out1 = str(tmp) et1 = etree.fromstring(out1) tmp = template.get_template('basic.xhtml') tmp.set_value('title', 'Basic Xhtml Page 1', 0) tmp.set_value('title', 'Basic Xhtml Page 2', 1) tmp.set_value('content', 'Content goes here') tmp.set_value('link', 'Link goes here') tmp.set_attribute('link', 'href', 'http://www.duckduckgo.com') out2 = str(tmp) et2 = etree.fromstring(out2) self.assert_(et1.find('head/title').text == 'Basic Xhtml Page', 'incorrect title') self.assert_(et2.find('head/title').text == 'Basic Xhtml Page 1', 'incorrect title') self.assert_(et2.find('body/h1').text == 'Basic Xhtml Page 2', 'incorrect title')
def test_basic_with_namespace(self): tmp = template.get_template('basic-with-namespace.xml') tmp.repeat('urls', 2) tmp.set_value('url', 'http://testhost/test1.html', 0) tmp.set_value('last-modified', '2012-01-01T23:59:59', 0) tmp.set_value('url', 'http://testhost/test2.html', 1) tmp.set_value('last-modified', '2012-01-02T12:59:59', 1) out = str(tmp) print(out)
def test_repeat3(self): tmp = template.get_template('repeat2.xhtml') tmp.repeat('posts', 50) out = str(tmp) et = etree.fromstring(out) li = et.findall("*//div[@class='contentcontainer']") self.assert_(len(li) == 50, 'expecting 50 content divs, actual %s' % len(li)) li = et.findall("*//div[@id='menucontainer']") self.assert_(len(li) == 1, 'expecting 1 menu divs, actual %s' % len(li))
def test_two_templates(self): tmp1 = template.get_template('twotemplates.xhtml') self.applydata(tmp1) print("\nXHTML:\n%s" % str(tmp1)) et = etree.fromstring(str(tmp1)) td = et.findall('.//td') self.assert_(td[1].text == '1', 'expected 1 was %s' % td[1].text) self.assert_(td[3].text == 'my item A') self.assert_(td[5].text == '2') self.assert_(td[7].text == 'my item B') tmp2 = template.get_template('twotemplates.xml') self.applydata(tmp2) print("\nXML:\n%s\n" % str(tmp2)) et = etree.fromstring(str(tmp2)) item = et.findall('item') self.assert_(item[0].attrib['id'] == '1') self.assert_(item[0].text == 'my item A') self.assert_(item[1].attrib['id'] == '2') self.assert_(item[1].text == 'my item B')
def test_translation(self): tmp = template.get_template('i18n.xhtml') tmp.translator = translate out = str(tmp) print(out) et = etree.fromstring(out) self.assert_(et.find('head/title').text == 'Titre de la page', 'the title should be translated') self.assert_(et.find('body/h1').text == 'Titre de la page', 'the heading should be translated') paras = et.findall('body/p') self.assert_(paras[0].text == 'Certains textes traduits', 'para1 should be translated') self.assert_(paras[1].text == 'Not translated text', 'para2 should not be translated')
def test_basic_list(self): tmp = template.get_template('basic-list.xhtml') lst = [ 'a', 'b', 'c', 'd', 'e', 'f', 'g' ] tmp.set_value('list-item', lst) out = str(tmp) print(out) et = etree.fromstring(out) li = et.findall('body/ul/li') self.assert_(li[0].text == 'a') self.assert_(li[1].text == 'b') self.assert_(li[5].text == 'f') self.assert_(li[6].text == 'g')
def test_hiding2(self): tmp = template.get_template('hiding2.xhtml') tmp.set_value('title', 'Navigation Example', '*') tmp.hide('autopayments') tmp.hide('exchange') tmp.hide('transactions') out = str(tmp) print(out) et = etree.fromstring(out) anchors = et.findall('body/ul/li/a[@href]') self.assert_(len(anchors) == 3, 'there should be only 3 anchor elements, found %s' % len(anchors)) self.assert_(anchors[0].attrib['href'] == '/accounts') self.assert_(anchors[1].attrib['href'] == '/transfer') self.assert_(anchors[2].attrib['href'] == '/bills')
def test_basic_props(self): tmp = template.get_template('basic-props.xhtml') t = Temp('100', '500') tmp.set_properties('prop', t) t2 = Temp2('http://somewebsite.com') tmp.set_properties('prop2', t2) tmp.set_value('prop2', 'A Link') out = str(tmp) print(out) et = etree.fromstring(out) dd = et.findall('body/dl/dd') self.assert_(dd[0].text == '100') self.assert_(dd[1].text == '500')
def test_repeat(self): tmp = template.get_template('repeat-complex.xhtml') tmp.repeat('posts', 5) # first post tmp.set_value('content', 'lorum ipsom 1', 0) tmp.repeat('taglinks', 1, 0) tmp.set_value('taglink', 'testtag1', 0) tmp.set_attribute('taglink', 'href', '/tags/testtag1', 0) # second post tmp.set_value('content', 'lorum ipsom 2', 1) tmp.repeat('taglinks', 1, 1) tmp.set_value('taglink', 'testtag2', 1) tmp.set_attribute('taglink', 'href', '/tags/testtag2', 1) # third post tmp.set_value('content', 'lorum ipsom 3', 2) tmp.repeat('taglinks', 2, 2) tmp.set_value('taglink', 'testtag3', 2) tmp.set_attribute('taglink', 'href', '/tags/testtag3', 2) tmp.set_value('taglink', 'testtag4', 3) tmp.set_attribute('taglink', 'href', '/tags/testtag4', 3) # fourth post tmp.set_value('content', 'lorum ipsom 4', 3) tmp.repeat('taglinks', 3, 4) tmp.set_value('taglink', 'testtag5', 4) tmp.set_attribute('taglink', 'href', '/tags/testtag5', 4) tmp.set_value('taglink', 'testtag6', 5) tmp.set_attribute('taglink', 'href', '/tags/testtag6', 5) tmp.set_value('taglink', 'testtag7', 6) tmp.set_attribute('taglink', 'href', '/tags/testtag7', 6) # fifth post tmp.set_value('content', 'lorum ipsom 5', 4) tmp.repeat('taglinks', 2, 7) tmp.set_value('taglink', 'testtag8', 7) tmp.set_attribute('taglink', 'href', '/tags/testtag8', 7) tmp.set_value('taglink', 'testtag9', 8) tmp.set_attribute('taglink', 'href', '/tags/testtag9', 8) out = str(tmp) print(out)
def test_blogrender(self): tmp = template.get_template('blogexample.xhtml') tmp.set_value('title', 'My Test Blog') menu = self.get_menu() tmp.repeat('menu', len(menu)) for x in range(0, len(menu)): tmp.set_properties('menu', menu[x], x) blogroll = self.get_blogroll() tmp.repeat('blogroll', len(blogroll)) for x in range(0, len(blogroll)): tmp.set_properties('blogroll', blogroll[x], x) post = Post('My First Post', '*****@*****.**', '10-Jan-2009 21:47pm', 'This is my first post...\n...and what a great post it is.') tmp.set_properties('post', post) out = str(tmp) print(out) et = etree.fromstring(out) self.assert_(et.find('body/div/h1').text == 'My First Post') p = et.findall('body/div/p/span') self.assert_(p[0].text == '10-Jan-2009 21:47pm') self.assert_(p[1].text == '*****@*****.**') p = et.findall('body/div/div/p') self.assert_(p[0].text == 'This is my first post...') self.assert_(p[1].text == '...and what a great post it is.') a = et.findall('body/div/ul/li/a') self.assert_(a[0].text == 'Home') self.assert_(a[0].attrib['href'] == '/home') self.assert_(a[1].text == 'About') self.assert_(a[1].attrib['href'] == '/about') self.assert_(a[2].text == 'Admin') self.assert_(a[2].attrib['href'] == '/admin') a = et.findall('body/div/ol/li/a') self.assert_(a[0].text == 'Some blog') self.assert_(a[0].attrib['href'] == 'http://www.someblog.com') self.assert_(a[1].text == 'Another blog') self.assert_(a[1].attrib['href'] == 'http://www.anotherblog.com')
def test_repeat(self): tmp = template.get_template('repeat.xhtml') tmp.set_value('title', 'Repeating Xhtml Page', '*') tmp.set_value('link', 'This is a link to Google') tmp.set_attribute('link', 'href', 'http://www.duckduckgo.com') tmp.repeat('list-item', 5) for x in range(0, 5): tmp.set_value('list-item', 'test%s' % x, x) out = str(tmp) print(out) et = etree.fromstring(out) li = et.findall('body/ul/li') self.assert_(len(li) == 5) for x in range(0, 5): self.assert_(li[x].text == 'test%s' % x, 'expecting test%s, actual %s' % (x, li[x].text))
def test_basic(self): tmp = template.get_template('basic.xhtml') tmp.set_value('title', 'Basic Xhtml Page', '*') tmp.set_value('content', 'Content goes here') tmp.set_value('link', 'Link goes here') tmp.set_attribute('link', 'href', 'http://www.duckduckgo.com') out = str(tmp) et = etree.fromstring(out) self.assert_(et.find('head/title').text == 'Basic Xhtml Page', 'incorrect title') self.assert_(et.find('body/h1').text == 'Basic Xhtml Page', 'incorrect heading') self.assert_(et.find('body/div').text == 'Content goes here', 'incorrect body') self.assert_(et.find('body/a').text == 'Link goes here', 'incorrect anchor text') self.assert_(et.find('body/a').attrib['href'] == 'http://www.duckduckgo.com', 'incorrect anchor href') f = open('../resources/basic-result.xhtml', 'w') f.write(out) f.close()
def process(pages, output_path): tmp = template.get_template('sitemap.xml') if not tmp: return tmp.repeat('urls', len(pages)) x = 0 for page in sorted(pages): pg = pages[page] domain = pg.config.get('site', 'domain') if pg.output_url is not None: tmp.set_value('url', 'http://' + domain + '/' + pg.output_url, x) else: tmp.set_value('url', 'http://' + domain + '/' + pg.url, x) tmp.set_value('lastmod', pg.last_modified.strftime('%Y-%m-%d'), x) x += 1 out = str(tmp) f = open(os.path.join(output_path, 'sitemap.xml'), 'w+') f.write(out) f.close() return pages
def test_basic_append(self): tmp = template.get_template('basic-append.xhtml') tmp.set_value('title', 'Append Title') tmp.set_value('content', 'Append Content') tmp.append('head', '<meta name="description" content="append description" />') tmp.append('content', '<p>some additional content</p>') out = str(tmp) print(out) et = etree.fromstring(out) title = et.findall('head/title') self.assert_(title[0].text == 'Append Title') meta = et.findall('head/meta') self.assert_(meta[0].attrib['name'] == 'description') self.assert_(meta[0].attrib['content'] == 'append description') content = et.findall('body/div') self.assert_(content[0].text == 'Append Content') appended_content = et.findall('body/div/p') self.assert_(appended_content[0].text == 'some additional content')
def test_basic_nonexistent_rid(self): tmp = template.get_template('basic.xhtml') tmp.repeat('nonexistent', 10) out = str(tmp)
def test_basic_nonexistent_aid(self): tmp = template.get_template('basic.xhtml') tmp.set_attribute('nonexistent', 'href', 'test') out = str(tmp)