def display_in_html(self, out, url): #get template path = os.path.join(os.path.dirname(__file__), 'empty.html') f = open(path) try: data = f.read() finally: f.close() #Edit title and subtitle in html and shadow titlers data = self.__replace_chunk(data, "<title>", "</title>", self.title + ' - ' + self.subtitle) data = re.sub(r'SiteTitle: "My TiddlyWiki"', 'SiteTitle: "'+self.title+'"', data) data = re.sub(r'SiteSubtitle: "a reusable non-linear personal web notebook"', 'SiteSubtitle: "'+self.subtitle+'"', data ) data = re.sub(r'SiteUrl: "http://www\.tiddlywiki\.com/"', 'SiteUrl: "'+url+'"', data ) #Update markupBlock data = self.__update_markup_block(data, "PRE-HEAD","MarkupPreHead") data = self.__update_markup_block(data, "POST-HEAD","MarkupPostHead") data = self.__update_markup_block(data, "PRE-BODY","MarkupPreBody") data = self.__update_markup_block(data, "POST-SCRIPT","MarkupPostBody") #find storearea and insert tiddlers match = re.match("^(.*?<div id=\"storeArea\">).*?(</div>.*?<!--POST-STOREAREA-->.*?)$", data, re.DOTALL) if match: begin = match.group(1) end = match.group(2) out.write(begin) tiddlers = {} for t in self.tiddlers: if t: tiddler = Tiddler.get(t) if tiddler: tiddlers[tiddler.title] = tiddler # add dynamic tiddlywiki if 'UploadTiddlerPlugin' not in tiddlers: tiddlers['UploadTiddlerPlugin'] = Namespace.get_tiddler('UploadTiddlerPlugin') tiddler = None if self.namespace.own_by(User.get_current_user()): tiddler = Tiddler.create_or_update(self.namespace, 'zzTiddlyHomeTweaks', self.owner.username, tags='systemConfig excludeLists excludeSearch', newTitle='zzTiddlyHomeTweaks', text= config.tweaks_tiddler%{'username': self.owner.username, 'filename': self.name, 'storeUrl': config.storeTiddler_url}) tiddlers['zzTiddlyHomeTweaks'] = tiddler keys = tiddlers.keys() keys.sort() for ti in keys: if ti: out.write(tiddlers[ti].displayInStorearea(self.name)) out.write(end) if tiddler: tiddler.delete() else: raise Error("Maformed empty.html file")
def display_in_xml(self, out): #get template path = os.path.join(os.path.dirname(__file__), 'empty.xml') f = open(path) try: data = f.read() finally: f.close() path = os.path.join(os.path.dirname(__file__), 'item.xml') f = open(path) try: item = f.read() finally: f.close() #find storearea items = '' tiddlers = [] for t in self.tiddlers: t = Tiddler.get(t) if t and t.modified: tiddlers.append((t.modified, t)) else: tiddlers.append((datetime.today(), t)) tiddlers.sort() tiddlers.reverse() for (modified, t) in tiddlers: if t: if t.html: text = util.html_escape(t.html) else: text = util.html_escape(t.text) items += item % { 'title': t.title, 'text': text, 'tag': t.tags, 'link': config.TH_url + self.owner.username + '/' + config.namespace_name + '/' + self.namespace.name + '/' + config.tiddler_name + '/' + util.url_encode(t.title) + '.html', 'date': modified.strftime("%Y-%m-%d %H:%M"), } data = data % { 'title': self.title, 'link': 'http://bidix.appspot.com/', 'description': self.subtitle, 'username': self.owner.username, 'pubDate': datetime.utcnow().strftime("%Y-%m-%d %H:%M"), 'lastBuildDate': datetime.utcnow().strftime("%Y-%m-%d %H:%M"), 'items': items, } out.write(data)
def get_tiddler(cls, title, owner_name="", namespace_name=""): if not owner_name: owner_name = config.TH_owner if not namespace_name: namespace_name = config.TH_namespace return Tiddler.get_by_key_name( title, parent=Namespace.get_by_key_name( namespace_name, parent=User.get_by_username(owner_name)))
def parse_from_string(cls, buffer): """A TiddlyWiki factory Returns a TiddlyWiki instance containing all tiddlers found in string buffer """ title, subtitle = cls.parse_title(buffer) tiddly_wiki = cls(title=title, subtitle=subtitle) for tiddler in Tiddler.finditer(buffer): tiddly_wiki.add_tiddler(tiddler) return tiddly_wiki
def _generate_tiddler(node): # TODO: move into TiddlyWiki class """ generate Tiddler instance from element node @param tiddler: element node @return: Tiddler instance """ tiddler = Tiddler(_get_title(node)) for attr, value in node.attrs: if attr in Tiddler.standard_fields: if attr == "created" or attr == "modified": value = convert_tiddler_timestamp(value) elif attr == "tags": value = read_bracketed_list(value) setattr(tiddler, attr, value) else: # extended field if not attr == "tiddler" or "title" in node.attrMap: # non-legacy attribute tiddler.fields[attr] = value tiddler.text = _get_text(node) return tiddler
def store(self, file): file = file.decode('utf-8') if file: #find storearea storeareaMatch = re.search("(<div id=\"storeArea\">.*?)<!--POST-STOREAREA-->", file, re.DOTALL) if storeareaMatch: storearea = storeareaMatch.group() if storearea: tiddlers = re.findall("(<div title=.*?</div>)", storearea, re.DOTALL) self.tiddlers = [] for t in tiddlers: ti = Tiddler.from_div(self.namespace, t, self.name) self.tiddlers.append(ti.key()) self.put()
def display_in_xml(self, out): #get template path = os.path.join(os.path.dirname(__file__), 'empty.xml') f = open(path) try: data = f.read() finally: f.close() path = os.path.join(os.path.dirname(__file__), 'item.xml') f = open(path) try: item = f.read() finally: f.close() #find storearea items = '' tiddlers = [] for t in self.tiddlers: t = Tiddler.get(t) if t and t.modified: tiddlers.append((t.modified, t)) else: tiddlers.append((datetime.today(),t)) tiddlers.sort() tiddlers.reverse() for (modified, t) in tiddlers: if t: if t.html: text = util.html_escape(t.html) else: text = util.html_escape(t.text) items += item%{ 'title': t.title, 'text': text, 'tag': t.tags, 'link': config.TH_url+self.owner.username+'/'+config.namespace_name+'/'+self.namespace.name+'/'+config.tiddler_name+'/'+util.url_encode(t.title) +'.html', 'date': modified.strftime("%Y-%m-%d %H:%M"), } data = data%{ 'title': self.title, 'link': 'http://bidix.appspot.com/', 'description': self.subtitle, 'username': self.owner.username, 'pubDate': datetime.utcnow().strftime("%Y-%m-%d %H:%M"), 'lastBuildDate': datetime.utcnow().strftime("%Y-%m-%d %H:%M"), 'items': items, } out.write(data)
def store(self, file): file = file.decode('utf-8') if file: #find storearea storeareaMatch = re.search( "(<div id=\"storeArea\">.*?)<!--POST-STOREAREA-->", file, re.DOTALL) if storeareaMatch: storearea = storeareaMatch.group() if storearea: tiddlers = re.findall("(<div title=.*?</div>)", storearea, re.DOTALL) self.tiddlers = [] for t in tiddlers: ti = Tiddler.from_div(self.namespace, t, self.name) self.tiddlers.append(ti.key()) self.put()
def delete(self): """ """ # Only owner can delete an instance if not self.own_by(User.get_current_user()): if User.get_current_user(): username = User.get_current_user().username else: username = '******' raise OwnerException("user '%s' try to update a namespace owns by '%s'."%(username, self.owner.username)) # first delete all tiddlers query = Tiddler.all().ancestor(self) tiddlers = query.fetch(999) for t in tiddlers: t.delete() # delete all tiddlywiki query = db.GqlQuery("SELECT * FROM Tiddlywiki " + " WHERE namespace = :1" , self ) tiddlywikis = query.fetch(999) for t in tiddlywikis: t.delete() return super(Namespace, self).delete()
def delete(self): """ """ # Only owner can delete an instance if not self.own_by(User.get_current_user()): if User.get_current_user(): username = User.get_current_user().username else: username = '******' raise OwnerException( "user '%s' try to update a namespace owns by '%s'." % (username, self.owner.username)) # first delete all tiddlers query = Tiddler.all().ancestor(self) tiddlers = query.fetch(999) for t in tiddlers: t.delete() # delete all tiddlywiki query = db.GqlQuery( "SELECT * FROM Tiddlywiki " + " WHERE namespace = :1", self) tiddlywikis = query.fetch(999) for t in tiddlywikis: t.delete() return super(Namespace, self).delete()
def get_tiddler(cls, title, owner_name="", namespace_name=""): if not owner_name: owner_name = config.TH_owner if not namespace_name: namespace_name = config.TH_namespace return Tiddler.get_by_key_name(title, parent=Namespace.get_by_key_name(namespace_name, parent=User.get_by_username(owner_name)))
def display_in_html(self, out, url): #get template path = os.path.join(os.path.dirname(__file__), 'empty.html') f = open(path) try: data = f.read() finally: f.close() #Edit title and subtitle in html and shadow titlers data = self.__replace_chunk(data, "<title>", "</title>", self.title + ' - ' + self.subtitle) data = re.sub(r'SiteTitle: "My TiddlyWiki"', 'SiteTitle: "' + self.title + '"', data) data = re.sub( r'SiteSubtitle: "a reusable non-linear personal web notebook"', 'SiteSubtitle: "' + self.subtitle + '"', data) data = re.sub(r'SiteUrl: "http://www\.tiddlywiki\.com/"', 'SiteUrl: "' + url + '"', data) #Update markupBlock data = self.__update_markup_block(data, "PRE-HEAD", "MarkupPreHead") data = self.__update_markup_block(data, "POST-HEAD", "MarkupPostHead") data = self.__update_markup_block(data, "PRE-BODY", "MarkupPreBody") data = self.__update_markup_block(data, "POST-SCRIPT", "MarkupPostBody") #find storearea and insert tiddlers match = re.match( "^(.*?<div id=\"storeArea\">).*?(</div>.*?<!--POST-STOREAREA-->.*?)$", data, re.DOTALL) if match: begin = match.group(1) end = match.group(2) out.write(begin) tiddlers = {} for t in self.tiddlers: if t: tiddler = Tiddler.get(t) if tiddler: tiddlers[tiddler.title] = tiddler # add dynamic tiddlywiki if 'UploadTiddlerPlugin' not in tiddlers: tiddlers['UploadTiddlerPlugin'] = Namespace.get_tiddler( 'UploadTiddlerPlugin') tiddler = None if self.namespace.own_by(User.get_current_user()): tiddler = Tiddler.create_or_update( self.namespace, 'zzTiddlyHomeTweaks', self.owner.username, tags='systemConfig excludeLists excludeSearch', newTitle='zzTiddlyHomeTweaks', text=config.tweaks_tiddler % { 'username': self.owner.username, 'filename': self.name, 'storeUrl': config.storeTiddler_url }) tiddlers['zzTiddlyHomeTweaks'] = tiddler keys = tiddlers.keys() keys.sort() for ti in keys: if ti: out.write(tiddlers[ti].displayInStorearea(self.name)) out.write(end) if tiddler: tiddler.delete() else: raise Error("Maformed empty.html file")