/samples/module/module-body.py

http://txt2tags.googlecode.com/ · Python · 59 lines · 31 code · 11 blank · 17 comment · 2 complexity · d8884dbc2af0d5914ed8739cfe903a5a 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 document body is a string.
  7. # Headers and config are set with Python code.
  8. # This way you can fully control txt2tags behavior.
  9. #
  10. import sys
  11. import setup
  12. setup.setup_paths()
  13. import txt2tags
  14. # Here is the marked body text, it must be a list.
  15. txt = "=Hi!=\nHave a **nice** day.\n\nBye."
  16. txt = txt.split('\n')
  17. # Set the three header fields
  18. headers = ['Header 1', 'Header 2', 'Header 3']
  19. # Set the configuration on the 'config' dict.
  20. config = txt2tags.ConfigMaster()._get_defaults()
  21. config['outfile'] = txt2tags.MODULEOUT # results as list
  22. config['target'] = 'html' # target type: HTML
  23. config['encoding'] = 'UTF-8' # document encoding
  24. config['css-sugar'] = 1 # CSS friendly
  25. config['toc'] = 1 # show Table Of Contents
  26. # The Pre (and Post) processing config is a list of lists:
  27. # [ [this, that], [foo, bar], [pattern, replace] ]
  28. config['preproc'] = []
  29. config['preproc'].append(['nice', 'VERY NICE'])
  30. config['preproc'].append(['day', 'life'])
  31. # Let's do the conversion
  32. try:
  33. headers = txt2tags.doHeader(headers, config)
  34. body, toc = txt2tags.convert(txt, config)
  35. footer = txt2tags.doFooter(config)
  36. toc = txt2tags.toc_tagger(toc, config)
  37. toc = txt2tags.toc_formatter(toc, config)
  38. full_doc = headers + toc + body + footer
  39. finished = txt2tags.finish_him(full_doc, config)
  40. print '\n'.join(finished)
  41. # Txt2tags error, show the message to the user
  42. except txt2tags.error, msg:
  43. print msg
  44. sys.exit(1)
  45. # Unknown error, show the traceback to the user
  46. except:
  47. print txt2tags.getUnknownErrorMessage()
  48. sys.exit(1)