def offerAssistance(self): """ Offer to fulfill certain incomplete tasks evident from the state of the passage text. (Technically, none of this needs to be on passageFrame instead of passageWidget.) """ # Offer to create passage for broken links if self.app.config.ReadBool('createPassagePrompt'): brokens = links = filter(lambda text: badLinkStyle(text) == TweeLexer.BAD_LINK, self.widget.getBrokenLinks()) if brokens : if len(brokens) > 1: brokenmsg = 'create ' + str(len(brokens)) + ' new passages to match these broken links?' else: brokenmsg = 'create the passage "' + brokens[0] + '"?' dialog = wx.MessageDialog(self, 'Do you want to ' + brokenmsg, 'Create Passages', \ wx.ICON_QUESTION | wx.YES_NO | wx.CANCEL | wx.YES_DEFAULT) check = dialog.ShowModal() if check == wx.ID_YES: for title in brokens: self.widget.parent.newWidget(title = title, pos = self.widget.parent.toPixels (self.widget.pos)) elif check == wx.ID_CANCEL: return # Offer to import external images if self.app.config.ReadBool('importImagePrompt'): regex = tweeregex.EXTERNAL_IMAGE_REGEX externalimages = re.finditer(regex, self.widget.passage.text) check = None downloadedurls = {} storyframe = self.widget.parent.parent for img in externalimages: if not check: dialog = wx.MessageDialog(self, 'Do you want to import the image files linked\nin this passage into the story file?', 'Import Images', \ wx.ICON_QUESTION | wx.YES_NO | wx.CANCEL | wx.YES_DEFAULT); check = dialog.ShowModal() if check == wx.ID_NO: break elif check == wx.ID_CANCEL: return # Download the image if it's at an absolute URL imgurl = img.group(4) or img.group(7) if not imgurl: continue # If we've downloaded it before, don't do it again if imgurl not in downloadedurls: # Internet image, or local image? if any(imgurl.startswith(t) for t in ['http://', 'https://', 'ftp://']): imgpassagename = storyframe.importImageURL(imgurl, showdialog=False) else: imgpassagename = storyframe.importImageFile(storyframe.getLocalDir()+os.sep+imgurl, showdialog=False) if not imgpassagename: continue downloadedurls[imgurl] = imgpassagename # Replace all found images for old, new in downloadedurls.iteritems(): self.widget.passage.text = re.sub(regex.replace(tweeregex.IMAGE_FILENAME_REGEX, re.escape(old)), lambda m: m.group(0).replace(old, new), self.widget.passage.text) self.bodyInput.SetText(self.widget.passage.text)
def updateSubmenus (self, event = None): """ Updates our passage menus. This should be called sparingly, i.e. not during a UI update event, as it is doing a bunch of removing and adding of items. """ # separate outgoing and broken links outgoing = [] incoming = [] broken = [] # Remove externals links = filter(lambda text: badLinkStyle(text) == TweeLexer.BAD_LINK, self.widget.passage.links) for link in links: if len(link) > 0: found = False for widget in self.widget.parent.widgets: if widget.passage.title == link: outgoing.append(link) found = True break if not found: broken.append(link) # incoming links for widget in self.widget.parent.widgets: if self.widget.passage.title in widget.passage.links \ and len(widget.passage.title) > 0: incoming.append(widget.passage.title) # repopulate the menus def populate (menu, links): for item in menu.GetMenuItems(): menu.DeleteItem(item) if len(links): for link in links: item = menu.Append(-1, link) self.Bind(wx.EVT_MENU, self.openOtherEditor, item) else: item = menu.Append(wx.ID_ANY, '(None)') item.Enable(False) outTitle = 'Outgoing Links' if len(outgoing) > 0: outTitle += ' (' + str(len(outgoing)) + ')' self.outLinksMenuTitle.SetText(outTitle) populate(self.outLinksMenu, outgoing) inTitle = 'Incoming Links' if len(incoming) > 0: inTitle += ' (' + str(len(incoming)) + ')' self.inLinksMenuTitle.SetText(inTitle) populate(self.inLinksMenu, incoming) brokenTitle = 'Broken Links' if len(broken) > 0: brokenTitle += ' (' + str(len(broken)) + ')' self.brokenLinksMenuTitle.SetText(brokenTitle) populate(self.brokenLinksMenu, broken)
def filterExternals(text): return tweelexer.badLinkStyle(text) == TweeLexer.BAD_LINK
def closeEditor(self, event = None): """ Do extra stuff on closing the editor """ #Closes this editor's fullscreen counterpart, if any. try: self.fullscreen.Destroy() except: pass # Show warnings if self.app.config.ReadBool('passageWarnings'): checks = self.widget.parent.parent.header.passageChecks() for check in checks: result = check(self.widget.passage) if result: wx.MessageDialog(self, result, 'Warning', wx.ICON_WARNING).ShowModal() # Offer to create passage for broken links if self.app.config.ReadBool('createPassagePrompt'): brokens = links = filter(lambda text: badLinkStyle(text) == TweeLexer.BAD_LINK, self.widget.getBrokenLinks()) if brokens : if len(brokens) > 1: brokenmsg = 'create ' + str(len(brokens)) + ' new passages to match these broken links?' else: brokenmsg = 'create the passage "' + brokens[0] + '"?' dialog = wx.MessageDialog(self, 'Do you want to ' + brokenmsg, 'Create Passages', \ wx.ICON_QUESTION | wx.YES_NO | wx.CANCEL | wx.YES_DEFAULT) check = dialog.ShowModal() if check == wx.ID_YES: for title in brokens: self.widget.parent.newWidget(title = title, pos = self.widget.parent.toPixels (self.widget.pos)) elif check == wx.ID_CANCEL: return # Offer to import external images if self.app.config.ReadBool('importImagePrompt'): regex = tweeregex.EXTERNAL_IMAGE_REGEX externalimages = re.finditer(regex, self.widget.passage.text) check = None downloadedurls = {} storyframe = self.widget.parent.parent for img in externalimages: if not check: dialog = wx.MessageDialog(self, 'Do you want to import the image files linked\nin this passage into the story file?', 'Import Images', \ wx.ICON_QUESTION | wx.YES_NO | wx.CANCEL | wx.YES_DEFAULT); check = dialog.ShowModal() if check == wx.ID_NO: break elif check == wx.ID_CANCEL: return # Download the image if it's at an absolute URL imgurl = img.group(4) or img.group(7) if not imgurl: continue # If we've downloaded it before, don't do it again if imgurl not in downloadedurls: # Internet image, or local image? if any(imgurl.startswith(t) for t in ['http://', 'https://', 'ftp://']): imgpassagename = storyframe.importImageURL(imgurl, showdialog=False) else: imgpassagename = storyframe.importImageFile(storyframe.getLocalDir()+os.sep+imgurl, showdialog=False) if not imgpassagename: continue downloadedurls[imgurl] = imgpassagename # Replace all found images for old, new in downloadedurls.iteritems(): self.widget.passage.text = re.sub(regex.replace(tweeregex.IMAGE_FILENAME_REGEX, re.escape(old)), lambda m: m.group(0).replace(old, new), self.widget.passage.text) self.widget.passage.update() event.Skip()