From 8f6afcb212a458fdbf6e5cd2b794fab6d7ee1633 Mon Sep 17 00:00:00 2001 From: Martin Vilcans Date: Sun, 27 Nov 2011 22:22:15 +0100 Subject: Renamed module annotated_html to html. Annotation will be a flag to normal html, if we need it. --- screenplain/export/annotated_html.py | 83 ------------------------------------ screenplain/export/html.py | 83 ++++++++++++++++++++++++++++++++++++ screenplain/main.py | 10 +++-- 3 files changed, 89 insertions(+), 87 deletions(-) delete mode 100644 screenplain/export/annotated_html.py create mode 100644 screenplain/export/html.py diff --git a/screenplain/export/annotated_html.py b/screenplain/export/annotated_html.py deleted file mode 100644 index 0084b8a..0000000 --- a/screenplain/export/annotated_html.py +++ /dev/null @@ -1,83 +0,0 @@ -import sys -import re -import cgi -from screenplain.types import * - - -def unspace(text): - text = re.sub(r'\s*\n\s*', '\n', text) - text = re.sub(r'\s\s+', ' ', text) - text = re.sub(r'>\s+<', '><', text) - return text.strip() - -paragraph_html = unspace(""" -
- %(margin)s -
%(type)s
- %(text)s -
-""") - -types = { - Slug: 'slug', - Dialog: 'dialog', - DualDialog: 'dual', - Action: 'action', - Transition: 'transition', -} - - -def to_html(text): - return re.sub(' ', '  ', text.to_html()) - - -def format_dialog(dialog): - yield '

%s

' % to_html(dialog.character) - - for parenthetical, text in dialog.blocks: - yield '

%s

' % ( - 'parenthetical' if parenthetical else 'dialog', - to_html(text) - ) - - -def format_dual(dual): - yield ( - '
' - '
' - ) - for html in format_dialog(dual.left): - yield html - yield ( - '
' - '
' - ) - for html in format_dialog(dual.right): - yield html - yield ( - '
' - '
' - '
' - ) - - -def to_annotated_html(screenplay, out): - for para in screenplay: - classname = types.get(type(para)) - if isinstance(para, Dialog): - html_text = ''.join(format_dialog(para)) - elif isinstance(para, DualDialog): - html_text = ''.join(format_dual(para)) - else: - lines = para.lines - html_text = ''.join( - '

%s

' % (classname, to_html(line)) - for line in para.lines - ) - - margin = '

 

' * para.top_margin - out.write(paragraph_html % { - 'type': classname, - 'text': html_text, - 'margin': margin - }) diff --git a/screenplain/export/html.py b/screenplain/export/html.py new file mode 100644 index 0000000..c247415 --- /dev/null +++ b/screenplain/export/html.py @@ -0,0 +1,83 @@ +import sys +import re +import cgi +from screenplain.types import * + + +def unspace(text): + text = re.sub(r'\s*\n\s*', '\n', text) + text = re.sub(r'\s\s+', ' ', text) + text = re.sub(r'>\s+<', '><', text) + return text.strip() + +paragraph_html = unspace(""" +
+ %(margin)s +
%(type)s
+ %(text)s +
+""") + +types = { + Slug: 'slug', + Dialog: 'dialog', + DualDialog: 'dual', + Action: 'action', + Transition: 'transition', +} + + +def to_html(text): + return re.sub(' ', '  ', text.to_html()) + + +def format_dialog(dialog): + yield '

%s

' % to_html(dialog.character) + + for parenthetical, text in dialog.blocks: + yield '

%s

' % ( + 'parenthetical' if parenthetical else 'dialog', + to_html(text) + ) + + +def format_dual(dual): + yield ( + '
' + '
' + ) + for html in format_dialog(dual.left): + yield html + yield ( + '
' + '
' + ) + for html in format_dialog(dual.right): + yield html + yield ( + '
' + '
' + '
' + ) + + +def convert(screenplay, out, annotated=False): + for para in screenplay: + classname = types.get(type(para)) + if isinstance(para, Dialog): + html_text = ''.join(format_dialog(para)) + elif isinstance(para, DualDialog): + html_text = ''.join(format_dual(para)) + else: + lines = para.lines + html_text = ''.join( + '

%s

' % (classname, to_html(line)) + for line in para.lines + ) + + margin = '

 

' * para.top_margin + out.write(paragraph_html % { + 'type': classname, + 'text': html_text, + 'margin': margin + }) diff --git a/screenplain/main.py b/screenplain/main.py index 0ae37cd..2e7ee77 100644 --- a/screenplain/main.py +++ b/screenplain/main.py @@ -8,7 +8,7 @@ from optparse import OptionParser from screenplain.parsers.spmd import parse output_formats = ( - 'text', 'pdf', 'fdx', 'annotated_html' + 'text', 'pdf', 'fdx', 'html' ) usage = 'Usage: %prog [options] input-file output-file' @@ -32,6 +32,8 @@ def main(args): options.output_format = 'pdf' elif output_file.endswith('.fdx'): options.output_format = 'fdx' + elif output_file.endswith('.html'): + options.output_format = 'html' else: options.output_format = 'text' @@ -55,9 +57,9 @@ def main(args): elif options.output_format == 'fdx': from screenplain.export.fdx import to_fdx to_fdx(screenplay, output) - elif options.output_format == 'annotated_html': - from screenplain.export.annotated_html import to_annotated_html - to_annotated_html(screenplay, output) + elif options.output_format == 'html': + from screenplain.export.html import convert + convert(screenplay, output) finally: output.close() -- cgit v1.2.3