aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMartin Vilcans <martin@librador.com>2011-11-27 23:44:21 +0100
committerMartin Vilcans <martin@librador.com>2011-11-28 00:10:42 +0100
commitff5938bc89ec32d990b1939992fe73471155b9d3 (patch)
tree7117b12cd2f806e80b291db1faa53238670f7a4d
parent8f6afcb212a458fdbf6e5cd2b794fab6d7ee1633 (diff)
downloadscreenplain-ff5938bc89ec32d990b1939992fe73471155b9d3.tar.gz
Export complete HTML with CSS.
This should now be a bit more ideomatic HTML. For example, slugs are <h2> tags.
-rw-r--r--screenplain/export/default.css87
-rw-r--r--screenplain/export/html.py133
-rw-r--r--screenplain/types.py5
3 files changed, 172 insertions, 53 deletions
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("""
-<div class="block">
- %(margin)s
- <div class="type %(type)s">%(type)s</div>
- %(text)s
-</div>
-""")
types = {
Slug: 'slug',
@@ -31,53 +21,98 @@ def to_html(text):
return re.sub(' ', '&nbsp; ', text.to_html())
-def format_dialog(dialog):
- yield '<p class="character">%s</p>' % to_html(dialog.character)
+def format_dialog(dialog, out):
+ out.write(
+ '<p class="character">'
+ )
+ out.write(to_html(dialog.character))
+ out.write('</p>')
for parenthetical, text in dialog.blocks:
- yield '<p class="%s">%s</p>' % (
- 'parenthetical' if parenthetical else 'dialog',
- to_html(text)
- )
+ if parenthetical:
+ out.write('<p class="parenthetical">')
+ out.write(to_html(text))
+ out.write('</p>')
+ else:
+ out.write('<p>')
+ out.write(to_html(text))
+ out.write('</p>')
-def format_dual(dual):
- yield (
- '<div class="dual-dialog">'
- '<div class="dual left">'
+def format_dual(dual, out):
+ out.write(
+ '<div class="left dialog">'
)
- for html in format_dialog(dual.left):
- yield html
- yield (
+ format_dialog(dual.left, out)
+ out.write(
'</div>'
- '<div class="dual right">'
+ '<div class="right dialog">'
)
- for html in format_dialog(dual.right):
- yield html
- yield (
- '</div>'
- '<br/>'
+ format_dialog(dual.right, out)
+ out.write(
'</div>'
+ '<br />'
)
+def format_slug(slug, out):
+ out.write('<h2>')
+ out.write(to_html(slug.line))
+ out.write('</h2>')
+
+
+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(
+ '<!DOCTYPE html>\n'
+ '<html>'
+ '<head>'
+ '<title>Screenplay</title>'
+ '<style>'
+ )
+ out.write(css)
+ out.write(
+ '</style>'
+ '</head>'
+ '<body>'
+ '<div class="screenplay">\n'
+ )
+ convert_body_html(screenplay, out)
+ out.write(
+ '</div>'
+ '</body>'
+ '</html>\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('<div class="dialog">')
+ format_dialog(para, out)
+ out.write('</div>')
elif isinstance(para, DualDialog):
- html_text = ''.join(format_dual(para))
+ out.write('<div class="dual">')
+ format_dual(para, out)
+ out.write('</div>')
else:
- lines = para.lines
- html_text = ''.join(
- '<p class="%s">%s</p>' % (classname, to_html(line))
- for line in para.lines
- )
-
- margin = '<p>&nbsp;</p>' * para.top_margin
- out.write(paragraph_html % {
- 'type': classname,
- 'text': html_text,
- 'margin': margin
- })
+ classname = types.get(type(para))
+ out.write('<div class="')
+ out.write(classname)
+ out.write('">')
+ for line in para.lines:
+ out.write('<p>')
+ out.write(to_html(line))
+ out.write('</p>')
+ out.write('</div>')
+ 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):