summaryrefslogtreecommitdiff
path: root/megapixels/app/site/parser.py
diff options
context:
space:
mode:
Diffstat (limited to 'megapixels/app/site/parser.py')
-rw-r--r--megapixels/app/site/parser.py70
1 files changed, 62 insertions, 8 deletions
diff --git a/megapixels/app/site/parser.py b/megapixels/app/site/parser.py
index 3792e6f1..dc53177b 100644
--- a/megapixels/app/site/parser.py
+++ b/megapixels/app/site/parser.py
@@ -16,9 +16,30 @@ def parse_markdown(metadata, sections, s3_path, skip_h1=False):
"""
groups = []
current_group = []
+ in_stats = False
+
+ if 'desc' in metadata and 'subdesc' in metadata:
+ groups.append(intro_section(metadata, s3_path))
+
for section in sections:
if skip_h1 and section.startswith('# '):
continue
+ elif section.strip().startswith('---'):
+ continue
+ elif section.lower().strip().startswith('ignore text'):
+ break
+ elif '### Statistics' in section:
+ if len(current_group):
+ groups.append(format_section(current_group, s3_path))
+ current_group = []
+ current_group.append(section)
+ in_stats = True
+ elif in_stats and not section.strip().startswith('## '):
+ current_group.append(section)
+ elif in_stats and section.strip().startswith('## '):
+ current_group = [format_section(current_group, s3_path, 'right-sidebar', tag='div')]
+ current_group.append(section)
+ in_stats = False
elif section.strip().startswith('```'):
groups.append(format_section(current_group, s3_path))
current_group = []
@@ -32,7 +53,7 @@ def parse_markdown(metadata, sections, s3_path, skip_h1=False):
current_group = []
elif section.startswith('+ '):
groups.append(format_section(current_group, s3_path))
- groups.append(format_metadata(section))
+ groups.append('<section>' + format_metadata(section) + '<section>')
current_group = []
elif '![fullwidth:' in section:
groups.append(format_section(current_group, s3_path))
@@ -52,6 +73,32 @@ def parse_markdown(metadata, sections, s3_path, skip_h1=False):
content = "".join(groups)
return content
+def intro_section(metadata, s3_path):
+ """
+ Build the intro section for datasets
+ """
+
+ section = "<section class='intro_section' style='background-image: url({})'>".format(s3_path + metadata['image'])
+ section += "<div class='inner'>"
+
+ parts = []
+ if 'desc' in metadata:
+ desc = metadata['desc']
+ if 'color' in metadata and metadata['title'] in desc:
+ desc = desc.replace(metadata['title'], "<span style='color: {}'>{}</span>".format(metadata['color'], metadata['title']))
+ section += "<div class='hero_desc'><span>{}</span></div>".format(desc, desc)
+
+ if 'subdesc' in metadata:
+ subdesc = markdown(metadata['subdesc']).replace('<p>', '').replace('</p>', '')
+ section += "<div class='hero_subdesc'><span>{}</span></div>".format(subdesc, subdesc)
+
+ section += "</div>"
+ section += "</section>"
+
+ if 'caption' in metadata:
+ section += "<section><div class='image'><div class='caption'>{}</div></div></section>".format(metadata['caption'])
+
+ return section
def fix_images(lines, s3_path):
"""
@@ -75,19 +122,26 @@ def fix_images(lines, s3_path):
real_lines.append(line)
return "\n".join(real_lines)
-
-def format_section(lines, s3_path, type=''):
+def format_section(lines, s3_path, type='', tag='section'):
"""
format a normal markdown section
"""
if len(lines):
+ lines = fix_meta(lines)
lines = fix_images(lines, s3_path)
if type:
- return "<section class='{}'>{}</section>".format(type, markdown(lines))
+ return "<{} class='{}'>{}</{}>".format(tag, type, markdown(lines), tag)
else:
- return "<section>" + markdown(lines) + "</section>"
+ return "<{}>{}</{}>".format(tag, markdown(lines), tag)
return ""
+def fix_meta(lines):
+ new_lines = []
+ for line in lines:
+ if line.startswith('+ '):
+ line = format_metadata(line)
+ new_lines.append(line)
+ return new_lines
def format_metadata(section):
"""
@@ -97,8 +151,7 @@ def format_metadata(section):
for line in section.split('\n'):
key, value = line[2:].split(': ', 1)
meta.append("<div><div class='gray'>{}</div><div>{}</div></div>".format(key, value))
- return "<section><div class='meta'>{}</div></section>".format(''.join(meta))
-
+ return "<div class='meta'>{}</div>".format(''.join(meta))
def format_applet(section, s3_path):
"""
@@ -107,12 +160,13 @@ def format_applet(section, s3_path):
# print(section)
payload = section.strip('```').strip().strip('```').strip().split('\n')
applet = {}
- print(payload)
+ # print(payload)
if ': ' in payload[0]:
command, opt = payload[0].split(': ')
else:
command = payload[0]
opt = None
+ print(command)
if command == 'python' or command == 'javascript' or command == 'code':
return format_section([ section ], s3_path)
if command == '':