diff -r 2444099672b2 sagenb/data/sage/html/base.html --- a/sagenb/data/sage/html/base.html Thu Jan 07 03:26:45 2010 +0800 +++ b/sagenb/data/sage/html/base.html Thu Jan 07 04:11:08 2010 +0800 @@ -1,4 +1,4 @@ -{%- macro render_title -%}{% block title %}{% endblock %}{%- endmacro -%} +{%- macro render_title() -%}{% block title %}{% endblock %}{%- endmacro -%} diff -r 2444099672b2 sagenb/data/sage/html/notebook/cell.html --- a/sagenb/data/sage/html/notebook/cell.html Thu Jan 07 03:26:45 2010 +0800 +++ b/sagenb/data/sage/html/notebook/cell.html Thu Jan 07 04:11:08 2010 +0800 @@ -12,12 +12,14 @@ print or not. #} {% if do_print %} - {% set wrap = 68 %} - {% set div_wrap = 68 %} + {% set wrap_ = 68 %} + {% set div_wrap_ = 68 %} +{% else %} + {% set wrap_ = wrap %} + {% set div_wrap_ = div_wrap %} {% endif %} {% set cell_cls = "cell_evaluated" if cell.evaluated() or do_print else "cell_not_evaluated" %} - -{% if div_wrap %} +{% if div_wrap_ %}
{% endif %} @@ -50,7 +52,7 @@ onFocus="cell_focused(this, {{ cell.id() }}); return true;">{{ cell.input_text().rstrip() }} evaluate @@ -79,7 +81,7 @@ {% if cell.introspect() %} {{ cell.output_text(0, html=true) }} {% else %} - {{ cell.output_text(wrap, html=true) }} + {{ cell.output_text(wrap_, html=true) }} {% endif %}
{% if not do_print %} @@ -99,7 +101,7 @@
{% endif %} -{% if div_wrap %} +{% if div_wrap_ %} {% endif %} diff -r 2444099672b2 sagenb/interfaces/expect.py --- a/sagenb/interfaces/expect.py Thu Jan 07 03:26:45 2010 +0800 +++ b/sagenb/interfaces/expect.py Thu Jan 07 04:11:08 2010 +0800 @@ -241,11 +241,11 @@ # The magic comment at the very start of the file allows utf8 characters. open(self._filename,'w').write( '# -*- coding: utf_8 -*-\nimport sys;sys.ps1="%s";print "START%s"\n'%( - self._prompt, self._number) + displayhook_hack(string)) + self._prompt, self._number) + displayhook_hack(string).encode('utf-8', 'ignore')) try: self._expect.sendline('\nimport os;os.chdir("%s");\nexecfile("%s")'%( remote, sage_input)) - except OSError, msg: + except OSError as msg: self._is_computing = False self._so_far = str(msg) diff -r 2444099672b2 sagenb/misc/sageinspect.py --- a/sagenb/misc/sageinspect.py Thu Jan 07 03:26:45 2010 +0800 +++ b/sagenb/misc/sageinspect.py Thu Jan 07 04:11:08 2010 +0800 @@ -483,8 +483,11 @@ if r is None: return '' - - s = format(str(r), embedded=EMBEDDED_MODE) + if isinstance(r, unicode): + r = r.encode('utf-8', 'ignore') + else: + r = str(r) + s = format(r, embedded=EMBEDDED_MODE) # If there is a Cython embedded position, it needs to be stripped pos = _extract_embedded_position(s) diff -r 2444099672b2 sagenb/misc/support.py --- a/sagenb/misc/support.py Thu Jan 07 03:26:45 2010 +0800 +++ b/sagenb/misc/support.py Thu Jan 07 04:11:08 2010 +0800 @@ -331,7 +331,7 @@ filename = sageinspect.sage_getfile(obj) try: lines, lineno = sageinspect.sage_getsourcelines(obj, is_binary=False) - except IOError, msg: + except IOError as msg: return html_markup(str(msg)) src = indent.join(lines) src = indent + format_src(src) @@ -467,6 +467,8 @@ if dir: if hasattr(system.__class__, 'chdir'): system.chdir(dir) + if isinstance(cmd, unicode): + cmd = cmd.encode('utf-8', 'ignore') return system.eval(cmd, sage_globals, locals = sage_globals) ###################################################################### diff -r 2444099672b2 sagenb/notebook/cell.py --- a/sagenb/notebook/cell.py Thu Jan 07 03:26:45 2010 +0800 +++ b/sagenb/notebook/cell.py Thu Jan 07 04:11:08 2010 +0800 @@ -91,6 +91,8 @@ sage: C == loads(dumps(C)) True """ + if isinstance(text, str): + text = text.decode('utf-8', 'ignore') self.__id = int(id) self.__text = text self.__worksheet = worksheet @@ -136,6 +138,8 @@ sage: C TextCell 0: 3+2 """ + if isinstance(input_text, str): + inpit_text = input_text.decode('utf-8', str) self.__text = input_text def set_worksheet(self, worksheet, id=None): @@ -297,8 +301,17 @@ sage: C == loads(dumps(C)) True """ + if isinstance(out, str): + out = out.encode('utf-8', 'ignore') + else: + out = unicode(out) + if isinstance(input, str): + input = input.encode('utf-8', 'ignore') + else: + input = unicode(input) + self.__id = int(id) - self.__out = str(out).replace('\r','') + self.__out = out.replace('\r','') self.__worksheet = worksheet self.__interrupted = False self.__completions = False @@ -355,8 +368,8 @@ sage: C Cell 0; in=2+3, out= """ - self.__out = '' - self.__out_html = '' + self.__out = u'' + self.__out_html = u'' self.__evaluated = False def evaluated(self): @@ -555,7 +568,7 @@ sage: nb.delete() """ if self.is_interactive_cell(): - self.__out_html = "" + self.__out_html = u"" else: self.__out_html = self.files_html(output) @@ -735,9 +748,13 @@ """ if ncols == 0: ncols = self.word_wrap_cols() - s = '' - + s = u'' + + if isinstance(self.__in, str): + self.__in = self.__in.decode('utf-8', 'ignore') + input_lines = self.__in + pr = 'sage: ' if prompts: @@ -813,7 +830,7 @@ '{{{id=0|\n2+3\n///\n5\n}}}' """ s = self.plain_text(ncols, prompts, max_out) - return '{{{id=%s|\n%s\n}}}'%(self.id(), s) + return u'{{{id=%s|\n%s\n}}}'%(self.id(), s) def is_last(self): """ @@ -997,6 +1014,9 @@ sage: nb.delete() """ # Stuff to deal with interact + if isinstance(input, str): + input = input.decode('utf-8', 'ignore') + if input.startswith('%__sage_interact__'): self.interact = input[len('%__sage_interact__')+1:] self.__version = self.version() + 1 @@ -1184,6 +1204,8 @@ sage: C.changed_input_text() '3+3' """ + if isinstance(new_text,str): + new_text = new_text.decode('utf-8', 'ignore') self.__changed_input = new_text self.__in = new_text @@ -1200,9 +1222,13 @@ sage: len(C.plain_text()) 12 """ + if isinstance(output, str): + output = output.decode('utf-8', 'ignore') + if isinstance(html, str): + html = html.decode('utf-8', 'ignore') if output.count('') > 1: - html = '

