/usr/lib/python2.7/dist-packages/sagenb/notebook/template.py is in python-sagenb 1.0.1+ds1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | # -*- coding: utf-8 -*-
"""
HTML Templating for the Notebook
AUTHORS:
- Bobby Moretti (2007-07-18): initial version
- Timothy Clemans and Mike Hansen (2008-10-27): major update
"""
from __future__ import absolute_import
#############################################################################
# Copyright (C) 2007 William Stein <wstein@gmail.com>
# Distributed under the terms of the GNU General Public License (GPL)
# The full text of the GPL is available at:
# http://www.gnu.org/licenses/
#############################################################################
import jinja2
import os, re, sys
from sagenb.misc.misc import SAGE_VERSION, DATA
from flask_babel import gettext, ngettext, lazy_gettext
from flask import current_app as app
if 'SAGENB_TEMPLATE_PATH' in os.environ:
if not os.path.isdir(os.environ['SAGENB_TEMPLATE_PATH']):
raise ValueError("Enviromental variable SAGENB_TEMPLATE_PATH points to\
a non-existant directory")
TEMPLATE_PATH = os.environ['SAGENB_TEMPLATE_PATH']
else:
TEMPLATE_PATH = os.path.join(DATA, 'sage')
css_illegal_re = re.compile(r'[^-A-Za-z_0-9]')
def css_escape(string):
r"""
Returns a string with all characters not legal in a css name
replaced with hyphens (-).
INPUT:
- ``string`` -- the string to be escaped.
EXAMPLES::
sage: from sagenb.notebook.template import css_escape
sage: css_escape('abcd')
'abcd'
sage: css_escape('12abcd')
'12abcd'
sage: css_escape(r'\'"abcd\'"')
'---abcd---'
sage: css_escape('my-invalid/identifier')
'my-invalid-identifier'
sage: css_escape(r'quotes"mustbe!escaped')
'quotes-mustbe-escaped'
"""
return css_illegal_re.sub('-', string)
def prettify_time_ago(t):
"""
Converts seconds to a meaningful string.
INPUT
- t -- time in seconds
"""
if t < 60:
s = int(t)
return ngettext('%(num)d second', '%(num)d seconds', s)
if t < 3600:
m = int(t/60)
return ngettext('%(num)d minute', '%(num)d minutes', m)
if t < 3600*24:
h = int(t/3600)
return ngettext('%(num)d hour', '%(num)d hours', h)
d = int(t/(3600*24))
return ngettext('%(num)d day', '%(num)d days', d)
def clean_name(name):
"""
Converts a string to a safe/clean name by converting non-alphanumeric characters to underscores.
INPUT:
- name -- a string
EXAMPLES::
sage: from sagenb.notebook.template import clean_name
sage: print(clean_name('this!is@bad+string'))
this_is_bad_string
"""
return ''.join([x if x.isalnum() else '_' for x in name])
def template(filename, **user_context):
"""
Returns HTML, CSS, etc., for a template file rendered in the given
context.
INPUT:
- ``filename`` - a string; the filename of the template relative
to ``sagenb/data/templates``
- ``user_context`` - a dictionary; the context in which to evaluate
the file's template variables
OUTPUT:
- a string - the rendered HTML, CSS, etc.
EXAMPLES::
sage: from sagenb.flask_version import base # random output -- depends on warnings issued by other sage packages
sage: app = base.create_app(tmp_dir(ext='.sagenb'))
sage: ctx = app.app_context()
sage: ctx.push()
sage: from sagenb.notebook.template import template
sage: s = template(os.path.join('html', 'yes_no.html')); type(s)
<type 'unicode'>
sage: 'Yes' in s
True
sage: u = unicode('Are Gröbner bases awesome?','utf-8')
sage: s = template(os.path.join('html', 'yes_no.html'), message=u)
sage: 'Gr\xc3\xb6bner' in s.encode('utf-8')
True
"""
from sagenb.notebook.notebook import MATHJAX, JEDITABLE_TINYMCE
from .misc import notebook
#A dictionary containing the default context
default_context = {'sitename': gettext('Sage Notebook'),
'sage_version': SAGE_VERSION,
'MATHJAX': MATHJAX,
'gettext': gettext,
'JEDITABLE_TINYMCE': JEDITABLE_TINYMCE,
'conf': notebook.conf() if notebook else None}
try:
tmpl = app.jinja_env.get_template(filename)
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
|