| 39 | | files = bbreader.readFiles(x) |
| 40 | | # If the resource is a file |
| 41 | | if files: |
| 42 | | hash = resid |
| 43 | | objDict[hash] = metadata |
| 44 | | excludeFromNav = True |
| 45 | | file = '%s/%s' %(bbase, files[0]) |
| 46 | | type = self.determineType(objDict[hash], files[0]) |
| 47 | | id = self.createIdFromFile(files[0]) |
| 48 | | path = '%s' %bbase |
| 49 | | if orgs.has_key(resid): |
| 50 | | title = orgs[resid] |
| | 45 | if metadata.has_key('text') and metadata['text']: |
| | 46 | mtext = metadata['text'] |
| | 47 | if type(mtext) == type(u''): |
| | 48 | mtext = mtext.encode('utf-8') |
| | 49 | ptext = unquotehtml(mtext) |
| | 50 | utils = getUtility(ISiteRoot).plone_utils |
| | 51 | soup = BeautifulSoup(ptext) |
| | 52 | doctext = bbreader.runDocumentFilters(utils, soup, [(EMBEDDED_STRING, 'embedded'),], bbase) |
| | 53 | # Handle Files |
| | 54 | files = bbreader.readFiles(x, bbase) |
| | 55 | entries = [] |
| | 56 | for y in files: |
| | 57 | dhash = resid + y |
| | 58 | objDict[dhash] = {} |
| | 59 | dexcludeFromNav = True |
| | 60 | dfile = y |
| | 61 | dfileparts = y.split('/') |
| | 62 | # File is embedded |
| | 63 | if 'embedded' in y: |
| | 64 | # Link is encrypted |
| | 65 | if len(dfileparts) > 0 and dfileparts[-1][0] == '!' and doctext: |
| | 66 | soup = BeautifulSoup(doctext) |
| | 67 | embeddedpath = bbreader.readEmbeddedTags(soup) |
| | 68 | if embeddedpath: |
| | 69 | dfile = embeddedpath |
| | 70 | dfilepath = self.createPathFromFile(dfile) |
| | 71 | did = self.createIdFromFile(dfile) |
| | 72 | dtype = self.determineType(objDict[dhash], dfile) |
| | 73 | portal = getUtility(ISiteRoot) |
| | 74 | utils = portal.plone_utils |
| | 75 | did = utils.normalizeString(did) |
| | 76 | dtitle = did |
| | 77 | if dfilepath: |
| | 78 | linkpath = '%s/%s' %(dfilepath, did) |
| 52 | | title = id |
| 53 | | self.applyCoreMetadata(objDict, hash, id, path, excludeFromNav, type, title, file=file) |
| 54 | | # If the resource is a document |
| 55 | | else: |
| 56 | | hash = resid |
| 57 | | objDict[hash] = metadata |
| 58 | | excludeFromNav = True |
| 59 | | type = 'Document' |
| 60 | | id = resid |
| 61 | | path = '' |
| 62 | | if orgs.has_key(resid): |
| 63 | | title = orgs[resid] |
| 64 | | else: |
| 65 | | title = id |
| 66 | | self.applyCoreMetadata(objDict, hash, id, path, excludeFromNav, type, title) |
| 67 | | # The resource is a table of contents page. |
| 68 | | elif restype == 'course/x-bb-coursetoc': |
| | 80 | linkpath = did |
| | 81 | binfile = y |
| | 82 | folder_files = source.getFolderFiles(dfilepath) |
| | 83 | if len(folder_files) == 1: |
| | 84 | binfile = folder_files[0] |
| | 85 | entries.append((linkpath, dtitle)) |
| | 86 | self.applyCoreMetadata(objDict[dhash], did, dfilepath, dexcludeFromNav, dtype, dtitle, file=binfile) |
| | 87 | if entries: |
| | 88 | filetoc = bbreader.createTocPage(entries) |
| | 89 | # Handle links |
| | 90 | if metadata.has_key('bbtype') and metadata['bbtype'] == 'Link': |
| 80 | | self.applyCoreMetadata(objDict, hash, id, path, excludeFromNav, type, title) |
| 81 | | |
| 82 | | # Build table of contents pages |
| | 101 | self.applyCoreMetadata(objDict[hash], id, filepath, excludeFromNav, ptype, title) |
| | 102 | elif restype in ['resource/x-bb-document', 'course/x-bb-coursetoc']: |
| | 103 | # Handle normal bb-documents |
| | 104 | hash = resid |
| | 105 | objDict[hash] = metadata |
| | 106 | filepath = '' |
| | 107 | excludeFromNav = True |
| | 108 | ptype = 'Document' |
| | 109 | id = resid + '.html' |
| | 110 | if orgs.has_key(resid): |
| | 111 | title = orgs[resid] |
| | 112 | else: |
| | 113 | title = id |
| | 114 | # It's a folder object: |
| | 115 | isFolder = metadata.has_key('bbtype') and metadata['bbtype'] == 'Folder' |
| | 116 | if isFolder or restype == 'course/x-bb-coursetoc': |
| | 117 | tocpages.append(resid) |
| | 118 | # It's a table of contents object |
| | 119 | if restype == 'course/x-bb-coursetoc': |
| | 120 | if orgs.has_key(resid): |
| | 121 | excludeFromNav = False |
| | 122 | orgstitle = orgs[resid].split('.') |
| | 123 | # Rewrite label tag |
| | 124 | if len(orgstitle) > 1 and orgstitle[-1] == 'label': |
| | 125 | title = re.sub('(((?<=[a-z])[A-Z])|([A-Z](?![A-Z]|$)))', ' \\1', orgs[resid].split('.')[1]) |
| | 126 | text = doctext |
| | 127 | if type(filetoc) == type(u''): |
| | 128 | text += filetoc.encode('utf-8') |
| | 129 | self.applyCoreMetadata(objDict[hash], id, filepath, excludeFromNav, ptype, title, text=text) |
| | 130 | # Build table of contents pages |
| 87 | | met = objDict[titem] |
| 88 | | path = met['path'] |
| 89 | | if path: |
| 90 | | linkpath = '%s/%s' %(path, objDict[titem]['id']) |
| | 135 | if objDict.has_key(titem): |
| | 136 | met = objDict[titem] |
| | 137 | path = met['path'] |
| | 138 | if path: |
| | 139 | linkpath = '%s/%s' %(path, objDict[titem]['id']) |
| | 140 | else: |
| | 141 | linkpath = objDict[titem]['id'] |
| | 142 | entries.append((linkpath, met['title'])) |
| | 143 | if entries: |
| | 144 | if objDict[z].has_key('text') and objDict[z]['text']: |
| | 145 | gtext = objDict[z]['text'] |
| | 146 | if type(gtext) == type(u''): |
| | 147 | gtext = gtext.encode('utf-8') |
| | 148 | objDict[z]['text'] = bbreader.createTocPage(entries).encode('utf-8') + gtext |
| 100 | | def applyCoreMetadata(self, objDict, hash, id, path, excludeFromNav, type, title, file=None, text=None): |
| | 156 | def applyCoreMetadata(self, metadata, id, path, excludeFromNav, type, title, file=None, text=None): |
| 102 | | objDict[hash]['id'] = id |
| 103 | | objDict[hash]['path'] = path |
| 104 | | objDict[hash]['excludeFromNav'] = excludeFromNav |
| 105 | | if not (objDict[hash].has_key('type') and objDict[hash]['type']): |
| 106 | | objDict[hash]['type'] = type |
| 107 | | if not (objDict[hash].has_key('title') and objDict[hash]['title']): |
| 108 | | objDict[hash]['title'] = title |
| | 158 | portal = getUtility(ISiteRoot) |
| | 159 | utils = portal.plone_utils |
| | 160 | metadata['id'] = utils.normalizeString(id) |
| | 161 | metadata['path'] = path |
| | 162 | metadata['excludeFromNav'] = excludeFromNav |
| | 163 | metadata['type'] = type |
| | 164 | if not (metadata.has_key('title') and metadata['title']): |
| | 165 | metadata['title'] = title |
| | 171 | def convertentity(m): |
| | 172 | """Convert a HTML entity into normal string (ISO-8859-1)""" |
| | 173 | if m.group(1)=='#': |
| | 174 | try: |
| | 175 | return chr(int(m.group(2))) |
| | 176 | except ValueError: |
| | 177 | return '&#%s;' % m.group(2) |
| | 178 | try: |
| | 179 | return htmlentitydefs.entitydefs[m.group(2)] |
| | 180 | except KeyError: |
| | 181 | return '&%s;' % m.group(2) |
| | 182 | |
| | 183 | def unquotehtml(s): |
| | 184 | """Convert a HTML quoted string into normal string (ISO-8859-1). |
| | 185 | |
| | 186 | Works with &#XX; and with > etc.""" |
| | 187 | return re.sub(r'&(#?)(.+?);',convertentity,s) |