class SnippetParser(): #The SnippetParser class handles the finding/replacing of snippets within a pages content. #The page content is passed to a Parser object, and it then tries to pattern match #the <span> tags used to represent snippets. If it finds a valid snippet tag, #it replaces it with the appropriate snippet text. def __init__(self): self.sm = SnippetManager() snippetRegex = '<span(?=.*?data-type="snippet_tag"\s*)(?=.*?data-snippet-id="([a-zA-Z0-9\s_-]+?)"\s*)[^>]+?>[^<>]*?<\/span>' def parsePage(self, pageText): result = self.parseSnippets(pageText) return result def parseSnippets(self, pageText): pattern = re.compile(self.snippetRegex) matches = pattern.finditer(pageText) snippets = self.sm.getSnippets(True) for match in matches: try: pageText = replace(pageText, match.group(0), snippets[match.group(1)].getText()) except KeyError: #The snippetID was invalid pageText = replace(pageText, match.group(0), '') return pageText
def getSnippets(self): sm = SnippetManager() snippets = sm.getSnippets() out = [] for snippet in snippets: security = getSecurityManager() doc = sm.folder[snippet.getId()] if security.checkPermission(ModifyPortalContent, doc): snippet.w = True else: snippet.w = False if security.checkPermission(DeleteObjects, doc): snippet.d = True else: snippet.d = False out.append(snippet) return out
def test_get_snippets(self): sm = SnippetManager() snippets = sm.getSnippets() self.assertTrue(len(snippets) == 2)