def docs(self): # Display documentation in web browser print("Serving documentation...") # Find and read README.md docnameString = os.path.dirname( os.path.realpath(__file__)) + "/README.md" newdocnameString = os.path.basename(docnameString) doccontentsString = codecs.open(newdocnameString, "r", "utf-8").read() # Augment file (see augment() in extras.py) newdoccontentsString = markdown.markdown(doccontentsString) newdoccontentsString = augment(newdoccontentsString, "eBooker Documentation") # Write text to file docfileFile = codecs.open("docs.html", "w", "utf-8") docfileFile.write(newdoccontentsString) docfileFile.close() # Open in web browser webbrowser.open("file://" + os.path.dirname(os.path.realpath(__file__)) + "/docs.html") print( "Success! The documentation has been served. Press RETURN when you are done reading it." ) # When user is done, delete the file getInput("") os.remove("docs.html")
def serve(self): # Serve current book in a web browser print("Serving book...") # Get list of HTML files filenameArray = glob.glob( os.path.dirname(os.path.realpath(__file__)) + "/*.html") filebufferString = "" # For each HTML file... for filenameString in filenameArray: # Format filename newfilenameString = os.path.basename(filenameString) chapternameString = os.path.splitext(newfilenameString)[0] chapternameString = chapternameString.replace("-", " ", 1) chapternameString = chapternameString.capitalize() filebufferString += "<h1>" + chapternameString + "</h1>" # Open file and read contents filecontentsString = codecs.open(newfilenameString, "r", "utf-8").read() filebufferString += markdown.markdown(filecontentsString) # Add horizontal divider to end filebufferString += "<hr/>" # Add "THE END!" filebufferString += "<center><h1>THE END!</h1></center>" # Augment file (see augment() in extras.py) filebufferString = augment(filebufferString, "Your Book") # Write text to file reviewfileFile = codecs.open("review-book.html", "w", "utf-8") reviewfileFile.write(filebufferString) reviewfileFile.close() # Open in web browser webbrowser.open("file://" + os.path.dirname(os.path.realpath(__file__)) + "/review-book.html") print( "Success! Your book is served. Press RETURN when you are done reviewing it." ) # When user is done, delete the file getInput("") os.remove("review-book.html")
def main(): while True: testerInst.test(0) cmd = getInput("eBooker > ") if cmd == "help": comInst.ehelp() elif cmd == "exit": comInst.exit() elif cmd == "about": comInst.about() elif cmd == "edit": comInst.edit() elif cmd == "serve": comInst.serve() elif cmd == "clear": comInst.clear() elif cmd == "debug": comInst.debug() elif cmd == "docs": comInst.docs() else: print("\"" + cmd + "\" is not a valid command. Type \"help\" for more options")
def test(self, n): try: assert ( # Test for Python files and LICENSE.txt os.path.exists("tester.py") and os.path.exists("extras.py") and os.path.exists("commands.py") and os.path.exists("internals.py") and os.path.exists("ebooker.py") and os.path.exists("LICENSE.txt") and type(self) is Tester) if n: assert ( # If running in CircleCI... # Test for docs/ and contents... # And README.md and CONTRIBUTING.md os.path.exists("README.md") and os.path.exists("CONTRIBUTING.md") and os.path.exists("docs/downloads.html") and os.path.exists("docs/about.html") and os.path.exists("docs/octocat1.png") and os.path.exists("docs/octocat2.png") and os.path.exists("docs/stylesheet.css") and os.path.exists("docs/index.html") and os.path.exists("docs") and type(self) is Tester) except: # Tests failed print("\nAn important project file is missing!") print( "Program execution stopped. Type RETURN to exit. Run \"debug\" command when you next open eBooker to report this problem." ) print( "If you didn't already, always cd into eBooker's folder before running this program." ) # Wait for RETURN, then exit getInput("") sys.exit(1)
def exit(self): # Begin process to exit eBooker exitloopBool = True while exitloopBool: # Check if user wants to exit exitBool = getInput("Would you like to quit eBooker? (y/n) ") if exitBool == "y": # Yes, so... exitloopBool = False # Exit clear() sys.exit() elif exitBool == "n": # Nope. The user changed their mind. exitloopBool = False print("OK, not exited!") else: # User didn't type in y or n print("Please type in \"y\" or \"n\".")
elif cmd == "debug": comInst.debug() elif cmd == "docs": comInst.docs() else: print("\"" + cmd + "\" is not a valid command. Type \"help\" for more options") # Run main() unless user hits Ctrl-C, in which case exit... # Or if there is an error, show a message try: # Run main() main() except KeyboardInterrupt: # Ctrl-C print("") sys.exit() except: # An error print("\nAn internal error occurred!") print( "Program execution stopped. Type RETURN to exit. Run \"debug\" command when you next open eBooker to report this problem." ) print( "If you didn't already, always cd into eBooker's folder before running this program." ) raise # Wait for RETURN, then exit getInput("") sys.exit(1)
def edit(self): # Edit or create a new file editloopBool = True while editloopBool: # Check whether user wants to create a new chapter or edit an existing one editBool = getInput( "Would you like to create a new chapter? (y/n) ") if editBool == "y": # User wants to create a new chapter editloopBool = False print("You want to create a new chapter.") newfileString = chapNum() # Create and open file unless file already exists print("Creating file...") newfileString = "chapter-" + str(newfileString) + ".html" if not os.path.exists(newfileString): # File doesn't exist newfileFile = codecs.open(newfileString, "a", "utf-8") # Put stuff in file based on OS if isNix: newfileFile.write( "Press CTRL-O then hit return to save. Press CTRL-X to exit.\n" ) newfileFile.write( "Don't worry if you can't see part of your lines; they will\n" ) newfileFile.write("be saved anyway.") else: newfileFile.write( "Press CTRL-S to save. Click on the X to exit.\n") # Close file newfileFile.close() print("Your file is created!") # Open file for editing openEditor(newfileString) # Notify user about debug command print("If you got an error, use the \"debug\" command.") else: # File does exist print( "That chapter already exists! Try running the edit command again but this time enter \"n\"." ) elif editBool == "n": # User wants to create a new file editloopBool = False print("You want to edit an existing chapter!") editfileString = chapNum() # Open file for editing unless file doesn't exist print("Opening file for editing...") editfileString = "chapter-" + str(editfileString) + ".html" if os.path.exists(editfileString): # File exists openEditor(editfileString) # Notify user about debug command print("If you got an error, use the \"debug\" command.") else: # File doesn't exist print( "That file doesn't exist. Try running the edit command again but this time enter \"y\"." ) else: # User didn't type in y or n print("Please type in \"y\" or \"n\".")