PageRenderTime 36ms CodeModel.GetById 31ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/samples/module/module-body.py

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