~allenap/scriptify/trunk

« back to all changes in this revision

Viewing changes to scriptify/pdf.py

  • Committer: Gavin Panella
  • Date: 2010-11-20 21:14:20 UTC
  • mfrom: (3.1.1 radio-play)
  • Revision ID: gavin@gromper.net-20101120211420-0fxuose377j06rz6
Merge radio-play.

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
 
6
6
import codecs
7
7
import copy
 
8
import optparse
8
9
import sys
9
10
 
10
11
from collections import defaultdict
76
77
            yield escape(part.text)
77
78
 
78
79
 
79
 
def process(text):
 
80
def process(text, radio=False):
80
81
    counts = defaultdict(lambda: 0)
81
82
    for element in parse.parse(text):
82
83
        counts[element.name] += 1
90
91
                escape(element.text), HeadingStyles[element.level])
91
92
        elif element.name == 'action':
92
93
            yield Paragraph(
93
 
                '<i>%s</i>' % escape(element.action), ActionStyle)
 
94
                ('<u>%s</u>' if radio else '<i>%s</i>') % escape(
 
95
                    element.action),
 
96
                ActionStyle)
94
97
        elif element.name == 'speech':
95
98
            yield Paragraph(
96
99
                '<b>%s</b>: %s' % (
105
108
            raise Exception("What is this: %r" % (element,))
106
109
 
107
110
 
108
 
def render(filename_in, filename_out):
 
111
def render(options, filename_in, filename_out):
109
112
    with codecs.open(filename_in, encoding='utf-8') as fd:
 
113
        doc_stream = process(fd.read(), options.radio)
110
114
        doc = SimpleDocTemplate(filename_out)
111
 
        doc.build(list(process(fd.read())), number_pages, number_pages)
 
115
        doc.build(list(doc_stream), number_pages, number_pages)
112
116
 
113
117
 
114
118
if __name__ == '__main__':
115
 
    render(*sys.argv[1:3])
 
119
    option_parser = optparse.OptionParser()
 
120
    option_parser.add_option(
 
121
        "--radio", action="store_true", help=(
 
122
            "Generate output suitable for a radio play."))
 
123
    options, arguments = option_parser.parse_args()
 
124
    render(options, *arguments)