summaryrefslogtreecommitdiff
path: root/animism-align
diff options
context:
space:
mode:
Diffstat (limited to 'animism-align')
-rw-r--r--animism-align/cli/app/settings/app_cfg.py10
-rw-r--r--animism-align/cli/app/utils/file_utils.py12
-rw-r--r--animism-align/cli/commands/site/export.py70
-rw-r--r--animism-align/static/site.html4
4 files changed, 68 insertions, 28 deletions
diff --git a/animism-align/cli/app/settings/app_cfg.py b/animism-align/cli/app/settings/app_cfg.py
index 492b5ed..69a6adc 100644
--- a/animism-align/cli/app/settings/app_cfg.py
+++ b/animism-align/cli/app/settings/app_cfg.py
@@ -106,3 +106,13 @@ ROMAN_NUMERALS = [
'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X',
'XI', 'XII', 'XIII', 'XIV', 'XV', 'XVI', 'XVII', 'XVIII', 'XIX', 'XX',
]
+TEXT_ANNOTATION_TYPES = [
+ 'section_heading',
+ # 'heading_text',
+ 'sentence',
+ # 'paragraph_end',
+ # 'pullquote_credit',
+ 'footnote',
+ # 'text_plate',
+ # 'subtitle',
+]
diff --git a/animism-align/cli/app/utils/file_utils.py b/animism-align/cli/app/utils/file_utils.py
index 1d19fd6..c61e779 100644
--- a/animism-align/cli/app/utils/file_utils.py
+++ b/animism-align/cli/app/utils/file_utils.py
@@ -195,12 +195,14 @@ def load_yaml(fp_in):
cfg = yaml.load(fp, Loader=yaml.Loader)
return cfg
-def load_text(fp_in):
+def load_text(fp_in, split=True):
"""Load a text file into an array
:param fp_in: (str) filepath
"""
with open(fp_in, 'rt') as fp:
- lines = fp.read().rstrip('\n').split('\n')
+ lines = fp.read().rstrip('\n')
+ if split:
+ lines = lines.split('\n')
return lines
def load_line_lookup(fp_in):
@@ -264,16 +266,16 @@ def write_pickle(data, fp_out, ensure_path=True):
pickle.dump(data, fp)
-def write_json(data, fp_out, minify=True, ensure_path=True, sort_keys=True, verbose=False):
+def write_json(data, fp_out, minify=True, ensure_path=True, sort_keys=True, verbose=False, default=None):
"""
"""
if ensure_path:
mkdirs(fp_out)
with open(fp_out, 'w') as fp:
if minify:
- json.dump(data, fp, separators=(',',':'), sort_keys=sort_keys)
+ json.dump(data, fp, separators=(',',':'), sort_keys=sort_keys, default=default)
else:
- json.dump(data, fp, indent=2, sort_keys=sort_keys)
+ json.dump(data, fp, indent=2, sort_keys=sort_keys, default=default)
if verbose:
log.info('Wrote JSON: {}'.format(fp_out))
diff --git a/animism-align/cli/commands/site/export.py b/animism-align/cli/commands/site/export.py
index 604e70b..4bd5ba1 100644
--- a/animism-align/cli/commands/site/export.py
+++ b/animism-align/cli/commands/site/export.py
@@ -3,6 +3,7 @@ import click
from app.settings import app_cfg
from app.utils.file_utils import load_text, write_json, write_text
from os.path import join
+from functools import reduce
import os
@click.command('info')
@@ -49,7 +50,7 @@ def cli(ctx, opt_output_dir):
index_html = index_html.replace('PAGE_TITLE', page_title)
index_html = index_html.replace('PAGE_DESCRIPTION', page_desc)
index_html = index_html.replace('PLAIN_CONTENT', plain_content(db, site_title))
- index_html = index_html.replace('BUNDLE_PATH', join('/', SITE_PATH, 'bundle.js'))
+ index_html = index_html.replace('BUNDLE_PATH', join(page_url, 'bundle.js'))
write_text(index_html, join(site_fp_out, 'index.html'))
# ------------------------------------------------
@@ -90,14 +91,19 @@ def prune_db(db):
- extraneous paragraphs
"""
seen_paras = {}
+ seen_media = {}
for a in IterateTable(db['annotation']):
seen_paras[a['paragraph_id']] = True
- filtered_order = filter(lambda i: i in seen_paras, db['paragraph']['order'])
- filtered_lookup = { id: db['paragraph']['lookup'][id] for id in filtered_order }
- db['paragraph'] = {
- 'order': filtered_order,
- 'lookup': filtered_lookup,
- }
+ if 'media_id' in a['settings']:
+ seen_media[a['settings']['media_id']] = True
+
+ db['paragraph'] = filter_db(db, 'paragraph', seen_paras)
+ db['media'] = filter_db(db, 'media', seen_media)
+
+def filter_db(db, table, seen):
+ order = filter(lambda i: i in seen, db[table]['order'])
+ lookup = { id: db[table]['lookup'][id] for id in order }
+ return { 'order': order, 'lookup': lookup }
def export_db():
"""Load the entire database and convert it to JSON"""
@@ -105,18 +111,19 @@ def export_db():
session = Session()
- classes = [ Episode, Venue, Annotation, Paragraph, Media, Upload ]
+ classes = [ Episode, Venue, Annotation, Paragraph, Media ]
data = {}
for c in classes:
e_q = session.query(c)
if c == Annotation or c == Paragraph:
- e_q = e_q.order(c.start_ts)
+ e_q = e_q.order_by(c.start_ts)
e_list = e_q.all()
order = list(map(get_id, e_list))
lookup = reduce(get_json_tup, e_list, {})
- data[c.__table__] = { 'order': order, 'lookup': lookup }
- print(f"""exported {c.__table__} ({len(order) rows})""")
+ table_name = str(c.__table__)
+ data[table_name] = { 'order': order, 'lookup': lookup }
+ print(f"""exported {table_name} ({len(order)} rows)""")
return data
def sanitize_obj(data):
@@ -124,15 +131,17 @@ def sanitize_obj(data):
del data['created_at']
if 'updated_at' in data:
del data['updated_at']
+ return data
def get_id(e):
return e.id
def get_json_tup(a,e):
a[e.id] = sanitize_obj(e.toJSON())
return a
+
def db_get(db, table, idx):
"""Get an indexed object out of our db table"""
- id = db[table]['order']
+ id = db[table]['order'][idx]
return db[table]['lookup'][id]
######################################################################
@@ -149,30 +158,45 @@ def plain_content(db, title):
return s
def transcript_to_html(db):
- s += h(2, "Transcript")
+ s = h(2, "Transcript")
para = ""
last_pid = 0
section_count = 0
+ notes = []
+ # check each annotation
for a in IterateTable(db['annotation']):
- # check if a is "text"
- if a['type'] == 'section_heading' || a['paragraph_id'] != last_pid:
+ # skip media annotations (for now..)
+ if a['type'] not in app_cfg.TEXT_ANNOTATION_TYPES:
+ continue
+ # if it's a section heading or the paragraph id changed, append
+ # print(f"{a['type']} {a['paragraph_id']}")
+ if a['type'] == 'section_heading' or a['paragraph_id'] != last_pid:
if len(para):
s += p(para)
para = ""
last_pid = a['paragraph_id']
+ # if it's a new section, add a heading
if a['type'] == 'section_heading':
s += h(3, f"{app_cfg.ROMAN_NUMERALS[section_count]}: {a['text']}")
section_count += 1
last_pid = a['paragraph_id']
- para += a['text'] + " "
+ elif a['type'] == 'footnote':
+ para += f"<sup>{len(notes)+1}</sup> "
+ notes.append(a['text'])
+ else:
+ para += a['text'] + " "
if len(para):
s += p(para)
+ if len(notes):
+ s += h(3, "Footnotes")
+ for i, note in enumerate(notes):
+ s += p(f"<sup>{i+1}</sup> " + note)
return s
def credits_to_html(db, ep_num):
- e = db_get(db, ep_num)
+ e = db_get(db, 'episode', ep_num - 1)
s = h(2, "Credits")
- s += map(pbr, to_paras(e['settings']['credits']))
+ s += pbr_to_paras(e['settings']['credits'])
return s
def episode_to_html(e):
@@ -192,7 +216,7 @@ def venue_to_html(e):
s += p(e['date'])
s += h(4, "Artists")
s += pbr(e['settings']['artists'])
- s += map(pbr, to_paras(e['settings']['credits']))
+ s += pbr_to_paras(e['settings']['credits'])
return s
@@ -203,11 +227,13 @@ def venue_to_html(e):
def table_to_html(db, table, title, fn):
"""Convert a simple table list to HTML"""
s = h(2, title)
- for e in IterateTable(db['episode']):
- e += d(fn(e))
+ for e in IterateTable(db[table]):
+ s += d(fn(e))
return d(s)
# Helper functions that wrap stuff in HTML
+def pbr_to_paras(s):
+ return "".join(list(map(pbr, to_paras(s))))
def to_paras(s):
return s.replace("# ", "").split("\n\n")
def d(s):
@@ -238,7 +264,7 @@ class IterateTable:
return self
def __next__(self):
self.index += 1
- if self.index < self.len:
+ if self.index >= self.len:
raise StopIteration
id = self.table['order'][self.index]
return self.table['lookup'][id]
diff --git a/animism-align/static/site.html b/animism-align/static/site.html
index a18500b..fd7eb84 100644
--- a/animism-align/static/site.html
+++ b/animism-align/static/site.html
@@ -7,12 +7,14 @@
<meta name="description" content="PAGE_DESCRIPTION" />
<link rel="stylesheet" type="text/css" href="SITE_PATH/site.css" />
<style>
+/*
html { background: #000; }
.plain_content { display: none; }
+*/
</style>
</head>
<body>
-<div class="plain_content">{PLAIN_CONTENT}</div>
+<div class="plain_content">PLAIN_CONTENT</div>
<script>
var s = document.createElement('script');
s.setAttribute('src', 'BUNDLE_PATH?' + (Date.now() / 3600))