├── md_preview ├── templates │ ├── __init__.py │ └── markdown_rendered.html ├── __init__.py └── md_preview.py ├── MANIFEST.in ├── CHANGES.txt ├── .gitignore ├── bin └── md-preview ├── README.md ├── setup.py ├── LICENSE.txt └── test.md /md_preview/templates/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /md_preview/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | from md_preview import * 3 | -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | include *.txt 2 | include *.md 3 | recursive-include md_preview/templates *.html 4 | -------------------------------------------------------------------------------- /CHANGES.txt: -------------------------------------------------------------------------------- 1 | v0.2.0, 2013-09-05 2 | * Rename the project to md-preview. 3 | * Use Markdown package(https://pypi.python.org/pypi/Markdown) instead of GitHub API. 4 | * Windows and Linux support(not fully tested). 5 | 6 | v0.1.0, 2013-09-04 -- Initial release. 7 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.py[cod] 2 | 3 | # C extensions 4 | *.so 5 | 6 | # Packages 7 | *.egg 8 | *.egg-info 9 | dist 10 | build 11 | eggs 12 | parts 13 | var 14 | sdist 15 | develop-eggs 16 | .installed.cfg 17 | lib 18 | lib64 19 | __pycache__ 20 | 21 | # Installer logs 22 | pip-log.txt 23 | 24 | # Unit test / coverage reports 25 | .coverage 26 | .tox 27 | nosetests.xml 28 | 29 | # Translations 30 | *.mo 31 | 32 | # Mr Developer 33 | .mr.developer.cfg 34 | .project 35 | .pydevproject 36 | -------------------------------------------------------------------------------- /bin/md-preview: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | import os 4 | import sys 5 | import codecs 6 | import md_preview 7 | 8 | md_files = sys.argv[1:] 9 | for md_file in md_files: 10 | if not os.path.exists(md_file): 11 | print "file not found: %s\n" % (md_file) 12 | continue 13 | 14 | text = '' 15 | with codecs.open(md_file, mode="r", encoding="utf-8") as fd: 16 | text = fd.read() 17 | 18 | html = md_preview.render(text) 19 | 20 | md_preview.view_in_browser(html) 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # MDPreview 2 | 3 | md-preview is just a good enough solution to previewing markdown documents 4 | as github styled HTML. 5 | 6 | ## Install 7 | 8 | $ pip install git+https://github.com/simple/md-preview.git 9 | 10 | 11 | ## Usage 12 | 13 | $ md-preview filename [ filename ... ] 14 | 15 | 16 | ## Caveats 17 | 18 | By dropping the [GitHub Markdown Rendering API](http://developer.github.com/v3/markdown/), it does not support GFM anymore(so I have changed the project name). 19 | 20 | ## Thanks 21 | 22 | HTML template is borrowed from [ghmarkdownrender](https://github.com/magnetikonline/ghmarkdownrender). 23 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | try: 2 | from setuptools import setup 3 | setup # quiet "redefinition of unused ..." warning from pyflakes 4 | # arguments that distutils doesn't understand 5 | setuptools_kwargs = { 6 | 'install_requires': [ 7 | "Markdown", 8 | ], 9 | 'zip_safe': False, 10 | } 11 | except ImportError: 12 | from distutils.core import setup 13 | setuptools_kwargs = {} 14 | 15 | setup( 16 | name='GfmPreview', 17 | version='0.2.0', 18 | author='Leo Ju', 19 | author_email='mr.simple@gmail.com', 20 | packages=['md_preview'], 21 | scripts=['bin/md-preview'], 22 | package_data={'md_preview': ['templates/*']}, 23 | url='https://github.com/simple/md-preview', 24 | license='LICENSE.txt', 25 | description='Simple markdown preview from command line', 26 | long_description=open('README.md').read(), 27 | **setuptools_kwargs 28 | ) 29 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2013 isnowfy 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /md_preview/md_preview.py: -------------------------------------------------------------------------------- 1 | import markdown as md 2 | import os 3 | import sys 4 | import tempfile 5 | import time 6 | 7 | 8 | def render(markdown): 9 | template_path = os.path.join( 10 | os.path.dirname(__file__), 'templates', 'markdown_rendered.html') 11 | 12 | template = '' 13 | with open(template_path, 'r') as fd: 14 | template = fd.read() 15 | 16 | html_content = md.markdown(markdown) 17 | html = template.replace('__CONTENT__', html_content) 18 | 19 | return html 20 | 21 | 22 | def get_temp_dir(): 23 | if os.name == 'nt': 24 | return tempfile.gettempdir() 25 | else: 26 | return '/tmp' 27 | 28 | 29 | def open_file(filepath): 30 | if os.name == 'nt': 31 | os.system("start " + filepath) 32 | elif sys.platform.startswith('darwin'): 33 | os.system("open " + filepath) 34 | else: 35 | os.system("xdg-open " + filepath) 36 | 37 | 38 | def view_in_browser(text): 39 | tmpdir = get_temp_dir() 40 | with tempfile.NamedTemporaryFile( 41 | mode='w', suffix='.html', dir=tmpdir, delete=False) as temp_fd: 42 | temp_fd.write(text.encode("utf-8")) 43 | temp_fd.close() 44 | open_file(temp_fd.name) 45 | time.sleep(2) 46 | os.unlink(temp_fd.name) 47 | -------------------------------------------------------------------------------- /test.md: -------------------------------------------------------------------------------- 1 | # GitHub Markdown render 2 | Display [Markdown](http://github.github.com/github-flavored-markdown/) formatted documents on your local development web server using GitHub's [Markdown Rendering API](http://developer.github.com/v3/markdown/) and CSS to mimic the visual display on GitHub itself. 3 | 4 | Handy for authoring/previewing `README.md` files (or any Markdown for that matter) in project repositories, hopefully avoiding noisy `git push` actions in commit logs due to excessive typos/errors. 5 | 6 | **Note:** this is intended for local development only, probably not a good idea for production use due to GitHub API rate limits per user. 7 | 8 | ## 한글이 들어가면??? ;ㅁ; 9 | 10 | ## Requires 11 | - PHP 5.4+ (developed against PHP 5.4.10) 12 | - [PHP cURL extension](http://php.net/manual/en/book.curl.php) (more than likely part of your PHP install) 13 | - Nginx or Apache URL rewrite support 14 | 15 | ## Usage 16 | Your project(s) Markdown files are accessible on your local web server in plain text, for example: 17 | 18 | http://localhost/projects/ghmarkdownrender/README.md 19 | http://localhost/projects/thummer/README.md 20 | http://localhost/projects/unrarallthefiles/README.md 21 | http://localhost/projects/webserverinstall.ubuntu12.04/install.md 22 | 23 | To view rendered Markdown using the same parsing and styling as GitHub project pages, request files with querystring switch: 24 | 25 | http://localhost/projects/ghmarkdownrender/README.md?ghmd 26 | http://localhost/projects/thummer/README.md?ghmd 27 | http://localhost/projects/unrarallthefiles/README.md?ghmd 28 | http://localhost/projects/webserverinstall.ubuntu12.04/install.md?ghmd 29 | 30 | Rendered HTML is cached in a PHP session based on markdown file modification time to reduce repeated GitHub API calls for the same file content. 31 | 32 | ## Install 33 | 34 | ### Configure index.php 35 | Generate a new [GitHub OAuth token](http://developer.github.com/v3/oauth/#create-a-new-authorization) using either: 36 | - The supplied [generatetoken.sh](generatetoken.sh) script 37 | - Directly from your [Authorized Applications](https://github.com/settings/applications) page on GitHub - click **Create new token** 38 | 39 | Make a note of the token generated. 40 | 41 | Update the following constants at the top of `index.php` in the `GitHubMarkdownRender` class: 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 |
GITHUB_TOKENYour generated GitHub OAuth token. Anonymous GitHub API calls are limited to 60 per hour, providing user credentials ramps this up to a more usable 5000 requests per hour.
DOC_ROOTYour local web server document root. (Assuming you are serving up all your project(s) directories over your default virtual host.)
53 | 54 | ### Setup URL rewrite rules 55 | Next, setup URL rewrite for your default virtual host so all requests to `/local/path/*.md?ghmd` are rewritten to `/path/to/ghmarkdownrender/index.php`. Refer to the supplied `rewrite.nginx.conf` & `rewrite.apache.conf` for examples. 56 | 57 | **Note:** 58 | - You may want to have requested raw Markdown files (e.g. `http://localhost/projects/ghmarkdownrender/README.md`) served up with a MIME type such as `text/plain` for convenience. 59 | - Nginx by default serves up unknown file types based on extension as `application/octet-stream`, forcing a browser download - see `/etc/nginx/mime.types` and modify to suit. 60 | - I haven't had a chance to test `rewrite.apache.conf` it should do the trick, would appreciate a pull-request if it needs fixing. 61 | 62 | ### Test 63 | You should now be able to call a Markdown document with a querystring of `?ghmd` to receive a familiar GitHub style Markdown display. The page footer will also display the total/available API rate limits, or if rendering was cached based on file modification time. 64 | 65 | ## CSS style issues 66 | Markdown display CSS has been lifted (deliberately) from GitHub.com. It's quite possible/likely there are some GitHub markdown CSS styles missing to make this complete. 67 | 68 | If anything missing is noted with your own markdown documents, it would be great to get any source examples or pull requests (add your example(s) to [test.md](test.md)) to help make things complete. 69 | -------------------------------------------------------------------------------- /md_preview/templates/markdown_rendered.html: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | GitHub Styled Markdown Preview 12 | 13 | 286 | 287 | 288 | 289 | 290 |
291 | __CONTENT__ 292 |
293 | 294 | 295 | 296 | --------------------------------------------------------------------------------