def proof (self, event = None): """ Builds an RTF version of the story. Pass whether to open the destination file afterwards. """ # ask for our destination dialog = wx.FileDialog(self, 'Proof Story', os.getcwd(), "", \ "RTF Document (*.rtf)|*.rtf", \ wx.SAVE | wx.FD_OVERWRITE_PROMPT | wx.FD_CHANGE_DIR) if dialog.ShowModal() == wx.ID_OK: path = dialog.GetPath() dialog.Destroy() else: dialog.Destroy() return try: # open destination for writing dest = open(path, 'w') # assemble our tiddlywiki and write it out tw = TiddlyWiki() for widget in self.storyPanel.sortedWidgets(): tw.addTiddler(widget.passage) order = map(lambda w: w.passage.title, self.storyPanel.sortedWidgets()) dest.write(tw.toRtf(order)) dest.close() except: self.app.displayError('building a proofing copy of your story')
def exportSource(self, event=None): """Asks the user to choose a file to export source to, then exports the wiki.""" dialog = wx.FileDialog( self, "Export Source Code", os.getcwd(), "", "Text File (*.txt)|*.txt", wx.SAVE | wx.FD_OVERWRITE_PROMPT | wx.FD_CHANGE_DIR, ) if dialog.ShowModal() == wx.ID_OK: try: path = dialog.GetPath() tw = TiddlyWiki() for widget in self.storyPanel.widgets: tw.addTiddler(widget.passage) dest = open(path, "w") order = map(lambda w: w.passage.title, self.storyPanel.sortedWidgets()) dest.write(tw.toTwee(order)) dest.close() except: self.app.displayError("exporting your source code") dialog.Destroy()
def proof(self, event=None): """ Builds an RTF version of the story. Pass whether to open the destination file afterwards. """ # ask for our destination dialog = wx.FileDialog(self, 'Proof Story', os.getcwd(), "", \ "RTF Document (*.rtf)|*.rtf", \ wx.SAVE | wx.FD_OVERWRITE_PROMPT | wx.FD_CHANGE_DIR) if dialog.ShowModal() == wx.ID_OK: path = dialog.GetPath() dialog.Destroy() else: dialog.Destroy() return try: # open destination for writing dest = open(path, 'w') # assemble our tiddlywiki and write it out tw = TiddlyWiki() for widget in self.storyPanel.sortedWidgets(): tw.addTiddler(widget.passage) order = map(lambda w: w.passage.title, self.storyPanel.sortedWidgets()) dest.write(tw.toRtf(order)) dest.close() except: self.app.displayError('building a proofing copy of your story')
def exportSource (self, event = None): """Asks the user to choose a file to export source to, then exports the wiki.""" dialog = wx.FileDialog(self, 'Export Source Code', os.getcwd(), "", \ 'Twee File (*.twee;* .tw; *.txt)|*.twee;*.tw;*.txt|All Files (*.*)|*.*', wx.SAVE | wx.FD_OVERWRITE_PROMPT | wx.FD_CHANGE_DIR) if dialog.ShowModal() == wx.ID_OK: try: path = dialog.GetPath() tw = TiddlyWiki() for widget in self.storyPanel.widgets: tw.addTiddler(widget.passage) dest = codecs.open(path, 'w', 'utf-8-sig', 'replace') order = map(lambda w: w.passage.title, self.storyPanel.sortedWidgets()) dest.write(tw.toTwee(order)) dest.close() except: self.app.displayError('exporting your source code') dialog.Destroy()
def exportSource(self, event=None): """Asks the user to choose a file to export source to, then exports the wiki.""" dialog = wx.FileDialog(self, 'Export Source Code', os.getcwd(), "", \ 'Twee File (*.twee;* .tw; *.txt)|*.twee;*.tw;*.txt|All Files (*.*)|*.*', wx.SAVE | wx.FD_OVERWRITE_PROMPT | wx.FD_CHANGE_DIR) if dialog.ShowModal() == wx.ID_OK: try: path = dialog.GetPath() tw = TiddlyWiki() for widget in self.storyPanel.widgets: tw.addTiddler(widget.passage) dest = codecs.open(path, 'w', 'utf-8-sig', 'replace') order = map(lambda w: w.passage.title, self.storyPanel.sortedWidgets()) dest.write(tw.toTwee(order)) dest.close() except: self.app.displayError('exporting your source code') dialog.Destroy()
def rebuild (self, event = None, displayAfter = False): """ Builds an HTML version of the story. Pass whether to open the destination file afterwards. """ try: # open destination for writing dest = open(self.buildDestination, 'w') # assemble our tiddlywiki and write it out tw = TiddlyWiki() for widget in self.storyPanel.widgets: tw.addTiddler(widget.passage) dest.write(tw.toHtml(self.app, self.target).encode('utf-8')) dest.close() if displayAfter: self.viewBuild() except: self.app.displayError('building your story')
def rebuild (self, event = None, displayAfter = False): """ Builds an HTML version of the story. Pass whether to open the destination file afterwards. """ try: # Remember current working dir and set to savefile's dir. InterTwine StoryIncludes are relative to the Twine file. cwd = os.getcwd() if self.saveDestination == '': twinedocdir = cwd else: twinedocdir = os.path.dirname(self.saveDestination) os.chdir(twinedocdir) # assemble our tiddlywiki and write it out hasstartpassage = False tw = TiddlyWiki() for widget in self.storyPanel.widgets: if widget.passage.title != 'StoryIncludes' and \ not any(t.startswith('Twine.') for t in widget.passage.tags): widget.passage.pos = widget.pos tw.addTiddler(widget.passage) if widget.passage.title == "Start": hasstartpassage = True # is there a Start passage? if hasstartpassage == False: self.app.displayError('building your story because there is no "Start" passage. ' + "\n" + 'Your story will build but the web browser will not be able to run the story. ' + "\n" + 'Please add a passage with the title "Start"') for widget in self.storyPanel.widgets: if widget.passage.title == 'StoryIncludes': lines = widget.passage.text.splitlines() lines.append(''); # State 0: Look for a filename ## State 1: have filename, look for filename, EXCEPT, INCLUDE, ALIAS ## State 2: EXCEPT mode, look for INCLUDE 3, ALIAS 4 or blank line 0 ## State 3: INCLUDE mode, look for EXCEPT 2, ALIAS 4 or blank line 0 ## State 4: ALIAS mode, look for EXCEPT 2, INCLUDE 2 or blank line 0 state = 0; state_filename = ''; excludepassages = TiddlyWiki.INFO_PASSAGES + ['Start'] for line in lines: if state == 0: state_filename = line state = 1 continue elif state == 1: try: if state_filename.strip() != '': extension = os.path.splitext(state_filename)[1] if extension == '.tws': if any(state_filename.startswith(t) for t in ['http://', 'https://', 'ftp://']): openedFile = urllib.urlopen(state_filename) else: openedFile = open(state_filename, 'r') s = StoryFrame(None, app = self.app, state = pickle.load(openedFile)) openedFile.close() for widget in s.storyPanel.widgets: if not any(widget.passage.title in t for t in excludepassages) and \ not any('Twine.private' in t for t in widget.passage.tags) and \ not any('Twine.system' in t for t in widget.passage.tags): tw.addTiddler(widget.passage) s.Destroy() elif extension == '.tw' or extension == '.txt' or extension == '.twee': if any(state_filename.startswith(t) for t in ['http://', 'https://', 'ftp://']): openedFile = urllib.urlopen(state_filename) s = openedFile.read() openedFile.close() t = tempfile.NamedTemporaryFile(delete=False) cleanuptempfile = True t.write(s) t.close() filename = t.name else: filename = state_filename cleanuptempfile = False tw1 = TiddlyWiki() tw1.addTweeFromFilename(filename) if cleanuptempfile: os.remove(filename) tiddlerkeys = tw1.tiddlers.keys() for tiddlerkey in tiddlerkeys: passage = tw1.tiddlers[tiddlerkey] if not any(passage.title == t for t in excludepassages) and \ not any('Twine.private' in t for t in passage.tags) and \ not any('Twine.system' in t for t in passage.tags): tw.addTiddler(passage) else: raise 'File format not recognized' except: self.app.displayError('opening the Twine file named ' + state_filename + ' which is referred to by the passage StoryIncludes') state_filename = line state = 1 continue break # Decode story settings for widget in self.storyPanel.widgets: if widget.passage.title == 'StorySettings': lines = widget.passage.text.splitlines() for line in lines: try: (skey,svalue) = line.split(':') tw.storysettings[skey.strip().lower()] = svalue.strip().lower() except: tw.storysettings[line.strip().lower()] = "true" break # Write the output file os.chdir(os.path.dirname(self.buildDestination)) dest = open(self.buildDestination, 'w') dest.write(tw.toHtml(self.app, self.target).encode('utf-8')) dest.close() os.chdir(cwd) if displayAfter: self.viewBuild() except: self.app.displayError('building your story')
def rebuild(self, event=None, displayAfter=False): """ Builds an HTML version of the story. Pass whether to open the destination file afterwards. """ try: # Remember current working dir and set to savefile's dir. InterTwine StoryIncludes are relative to the Twine file. cwd = os.getcwd() if self.saveDestination == '': twinedocdir = cwd else: twinedocdir = os.path.dirname(self.saveDestination) os.chdir(twinedocdir) if not self.storyPanel.findWidget('StoryIncludePassages'): self.storyPanel.newWidget(title='StoryIncludePassages', pos=(-1000, -1000), quietly=True) self.storyPanel.findWidget( 'StoryIncludePassages').passage.tags.append('Twine.hide') self.storyPanel.findWidget( 'StoryIncludePassages').passage.tags.append('Twine.system') self.storyPanel.findWidget('StoryIncludePassages').pos = [ -1000, -1000 ] else: self.storyPanel.findWidget( 'StoryIncludePassages').passage.tags[:] = ['Twine.hide'] self.storyPanel.findWidget( 'StoryIncludePassages').passage.tags.append('Twine.system') self.storyPanel.findWidget('StoryIncludePassages').pos = [ -1000, -1000 ] # assemble our tiddlywiki and write it out hasstartpassage = False tw = TiddlyWiki() for widget in self.storyPanel.widgets: # if widget.passage.title != 'StoryIncludes' and \ # not any('Twine.private' in t for t in widget.passage.tags) and \ # not any('Twine.system' in t for t in widget.passage.tags): if widget.passage.title != 'StoryIncludes' and \ not any(t.startswith('Twine.') for t in widget.passage.tags): tw.addTiddler(widget.passage) if widget.passage.title == "Start": hasstartpassage = True # is there a Start passage? if hasstartpassage == False: self.app.displayError( 'building your story because there is no "Start" passage. ' + "\n" + 'Your story will build but the web-browser will not be able to run the story. ' + "\n" + 'Please add a passage with the title "Start"') for widget in self.storyPanel.widgets: if widget.passage.title == 'StoryIncludes': lines = widget.passage.text.splitlines() lines.append('') # State 0: Look for a filename ## State 1: have filename, look for filename, EXCEPT, INCLUDE, ALIAS ## State 2: EXCEPT mode, look for INCLUDE 3, ALIAS 4 or blank line 0 ## State 3: INCLUDE mode, look for EXCEPT 2, ALIAS 4 or blank line 0 ## State 4: ALIAS mode, look for EXCEPT 2, INCLUDE 2 or blank line 0 state = 0 state_filename = '' excludepassages = [ 'Start', 'StoryMenu', 'StoryTitle', 'StoryAuthor', 'StorySubtitle', 'StoryIncludes', 'StorySettings' ] for line in lines: if state == 0: state_filename = line state = 1 continue elif state == 1: try: if state_filename.strip() != '': extension = os.path.splitext( state_filename)[1] if extension == '.tws': if any( state_filename.startswith(t) for t in ['http://', 'https://', 'ftp://']): openedFile = urllib.urlopen( state_filename) else: openedFile = open( state_filename, 'r') s = StoryFrame( None, app=self.app, state=pickle.load(openedFile)) openedFile.close() for widget in s.storyPanel.widgets: if not any(widget.passage.title in t for t in excludepassages) and \ not any('Twine.private' in t for t in widget.passage.tags) and \ not any('Twine.system' in t for t in widget.passage.tags): tw.addTiddler(widget.passage) if self.storyPanel.findWidget( 'StoryIncludePassages' ): self.storyPanel.findWidget( 'StoryIncludePassages' ).passage.tags.append( widget.passage.title) s.Destroy() elif extension == '.tw' or extension == '.txt' or extension == '.twee': if any( state_filename.startswith(t) for t in ['http://', 'https://', 'ftp://']): openedFile = urllib.urlopen( state_filename) s = openedFile.read() openedFile.close() t = tempfile.NamedTemporaryFile( delete=False) cleanuptempfile = True t.write(s) t.close() filename = t.name else: filename = state_filename cleanuptempfile = False tw1 = TiddlyWiki() tw1.addTweeFromFilename(filename) if cleanuptempfile: os.remove(filename) tiddlerkeys = tw1.tiddlers.keys() for tiddlerkey in tiddlerkeys: passage = tw1.tiddlers[tiddlerkey] if not any(passage.title == t for t in excludepassages) and \ not any('Twine.private' in t for t in passage.tags) and \ not any('Twine.system' in t for t in passage.tags): tw.addTiddler(passage) if self.storyPanel.findWidget( 'StoryIncludePassages' ): self.storyPanel.findWidget( 'StoryIncludePassages' ).passage.tags.append( passage.title) else: raise 'File format not recognized' except: self.app.displayError( 'opening the Twine file named ' + state_filename + ' which is referred to by the passage StoryIncludes' ) state_filename = line state = 1 continue break # Decode story settings for widget in self.storyPanel.widgets: if widget.passage.title == 'StorySettings': lines = widget.passage.text.splitlines() for line in lines: (skey, svalue) = line.split(':') tw.storysettings[skey.strip()] = svalue.strip() break # Write the output file os.chdir(os.path.dirname(self.buildDestination)) dest = open(self.buildDestination, 'w') dest.write( tw.toHtml(self.app, self.target, savePath=self.saveDestination).encode('utf-8')) dest.close() os.chdir(cwd) if displayAfter: self.viewBuild() except: self.app.displayError('building your story')