Permalink
Browse files

Working application.

  • Loading branch information...
1 parent 9bc5190 commit 115e9cbc54372083691eaa1363dc8908441ae9c1 @joeyespo committed Nov 17, 2012
Showing with 167 additions and 0 deletions.
  1. +14 −0 grip/__init__.py
  2. +13 −0 grip/command.py
  3. +15 −0 grip/config.py
  4. +19 −0 grip/renderer.py
  5. +25 −0 grip/server.py
  6. +15 −0 grip/templates/base.html
  7. +25 −0 grip/templates/index.html
  8. +15 −0 grip/watcher.py
  9. +3 −0 requirements.txt
  10. +23 −0 setup.py
View
@@ -0,0 +1,14 @@
+"""\
+Grip
+
+Preview your readme files with the Github mockup renderers.
+"""
+
+import command
+from .server import serve
+from .renderer import render_content, render_page
+from .watcher import find_readme
+
+__all__ = ['command', 'serve', 'render_content', 'render_page', 'find_readme']
+__version__ = '0.1'
+__description__ = '\n\n'.join(__doc__.split('\n\n')[1:])
View
@@ -0,0 +1,13 @@
+from .server import serve
+
+
+def main():
+ """The entry point of the command-line interface."""
+
+ # TODO: process arguments
+
+ try:
+ serve()
+ except ValueError, ex:
+ # Show input error
+ print 'Error:', ex
View
@@ -0,0 +1,15 @@
+"""\
+Default Config
+
+Do NOT change the values here.
+Override them using command-line arguments or with a local_config.py instead.
+"""
+
+HOST = 'localhost'
+PORT = 8080
+DEBUG = False
+
+STYLE_URLS = [
+ 'https://a248.e.akamai.net/assets.github.com/assets/github-26581c8456a7088e423db87d6ed68794e9042e29.css',
+ 'https://a248.e.akamai.net/assets.github.com/assets/github2-38ac9c88be26119b09e2a8dbf21887c7e34791e1.css',
+]
View
@@ -0,0 +1,19 @@
+from .config import STYLE_URLS
+from jinja2 import Environment, PackageLoader
+import requests
+
+
+env = Environment(loader=PackageLoader('grip', 'templates'))
+index_template = env.get_template('index.html')
+
+
+def render_content(text):
+ """Renders the specified markup."""
+ headers = {'content-type': 'text/plain'}
+ r = requests.post('https://api.github.com/markdown/raw', headers=headers, data=text)
+ return r.text
+
+
+def render_page(text, filename=None):
+ """Renders the specified markup text to an HTML page."""
+ return index_template.render(content=render_content(text), filename=filename, style_urls=STYLE_URLS)
View
@@ -0,0 +1,25 @@
+from flask import Flask
+from .renderer import render_page
+from .watcher import find_readme, read_file
+
+
+def serve(directory='.', readme_file='README'):
+ """Starts a server to render the readme from the specified directory."""
+
+ # Get the README filename
+ filename = find_readme(directory, readme_file)
+ if not filename:
+ raise ValueError('No %s file found at %s' % ('README' if readme_file == 'README' else repr(readme_file), repr(directory)))
+
+ # Flask application
+ app = Flask('grip')
+ app.config.from_pyfile('config.py')
+ app.config.from_pyfile('local_config.py', silent=True)
+
+ # Views
+ @app.route('/')
+ def index():
+ return render_page(read_file(filename), filename)
+
+ # Run local server
+ app.run(app.config['HOST'], app.config['PORT'], debug=True, use_reloader=app.config['DEBUG_GRIP'])
View
@@ -0,0 +1,15 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+ <meta charset="utf-8" />
+ <title>{% block title %}Grip{% endblock %}</title>
+ {% block styles %}{% endblock %}
+ {% block priority_scripts %}{% endblock %}
+</head>
+<body>
+ <div class="page">
+ {% block page %}{% endblock %}
+ </div>
+ {% block scripts %}{% endblock %}
+</body>
+</html>
View
@@ -0,0 +1,25 @@
+{% extends "base.html" %}
+
+{% block title %}Grip{% if filename %} - {% endif %}{{ filename or '' }}{% endblock %}
+
+{% block styles %}
+ {% for style_url in style_urls %}
+ <link rel="stylesheet" href="{{ style_url }}" />
+ {% endfor %}
+ <style>
+ .previewPage {
+ margin: 64px auto;
+ width: 920px;
+ }
+ </style>
+{% endblock %}
+
+{% block page %}
+ <div class="previewPage">
+ <div id="readme" class="announce md">
+ <article class="markdown-body entry-content">
+ {{ content|safe }}
+ </article>
+ </div>
+ </div>
+{% endblock %}
View
@@ -0,0 +1,15 @@
+import os
+
+
+def find_readme(directory='.', readme_file='README'):
+ """Returns the readme filename at the specified directory, or None."""
+ for filename in os.listdir(directory):
+ if os.path.splitext(filename.lower())[0] == readme_file.lower():
+ return os.path.join(os.path.abspath(directory), filename)
+ return None
+
+
+def read_file(filename):
+ """Reads the contents of the specified file."""
+ with open(filename) as f:
+ return f.read()
View
@@ -0,0 +1,3 @@
+flask>=0.8
+jinja2>=2.6
+requests>=0.14
View
@@ -0,0 +1,23 @@
+from setuptools import setup, find_packages
+import grip as package
+
+
+def read(fname):
+ import os
+ with open(os.path.join(os.path.dirname(__file__), fname)) as f:
+ return f.read()
+
+
+setup(
+ name=package.__name__,
+ author='Joe Esposito',
+ author_email='joe@joeyespo.com',
+ url='http://github.com/joeyespo/grip',
+ license='MIT',
+ version=package.__version__,
+ description=package.__description__,
+ long_description=read('README.md'),
+ packages=find_packages(),
+ entry_points={'console_scripts': ['grip = grip.command:main']},
+ install_requires=read('requirements.txt').split('\n'),
+)

0 comments on commit 115e9cb

Please sign in to comment.