Skip to content

Commit

Permalink
Add a handler to serve the pygments CSS, and remove the old crappy way.
Browse files Browse the repository at this point in the history
  • Loading branch information
coderanger committed Feb 17, 2010
1 parent 3ea1c11 commit 3393a63
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 26 deletions.
3 changes: 2 additions & 1 deletion nomic/browser.py
Expand Up @@ -3,6 +3,7 @@
import pygments.lexers
import pygments.formatters

from nomic.chrome import add_stylesheet
from nomic.db import File
from nomic.util import _user

Expand Down Expand Up @@ -62,6 +63,6 @@ def _get_file(self, path, path_segs, file):
lexer = pygments.lexers.guess_lexer_for_filename(file.path, file.data)
formatter = pygments.formatters.get_formatter_by_name('html', linenos='table', lineanchors='line', anchorlinenos=True, nobackground=True)
highlighted = pygments.highlight(file.data, lexer, formatter)
pygments_css = formatter.get_style_defs(' #browser .code')
latest_file = File.from_path(file.path)
add_stylesheet(self.request, '/pygments.css')
self.response.out.write(self.env.get_template('browser_file.html').render(locals()))
12 changes: 12 additions & 0 deletions nomic/chrome.py
@@ -1,4 +1,7 @@
# Copyright 2010 Noah Kantrowitz
from google.appengine.ext import webapp
import pygments
import pygments.formatters

def add_link(req, rel, href, title=None, mimetype=None, classname=None):
link = {
Expand All @@ -23,3 +26,12 @@ def add_script(req, filename, mimetype='text/javascript'):
'mimetype': mimetype,
}
req._head_js.append(js)

class PygmentsHandler(webapp.RequestHandler):

def get(self):
formatter = pygments.formatters.get_formatter_by_name('html', linenos='table', lineanchors='line', anchorlinenos=True, nobackground=True)
self.response.headers['Content-Type'] = 'text/css'
self.response.out.write(formatter.get_style_defs('.code'))


2 changes: 2 additions & 0 deletions nomic/main.py
Expand Up @@ -7,6 +7,7 @@

from nomic.browser import BrowserHandler
from nomic.proposal import CreateProposalHandler, ViewProposalHandler, ListProposalHandler
from nomic.chrome import PygmentsHandler
from nomic.util import _user

class MainHandler(webapp.RequestHandler):
Expand All @@ -30,4 +31,5 @@ def get(self):
('/proposal/(\d+)', ViewProposalHandler),
('/proposal(?:/)?', ListProposalHandler),
('/about', AboutHandler),
('/pygments.css', PygmentsHandler),
]
9 changes: 4 additions & 5 deletions nomic/proposal.py
Expand Up @@ -50,18 +50,16 @@ def _handle_preview(self):
lexer = pygments.lexers.get_lexer_by_name('diff')
formatter = pygments.formatters.get_formatter_by_name('html', nobackground=True)
highlighted = pygments.highlight(diff_data, lexer, formatter)
pygments_css = formatter.get_style_defs(' #proposal .code')
add_stylesheet(self.request, '/pygments.css')
self.response.out.write(self.env.get_template('proposal_preview.html').render(locals()))

def _handle_create(self):
user, user_admin, user_url = _user(self)
path = self.request.get('path')
data = self.request.get('data').replace(' ', '\t')
title = self.request.get('title')
js_files = [
'http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js',
'/htdocs/jquery.tabby.js',
]
add_script(self.request, 'http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js')
add_script(self.request, '/htdocs/jquery.tabby.js')
self.response.out.write(self.env.get_template('proposal_create.html').render(locals()))

def _handle_save(self):
Expand All @@ -84,6 +82,7 @@ def get(self, id):
highlighted = pygments.highlight(prop.diff, lexer, formatter)
pygments_css = formatter.get_style_defs(' #proposal .code')
vote = prop.get_vote(user)
add_stylesheet(self.request, '/pygments.css')
self.response.out.write(self.env.get_template('proposal_view.html').render(locals()))

def post(self, id):
Expand Down
15 changes: 9 additions & 6 deletions nomic/templates/base.html
@@ -1,12 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>{% block title %}PyNomic{% endblock %}</title>
<link rel="stylesheet" type="text/css" href="/htdocs/style.css" media="screen" />
{% for src in js_files %}
<script type="text/javascript" src="{{ src }}"></script>
{% endfor %}
<head>
<title>{% block title %}PyNomic{% endblock %}</title>
<link rel="stylesheet" type="text/css" href="/htdocs/style.css" />
{% for link in request._head_links %}
<link rel="{{ link.rel }}" href="{{ link.href }}" {% if link.mimetype %}type="{{ link.mimetype }}"{% endif %} />
{% endfor %}
{% for js in request._head_js %}
<script type="{{ js.mimetype }}" src="{{ js.href }}"></script>
{% endfor %}
{% block css %}{% endblock %}
</head>
<body>
Expand Down
2 changes: 0 additions & 2 deletions nomic/templates/browser_file.html
Expand Up @@ -9,8 +9,6 @@
#browser_path .root {
color: #555;
}

{{ pygments_css }}
</style>
{% endblock %}

Expand Down
6 changes: 0 additions & 6 deletions nomic/templates/proposal_preview.html
@@ -1,11 +1,5 @@
{% extends "base.html" %}

{% block css %}
<style type="text/css">
{{ pygments_css }}
</style>
{% endblock %}

{% block content %}
<div id="proposal">
<h1>{{ title }}</h1>
Expand Down
6 changes: 0 additions & 6 deletions nomic/templates/proposal_view.html
@@ -1,11 +1,5 @@
{% extends "base.html" %}

{% block css %}
<style type="text/css">
{{ pygments_css }}
</style>
{% endblock %}

{% block content %}
<div id="proposal">
<h1>{{ prop.title }}</h1>
Expand Down

0 comments on commit 3393a63

Please sign in to comment.