def mk_moplate_filters(self, params): """ Create moplate-level extra filters Builds a list of filters specific to this moplate, that will be applied to the extracted content of every mobile page. This method is a hook that can be altered by subclasses. By default, it returns an empty list, unless the Moplate was instantiated with a non-empty imgsub argument; in that case it will contain a properly initialized imgsub filter. The params argument can be used to generate filters specific to the parameter set. Note that these are applied after any component-level filters, but before any site-level filters. @param params : Moplate template parameters @type params : dict @return : Filters @rtype : list of callable """ from mobilize.filters import imgsub filts = [] if self.imgsubs: filts.append(lambda elem: imgsub(elem, self.imgsubs)) return filts
def mk_site_filters(self, params): ''' Create global mobile-site filters Builds a list of filters that will be passed on to every moplate during rendering, to be applied to the extracted content of every mobile page. Note that these are applyed *after* any moplate-level filters, which are themselves applied after any component-level filters. This list can be altered or added to by subclasses. @param params : Site-level template parameters @type params : dict @return : Filters @rtype : list of callable ''' from mobilize.images import to_imgserve site_filters = [] if self.imgsubs: site_filters.append(lambda elem: filters.imgsub(elem, self.imgsubs)) if 'fullsite' in params and 'request_path' in params: desktop_url = 'http://%(fullsite)s%(request_path)s' % params site_filters.extend(( lambda elem: filters.absimgsrc(elem, desktop_url), to_imgserve, lambda elem: filters.abslinkfilesrc(elem, desktop_url), )) return site_filters
def test_imgsub(self): from mobilize.filters import imgsub html1 = '''<div> <ul> <li><img id="a" alt="ima image" width="10" height="20" src="/path/to/a.png"/></li> <li><img id="b" alt="ima image" width="10" height="20" src="/path/to/b.png"/></li> <li><img id="c" alt="ima image" width="10" height="20" src="/path/to/c.png"/></li> <li><img id="d" alt="ima image" width="10" height="20" src="/path/to/d.png"/></li> <li><img id="e" alt="ima image" width="10" height="20" src="/path/to/e.png"/></li> <li><img id="f" alt="ima image" width="10" height="20" src="/path/to/f.png"/></li> <li><img id="g" alt="ima image" width="10" height="20" src="/path/to/g.png"/></li> </ul> <section> <div> <p> <aside> <div> <span> <img id="h" alt="ima image" width="10" height="20" src="/path/to/h.png"/> </span> </div> </aside> </p> </div> </section> </div> ''' # This dict defines the mapping of img IDs to the values in # html1 and html2 above. You can create a copy with certain # overriden values with the id2src() functions, following, in order # to make a specific test. # E.g.: # d = id2src1(a='/path/to/NEWa.png') # d['a'] # -> '/path/to/NEWa.png' # d['b'] # -> '/path/to/b.png' _id2src = { 'a' : '/path/to/a.png', 'b' : '/path/to/b.png', 'c' : '/path/to/c.png', 'd' : '/path/to/d.png', 'e' : '/path/to/e.png', 'f' : '/path/to/f.png', 'g' : '/path/to/g.png', 'h' : '/path/to/h.png', } def id2src(**overrides): d = dict(_id2src) d.update(overrides) return d testdata = [ {'subs' : { '/path/to/c.png' : '/mobile/c.png', '/path/to/e.png' : '/mobile/a.png', }, 'expected' : id2src( c = '/mobile/c.png', e = '/mobile/a.png', ), }, {'subs' : {}, 'expected' : id2src(), }, {'subs' : { '/path/to/h.png' : '/mobile/h.png', '/path/to/f.png' : '/mobile/h.png', }, 'expected' : id2src( h = '/mobile/h.png', f = '/mobile/h.png', ), }, ] for ii, td in enumerate(testdata): root = html.fromstring(html1) # check test precondition for imgid, srcval in _id2src.items(): img = root.get_element_by_id(imgid) self.assertEqual(img.tag, 'img') self.assertEqual(img.attrib['src'], srcval) imgsub(root, td['subs']) for imgid, srcval in td['expected'].items(): img = root.get_element_by_id(imgid) self.assertEqual(img.attrib['src'], srcval) # test for callable imgsub value def alter_c(elem): self.assertEqual('img', elem.tag) elem.attrib['src'] = '/mobile/c.png' elem.attrib['width'] = '45' elem.attrib['height'] = '95' def alter_h(elem): self.assertEqual('img', elem.tag) elem.attrib['src'] = '/mobile/h.png' elem.attrib['width'] = '145' del elem.attrib['height'] subs = { '/path/to/c.png' : alter_c, '/path/to/h.png' : alter_h, } root = html.fromstring(html1) imgsub(root, subs) img_c = root.get_element_by_id('c') self.assertEqual('/mobile/c.png', img_c.attrib['src']) self.assertEqual('45', img_c.attrib['width']) self.assertEqual('95', img_c.attrib['height']) img_h = root.get_element_by_id('h') self.assertEqual('/mobile/h.png', img_h.attrib['src']) self.assertEqual('145', img_h.attrib['width']) self.assertFalse('height' in img_h.attrib)