/samples/module/module-full.py

http://txt2tags.googlecode.com/ · Python · 39 lines · 16 code · 8 blank · 15 comment · 2 complexity · 4a3a9b9f900705f3ef3cd0d8e50682f7 MD5 · raw file

  1. #!/usr/bin/env python
  2. #
  3. # Sample of txt2tags being used as a module (http://txt2tags.org)
  4. #
  5. # Details:
  6. # The full marked text is a string with headers, config and body.
  7. # No post config or setting is made.
  8. #
  9. import sys
  10. import setup
  11. setup.setup_paths()
  12. import txt2tags
  13. # Here is the marked text, it must be a list.
  14. txt = "Header1\nHeader2\nHeader3\n%!target: html\nBody line 1."
  15. txt = txt.split('\n')
  16. # Let's do the conversion
  17. try:
  18. # First we parse the text, splitting parts and getting config.
  19. data = txt2tags.process_source_file(contents=txt)
  20. # Then we convert it, dumping results to the 'tagged' list.
  21. tagged, config = txt2tags.convert_this_files([data])
  22. # Show the tagged file on the screen.
  23. print '\n'.join(tagged)
  24. # Txt2tags error, show the message to the user
  25. except txt2tags.error, msg:
  26. print msg
  27. sys.exit(1)
  28. # Unknown error, show the traceback to the user
  29. except:
  30. print txt2tags.getUnknownErrorMessage()
  31. sys.exit(1)