8000 Merge pull request #26 from theskumar-archive/markdown-support · flask-api/flask-api@d9597d9 · GitHub
[go: up one dir, main page]

Skip to content

Commit d9597d9

Browse files
committed
Merge pull request #26 from theskumar-archive/markdown-support
improvement(views): add markdown support for the browsable API
2 parents 418e21e + 76db79b commit d9597d9

File tree

4 files changed

+31
-2
lines changed

4 files changed

+31
-2
lines changed

docs/index.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,14 @@ Requirements:
3838
* Python 2.7+ or 3.3+
3939
* Flask 0.10+
4040

41-
Install using `pip`.
41+
The following packages are optional:
42+
43+
* Markdown (2.1.0+) - Markdown support for the browsable API.
44+
45+
Install using `pip`, including any optional packages you want...
4246

4347
pip install Flask-API
48+
pip install markdown # Markdown support for the browsable API.
4449

4550
Import and initialize your application.
4651

flask_api/compat.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# -*- coding: utf-8 -*-
2+
from __future__ import unicode_literals, absolute_import
3+
4+
# Markdown is optional
5+
try:
6+
import markdown
7+
8+
def apply_markdown(text):
9+
"""
10+
Simple wrapper around :func:`markdown.markdown` to set the base level
11+
of '#' style headers to <h2>.
12+
"""
13+
14+
extensions = ['headerid(level=2)']
15+
safe_mode = False
16+
md = markdown.Markdown(extensions=extensions, safe_mode=safe_mode)
17+
return md.convert(text)
18+
19+
except ImportError:
20+
apply_markdown = None

flask_api/renderers.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from flask.json import JSONEncoder
55
from flask.globals import _request_ctx_stack
66
from flask_api.mediatypes import MediaType
7+
from flask_api.compat import apply_markdown
78
import json
89
import re
910

@@ -108,6 +109,9 @@ def render(self, data, media_type, **options):
108109
view_description = dedent(view_description)
109110
mock_content = html_escape(mock_content)
110111

112+
if view_description and apply_markdown:
113+
view_description = apply_markdown(view_description)
114+
111115
status = options['status']
112116
headers = options['headers']
113117
headers['Content-Type'] = str(mock_media_type)

flask_api/templates/base.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@
111111

112112
<div class="content-main">
113113
<div class="page-header"><h1>{{ view_name }}</h1></div>
114-
{% if view_description %}<div style="margin-top: -10px; margin-bottom: 10px">{{ view_description }}</div>{% endif %}
114+
{% if view_description %}<div style="margin-top: -10px; margin-bottom: 10px">{{ view_description|safe }}</div>{% endif %}
115115
<div class="request-info" style="clear: both" >
116116
<pre class="prettyprint"><b>{{ request.method }}</b> {{ request.full_path }}</pre>
117117
</div>

0 commit comments

Comments
 (0)
0