-
Notifications
You must be signed in to change notification settings - Fork 10
/
blueprint.py
223 lines (184 loc) · 6.68 KB
/
blueprint.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# -*- coding: utf-8 -*-
import codecs
import datetime
import dateutil.parser
import dateutil.tz
import getpass
import json
import markdown as Markdown
import os
import re
import requests
from clint.textui import colored, puts
from flask import Blueprint
from jinja2 import evalcontextfilter, contextfunction, Template, Markup
from six.moves import input as raw_input
from tarbell.hooks import register_hook
from time import time
NAME = "Basic Bootstrap 3 template"
ISSUES = [
("Edit index.html", "Create new content in `index.html` by replacing the `{% block content %} ... {% endblock %}'"),
("Add Google analytics ID to spreadsheet", "Add your tracking code."),
("Device testing", "Chrome, Firefox, IE 8+, Safari, iPhone, iPad, Android"),
("Publish the project to production", "Are you ready to ship?"),
]
blueprint = Blueprint('base', __name__)
@register_hook('newproject')
def create_repo(site, git):
create = raw_input("Want to create a Github repo for this project [Y/n]? ")
if create and not create.lower() == "y":
return puts("Not creating Github repo...")
name = site.path.split('/')[-1]
user = raw_input("What is your Github username? ")
password = getpass.getpass("What is your Github password? ")
headers = {'Content-type': 'application/json', 'Accept': 'application/json'}
data = {'name': name, 'has_issues': True, 'has_wiki': True}
resp = requests.post('https://api.github.com/user/repos', auth=(user, password), headers=headers, data=json.dumps(data))
puts("Created {0!s}".format(colored.green("https://github.com/{0}/{1}".format(user, name))))
clone_url = resp.json().get("clone_url")
puts(git.remote.add("origin", "[email protected]:{0}/{1}.git".format(user,name)))
puts(git.push("origin", "master"))
for title, description in ISSUES:
puts("Creating {0!s}".format(colored.yellow(title)))
data = {'title': title, 'body': description}
resp = requests.post('https://api.github.com/repos/{0}/{1}/issues'.format(user, name), auth=(user, password), headers=headers, data=json.dumps(data))
@contextfunction
def read_file(context, path, absolute=False, encoding='utf-8'):
"""
Read the file at `path`. If `absolute` is True, use absolute path,
otherwise path is assumed to be relative to Tarbell template root dir.
"""
if not absolute:
path = os.path.join(os.path.dirname(__file__), '..', path)
try:
return codecs.open(path, 'r', encoding).read()
except IOError:
return None
@contextfunction
def render_file(context, path, absolute=False):
"""
Render a file with the current context
"""
file_contents = read_file(context, path, absolute)
template = Template(file_contents)
return template.render(**context)
@blueprint.app_context_processor
def context_processor():
"""
Add helper functions to context for all projects.
"""
return {
'read_file': read_file,
'render_file': render_file,
}
@blueprint.app_template_filter()
def process_text(text):
try:
return Markup(text)
except TypeError:
return u''
@blueprint.app_template_filter()
def drop_cap(text):
"""
Add drop-cap class to beginning of content.
"""
if type(text).__name__ == 'Markup':
return text
if text:
content = '<span class="drop-cap">%s</span>%s' % (text[:1], text[1:])
else:
content = ''
return Markup(content)
@blueprint.app_template_filter()
def format_date(value, format='%b. %d, %Y', convert_tz=None):
"""
Format a date.
"""
if isinstance(value, float) or isinstance(value, int):
seconds = (value - 25569) * 86400.0
parsed = datetime.datetime.utcfromtimestamp(seconds)
else:
parsed = dateutil.parser.parse(value)
if convert_tz:
local_zone = dateutil.tz.gettz(convert_tz)
parsed = parsed.astimezone(tz=local_zone)
return parsed.strftime(format)
@blueprint.app_template_filter()
def strong_to_b(value):
"""
Replaces enclosing <strong> and </strong>s with <p><b>s
"""
value = re.sub(r'^<p><strong>(.+?)</strong></p>$', u'<p><b>\\1</b></p>', value)
return value
@blueprint.app_template_filter()
def strip_p(value):
"""
Removes enclosing <p> and </p> tags from value.
"""
value = re.sub(r'^<p>(.+?)</p>$', u'\\1', value)
return value
@blueprint.app_template_filter()
def wrap_p(value):
"""
Adds enclosing <p> and </p> tags to value.
"""
return '<p>%s</p>' % value
@blueprint.app_template_filter()
def replace_windows_linebreaks(value):
"""
Replace all \r (the Windows/MS-style linebreak character) with
better-tasting, lower-fat unix-style \ns. Takes a string, returns
a string.
"""
return re.sub(r'\r', '\n', value)
@blueprint.app_template_filter('paragraphs')
def get_paragraphs(value):
"""
Take a block of text and return an array of paragraphs. Only works if
paragraphs are denoted by <p> tags and not double <br>.
Use `br_to_p` to convert text with double <br>s to <p> wrapped paragraphs.
"""
value = re.sub(r'</p>\w*<p>', u'</p>\n\n<p>', value)
paras = re.split('\n{2,}', value)
return paras
@blueprint.app_template_filter()
def br_to_p(value):
"""
Converts text where paragraphs are separated by two <br> tags to text
where the paragraphs are wrapped by <p> tags.
"""
value = re.sub(r'<br\s*/*>\s*<br\s*/*>', u'\n\n', value)
paras = re.split('\n{2,}', value)
paras = [u'<p>%s</p>' % p.strip() for p in paras if p]
paras = u'\n\n'.join(paras)
return paras
@blueprint.app_template_filter()
def section_heads(value):
"""
Search through a block of text and replace <p><b>text</b></p>
with <h4 class="section-head">text</h4
"""
value = re.sub(r'<p>\w*<b>(.+?)</b>\w*</p>', u'<h4 class="section-head">\\1</h4>', value)
return value
@blueprint.app_template_filter()
@evalcontextfilter
def linebreaks(eval_ctx, value):
"""Converts newlines into <p> and <br />s."""
value = re.sub(r'\r\n|\r|\n', '\n', value) # normalize newlines
paras = re.split('\n{2,}', value)
paras = [u'<p>%s</p>' % p.replace('\n', '<br />') for p in paras]
paras = u'\n\n'.join(paras)
return Markup(paras)
@blueprint.app_template_filter()
@evalcontextfilter
def linebreaksbr(eval_ctx, value):
"""Converts newlines into <p> and <br />s."""
value = re.sub(r'\r\n|\r|\n', '\n', value) # normalize newlines
paras = re.split('\n{2,}', value)
paras = [u'%s' % p.replace('\n', '<br />') for p in paras]
paras = u'\n\n'.join(paras)
return Markup(paras)
@blueprint.app_template_filter()
def markdown(value):
"""Run text through markdown process"""
return Markup(Markdown.markdown(value))