From e70de69db36be431e27ed918bf67639027c8e8f3 Mon Sep 17 00:00:00 2001 From: Martin Vilcans Date: Mon, 28 Nov 2011 01:13:34 +0100 Subject: Created context manager for working with HTML tags. This simplifies the code a little. --- screenplain/export/html.py | 108 +++++++++++++++++++++++++++------------------ 1 file changed, 64 insertions(+), 44 deletions(-) diff --git a/screenplain/export/html.py b/screenplain/export/html.py index fe3de9b..a27d989 100644 --- a/screenplain/export/html.py +++ b/screenplain/export/html.py @@ -17,66 +17,90 @@ types = { } +class tags(object): + """Handler for automatically opening and closing tags. + + E.g. + + >>> import sys + >>> from __future__ import with_statement + >>> with tags(sys.stdout, 'div', 'p', 'a'): + ... sys.stdout.write('hello') + ... +

hello

+ + Tags with attributes are also supported: + + >>> with tags(sys.stdout, 'div class="foo"'): + ... sys.stdout.write('hello') +
hello
+ + """ + def __init__(self, out, *tags): + self.out = out + self.tags = list(tags) + + def __enter__(self): + for tag in self.tags: + self.out.write('<%s>' % tag) + + def __exit__(self, exception_type, value, traceback): + if not exception_type: + for tag in reversed(self.tags): + self.out.write('' % tag.split()[0]) + return False + + def to_html(text): return re.sub(' ', '  ', text.to_html()) def format_dialog(dialog, out): - out.write( - '

' - ) - out.write(to_html(dialog.character)) - out.write('

') + with tags(out, 'div class="dialog"'): + _write_dialog_block(dialog, out) + + +def format_dual(dual, out): + with tags(out, 'div class="dual"'): + with tags(out, 'div class="left"'): + _write_dialog_block(dual.left, out) + with tags(out, 'div class="right"'): + _write_dialog_block(dual.right, out) + out.write('
') + + +def _write_dialog_block(dialog, out): + with tags(out, 'p class="character"'): + out.write(to_html(dialog.character)) for parenthetical, text in dialog.blocks: if parenthetical: - out.write('

') - out.write(to_html(text)) - out.write('

') + with tags(out, 'p class="parenthetical"'): + out.write(to_html(text)) else: - out.write('

') - out.write(to_html(text)) - out.write('

') - - -def format_dual(dual, out): - out.write( - '
' - ) - format_dialog(dual.left, out) - out.write( - '
' - '
' - ) - format_dialog(dual.right, out) - out.write( - '
' - '
' - ) + with tags(out, 'p'): + out.write(to_html(text)) def format_slug(slug, out): - out.write('

') - out.write(to_html(slug.line)) - out.write('

') + with tags(out, 'h2'): + out.write(to_html(slug.line)) def format_action(para, out): if para.centered: - out.write('
') + tag = 'div class="action centered"' else: - out.write('
') - for line in para.lines: - out.write('

') - out.write(to_html(line)) - out.write('

') - out.write('
') + tag = 'div class="action"' + with tags(out, tag): + for line in para.lines: + with tags(out, 'p'): + out.write(to_html(line)) def format_transition(para, out): - out.write('
') - out.write(to_html(para.line)) - out.write('
') + with tags(out, 'div class="transition"'): + out.write(to_html(para.line)) def _read_file(filename): @@ -138,13 +162,9 @@ def convert_bare(screenplay, out): elif isinstance(para, Action): format_action(para, out) elif isinstance(para, Dialog): - out.write('
') format_dialog(para, out) - out.write('
') elif isinstance(para, DualDialog): - out.write('
') format_dual(para, out) - out.write('
') elif isinstance(para, Transition): format_transition(para, out) else: -- cgit v1.2.3