Permalink
Browse files

Retrieve Github Readme styles when run.

  • Loading branch information...
1 parent fc5363b commit 0310f850ba4750cf584acae052b5fc41b4664d4c @joeyespo committed Dec 1, 2012
Showing with 27 additions and 4 deletions.
  1. +3 −4 grip/default_config.py
  2. +24 −0 grip/server.py
View
@@ -10,7 +10,6 @@
DEBUG = True
DEBUG_GRIP = False
-STYLE_URLS = [
- 'https://a248.e.akamai.net/assets.github.com/assets/github-dd95960936fd09bc718d54eac1f1683ed3376505.css',
- 'https://a248.e.akamai.net/assets.github.com/assets/github2-69752b2a6bc9496df24286132ccbb217ec68581d.css',
-]
+STYLE_URLS = []
+STYLE_URL_SOURCE = 'https://github.com/joeyespo/grip'
+STYLE_URL_RE = '<link.+href=[\'"]?([^\'" >]+)[\'"]?.+media=[\'"]?screen[\'"]?.+rel=[\'"]?stylesheet[\'"]?.+/>'
View
@@ -1,3 +1,5 @@
+import re
+import requests
from flask import Flask
from .renderer import render_page
from .watcher import find_readme, read_file
@@ -16,6 +18,16 @@ def serve(directory='.', readme_file='README', port=None):
app.config.from_pyfile('default_config.py')
app.config.from_pyfile('local_config.py', silent=True)
+ # Get styles from style source
+ @app.before_first_request
+ def retrieve_styles():
+ if not app.config['STYLE_URL_SOURCE'] or not app.config['STYLE_URL_RE']:
+ return
+ styles = _get_styles(app.config['STYLE_URL_SOURCE'], app.config['STYLE_URL_RE'])
+ app.config['STYLE_URLS'] += styles
+ if app.config['DEBUG_GRIP']:
+ print ' * Retrieved %s style URL%s' % (len(styles), '' if len(styles) == 1 else 's')
+
# Set overridden config values
if port is not None:
app.config['PORT'] = port
@@ -27,3 +39,15 @@ def index():
# Run local server
app.run(app.config['HOST'], app.config['PORT'], debug=app.config['DEBUG'], use_reloader=app.config['DEBUG_GRIP'])
+
+
+def _get_styles(source_url, pattern):
+ """Gets the specified resource and parses all styles in the form of the specified pattern."""
+ try:
+ r = requests.get(source_url)
+ if not 200 <= r.status_code < 300:
+ print ' * Warning: retrieving styles gave status code', r.status_code
+ return re.findall(pattern, r.text)
+ except Exception, e:
+ print ' * Error: could not retrieve styles:', str(e)
+ return []

0 comments on commit 0310f85

Please sign in to comment.