Commits

Michael Schurter committed abd0631

Remember last chosen lexer and fix paths to use base path

  • Participants
  • Parent commits 4bded1e

Comments (0)

Files changed (3)

File librarypaste/pastebin.py

 lookup = TemplateLookup(directories=[os.path.join(BASE, 'templates')])
 
 
+def get_context():
+    ds = cherrypy.request.app.config['datastore']['datastore']
+    d = {'request': cherrypy.request}
+    return d
+
+
 class PasteViewPage(object):
     def default(self, pasteid=None):
         ds = cherrypy.request.app.config['datastore']['datastore']
-        d = {}
+        context = get_context()
+        print context
         page = lookup.get_template('view.html')
         try:
             paste_data = ds.retrieve(pasteid)
         except:
-            raise cherrypy.NotFound("The paste '%s' could not be found." % pasteid)
+            raise cherrypy.NotFound("The paste '%s' could not be found."
+                    % pasteid)
         if paste_data['type'] == 'file':
             cherrypy.response.headers['Content-Type'] = paste_data['mime']
-            cherrypy.response.headers['Content-Disposition'] = 'inline; filename="%s"' % paste_data['filename']
+            cherrypy.response.headers['Content-Disposition'] = \
+                    'inline; filename="%s"' % paste_data['filename']
             cherrypy.response.headers['filename'] = paste_data['filename']
             return paste_data['data']
 
-        d['linenums'] = '\n'.join([str(x) for x in xrange(1, paste_data['code'].count('\n')+2)])
+        context['linenums'] = '\n'.join(
+                [str(x) for x in xrange(1, paste_data['code'].count('\n')+2)])
         if paste_data['fmt'] == '_':
             lexer = get_lexer_by_name('text')
         else:
             lexer = get_lexer_by_name(paste_data['fmt'])
         htmlformatter = HtmlFormatter(linenos='table')
-        d['code'] = highlight(paste_data['code'], lexer, htmlformatter)
-        d['pasteid'] = pasteid
-        d['plainurl'] = cherrypy.url('/plain/%s' % pasteid)
-        d['homeurl'] = cherrypy.url('/')
-        d['title'] = 'Paste %s%s%s on %s' % (pasteid,
+        context['code'] = highlight(paste_data['code'], lexer, htmlformatter)
+        context['pasteid'] = pasteid
+        context['plainurl'] = cherrypy.url('/plain/%s' % pasteid)
+        context['homeurl'] = cherrypy.url('/')
+        context['title'] = 'Paste %s%s%s on %s' % (pasteid,
             ' (%s)' % paste_data['fmt'] if paste_data['fmt'] != '_' else '',
             ' by %s' % paste_data['nick'] if paste_data['nick'] else '',
         paste_data['time'].strftime('%b %d, %H:%M'))
-        return page.render(**d)
+        return page.render(**context)
     default.exposed = True
 
 class LastPage(object):
 
 class AboutPage(object):
     def index(self):
-        d = {}
+        d = get_context()
         page = lookup.get_template('about.html')
         d['title'] = 'About Library Paste'
         d['about'] = '''
         if cherrypy.request.method == 'POST':
             return self.post(*args, **kwargs)
 
-        d = {}
+        d = get_context()
         page = lookup.get_template('entry.html')
 
         d['title'] = "Library Paste"
 
+        d['fave'] = ('' if not 'paste-lexer' in cherrypy.request.cookie
+                else cherrypy.request.cookie['paste-lexer'].value)
         d['lexers'] = sorted([(l[0], l[1][0]) for l in get_all_lexers()])
         d['pre_nick'] = ('' if not 'paste-nick' in cherrypy.request.cookie
             else cherrypy.request.cookie['paste-nick'].value)
         if nick:
             cherrypy.response.cookie['paste-nick'] = nick
             cherrypy.response.cookie['paste-nick']['expires'] = 60 * 60 * 24 * 30 #store cookies for 30 days
-        
+
+        if fmt:
+            cherrypy.response.cookie['paste-lexer'] = fmt
+            cherrypy.response.cookie['paste-lexer']['expires'] = 60 * 60 * 24 * 30
+
         if makeshort:
             redirid = shortid
             cherrypy.response.cookie['paste-short'] = 1

File librarypaste/templates/base.html

 	<head>
 		<title>${title}</title>
 		<link rel="shortcut icon" href="static/favicon.ico" type="image/x-icon"/>
-		<link rel="icon" href="static/favicon.ico" type="image/x-icon"/>
-        <link rel="stylesheet" href="static/base.css" type="text/css"/>
-        <link rel="stylesheet" href="static/pygments.css" type="text/css"/>
+        <link rel="icon" href="${request.script_name}/static/favicon.ico" type="image/x-icon"/>
+        <link rel="stylesheet" href="${request.script_name}/static/base.css" type="text/css"/>
+        <link rel="stylesheet" href="${request.script_name}/static/pygments.css" type="text/css"/>
         <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
 	</head>
 	<body>
         <div class="header">
-            <a href="/"><img class="logo" src="/static/librarypaste.png" alt="Library Paste" />
+            <a href="${request.script_name}">
+                <img class="logo" src="${request.script_name}/static/librarypaste.png" alt="Library Paste" />
 			Library Paste</a>
-			| <a href="/about">About</a>
+            | <a href="${request.script_name}/about">About</a>
         </div>
 	<div class="main">
 	

File librarypaste/templates/entry.html

     <select name="fmt" id="fmt">
         <option value="_">None (Plain Text)</option>
         % for lex_desc, lex_id in lexers:
+            % if fave and fave == lex_id:
+            <option value="${lex_id}" selected="selected">${lex_desc}</option>
+            % else:
             <option value="${lex_id}">${lex_desc}</option>
+            % endif
         % endfor
     </select>
 	% else: