from WikiPlugin import * from AppData import * def getName(): # # This is the name that appears on the 'plugin' menu. # It should be unique # return "Lesson 07 - Manage Pages" def getVendorName(): # # The name of the person who developed the plugin # return "Dogmelon" def getVendorVersionString(): # # A version string for this plugin # return "1.0" def getDescription(): # # A brief, helpful description of the plugin # return "Lesson 07 - Learn how to manage pages via the plugin API" def preExecute(): # # Do anything you need to do before running the plugin. # This could include popping up a dialog to collect parameters. # returns a list of parameters, which will be passed to the execute() method # STAGE_NAME = "preExecute" return [] def lesson07_setupBook( outputFile, newBookName ): collection = theAppData.library.getDataSource( 0 ) book = WikiBook( collection ) book.setDisplayName( newBookName ) ret = theAppData.library.addBook( book, collection ) if ret: outputFile.write( "adding book '%s' was successful\n"%( newBookName ) ) else: outputFile.write( "adding book '%s' was NOT successful\n"%( newBookName ) ) homePageName = "My Home Page" homePageText = "This is my home page. It worked!" book.addPageWithNameAndContents( homePageName, homePageText ) book.setHomePage(homePageName) def addFooter( page, homePageName ): text = page.getText() text = text + "\n___\n=[[%s][go Home]]=\n"%homePageName page.setText( text ) def lesson07_addFooterToNewPages( outputFile, book ): outputFile.write( "\n\nin lesson07_addFooterToNewPages()\n" ) numPages = book.getNumPages() homePageName = book.getHomePage() for i in range( numPages ): page = book.getPage(i) name = page.getName() if( name == homePageName ): outputFile.write( "skipping home page '%s'\n"%homePageName ) else: outputFile.write( "adding footer to '%s'\n"%name ) addFooter( page, homePageName ) def lesson07_setupHomePageAsDirectory( outputFile, book ): outputFile.write( "\n\nin lesson07_setupHomePageAsDirectory()\n" ) numPages = book.getNumPages() homeText = "=This page was automatically created by a plugin=\n" homeText = homeText + "___\n" homeText = homeText + "++Directory of Pages\n" homeText = homeText + "=number of pages in directory: %d=\n"%(numPages-1) homePageName = book.getHomePage() homePage = book.findPage( homePageName ) for i in range( numPages ): name = book.getPageName(i) if( name == homePageName ): outputFile.write( "skipping home page '%s'\n"%homePageName ) else: outputFile.write( "adding directory entry for '%s'\n"%name ) homeText = homeText + "- [%s]\n"%name homePage.setText( homeText ) def lesson07_renamePage( outputFile, book ): outputFile.write( "\n\nin lesson07_renamePage()\n" ) oldName = "page 4" newName = "page wookie" book.addPageWithNameAndContents( oldName, "This page is going to be renamed" ) page = book.findPage( oldName ) if page == None: outputFile.write( "'%s' doesn't exist\n"%oldName ) else: outputFile.write( "'%s' does exist\n"%oldName ) page = book.findPage( newName ) if page == None: outputFile.write( "'%s' doesn't exist\n"%newName ) else: outputFile.write( "'%s' does exist\n"%newName ) outputFile.write( "(renaming '%s' to '%s')\n"%(oldName,newName) ) book.renamePage( oldName, newName ) page = book.findPage( oldName ) if page == None: outputFile.write( "'%s' doesn't exist\n"%oldName ) else: outputFile.write( "'%s' does exist\n"%oldName ) page = book.findPage( newName ) if page == None: outputFile.write( "'%s' doesn't exist\n"%newName ) else: outputFile.write( "'%s' does exist\n"%newName ) def lesson07_removePage( outputFile, book ): outputFile.write( "\n\nin lesson07_removePage()\n" ) pageName = "page wookie" doomedPage = book.findPage( pageName ) if doomedPage == None: outputFile.write( "'%s' doesn't exist\n"%pageName ) else: outputFile.write( "'%s' does exist\n"%pageName ) outputFile.write( "(removing '%s')\n"%(pageName) ) book.removePage( doomedPage ) doomedPage = book.findPage( pageName ) if doomedPage == None: outputFile.write( "'%s' doesn't exist\n"%pageName ) else: outputFile.write( "'%s' does exist\n"%pageName ) def execute( parameterList ): # # Here is where you actually do the plugin tasks. # # If anything goes wrong, you can throw a WikiPluginException # as follows: # STAGE_NAME = "execute" bThrowException = False outputFile = open( "C:/lesson07.txt", "w" ) outputFile.write( "Lesson 07\n\n" ) newBookName = "Lesson 7 Book" # don't run the plugin if we don't have any collections numCollections = theAppData.library.getNumDataSources() if numCollections == 0: outputFile.write( "No collections! Exiting.\n" ) outputFile.close() return # create the book lesson07_setupBook( outputFile, newBookName ) book = theAppData.library.findBook( newBookName ) numPages = book.getNumPages() outputFile.write( "Initial Number of Pages: %d\n"%(numPages) ) # add three new pages: outputFile.write( "Adding three new pages\n" ) book.addPageWithNameAndContents( "page 1", "This is page 1" ) book.addPageWithNameAndContents( "page 2", "This is page 2" ) book.addPageWithNameAndContents( "page 3", "This is page 3" ) # verify that the pages were added correctly numPages = book.getNumPages() outputFile.write( "Current Number of Pages: %d\n"%(numPages) ) if( numPages != 4 ): outputFile.write( "Not enough pages, something wrong! Exiting.\n" ) outputFile.close() return outputFile.write( "\nPage Summary\n" ) outputFile.write( "#: Name - Contents\n" ) for i in range( numPages ): page = book.getPage(i) name = page.getName() text = page.getText() outputFile.write( "%d: '%s' - '%s'\n"%(i,name,text) ) lesson07_addFooterToNewPages( outputFile, book ) lesson07_setupHomePageAsDirectory( outputFile, book ) lesson07_renamePage( outputFile, book ) lesson07_removePage( outputFile, book ) outputFile.write( "\nFinished.\n" ) outputFile.close() # update library panel theAppData.appFrame.onRefresh() if bThrowException: raise WikiPluginException( STAGE_NAME, "Some error string describing the problem" ) def postExecute(): # # Do anything you need to clean up after running the plugin. # STAGE_NAME = "postExecute" pass