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.

174 lines
6.8 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 api_thumb_url, pandoc2html, img_fullurl, api_file_url, write_html_file
#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):
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
gallery_imgs.append(imgsrc)
print 'gallery_imgs', gallery_imgs
# from <gallery>.*</gallery> imgs, return list of img ET elements
# replace <gallery>.*</gallery> with ''
return content, gallery_imgs
9 years ago
def replace_video(content):
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
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
if video_src:
videos.append(video_src)
iframe = "<iframe src='{}' width='600px' height='450px'></iframe>".format(video_src)
# content = re.sub(video_exp, ' iframe ', content)
else:
content = re.sub(video_exp, '', content)
9 years ago
return content, videos
def create_workpage( work, work_key, tree): # replace text content in dict with html nodes, holding the content
# pprint.pprint( work)
for key in work.keys():
# print work[key]
div_header = tree.find(".//div[@class='header']")
div_body = tree.find(".//div[@class='body']")
div_av = tree.find(".//div[@class='av']")
if key in ['Description', 'Extra']:
mw_content = work[key]
# if re.search(gallery_exp, mw_content):
# # replace_gallery must replace the gallery inline
# mw_content, gallery_imgs = replace_gallery(mw_content)
# work['Images'] = gallery_imgs
# for imgsrc in gallery_imgs:
# img_el = ET.SubElement(div_av, 'img', attrib={'src': imgsrc})
# elif re.search(video_exp, mw_content):
# mw_content, videos = replace_video(mw_content)
# work['Video'] = videos
# # for video in videos:
# # iframe_el = ET.SubElement(div_av, 'iframe', attrib={'src': video, 'width':'600px', 'height':'450px'})
# # print 'VIDEO', ET.tostring(iframe_el)
print '--------------'
print 'mw_content', mw_content
print '--------------'
html_content = pandoc2html( mw_content if key in work.keys() else '') # convert to HTML
print 'html_content', html_content
document_el = html5lib.parse(html_content, namespaceHTMLElements=False)#ET.fromstring(html_content)
print ET.tostring(document_el)
print 'document_el', document_el, ET.iselement(document_el)
all_el = document_el.findall('body//')
if all_el:
all_el.reverse()
for el in all_el:
print 'el', ET.tostring(el)
div_body.append(el)
imgs = document_el.findall('.//img')
# if imgs:
# for img in imgs:
# src = api_file_url(img.get('src'))
# img.set('src', src)
# print 'IMG', img, src
elif key in ['Thumbnail']:
thumb = api_file_url(work[key])
img_el = ET.SubElement(div_av, 'img', attrib={'src': thumb})
elif key in ['Website']:
work_el = ET.SubElement(div_header, 'a', attrib={'href': work[key], 'id':key})
work_el.text = work[key]
elif key in ['Title']:
work_el = ET.SubElement(div_header, 'h1', attrib={'id': key})
work_el.text = (work[key]).replace('_', ' ')
elif key in ['Creator', 'Date', 'Bio']:
work_el =ET.SubElement(div_header, 'p', attrib={'id': key})
work_el.text = work[key]
# print "****************************"
# print ET.tostring(div_body)
# print "****************************"
# # elif key in ['Thumbnail_url']:
# # print Thumbnail_url, work[key]
# elif key in ['Thumbnail_url']:
# print 'THUMBNAIL_URL', work[key]
# # ERROR - Thumbnail url is None
# # work_el = ET.SubElement(div_header, 'img', attrib={'src': work[key], 'id': key})
# else:
# work_el = None # remove keys with None value?
# work[key] = work_el
# work.pop('Thumbnail_url', None) #remove Thumbnail_url
# pprint.pprint(work)
def edit_index(filepath, json_allworks_dict):
input_file = open(filepath, 'r')
tree = html5lib.parse(input_file, namespaceHTMLElements=False)
div_section02 = tree.find(".//div[@id='section02']")
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
worktemplate = open('web/work-template.html', 'r')
for key in json_allworks.keys():
work=json_allworks[key]
title = work['Title']
date = work['Date']
creator = (work['Creator'].encode('ascii', 'ignore')).replace(' ','_')
work_file = 'web/{}-{}-{}.html'.format(date, key, creator)
# print work_file
work_tree = html5lib.parse(worktemplate, namespaceHTMLElements=False)
create_workpage(work, key, work_tree )
write_html_file(work_tree, work_file)
### ISSSUES
# pandoc mw->HTML NOT WORKING
# sub gallery/videos with corresponding elements
# Gallaries, Files, videos, in orginal places correct place
# Specificy positions in template
# insert <p> into <div class="body">
# separate Extra and Description