From 5405d9861f33fd688f901818c36b1af6cded9e68 Mon Sep 17 00:00:00 2001 From: Castro0o Date: Sat, 25 Apr 2015 13:43:32 +0200 Subject: [PATCH] Failure: we'll start a new a approach from now onwards --- mmdc_pages.py | 138 ++++++++++++++++++++++++++--------------- web/work-template.html | 17 +++-- 2 files changed, 101 insertions(+), 54 deletions(-) diff --git a/mmdc_pages.py b/mmdc_pages.py index b7e572e..ecda56b 100755 --- a/mmdc_pages.py +++ b/mmdc_pages.py @@ -52,6 +52,29 @@ def replace_video(content): content = re.sub(video_exp, '', content) return content, videos + +def workpage_div_content(tree, div_id, element, content): + parent_str = ".//div[@id='{}']".format( div_id.lower() ) + parent = tree.find(parent_str) + if element is 'img': + lxml.SubElement(parent, 'img', attrib={'src': content}) + elif element in ['p','h1','h2']: + sub = lxml.SubElement(parent, element) + sub.text = content + elif element == 'a': + sub = ET.SubElement(parent, element, attrib={'href':content}) + sub.text = content + else:# element == 'span': + test_el = ET.Element('div', attrib={'id':'test', 'style':'background:yellow'}) + test = ET.SubElement(parent, test_el) +# parent.append(test) +# sub = ET.SubElement(parent, element) + #sub.text = content = ' '+ div_id + # parent.append(content) + + print parent_str, ET.tostring(parent) + + 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(): @@ -60,60 +83,75 @@ def create_workpage( work, work_key, tree): # replace text content in dict with 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) + + if key in ['Creator', 'Date', 'Bio']: + workpage_div_content(tree, key, 'p', work[key]) + elif key == 'Title': + workpage_div_content(tree, key, 'h1', work[key]) + elif key == 'Thumbnail': + thumb = api_file_url(work[key]) + workpage_div_content(tree, key, 'img', thumb) + elif key == 'Website': + workpage_div_content(tree, key, 'a', work[key]) + elif key in ['Description', 'Extra']: + test_el = ET.Element('div', attrib={'id':'test', 'style':'background:yellow'}) + workpage_div_content(tree, key, test_el, '') + + # HOW TO INSERT EXISTING HTML CHUNCK INTO TEMPLATE ??? + + + + + + + + + + # test_sub = ET.SubElement(test_el, 'span') + # test_sub.text = content + # sub = ET.SubElement(parent, test_el) + + + # 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 +# 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] diff --git a/web/work-template.html b/web/work-template.html index 270dbf3..cda5e4f 100644 --- a/web/work-template.html +++ b/web/work-template.html @@ -6,10 +6,19 @@ -
-
-
-
+ + +
+
+
+
+