You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
1.9 KiB
Python

#! /usr/bin/env python
# -*- coding: utf-8 -*-
##############
# FROM THE JSON DICTIONARY CREATE AN INDEX PAGE
#####
import xml.etree.ElementTree as ET
import html5lib, urllib2, json, pprint, re
from mmdc_modules import write_html_file
json_allworks_file = open('allworks_mmdc.json', 'r') # save json
json_allworks = json.loads(json_allworks_file.read())
def insert_work(parent, element, work_dict, work_key):
if element == 'Graduation_work thumbnail':
print 'Graduation_work thumbnail'
# Content from json_allworks
thumb = work_dict['Thumbnail_url']
date = work_dict['Date']
title = (work_dict['Title']).replace('_', ' ')
creator = work_dict['Creator']
website = work_dict['Website'] if 'Website' in work_dict.keys() else ''
# HTML Elements
child_div = ET.SubElement(parent, 'div', attrib={'class':'item', 'id':work_key})
grandchild_a = ET.SubElement(child_div, 'a', attrib={'href':'#', 'class':'work'}) #href article
grandchild_img = ET.SubElement(grandchild_a, 'img', attrib={'class':'work', 'src':thumb})
grandchild_textbox = ET.SubElement(child_div, 'div', attrib={'class':'work'})
for content in [title, creator, date]:
grandgrandchild_p = ET.SubElement(grandchild_textbox, 'p', attrib={'class':'work'})
grandgrandchild_p.text = content
def edit_index(filepath, json_allworks_dict):
input_file = open(filepath, 'r')
tree = html5lib.parse(input_file, namespaceHTMLElements=False)
div_section02 = (tree.findall(".//div[@id='section02']"))[0]
for key in json_allworks_dict.keys():
graduation_work=json_allworks_dict[key]
insert_work(div_section02, 'Graduation_work thumbnail', graduation_work, key )
return tree
index_tree = edit_index('web/index-template.html', json_allworks)
write_html_file(index_tree, 'web/index.html')