WARNING: multiple @interacts in one cell disabled (not yet implemented).

' - output = '' + html = u'

WARNING: multiple @interacts in one cell disabled (not yet implemented).

' + output = u'' # In interacting mode, we just save the computed output # (do not overwrite). @@ -1222,7 +1248,7 @@ url = "" if not self.computing(): file = os.path.join(self.directory(), "full_output.txt") - open(file,"w").write(output) + open(file,"w").write(output.encode('utf-8', 'ignore')) url = "full_output.txt"%( self.url_to_self()) html+="
" + url @@ -1348,13 +1374,16 @@ # during interact. return '' + if isinstance(self.__out, str): + self.__out = self.__out.decode('utf-8', 'ignore') + is_interact = self.is_interactive_cell() if is_interact and ncols == 0: if 'Traceback (most recent call last)' in self.__out: s = self.__out.replace('cell-interact','') is_interact=False else: - return '

Click to the left again to hide and once more to show the dynamic interactive window

' + return u'

Click to the left again to hide and once more to show the dynamic interactive window

' else: s = self.__out @@ -1502,6 +1531,8 @@ sage: W.quit() sage: nb.delete() """ + if isinstance(html, str): + html = html.decode('utf-8', 'ignore') self.__introspect_html = html self.introspection_status = 'done' @@ -1542,8 +1573,8 @@ try: return self.__introspect_html except AttributeError: - self.__introspect_html = '' - return '' + self.__introspect_html = u'' + return u'' def introspect(self): """ @@ -1908,6 +1939,10 @@ files = '' else: files = (' '*3).join(files) + if isinstance(files, str): + files = files.decode('utf-8', 'ignore') + if isinstance(images, str): + iamges = images.decode('utf-8', 'ignore') return images + files ######## diff -r 2444099672b2 sagenb/notebook/notebook.py --- a/sagenb/notebook/notebook.py Thu Jan 07 03:26:45 2010 +0800 +++ b/sagenb/notebook/notebook.py Thu Jan 07 04:11:08 2010 +0800 @@ -1274,7 +1274,7 @@ return template(os.path.join("html", "notebook", "worksheet_revision_list.html"), data = data, worksheet = worksheet, - worksheet_filename = worksheet.filename(), + notebook = self, username = username) @@ -1346,7 +1346,7 @@ return template(os.path.join("html", "notebook", "worksheet_share.html"), worksheet = worksheet, - worksheet_filename = worksheet.filename(), + notebook = self, username = username, other_users = other_users) def html_download_or_delete_datafile(self, ws, username, filename): @@ -1383,7 +1383,7 @@ text_file_content = open(os.path.join(ws.data_directory(), filename)).read() return template(os.path.join("html", "notebook", "download_or_delete_datafile.html"), - worksheet = ws, worksheet_filename = ws.filename(), + worksheet = ws, notebook = self, username = username, filename_ = filename, file_is_image = file_is_image, @@ -1503,7 +1503,7 @@ return template(os.path.join("html", "notebook", "plain_text_window.html"), worksheet = worksheet, - worksheet_filename = worksheet.filename(), + notebook = self, username = username, plain_text = plain_text, JSMATH = JSMATH, JEDITABLE_TINYMCE = JEDITABLE_TINYMCE) @@ -1531,7 +1531,7 @@ return template(os.path.join("html", "notebook", "edit_window.html"), worksheet = worksheet, - worksheet_filename = worksheet.filename(), + notebook = self, username = username) def html_beforepublish_window(self, worksheet, username): @@ -1568,9 +1568,8 @@ """ return template(os.path.join("html", "notebook", "beforepublish_window.html"), worksheet = worksheet, - worksheet_filename = worksheet.filename(), - username = username, - JSMATH = JSMATH, JEDITABLE_TINYMCE = JEDITABLE_TINYMCE) + notebook = self, + username = username) def html_afterpublish_window(self, worksheet, username, url, dtime): r""" @@ -1597,9 +1596,8 @@ return template(os.path.join("html", "notebook", "afterpublish_window.html"), worksheet = worksheet, - worksheet_filename = worksheet.filename(), - username = username, url = url, time = time, - JSMATH = JSMATH, JEDITABLE_TINYMCE = JEDITABLE_TINYMCE) + notebook = self, + username = username, url = url, time = time) def html_upload_data_window(self, ws, username): r""" diff -r 2444099672b2 sagenb/notebook/template.py --- a/sagenb/notebook/template.py Thu Jan 07 03:26:45 2010 +0800 +++ b/sagenb/notebook/template.py Thu Jan 07 04:11:08 2010 +0800 @@ -15,9 +15,7 @@ # http://www.gnu.org/licenses/ ############################################################################# -import jinja - -from jinja.filters import stringfilter, simplefilter +import jinja2 import os, re, sys @@ -27,11 +25,10 @@ TEMPLATE_PATH = os.path.join(DATA, 'sage') -env = jinja.Environment(loader=jinja.FileSystemLoader(TEMPLATE_PATH)) +env = jinja2.Environment(loader=jinja2.FileSystemLoader(TEMPLATE_PATH)) css_illegal_re = re.compile(r'[^-A-Za-z_0-9]') -@stringfilter def css_escape(string): r""" Returns a string with all characters not legal in a css name @@ -43,32 +40,16 @@ EXAMPLES:: - sage: from sagenb.notebook.template import env, css_escape - sage: escaper = css_escape() - sage: print(escaper(env, {}, '12abcd')) - 12abcd - sage: print(escaper(env, {}, 'abcd')) - abcd - sage: print(escaper(env, {}, r'\'"abcd\'"')) - ---abcd--- - sage: print(escaper(env, {}, 'my-invalid/identifier')) - my-invalid-identifier - sage: print(escaper(env, {}, r'quotes"mustbe!escaped')) - quotes-mustbe-escaped - - The following doctests originally accompanied #7269's support for - Jinja2. - - sage: from sagenb.notebook.template import css_escape # not tested - sage: css_escape('abcd') # not tested + sage: from sagenb.notebook.template import css_escape + sage: css_escape('abcd') 'abcd' - sage: css_escape('12abcd') # not tested + sage: css_escape('12abcd') '12abcd' - sage: css_escape(r'\'"abcd\'"') # not tested + sage: css_escape(r'\'"abcd\'"') '---abcd---' - sage: css_escape('my-invalid/identifier') # not tested + sage: css_escape('my-invalid/identifier') 'my-invalid-identifier' - sage: css_escape(r'quotes"mustbe!escaped') # not tested + sage: css_escape(r'quotes"mustbe!escaped') 'quotes-mustbe-escaped' """ return css_illegal_re.sub('-', string) @@ -119,11 +100,11 @@ return ''.join([x if x.isalnum() else '_' for x in name]) env.filters['css_escape'] = css_escape -env.filters['number_of_rows'] = simplefilter(number_of_rows) -env.filters['clean_name'] = stringfilter(clean_name) -env.filters['prettify_time_ago'] = simplefilter(prettify_time_ago) -env.filters['math_parse'] = stringfilter(math_parse) -env.filters['max'] = simplefilter(max) +env.filters['number_of_rows'] = number_of_rows +env.filters['clean_name'] = clean_name +env.filters['prettify_time_ago'] = prettify_time_ago +env.filters['math_parse'] = math_parse +env.filters['max'] = max def template(filename, **user_context): """ @@ -164,9 +145,10 @@ 'conf': notebook.conf() if notebook else None} try: tmpl = env.get_template(filename) - except jinja.exceptions.TemplateNotFound: + except jinja2.exceptions.TemplateNotFound: return "Notebook Bug -- missing template %s"%filename context = dict(default_context) context.update(user_context) r = tmpl.render(**context) return r + diff -r 2444099672b2 sagenb/notebook/twist.py --- a/sagenb/notebook/twist.py Thu Jan 07 03:26:45 2010 +0800 +++ b/sagenb/notebook/twist.py Thu Jan 07 04:11:08 2010 +0800 @@ -82,7 +82,14 @@ SEP = '___S_A_G_E___' def encode_list(v): - return SEP.join([str(x) for x in v]) + seq = [] + for x in v: + if isinstance(x, unicode): + x = x.encode('utf-8', 'ignore') + else: + x = str(x) + seq.append(x) + return SEP.join(seq) @@ -1090,9 +1097,11 @@ def render(self, ctx): id = self.id(ctx) if not ctx.args.has_key('input'): - input = '' + input = u'' else: input = ctx.args['input'][0] + if isinstance(input, str): + input = input.decode('utf-8') cell = self.worksheet.new_cell_before(id, input=input) self.worksheet.increase_state_number() @@ -1109,6 +1118,8 @@ input = '' else: input = ctx.args['input'][0] + if isinstance(input, str): + input = input.decode('utf-8') cell = self.worksheet.new_text_cell_before(id, input=input) s = encode_list([cell.id(), cell.html(editing=True), id]) @@ -1125,6 +1136,8 @@ input = '' else: input = ctx.args['input'][0] + if isinstance(input, str): + input = input.decode('utf-8') cell = self.worksheet.new_cell_after(id, input=input) s = encode_list([cell.id(), cell.html(div_wrap=False), id]) return HTMLResponse(stream = s) @@ -1139,6 +1152,8 @@ input = '' else: input = ctx.args['input'][0] + if isinstance(input, str): + input = input.decode('utf-8') cell = self.worksheet.new_text_cell_after(id, input=input) s = encode_list([cell.id(), cell.html(editing=True), id]) @@ -1249,10 +1264,13 @@ def render(self, ctx): id = self.id(ctx) if not ctx.args.has_key('input'): - input_text = '' + input_text = u'' else: input_text = ctx.args['input'][0] input_text = input_text.replace('\r\n', '\n') # DOS + if isinstance(input_text, str): + input_text = input_text.decode('utf-8') + W = self.worksheet W.increase_state_number() diff -r 2444099672b2 sagenb/notebook/worksheet.py --- a/sagenb/notebook/worksheet.py Thu Jan 07 03:26:45 2010 +0800 +++ b/sagenb/notebook/worksheet.py Thu Jan 07 04:11:08 2010 +0800 @@ -714,7 +714,9 @@ 'A renamed worksheet' """ if len(name.strip()) == 0: - name = 'Untitled' + name = u'Untitled' + if isinstance(name, str): + name = name.decode('utf-8', 'ignore') self.__name = name def set_filename_without_owner(self, nm): @@ -1912,8 +1914,8 @@ if os.path.exists(worksheet_html) and open(worksheet_html).read() == E: # we already wrote it out... return - open(filename, 'w').write(bz2.compress(E)) - open(worksheet_html, 'w').write(self.body()) + open(filename, 'w').write(bz2.compress(E.encode('utf-8', 'ignore'))) + open(worksheet_html, 'w').write(self.body().encode('utf-8', 'ignore')) self.limit_snapshots() try: X = self.__saved_by_info @@ -3435,7 +3437,7 @@ """ # The extra newline below is necessary, since otherwise source # code introspection doesn't include the last line. - return 'open("%s","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("%s"),globals())+"\\n"); execfile(os.path.abspath("%s"))'%(CODE_PY, base64.b64encode(s), CODE_PY) + return 'open("%s","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("%s"),globals())+"\\n"); execfile(os.path.abspath("%s"))'%(CODE_PY, base64.b64encode(s.encode('utf-8', 'ignore')), CODE_PY) ########################################################## # Loading and attaching files @@ -3622,7 +3624,7 @@ os.makedirs(code) spyx = os.path.abspath(os.path.join(code, 'sage%s.spyx'%id)) if not (os.path.exists(spyx) and open(spyx).read() == cmd): - open(spyx,'w').write(cmd) + open(spyx,'w').write(cmd.encode('utf-8', 'ignore')) return '_support_.cython_import_all("%s", globals())'%spyx def check_for_system_switching(self, input, cell): diff -r 2444099672b2 sagenb/storage/filesystem_storage.py --- a/sagenb/storage/filesystem_storage.py Thu Jan 07 03:26:45 2010 +0800 +++ b/sagenb/storage/filesystem_storage.py Thu Jan 07 04:11:08 2010 +0800 @@ -291,7 +291,7 @@ # only save if loaded # todo -- add check if changed filename = self._worksheet_html_filename(username, id_number) - open(self._abspath(filename),'w').write(worksheet.body()) + open(self._abspath(filename),'w').write(worksheet.body().encode('utf-8', 'ignore')) def load_worksheet(self, username, id_number): """ @@ -339,7 +339,8 @@ if title: # change the title basic['name'] = title - + if isinstance(basic['name'], unicode): + basic['name'] = basic['name'].encode('utf-8', 'ignore') # Remove metainformation that perhaps shouldn't be distributed for k in ['owner', 'ratings', 'worksheet_that_was_published', 'viewers', 'tags', 'published_id_number', 'collaborators', 'auto_publish']: @@ -397,7 +398,7 @@ worksheet_txt = members[0].name W = self.load_worksheet(username, id_number) - W.edit_save_old_format(T.extractfile(worksheet_txt).read()) + W.edit_save_old_format(T.extractfile(worksheet_txt).read().decode('utf-8', 'ignore')) # '/' is right, since old worksheets always unix dir = worksheet_txt.split('/')[0]