-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtasks.py
127 lines (107 loc) · 3.06 KB
/
tasks.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
# -*- coding: utf-8 -*-
import datetime
import os
import shlex
import shutil
from invoke import task
from invoke.main import program
from pelican import main as pelican_main
from pelican.settings import DEFAULT_CONFIG, get_settings_from_file
SETTINGS_FILE_BASE = "pelicanconf.py"
SETTINGS = {}
SETTINGS |= DEFAULT_CONFIG
LOCAL_SETTINGS = get_settings_from_file(SETTINGS_FILE_BASE)
SETTINGS.update(LOCAL_SETTINGS)
CONFIG = {
"settings_base": SETTINGS_FILE_BASE,
"settings_publish": "publishconf.py",
"deploy_path": SETTINGS["OUTPUT_PATH"],
"github_pages_branch": "master",
"username": "girisagar46",
"blog_repo": "girisagar46.github.io.git",
"commit_message": f"'Publish site on {datetime.date.today().isoformat()}'",
"host": "localhost",
"port": 8000,
"GH_TOKEN": os.getenv("GH_TOKEN"),
}
TEMPLATE = """
Title: {title}
Date: {year}-{month}-{day} {hour}:{minute}
Modified: {year}-{month}-{day} {hour}:{minute}
Category:
Tags:
Slug: {slug}
Summary:
Status: draft
"""
@task
def clean(c):
"""Remove generated files"""
if os.path.isdir(CONFIG["deploy_path"]):
shutil.rmtree(CONFIG["deploy_path"])
os.makedirs(CONFIG["deploy_path"])
@task
def serve(ctx):
"""`build`, then `serve`"""
ctx.run("pelican -r -s {settings_base} --listen".format(**CONFIG))
@task
def publish(c):
"""Publish to production via rsync"""
pelican_run("-s {settings_publish}".format(**CONFIG))
print("Invoking gph-import.")
c.run(
"ghp-import -m {commit_message} -b {github_pages_branch} {deploy_path}".format(
**CONFIG
)
)
print("Pushing to master...")
c.run(
"git push -fq https://{username}:{GH_TOKEN}@github.com/{username}/{blog_repo} {github_pages_branch}".format(
**CONFIG
)
)
@task
def publish_locally(c):
"""Publish to GitHub Pages"""
clean(c)
pelican_run("-s {settings_publish}".format(**CONFIG))
c.run(
"ghp-import -m {commit_message} -b {github_pages_branch} {deploy_path}".format(
**CONFIG
)
)
c.run("git push -u origin {github_pages_branch}".format(**CONFIG))
@task
def gh_pages(c):
"""Publish to GitHub Pages"""
c.run(
"ghp-import -b {github_pages_branch} "
"-m {commit_message} "
"{deploy_path} -p".format(**CONFIG)
)
def pelican_run(cmd):
cmd += f" {program.core.remainder}"
pelican_main(shlex.split(cmd))
@task
def new_post(c, title):
"""
creates new post
Usage: $ invoke new-post title="My Brand New Awesome Post"
:param title: Title of the blog post
:return:
"""
today = datetime.datetime.now()
post_title = title.split("=")[1]
slug = post_title.lower().strip().replace(" ", "-")
file_location = f"content/articles/{slug}.md"
template = TEMPLATE.strip().format(
title=post_title,
year=today.year,
month=today.month,
day=today.day,
hour=today.hour,
minute=today.minute,
slug=slug,
)
with open(file_location, "w") as output_article:
output_article.write(template)