From f29785e72f42a6f9d6f69f031f00883348e91824 Mon Sep 17 00:00:00 2001 From: Martin Vilcans Date: Thu, 8 Sep 2011 22:46:39 +0200 Subject: Added command-line options --- screenplain/export/pdf.py | 7 +++---- screenplain/export/text.py | 7 ++++--- screenplain/main.py | 32 +++++++++++++++++++++++++++++--- 3 files changed, 36 insertions(+), 10 deletions(-) diff --git a/screenplain/export/pdf.py b/screenplain/export/pdf.py index b25a0e3..48eeab8 100644 --- a/screenplain/export/pdf.py +++ b/screenplain/export/pdf.py @@ -5,14 +5,13 @@ from reportlab.lib import pagesizes from screenplain.parse import parse, get_pages -def to_pdf(input): +def to_pdf(screenplay, output_file): # pagesizes.letter, pagesizes.A4 page_width, page_height = pagesizes.A4 - c = canvas.Canvas('out.pdf', pagesize=pagesizes.A4) + c = canvas.Canvas(output_file, pagesize=pagesizes.A4) c.setFont('Courier', 12) - paragraphs = parse(input) - for page_no, page in enumerate(get_pages(paragraphs), 1): + for page_no, page in enumerate(get_pages(screenplay), 1): if page_no != 1: c.showPage() c.setFont('Courier', 12) diff --git a/screenplain/export/text.py b/screenplain/export/text.py index e135646..8f7cb3d 100644 --- a/screenplain/export/text.py +++ b/screenplain/export/text.py @@ -1,9 +1,10 @@ import sys +import codecs from screenplain.parse import parse, get_pages -def to_text(input, out): - paragraphs = parse(input) - for page_no, page in enumerate(get_pages(paragraphs)): +def to_text(screenplay, output_file): + out = codecs.open(output_file, 'w', 'utf-8') + for page_no, page in enumerate(get_pages(screenplay)): # page_no is 0-based if page_no != 0: out.write('\f') diff --git a/screenplain/main.py b/screenplain/main.py index bbe6450..79ff3eb 100644 --- a/screenplain/main.py +++ b/screenplain/main.py @@ -5,11 +5,37 @@ import fileinput import sys +import codecs +from optparse import OptionParser + +from parse import parse + +usage = 'Usage: %prog [options] input-file output-file' if __name__ == '__main__': - if True: + parser = OptionParser(usage=usage) + parser.add_option( + '-f', '--format', dest='output_format', + metavar='FORMAT', + help='Set what kind of file to create. FORMAT can be pdf or text.' + ) + options, args = parser.parse_args() + if len(args) != 2: + parser.error('Expected input-file and output-file arguments') + input_file, output_file = args + + if options.output_format == None: + if output_file.endswith('.pdf'): + options.output_format = 'pdf' + else: + options.output_format = 'text' + + input = codecs.open(input_file, 'r', 'utf-8') + screenplay = parse(input) + + if options.output_format == 'text': from screenplain.export.text import to_text - to_text(fileinput.input(), sys.stdout) + to_text(screenplay, output_file) else: from screenplain.export.pdf import to_pdf - to_pdf(fileinput.input()) + to_pdf(screenplay, output_file) -- cgit