| 1 | import re |
|---|
| 2 | |
|---|
| 3 | from urlparse import urlparse, urlunparse, urlsplit |
|---|
| 4 | |
|---|
| 5 | from zope.interface import implements |
|---|
| 6 | from zope.component import getUtility, getMultiAdapter |
|---|
| 7 | |
|---|
| 8 | from enpraxis.educommons.utilities.interfaces import IECUtility |
|---|
| 9 | from enpraxis.staticsite.utilities.staticsiteutility import StaticSiteUtility |
|---|
| 10 | from enpraxis.staticsite.utilities.interfaces import IStaticSiteUtility |
|---|
| 11 | |
|---|
| 12 | class eduStaticSiteUtility(StaticSiteUtility): |
|---|
| 13 | """ Deploy a static site """ |
|---|
| 14 | |
|---|
| 15 | implements(IStaticSiteUtility) |
|---|
| 16 | |
|---|
| 17 | def runDocumentFilters(self, portal, current, soup, ssprops): |
|---|
| 18 | self.filterBaseTag(soup, current) |
|---|
| 19 | self.filterIgnoredSections(soup, ssprops) |
|---|
| 20 | self.filterIgnoredActions(soup, ssprops) |
|---|
| 21 | self.filterDocActionImages(soup, portal.portal_url(), current) |
|---|
| 22 | self.filterCSSLinks(soup, current) |
|---|
| 23 | self.filterIEFixesCSS(soup, current) |
|---|
| 24 | self.filterJSLinks(soup, current) |
|---|
| 25 | self.filterS5BaseUrl(soup, current) |
|---|
| 26 | self.filterImageFullscreenBackLink(soup, current) |
|---|
| 27 | self.filterCourseDownloadLink(soup, current, portal, ssprops) |
|---|
| 28 | self.filterAttributionLinks(soup, current, portal, ssprops) |
|---|
| 29 | self.filterCSSValidatorLink(soup, current, portal, ssprops) |
|---|
| 30 | links = self.getDocumentLinks(soup) |
|---|
| 31 | for x in links: |
|---|
| 32 | orig = x['href'] |
|---|
| 33 | x['href'] = self.filterDocumentLink(x['href'], |
|---|
| 34 | current, |
|---|
| 35 | portal, |
|---|
| 36 | ssprops.getProperty('views_to_add')) |
|---|
| 37 | print ' %s => %s' %(orig, x['href']) |
|---|
| 38 | |
|---|
| 39 | return soup.prettify() |
|---|
| 40 | |
|---|
| 41 | def filterCourseDownloadLink(self, soup, current, portal, ssprops): |
|---|
| 42 | link = soup.find('dd', id='download_course') |
|---|
| 43 | if link: |
|---|
| 44 | href = link.a['href'] |
|---|
| 45 | result = current |
|---|
| 46 | hr = urlparse(current) |
|---|
| 47 | p = urlparse(portal.portal_url()) |
|---|
| 48 | if p[1] == hr[1]: |
|---|
| 49 | h = hr[2].split('/') |
|---|
| 50 | if h[-1] == 'index.html': |
|---|
| 51 | h = h[:-1] |
|---|
| 52 | for view in ssprops.getProperty('views_to_add'): |
|---|
| 53 | if view in h[-1]: |
|---|
| 54 | h[-1] = h[-1].replace('-%s.html' % view, '') |
|---|
| 55 | result = portal.portal_catalog.searchResults(query={'path':'/'.join(h),}, id=h[-1])[0].getObject() |
|---|
| 56 | course = getUtility(IECUtility).FindECParent(result) |
|---|
| 57 | zip_url = '%s/%s.zip' % (course.absolute_url(), course.id) |
|---|
| 58 | link.a['href'] = zip_url |
|---|
| 59 | |
|---|
| 60 | def filterAttributionLinks(self, soup, current, portal, ssprops): |
|---|
| 61 | if current == portal.portal_url(): |
|---|
| 62 | current += '/index.html' |
|---|
| 63 | elif '.htm' not in current: |
|---|
| 64 | current += '.html' |
|---|
| 65 | ccite = soup.find(id='click_citation') |
|---|
| 66 | pcite = soup.find(id='print_citation') |
|---|
| 67 | scite = soup.find(id='skinless_citation') |
|---|
| 68 | portal_url = portal.portal_url() |
|---|
| 69 | deploy_url = ssprops.getProperty('deployment_url') |
|---|
| 70 | if ccite: |
|---|
| 71 | sstring = ccite['onclick'] |
|---|
| 72 | pattern = re.compile( r"\b(http:\/\/).*\b\." ) |
|---|
| 73 | nstring = pattern.sub('%s.' % current, sstring) |
|---|
| 74 | nstring = nstring.replace(portal_url, deploy_url) |
|---|
| 75 | ccite['onclick'] = nstring |
|---|
| 76 | if pcite: |
|---|
| 77 | sstring = pcite.contents[0] |
|---|
| 78 | pattern = re.compile( r"\b(http:\/\/).*\b\." ) |
|---|
| 79 | nstring = pattern.sub('%s.' % current, sstring) |
|---|
| 80 | nstring = nstring.replace(portal_url, deploy_url) |
|---|
| 81 | pcite.contents[0].replaceWith(nstring) |
|---|
| 82 | if scite: |
|---|
| 83 | sstring = scite.span.contents[0] |
|---|
| 84 | pattern = re.compile( r"\b(http:\/\/).*\b\." ) |
|---|
| 85 | nstring = pattern.sub('%s.' % current, sstring) |
|---|
| 86 | nstring = nstring.replace(portal_url, deploy_url) |
|---|
| 87 | scite.span.contents[0].replaceWith(nstring) |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | #rewrite obj ids on FS to match doc_action links |
|---|
| 96 | |
|---|
| 97 | |
|---|