canvasapp/templates.py

443 lines
14 KiB
Python

import os, re, codecs
import util
from pipelines import get_doc, get_doc_generic, put_file
# build web pages from fragments
output_type = ".php" # use php when uploading
output_type2 = ".html" # use php when uploading
which_template = "/template.html"
which_template2 = "/template2.html"
masonry_template = """ <div class="masonry-item">
<div class="masonry-content">
<a href="%%LINK%%">
<img src="%%IMGSRC%%"
alt="%%IMGALT%%"></a>
<h3 class="masonry-title">%%TITLE%%</h3>
<p class="masonry-description">%%CARDBODY%%</p>
</div>
</div>"""
def item_to_masonry(item):
# link title img imgalt desc
#print(item)
#print(item[1] + "\n" )
ix = {'TITLE': "<a href='%s'>%s</a>" % (item[0], item[1]),
'IMGSRC': item[2] or 'http://www.gavilan.edu/_files/img/blank.gif', 'IMGALT': item[3],
'CARDBODY': item[4], 'LINK': item[0] }
output = ''
for L in masonry_template.split('\n'):
match = re.search(r'%%(\w+)%%',L)
if match:
tag = match.group(1)
#print("Found a tag: %s" % tag)
line = re.sub(r'%%\w+%%', ix[tag], L)
output += line
else:
output += L
return output
def try_untemplate():
from bs4 import BeautifulSoup as bs
import bs4
dir1 = 'C:/Users/peter/Documents/gavilan/www mirror'
j = 0
j_in_dir = []
for x in os.listdir(dir1):
if x in ['student','staff']:
for xy in os.listdir(dir1+'/'+x):
j+= 1
print("%i.\t%s" % (j,x+'/'+xy))
j_in_dir.append(x+'/'+xy)
else:
j+= 1
print("%i.\t%s" % (j,x))
j_in_dir.append(x)
dir2 = j_in_dir[int(input("Choose a folder to look in: "))-1]
dir = dir1 + '/' + dir2
i = 0
f_in_dir = []
for x in os.listdir(dir):
if x.endswith('php'):
i+= 1
print("%i.\t%s" % (i,x))
f_in_dir.append(x)
choices = input("Choose inputs. Separate with a space: ")
for C in choices.split(" "):
#choice = int( input("Choose a page to make into template: ") ) - 1
choice = int( C ) - 1
print(f_in_dir[choice])
raw_html_in = open(dir + "/" + f_in_dir[choice],'r').read()
php_sig = '!!!PHP!!!'
php_elements = []
def php_remove(m):
php_elements.append(m.group())
return php_sig
def php_add(m):
return php_elements.pop(0)
# Pre-parse HTML to remove all PHP elements
html = re.sub(r'<\?php.*?\?>', php_remove, raw_html_in, flags=re.S+re.M)
# Try just poppin the first php tag. We probably leave it behind...
php_elements.pop(0)
bb = bs(html,'html.parser')
if not os.path.isdir(dir + '/template'):
os.mkdir(dir + '/template',0o777)
output_f = '.'.join(f_in_dir[choice].split('.')[:-1]) + '.html'
output = open( dir + '/template/' + output_f, 'w', encoding='utf-8')
b = bb.find(id='breadcrumbs').get_text()
b = re.sub(r'\s+',' ',b)
parts = b.split(' > ')
b = parts[-1]
c = bb.find('h1',class_='page-heading').get_text()
a = bb.find('article')
a.div.extract() # the first div has the h1 header
a_out = ""
for ea in a.contents:
try:
a_out += ea.prettify(formatter="html")
except:
if type(ea) == bs4.element.Comment:
a_out += "<!-- %s -->\n" % ea.string
else:
a_out += ea.string + "\n"
# some article cleanup
a_out = re.sub( r'\n{3,}','\n\n',a_out)
a_out = re.sub( r'(&#160;)+',' ',a_out)
a_out = re.sub(php_sig, php_add, a_out)
print("breadcrumb: %s" % b)
print("\n\ntitle: %s\n" % c)
#print("\n\narticle: %s" % a_out.strip())
output.write("BREADCRUMB=%s\n" % b)
output.write("TITLE=%s\n" % c)
output.write("ARTICLE=%s" % a_out)
output.close()
def do_template(temp,source,side):
subs = {'BANNER':'http://www.gavilan.edu/_files/img/blank.gif', 'SIDEBAR':''.join(side),}
state = 0
items = ""
output = ""
for L in source:
if state:
if re.search('%%ITEMS%%',L):
subs['ARTICLE'] += items
else:
subs['ARTICLE'] += L
else:
parts = L.split('=',1)
if parts[0] == 'ITEM':
i_parts = parts[1].split('|')
items += item_to_masonry(i_parts) + "\n"
if parts[0] == 'ARTICLE':
subs['ARTICLE'] = ""
state = 1
else: subs[parts[0].strip()] = parts[1].strip()
#subs['ITEMS'] = items
#print("Building page with this: " + str(subs))
for L in temp:
if len(L)<200:
match = re.search(r'%%(\w+)%%',L)
if match:
tag = match.group(1)
line = re.sub(r'%%\w+%%', subs[tag], L)
output += line
else:
output += L
else:
output += L
return output
def remove_filetype(f):
parts = f.split(r'.')
return '.'.join(parts[:-1])
def make():
dir1 = 'C:/Users/peter/Documents/gavilan/www mirror'
j = 0
j_in_dir = []
for x in os.listdir(dir1):
if x in ['student','staff']:
for xy in os.listdir(dir1+'/'+x):
j += 1
print("%i.\t%s" % (j,x+'/'+xy))
j_in_dir.append(x+'/'+xy)
else:
j+= 1
print("%i.\t%s" % (j,x))
j_in_dir.append(x)
dir2 = j_in_dir[int(input("Choose a folder to look in: "))-1]
in_dir = dir1 + '/' + dir2
#in_dir = r"C:/Users/peter/Documents/gavilan/www mirror/finaid_2019"
# how many slashes? Whats the depth? Any more than zero, start adding ../ s.
depth = dir2.count("/")
print("Depth is %i\n\n" % depth)
src = in_dir + r"/template/"
sidebar = ""
template = dir1 + which_template
if depth:
template = dir1 + which_template2
pages = []
for F in os.listdir(src):
if re.search(r'sidebar',F):
sidebar = F
#elif re.search(r'template',F):
# template = F
elif F.endswith('.html'):
pages.append(F)
print("Template: %s\nSidebar: %s\nPages: %s" % (template,sidebar,str(pages)))
template_text = open(template,'r').readlines()
side_txt = open(src + sidebar, 'r').readlines()
for P in pages:
in1_text = open(src + P, 'r').readlines()
out_file = open(in_dir + "/" + remove_filetype(P) + output_type , 'w')
out_file2 = open(in_dir + "/" + remove_filetype(P) + output_type2 , 'w')
print(P)
out_file.write( do_template( template_text, in1_text, side_txt) )
out_file.close()
out_file2.write( do_template( template_text, in1_text, side_txt) )
out_file2.close()
def txt_2_table():
input = open("C:/Users/peter/Documents/gavilan/www mirror/counseling_2019/who_call.txt",'r').readlines()
output = ''
state = ''
for L in input:
parts = L.split(r' ')
if state=='begintable': state= 'intable'
if state=='': output += "<p>"
if state=='intable': # in a table and a line is beginning
output += "<tr><td>"
for P in parts:
P = P.strip()
print(P)
if P=='NT':
if state=='intable':
output += "</td></tr></table>\n"
output += "<br /><table><tr><td>\n"
state = 'begintable'
continue
elif P=='ET':
output += '</td></tr></table>\n'
state = ''
continue
elif P=='|': # between cells
output += "</td><td>"
continue
output += " " + P # the normal case - input to output
if state=='intable': # in a table and a line just ended
output += "</td></tr>\n"
if state=='begintable': # in a table and the header just ended
output += "</td></tr>\n"
state = 'intable'
if state=='': output += "</p>"
output = open("C:/Users/peter/Documents/gavilan/www mirror/counseling_2019/who_call_out.txt.html",'w').write(output)
# https://docs.google.com/document/d/1Jw3rSGxuCkujMLrm-5p_zxSzCQavfwo_7Esthjzg0rQ/edit?usp=sharing
def studenttech_faq():
"""f = "../www mirror/student/online/template/tech_faq.html"
input = open(f,'r')
lines = input.readlines()
input.close()
output = open(f,'w')
for L in lines:
output.write(L )
if re.search('<!--START-->',L):
break
output.write( get_doc('1Jw3rSGxuCkujMLrm-5p_zxSzCQavfwo_7Esthjzg0rQ', 1) )"""
codecs.open('qanda_student/public/questions.json','w','utf-8').write( \
get_doc_generic('1Jw3rSGxuCkujMLrm-5p_zxSzCQavfwo_7Esthjzg0rQ', bracket=0,verbose=0))
put_file('/gavilan.edu/student/', 'qanda_student/public/', 'questions.json')
print("I uploaded the questions, but remember to do the images too if they changed.")
# https://docs.google.com/document/d/1tI_b-q75Lzu25HcA0GCx9bGfUt9ccM8m2YrrioDFZcA/edit?usp=sharing
def de_faq():
"""f = "cache/faq_template.html"
input = codecs.open(f,'r','utf-8')
lines = input.readlines()
input.close()
output = codecs.open('cache/de_teach_faq.html','w','utf-8')
for L in lines:
output.write(L )
if re.search('<!-- BEGIN -->',L):
output.write( get_doc_generic('1tI_b-q75Lzu25HcA0GCx9bGfUt9ccM8m2YrrioDFZcA', bracket=0,verbose=1))
"""
codecs.open('qanda/public/questions.json','w','utf-8').write( \
get_doc_generic('1tI_b-q75Lzu25HcA0GCx9bGfUt9ccM8m2YrrioDFZcA', bracket=0,verbose=0))
put_file('/gavilan.edu/staff/tlc/canvas_help/', 'qanda/public/', 'questions.json')
print("I uploaded the questions, but remember to do the images too if they changed.")
def degwork_faq():
f = "../www mirror/counseling_2019/template/degreeworks.html"
input = open(f,'r')
lines = input.readlines()
input.close()
output = open(f,'w')
for L in lines:
output.write(L )
if re.search('<!--START-->',L):
break
output.write( '<meta charset="utf-8" />\n' + get_doc('1ctmPkWwrIJ1oxlj8Z8UXYjijUzMW2VxnsVDSE1KfKME') )
def vrc_faq():
# https://docs.google.com/document/d/1anAmnSusL-lTSAz-E4lcjlzq1CA8YJyUfUHxnKgmJEo/edit?usp=sharing
f = "../www mirror/student/veterans/template/faq.html"
input = open(f,'r')
lines = input.readlines()
input.close()
output = open(f,'w')
for L in lines:
output.write(L )
if re.search('<!--START-->',L):
break
output.write( '<meta charset="utf-8" />\n' + get_doc('1anAmnSusL-lTSAz-E4lcjlzq1CA8YJyUfUHxnKgmJEo',verbose=1) )
def counseling_faq():
f = "../www mirror/counseling_2019/template/faq.html"
input = open(f,'r')
lines = input.readlines()
input.close()
output = open(f,'w')
for L in lines[0:3]:
output.write(L)
output.write( get_doc('101iOplZearjv955FX2FX9AM6bUnkcryo7BShKuzE9tI') )
def finaid_faq():
f = "../www mirror/finaid_2019/template/faq.html"
input = open(f,'r')
lines = input.readlines()
input.close()
output = open(f,'w')
i = 0
for L in lines[0:3]:
#print("%i, %s" % (i,L))
output.write(L)
i+=1
output.write( get_doc('1-FarjfyzZceezdSBXDHpP2cF_vaa9Qx6HvnIqwipmA4') )
def coun_loc():
f = "../www mirror/counseling_2019/template/location.html"
input = open(f,'r')
lines = input.readlines()
input.close()
output = open(f,'w')
i = 0
for L in lines[0:3]:
#print("%i, %s" % (i,L))
output.write(L)
i+=1
output.write( get_doc('1hxQZ9iXMWvQQtaoVlRgor9v4pdqdshksjeHD2Z4E6tg') )
def tutor_faq():
f = "../www mirror/student/learningcommons/template/faq.html"
input = open(f,'r')
lines = input.readlines()
input.close()
output = open(f,'w')
i = 0
for L in lines[0:3]:
#print("%i, %s" % (i,L))
output.write(L)
i+=1
output.write( get_doc('1gCYmGOanQ2rnd-Az2HWFjYErBm_4tp_RuJs6a7MkYrE',1) )
def test_repl():
from interactive import MyRepl
c = MyRepl()
c.set_my_dict( { "Peter": "thats me", "Mike": "a VP", "Pablo": "isn't here", "Mary": "Far away" })
c.inputloop()
if __name__ == "__main__":
print ('')
options = { 1: ['Build www pages', make] ,
2: ['De-template an existing page', try_untemplate],
3: ['Text to table', txt_2_table],
4: ['Pull the Counseling FAQ from gdocs', counseling_faq] ,
5: ['Pull the DegreeWorks FAQ from gdocs', degwork_faq] ,
6: ['Pull the Finaid FAQ from gdocs', finaid_faq] ,
7: ['Pull the Tutoring FAQ from gdocs', tutor_faq] ,
8: ['Pull the Counseling Location page from gdocs', coun_loc] ,
9: ['Pull the student tech faq page from gdocs', studenttech_faq] ,
10: ['Pull the DE faq page from gdocs', de_faq] ,
11: ['Pull the VRC faq page from gdocs', vrc_faq] ,
12: ['Test a REPL', test_repl ],
}
for key in options:
print(str(key) + '.\t' + options[key][0])
print('')
resp = input('Choose: ')
# Call the function in the options dict
options[ int(resp)][1]()