# HG changeset patch # User Nicolas M. Thiery # 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/sagenb/notebook/notebook.py +++ b/sagenb/notebook/notebook.py @@ -1068,8 +1068,12 @@ class Notebook(object): elif ext.lower() == '.sws': # An sws file (really a tar.bz2) which defines a worksheet with graphics, etc. W = self._import_worksheet_sws(filename, owner) + elif ext.lower() == '.html': + # An html file, which should contain the static version of + # a sage help page, as generated by Sphinx + W = self._import_worksheet_html(filename, owner) else: - # We only support txt or sws files. + # We only support txt, sws, and html files raise ValueError, "unknown extension '%s'"%ext self.__worksheets[W.filename()] = W return W @@ -1101,7 +1105,7 @@ class Notebook(object): """ # Open the worksheet txt file and load it in. worksheet_txt = open(filename).read() - # Create a new worksheet with the write title and owner. + # Create a new worksheet with the right title and owner. worksheet = self.new_worksheet_with_title_from_text(worksheet_txt, owner) # Set the new worksheet to have the contents specified by that file. worksheet.edit_save(worksheet_txt) @@ -1160,6 +1164,71 @@ class Notebook(object): return worksheet + def _import_worksheet_html(self, filename, owner): + r""" + Import a static html help page generated by Sphinx as a new + worksheet. + + INPUT: + + - ``filename`` - a string; a filename that ends in .txt + + - ``owner`` - a string; the imported worksheet's owner + + OUTPUT: + + - a new instance of Worksheet + + EXAMPLES: + + We write a plain text worksheet to a file and import it + using this function.:: + + sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') + sage: name = tmp_filename() + '.html' + sage: fd = open(name,'w') + sage: fd.write('\n'.join( + ... ['Test Notebook', + ... '', + ... '
',
+            ...            'sage: 1+1',
+            ...            '2',
+            ...          '
', + ... ''])) + sage: fd.close() + sage: W = nb._import_worksheet_html(name, 'admin') + sage: W.name() + u'Test Notebook' + sage: W.owner() + 'admin' + sage: W.cell_list() + sage: cell = W.cell_list()[1] + sage: cell.input_text() + u'1+1' + sage: cell.output_text() + u'
2
' + """ + # Inspired from sagenb.notebook.twist.WorksheetFile.render + doc_page_html = open(filename).read() + from docHTMLProcessor import SphinxHTMLProcessor + # FIXME: does SphinxHTMLProcessor raise an appropriate message + # if the html file does not contain a Sphinx HTML page? + doc_page = SphinxHTMLProcessor().process_doc_html(doc_page_html) + + from twist import extract_title + title = extract_title(doc_page_html).replace('—','--') + doc_page = title + '\nsystem:sage\n\n' + doc_page + + worksheet = self.create_new_worksheet(title, owner) + worksheet.edit_save(doc_page) + + #FIXME: For some reason, an extra cell gets added + #so we remove it here. + cells = worksheet.cell_list() + cells.pop() + + return worksheet + def change_worksheet_name_to_avoid_collision(self, worksheet): """ Change the display name of the worksheet if there is already a diff --git a/sagenb/notebook/twist.py b/sagenb/notebook/twist.py --- a/sagenb/notebook/twist.py +++ b/sagenb/notebook/twist.py @@ -34,7 +34,7 @@ reactor = None ############################################################ -import os, shutil, time +import os, shutil, time, urlparse import bz2 from cgi import escape @@ -446,8 +446,14 @@ class UploadWorksheet(resource.PostableR url = ctx.args['url'][0].strip() dir = '' # we will delete the directory below if it is used if url != '': - # downloading a file from the internet - filename = tmp_filename()+".sws" + # The file will be downloaded from the internet and saved + # to a temporary file with the same extension + path = urlparse.urlparse(url).path + extension = os.path.splitext(path)[1].lower() + if extension not in [".txt", ".sws", ".zip", ".html"]: + # Or shall we try to import the document as an sws in doubt? + return HTMLResponse(stream=message("Unknown worksheet extension: %s. %s" %(extension,backlinks))) + filename = tmp_filename()+extension else: # uploading a file from the user's computer dir = tmp_dir() @@ -480,7 +486,8 @@ class UploadWorksheet(resource.PostableR zip_file = zipfile.ZipFile(filename) sws_file = os.path.join(dir, "tmp.sws") for sws in zip_file.namelist(): - if sws.endswith('.sws'): + extension = os.path.splitext(sws)[1].lower() + if extension in ['.html', '.txt', '.sws']: open(sws_file, 'w').write(zip_file.read(sws)) # 2.6 zip_file.extract(sws, sws_file) W = notebook.import_worksheet(sws_file, self.username) if new_name: