From ff5938bc89ec32d990b1939992fe73471155b9d3 Mon Sep 17 00:00:00 2001 From: Martin Vilcans Date: Sun, 27 Nov 2011 23:44:21 +0100 Subject: Export complete HTML with CSS. This should now be a bit more ideomatic HTML. For example, slugs are

tags. --- screenplain/export/default.css | 87 +++++++++++++++++++++++++++ screenplain/export/html.py | 133 ++++++++++++++++++++++++++--------------- screenplain/types.py | 5 +- 3 files changed, 172 insertions(+), 53 deletions(-) create mode 100644 screenplain/export/default.css diff --git a/screenplain/export/default.css b/screenplain/export/default.css new file mode 100644 index 0000000..bb766fd --- /dev/null +++ b/screenplain/export/default.css @@ -0,0 +1,87 @@ +/* Reset */ +html, body, div, span, object, iframe, +h1, h2, h3, h4, h5, h6, p, blockquote, pre, +abbr, address, cite, code, del, dfn, em, img, ins, kbd, q, samp, +small, strong, sub, sup, var, b, i, dl, dt, dd, ol, ul, li, +fieldset, form, label, legend, +table, caption, tbody, tfoot, thead, tr, th, td, +article, aside, canvas, details, figcaption, figure, +footer, header, hgroup, menu, nav, section, summary, +time, mark, audio, video { + margin: 0; + padding: 0; + border: 0; + font-size: 100%; + font: inherit; + vertical-align: baseline; +} + +article, aside, details, figcaption, figure, +footer, header, hgroup, menu, nav, section { + display: block; +} + +/* Styles for Screenplain */ + +.screenplay { + background: white; + font-family: Courier; + font-size: 12pt; + max-width: 68em; +} + +em { + font-style: italic; +} +strong { + font-weight: bold; +} +.screenplay br { + clear: both; +} + +/* Slug */ +.screenplay > h2 { + margin-top: 2em; + font-weight: bold; +} +.screenplay div.action { + margin-top: 1em; +} +.screenplay div.dialog, .screenplay div.dual { + margin-top: 1em; +} +.screenplay div.transition { + text-align: right; + margin-top: 1em; +} + +.screenplay .dialog p.character { + padding-left: 11em; +} +.screenplay .dialog p.parenthetical { + padding-left: 8em; +} +.screenplay .dialog p { + padding-left: 5em; + width: 22.5em; +} + +.screenplay .dual > div { + float: left; +} +.screenplay .dual p.character { + padding-left: 6em; +} +.screenplay .dual p.parenthetical { + padding-left: 3em; +} +.screenplay .dual p { + padding-left: 0; +} +.screenplay .dual .right { + margin-left: .5em; +} +.screenplay .dual .right p.character { + padding-left: 6em; +} diff --git a/screenplain/export/html.py b/screenplain/export/html.py index c247415..5b7ea5c 100644 --- a/screenplain/export/html.py +++ b/screenplain/export/html.py @@ -1,22 +1,12 @@ +from __future__ import with_statement import sys import re import cgi -from screenplain.types import * - +import os +import os.path -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() +from screenplain.types import * -paragraph_html = unspace(""" -
- %(margin)s -
%(type)s
- %(text)s -
-""") types = { Slug: 'slug', @@ -31,53 +21,98 @@ def to_html(text): return re.sub(' ', '  ', text.to_html()) -def format_dialog(dialog): - yield '

%s

' % to_html(dialog.character) +def format_dialog(dialog, out): + out.write( + '

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

') for parenthetical, text in dialog.blocks: - yield '

%s

' % ( - 'parenthetical' if parenthetical else 'dialog', - to_html(text) - ) + if parenthetical: + out.write('

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

') + else: + out.write('

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

') -def format_dual(dual): - yield ( - '
' - '
' +def format_dual(dual, out): + out.write( + '
' ) - for html in format_dialog(dual.left): - yield html - yield ( + format_dialog(dual.left, out) + out.write( '
' - '
' + '
' ) - for html in format_dialog(dual.right): - yield html - yield ( - '
' - '
' + format_dialog(dual.right, out) + out.write( '
' + '
' ) +def format_slug(slug, out): + out.write('

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

') + + +def _read_file(filename): + path = os.path.join(os.path.dirname(__file__), filename) + with open(path) as stream: + return stream.read() + + def convert(screenplay, out, annotated=False): + + css = _read_file('default.css') + out.write( + '\n' + '' + '' + 'Screenplay' + '' + '' + '' + '
\n' + ) + convert_body_html(screenplay, out) + out.write( + '
' + '' + '\n' + ) + + +def convert_body_html(screenplay, out): for para in screenplay: - classname = types.get(type(para)) - if isinstance(para, Dialog): - html_text = ''.join(format_dialog(para)) + if isinstance(para, Slug): + # Slugs are h2 tags not inside a div + format_slug(para, out) + elif isinstance(para, Dialog): + out.write('
') + format_dialog(para, out) + out.write('
') elif isinstance(para, DualDialog): - html_text = ''.join(format_dual(para)) + out.write('
') + format_dual(para, out) + out.write('
') 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 - }) + classname = types.get(type(para)) + out.write('
') + for line in para.lines: + out.write('

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

') + out.write('
') + out.write('\n') diff --git a/screenplain/types.py b/screenplain/types.py index c81ffd2..1eb7c26 100644 --- a/screenplain/types.py +++ b/screenplain/types.py @@ -6,10 +6,7 @@ class Slug(object): top_margin = 1 def __init__(self, lines): - self.lines = lines - - def format(self): - return self.lines + self.line = lines[0] class Dialog(object): -- cgit v1.2.3