Ticket #10652: trac_10652-upload-static-documentation-as-worksheet-nt.patch
File trac_10652-upload-static-documentation-as-worksheet-nt.patch, 6.4 KB (added by , 11 years ago) |
---|
-
sagenb/notebook/notebook.py
# HG changeset patch # User Nicolas M. Thiery <nthiery@users.sf.net> # Date 1295356578 -3600 # Node ID 6f504ca60bdae1324585b89da892e49c1d20316a # Parent f44547d9b046da50701209175fff1befcbb9d5bd #10652: Add support for uploading static html doc page as a worksheet in the notebook This patch adds (experimental) support for uploading a static html doc page as a worksheet in the notebook. As a side effect, it: - allows for uploading a .txt file from a URL (was broken) - allows for uploading a zip file containing .txt and .html files diff --git a/sagenb/notebook/notebook.py b/sagenb/notebook/notebook.py
a b class Notebook(object): 1068 1068 elif ext.lower() == '.sws': 1069 1069 # An sws file (really a tar.bz2) which defines a worksheet with graphics, etc. 1070 1070 W = self._import_worksheet_sws(filename, owner) 1071 elif ext.lower() == '.html': 1072 # An html file, which should contain the static version of 1073 # a sage help page, as generated by Sphinx 1074 W = self._import_worksheet_html(filename, owner) 1071 1075 else: 1072 # We only support txt or sws files.1076 # We only support txt, sws, and html files 1073 1077 raise ValueError, "unknown extension '%s'"%ext 1074 1078 self.__worksheets[W.filename()] = W 1075 1079 return W … … class Notebook(object): 1101 1105 """ 1102 1106 # Open the worksheet txt file and load it in. 1103 1107 worksheet_txt = open(filename).read() 1104 # Create a new worksheet with the writetitle and owner.1108 # Create a new worksheet with the right title and owner. 1105 1109 worksheet = self.new_worksheet_with_title_from_text(worksheet_txt, owner) 1106 1110 # Set the new worksheet to have the contents specified by that file. 1107 1111 worksheet.edit_save(worksheet_txt) … … class Notebook(object): 1160 1164 1161 1165 return worksheet 1162 1166 1167 def _import_worksheet_html(self, filename, owner): 1168 r""" 1169 Import a static html help page generated by Sphinx as a new 1170 worksheet. 1171 1172 INPUT: 1173 1174 - ``filename`` - a string; a filename that ends in .txt 1175 1176 - ``owner`` - a string; the imported worksheet's owner 1177 1178 OUTPUT: 1179 1180 - a new instance of Worksheet 1181 1182 EXAMPLES: 1183 1184 We write a plain text worksheet to a file and import it 1185 using this function.:: 1186 1187 sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') 1188 sage: name = tmp_filename() + '.html' 1189 sage: fd = open(name,'w') 1190 sage: fd.write('\n'.join( 1191 ... ['<html><head><title>Test Notebook</title></head>', 1192 ... '<body>', 1193 ... '<div class="highlight-python"><div class="highlight"><pre>', 1194 ... '<span class="gp">sage: </span><span class="mi">1</span><span class="o">+</span><span class="mi">1</span>', 1195 ... '<span class="go">2</span>', 1196 ... '</pre></div></div>', 1197 ... '</body></html>'])) 1198 sage: fd.close() 1199 sage: W = nb._import_worksheet_html(name, 'admin') 1200 sage: W.name() 1201 u'Test Notebook' 1202 sage: W.owner() 1203 'admin' 1204 sage: W.cell_list() 1205 sage: cell = W.cell_list()[1] 1206 sage: cell.input_text() 1207 u'1+1' 1208 sage: cell.output_text() 1209 u'<pre class="shrunk">2</pre>' 1210 """ 1211 # Inspired from sagenb.notebook.twist.WorksheetFile.render 1212 doc_page_html = open(filename).read() 1213 from docHTMLProcessor import SphinxHTMLProcessor 1214 # FIXME: does SphinxHTMLProcessor raise an appropriate message 1215 # if the html file does not contain a Sphinx HTML page? 1216 doc_page = SphinxHTMLProcessor().process_doc_html(doc_page_html) 1217 1218 from twist import extract_title 1219 title = extract_title(doc_page_html).replace('—','--') 1220 doc_page = title + '\nsystem:sage\n\n' + doc_page 1221 1222 worksheet = self.create_new_worksheet(title, owner) 1223 worksheet.edit_save(doc_page) 1224 1225 #FIXME: For some reason, an extra cell gets added 1226 #so we remove it here. 1227 cells = worksheet.cell_list() 1228 cells.pop() 1229 1230 return worksheet 1231 1163 1232 def change_worksheet_name_to_avoid_collision(self, worksheet): 1164 1233 """ 1165 1234 Change the display name of the worksheet if there is already a -
sagenb/notebook/twist.py
diff --git a/sagenb/notebook/twist.py b/sagenb/notebook/twist.py
a b reactor = None 34 34 35 35 ############################################################ 36 36 37 import os, shutil, time 37 import os, shutil, time, urlparse 38 38 import bz2 39 39 from cgi import escape 40 40 … … class UploadWorksheet(resource.PostableR 446 446 url = ctx.args['url'][0].strip() 447 447 dir = '' # we will delete the directory below if it is used 448 448 if url != '': 449 # downloading a file from the internet 450 filename = tmp_filename()+".sws" 449 # The file will be downloaded from the internet and saved 450 # to a temporary file with the same extension 451 path = urlparse.urlparse(url).path 452 extension = os.path.splitext(path)[1].lower() 453 if extension not in [".txt", ".sws", ".zip", ".html"]: 454 # Or shall we try to import the document as an sws in doubt? 455 return HTMLResponse(stream=message("Unknown worksheet extension: %s. %s" %(extension,backlinks))) 456 filename = tmp_filename()+extension 451 457 else: 452 458 # uploading a file from the user's computer 453 459 dir = tmp_dir() … … class UploadWorksheet(resource.PostableR 480 486 zip_file = zipfile.ZipFile(filename) 481 487 sws_file = os.path.join(dir, "tmp.sws") 482 488 for sws in zip_file.namelist(): 483 if sws.endswith('.sws'): 489 extension = os.path.splitext(sws)[1].lower() 490 if extension in ['.html', '.txt', '.sws']: 484 491 open(sws_file, 'w').write(zip_file.read(sws)) # 2.6 zip_file.extract(sws, sws_file) 485 492 W = notebook.import_worksheet(sws_file, self.username) 486 493 if new_name: