def test_finder(): with ZipFile('all.key') as file: raw = file.read('index.apxl') doc = minidom.parseString(raw) pattern = '//sf:shape[starts-with(@sf:href,\'http://localhost/\')]' strip = 'http://localhost/' finder = Finder(doc, pattern, strip) actual = finder.snippets() expected = ['example.py?bar'] assert(actual == expected)
def snippetize(self): '''Performs the snippet replacement.''' with ZipFile(self.keynote_file) as original: with ZipFile(self.out_file, 'w') as updated: for item in original.filelist: if item.filename != 'index.apxl': contents = original.read(item.filename) updated.writestr(item, contents) raw = original.read('index.apxl') doc = minidom.parseString(raw) pattern = '//sf:shape[starts-with(@sf:href,\'http://localhost/\')]' strip = 'http://localhost/' finder = Finder(doc, pattern, strip) template = '''<?xml version="1.0"?> <key:presentation xmlns:sfa="http://developer.apple.com/namespaces/sfa" xmlns:sf="http://developer.apple.com/namespaces/sf" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:key="http://developer.apple.com/namespaces/keynote2"> <sf:text-body> %s </sf:text-body> </key:presentation> ''' replacer = Replacer(doc, template) names = self.only and [self.only] or finder.snippets() for name in names: filename, _, part = name.partition('?') path = expanduser(join(self.base, filename)) with open(path) as file: code = self.extractor.extract(file.read(), part) lexer = get_lexer_for_filename(filename, stripall=True) snippet = highlight(code, lexer, self.formatter) parent = '//sf:shape[@sf:href="http://localhost/%s"]//sf:text-storage' % name child = 'sf:text-body' replacer.replace(parent, child, snippet) with ZipFile(self.out_file, 'a') as updated: updated.writestr('index.apxl', doc.toxml().encode('utf-8'))