def importSource (self, event = None): """Asks the user to choose a file to import source from, then imports into the current story.""" dialog = wx.FileDialog(self, 'Import Source Code', os.getcwd(), '', \ 'Twee File (*.twee;* .tw; *.txt)|*.twee;*.tw;*.txt|All Files (*.*)|*.*', wx.OPEN | wx.FD_CHANGE_DIR) if dialog.ShowModal() == wx.ID_OK: try: # have a TiddlyWiki object parse it for us tw = TiddlyWiki() tw.addTweeFromFilename(dialog.GetPath()) # add passages for each of the tiddlers the TiddlyWiki saw if len(tw.tiddlers): lastpos = [0, 0] for t in tw.tiddlers: tiddler = tw.tiddlers[t] new = self.storyPanel.newWidget(title = tiddler.title, text = tiddler.text, quietly = True, pos = lastpos) new.passage.tags = tiddler.tags lastpos = new.pos self.setDirty(True, 'Import') else: dialog = wx.MessageDialog(self, 'No passages were found in this file. Make sure ' + \ 'this is a Twee source file.', 'No Passages Found', \ wx.ICON_INFORMATION | wx.OK) dialog.ShowModal() except: self.app.displayError('importing your source code')
def importSource(self, event=None): """Asks the user to choose a file to import source from, then imports into the current story.""" dialog = wx.FileDialog(self, 'Import Source Code', os.getcwd(), '', \ 'Twee File (*.twee;* .tw; *.txt)|*.twee;*.tw;*.txt|All Files (*.*)|*.*', wx.OPEN | wx.FD_CHANGE_DIR) if dialog.ShowModal() == wx.ID_OK: try: # have a TiddlyWiki object parse it for us tw = TiddlyWiki() tw.addTweeFromFilename(dialog.GetPath()) # add passages for each of the tiddlers the TiddlyWiki saw if len(tw.tiddlers): for t in tw.tiddlers: tiddler = tw.tiddlers[t] new = self.storyPanel.newWidget(title=tiddler.title, text=tiddler.text, quietly=True) new.tags = tiddler.tags self.setDirty(True, 'Import') else: dialog = wx.MessageDialog(self, 'No passages were found in this file. Make sure ' + \ 'this is a Twee source file.', 'No Passages Found', \ wx.ICON_INFO | wx.OK) dialog.ShowModal() except: self.app.displayError('importing your source code')
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')