-
Notifications
You must be signed in to change notification settings - Fork 1
/
fabfile.py
143 lines (110 loc) · 3.62 KB
/
fabfile.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
# -*- coding: utf-8 -*-
from fabric.api import env, local, puts
import fabric.contrib.project as project
import os
import sys
PY3 = sys.version_info > (3,)
# Local path configuration (can be absolute or relative to fabfile)
env.deploy_path = 'output'
DEPLOY_PATH = env.deploy_path
# Remote server configuration
env.user = 'www-data'
env.hosts = ['chtoes.li']
path = {
'dev': '/var/www/dev.chtoes.li/public/',
'prod': '/var/www/chtoes.li/public/',
'ci': '/var/www/ci.chtoes.li/public/'
}
TEMPLATE = 'Title:\n\
Date: {{date}}\n\
Slug: {{slug}}\n\
Category: {{ category }}\n\
Source: http://what-if.xkcd.com/{{num}}/\n\
SourceNum: {{num}}\n\
SourceTitle: {{title}}\n\
Formulas: False\n\
Description: \n\
Image: https://chtoes.li/uploads/{{num}}-{{slug}}/front.png\n\
\n\
\n'
def new(num, title=None, category='What If?', overwrite='no'):
import datetime
if title is None:
if PY3:
from urllib.request import urlopen
else:
from urllib2 import urlopen
from bs4 import BeautifulSoup
html = urlopen('http://what-if.xkcd.com/{}/'.format(num))
title = BeautifulSoup(html, 'lxml').title.string
slug = slugify(title)
CATEGORIES = {
'What If?': 'what-if',
'Новости проекта': 'news',
'Прочее': 'other',
}
category_slug = CATEGORIES[category]
now = datetime.datetime.now()
post_date = now.strftime('%Y-%m-%d')
params = dict(
date=post_date,
title=title,
slug=slug,
num=num,
category=category
)
out_file = 'content/{}/{:03d}-{}.md'.format(category_slug, int(num), slug)
local("mkdir -p '{}' || true".format(os.path.dirname(out_file)))
if not os.path.exists(out_file) or overwrite.lower() == 'yes':
render(TEMPLATE, out_file, **params)
else:
print("{} already exists. Pass 'overwrite=yes' to destroy it."
.format(out_file))
def slugify(text):
import re
normalized = ''.join([c.lower() if c.isalnum() else '-'
for c in text])
no_repetitions = re.sub(r'--+', '-', normalized)
clean_start = re.sub(r'^-+', '', no_repetitions)
clean_end = re.sub(r'-+$', '', clean_start)
return clean_end
def render(template, destination, **kwargs):
from jinja2 import Template
template = Template(TEMPLATE)
text = template.render(**kwargs)
with open(destination, 'w') as output:
puts('Rendering to {}'.format(destination))
if PY3:
output.write(text)
else:
output.write(text.encode('utf-8'))
def clean():
if os.path.isdir(DEPLOY_PATH):
local('rm -rf {deploy_path}'.format(**env))
local('mkdir {deploy_path}'.format(**env))
def build(environment):
local('pelican -s pelicanconf-{}.py'.format(environment))
def rebuild(environment):
clean()
build(environment)
def regenerate(environment):
local('pelican -r -s pelicanconf-{}.py'.format(environment))
def serve():
# Determine major version of python as the one executes this script now.
if PY3:
local('cd {deploy_path} && python3 -m http.server'.format(**env))
else:
local('cd {deploy_path} && python2 -m SimpleHTTPServer'.format(**env))
def reserve(environment):
build(environment)
serve()
def preview(environment):
local('pelican -s pelicanconf-{}.py'.format(environment))
def publish(environment):
local('pelican -s pelicanconf-{}.py'.format(environment))
project.rsync_project(
remote_dir=path[environment],
exclude='.DS_Store',
local_dir=DEPLOY_PATH.rstrip('/') + '/',
delete=True
)