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.

120 lines
4.7 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
9 years ago
from mmdc_modules import api_thumb_url, pandoc2html, img_fullurl, api_file_url
#import mmdc_create_json import api_thumb_url
json_allworks_file = open('allworks_mmdc.json', 'r') # save json
json_allworks = json.loads(json_allworks_file.read())
pages_path = 'web/work'
#def generate_xml():
gallery_exp=re.compile('<gallery>.*?</gallery>')
file_exp=re.compile('File:(.*?)(?=File:|<\/gallery>)')
9 years ago
img_exp=re.compile('(File:|Image:)((.*?)\.(gif|jpg|jpeg|png))(?=\||File:|Image:|<\/gallery>)', re.I)
video_exp=re.compile('\{\{(.*?)\|(.*?)\}\}')
def replace_gallery(content):
# from <gallery>.*</gallery> imgs, return list of img ET elements
# replace <gallery>.*</gallery> with ''
gallery_imgs = []
gallery_found = re.findall(gallery_exp, content)
content = re.sub(gallery_exp, '', content)
for gallery in gallery_found: # in case there is more than 1 <gallery>
allfiles =re.findall(img_exp, gallery)
for imgfile in allfiles:
9 years ago
imgfile = imgfile[1]
imgsrc = api_file_url(imgfile) # search for original image
img_el = ET.Element('img', attrib={'src': imgsrc})
gallery_imgs.append(img_el)
return content, gallery_imgs
9 years ago
def replace_video(content):
print '-- Replacing Videos --'
videos = []
videos_found = re.findall(video_exp, content)
for video in videos_found:
video_provider = str(video[0])
video_hash = str(video[1])
video_src = None
print video_provider, type(video_provider)
if (video_provider.lower()) == 'youtube':
video_src="https://www.youtube.com/embed/" + video_hash
elif (video_provider.lower()) == 'vimeo':
video_src="https://player.vimeo.com/video/" + video_hash
print 'VIMEO'
if video_src:
iframe_el = ET.Element('iframe', attrib={'src':video_src, 'width':'600px', 'height':'450px'})
videos.append(iframe_el)
content = re.sub(video_exp, '', content)
return content, videos
def create_workpage( allworks_dict, work_key): # replace text content in dict with html nodes, holding the content
for key in allworks_dict.keys():
if key in ['Description', 'Extra']:
mw_content = allworks_dict[key]
if re.search(gallery_exp, mw_content):
mw_content, gallery_imgs = replace_gallery(mw_content)
9 years ago
allworks_dict['Images'] = gallery_imgs
elif re.search(video_exp, mw_content):
print '-- Searching for Video --'
mw_content, videos = replace_video(mw_content)
allworks_dict['Video'] = videos
print mw_content, videos
9 years ago
allworks_dict[key] = pandoc2html( mw_content if key in allworks_dict.keys() else '' ) # convert to HTML
work_htmltree = html5lib.parseFragment(allworks_dict[key], namespaceHTMLElements=False)
# print work_htmltree
# print ET.tostring(work_htmltree)
# vimeo/youtube: {{vimeo|44977056}}
# External urls: [http://www.scribd.com/doc/105882261/THE-DICTATOR-S-PRACTICAL-INTERNET-GUIDE-TO-POWER-RETENTION scribd]
elif key in ['Website']:
work_htmltree = ET.Element('a', attrib={'href': allworks_dict[key], 'id':key})
work_htmltree.text = allworks_dict[key]
elif key in ['Title']:
work_htmltree = ET.Element('h1', attrib={'id': key})
work_htmltree.text
elif key in ['Creator', 'Date', 'Bio']:
work_htmltree = ET.Element('p', attrib={'id': key})
work_htmltree.text
elif key in ['Thumbnail_url']:
work_htmltree = ET.Element('img', attrib={'src': allworks_dict[key], 'id': key})
print ET.tostring(work_htmltree)
else:
work_htmltree = None
# remove keys with None value?
# print work_htmltree
allworks_dict[key] = work_htmltree
allworks_dict.pop('Thumbnail', None) #remove thumnail
9 years ago
pprint.pprint(allworks_dict)
for key in json_allworks.keys():
graduation_work=json_allworks[key]
print (graduation_work['Creator']).encode('utf-8')
# pprint.pprint(graduation_work)
# purge graduation_work from keys with empty vals
# for key in graduation_work:
# if graduation_work[key] in [None, '']:
# print graduation_work
create_workpage(graduation_work, key )
print '----------'