summaryrefslogtreecommitdiff
path: root/tools/pelican-quickstart
blob: 4048c2bfd2e9227c6af60453f4e7a9f6bfe755a9 (plain)
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#!/usr/bin/env python
# -*- coding: utf-8 -*- #

import os, sys, argparse, string
from pelican import __version__

TEMPLATES={
    'Makefile' : '''
PELICAN=$pelican
PELICANOPTS=$pelicanopts

BASEDIR=$$(PWD)
INPUTDIR=$$(BASEDIR)/src
OUTPUTDIR=$$(BASEDIR)/output
CONFFILE=$$(BASEDIR)/pelican.conf.py

FTP_HOST=$ftp_host
FTP_USER=$ftp_user
FTP_TARGET_DIR=$ftp_target_dir

SSH_HOST=$ssh_host
SSH_USER=$ssh_user
SSH_TARGET_DIR=$ssh_target_dir

DROPBOX_DIR=$dropbox_dir

help:
\t@echo 'Makefile for a pelican Web site                                       '
\t@echo '                                                                      '
\t@echo 'Usage:                                                                '
\t@echo '   make html                        (re)generate the web site         '
\t@echo '   make clean                       remove the generated files        '
\t@echo '   ftp_upload                       upload the web site using FTP     '
\t@echo '   ssh_upload                       upload the web site using SSH     '
\t@echo '   dropbox_upload                   upload the web site using Dropbox '
\t@echo '                                                                      '


html: clean $$(OUTPUTDIR)/index.html
\t@echo 'Done'

$$(OUTPUTDIR)/%.html:
\t$$(PELICAN) $$(INPUTDIR) -o $$(OUTPUTDIR) -s $$(CONFFILE)

clean:
\trm -fr $$(OUTPUTDIR)
\tmkdir $$(OUTPUTDIR)

dropbox_upload: $$(OUTPUTDIR)/index.html
\tcp -r $$(OUTPUTDIR)/* $$(DROPBOX_DIR)

ssh_upload: $$(OUTPUTDIR)/index.html
\tscp -r $$(OUTPUTDIR)/* $$(SSH_USER)@$$(SSH_HOST):$$(SSH_TARGET_DIR)

ftp_upload: $$(OUTPUTDIR)/index.html
\tlftp ftp://$$(FTP_USER)@$$(FTP_HOST) -e "mirror -R $$(OUTPUT_DIR)/* $$(FTP_TARGET_DIR) ; quit"

github: $$(OUTPUTDIR)/index.html
\tghp-import $$(OUTPUTDIR)
\tgit push origin gh-pages

.PHONY: html help clean ftp_upload ssh_upload dropbox_upload github
    ''',

    'pelican.conf.py': '''#!/usr/bin/env python
# -*- coding: utf-8 -*- #

AUTHOR = u"$author"
SITENAME = u"$sitename"
SITEURL = '/'

TIMEZONE = 'Europe/Paris'

DEFAULT_LANG='$lang'

# Blogroll
LINKS =  (
    ('Pelican', 'http://docs.notmyidea.org/alexis/pelican/'),
    ('Python.org', 'http://python.org'),
    ('Jinja2', 'http://jinja.pocoo.org'),
    ('You can modify those links in your config file', '#')
         )

# Social widget
SOCIAL = (
          ('You can add links in your config file', '#'),
         )

DEFAULT_PAGINATION = $default_pagination


    '''
}

CONF = {
    'pelican' : 'pelican',
    'pelicanopts' : None,
    'basedir': '.',
    'ftp_host': 'localhost',
    'ftp_user': 'anonymous',
    'ftp_target_dir': '/',
    'ssh_host': 'locahost',
    'ssh_user': 'root',
    'ssh_target_dir': '/var/www',
    'dropbox_dir' : '~/Dropbox/Public/',
    'default_pagination' : 7,
    'lang': 'en'
}


class _dict(dict):
    def __init__(self, *args, **kwargs):
        dict.__init__(self, *args, **kwargs)

    def __getitem__(self, i):
        return dict.get(self,i,None)

    def has_key(k):
        return True


def ask(question, answer=str, default=None, l=None):
    if answer == str:
        r = ''
        while True:
            if default:
                r = raw_input('> {0} [{1}] '.format(question, default))
            else:
                r = raw_input('> {0} '.format(question, default))

            r = r.strip()

            if len(r) <= 0:
                if default:
                    r = default
                    break
                else:
                    print('You must enter something')
            else:
                if l and len(r) != l:
                    print('You must enter a {0} letters long string'.format(l))
                else:
                   break

        return r

    elif answer == bool:
        r = None
        while True:
            if default is True:
                r = raw_input('> {0} (Y/n) '.format(question))
            elif default is False:
                r = raw_input('> {0} (y/N) '.format(question))
            else:
                r = raw_input('> {0} (y/n) '.format(question))

            r = r.strip().lower()

            if r in ('y', 'yes'):
                r = True
                break
            elif r in ('n', 'no'):
                r = False
                break
            elif not r:
                r = default
                break
            else:
                print("You must answer `yes' or `no'")
        return r
    elif answer == int:
        r = None
        while True:
            if default:
                r = raw_input('> {0} [{1}] '.format(question, default))
            else:
                r = raw_input('> {0} '.format(question))

            r = r.strip()

            if not r:
                r = default
                break

            try:
                r = int(r)
                break
            except:
                print('You must enter an integer')
        return r
    else:
        raise NotImplemented('Arguent `answer` must be str, bool or integer')


def main():
    parser = argparse.ArgumentParser(description="A kickstarter for pelican")
    parser.add_argument('-p', '--path', default=".",
            help="The path to generate the blog into")
    parser.add_argument('-t', '--title', default=None, metavar="title",
            help='Set the title of the website')
    parser.add_argument('-a', '--author', default=None, metavar="author",
            help='Set the author name of the website')
    parser.add_argument('-l', '--lang', default=None, metavar="lang",
            help='Set the default lang of the website')

    args = parser.parse_args()

    print('''Welcome to pelican-quickstart v{v}.

This script will help you creating a new Pelican based website.

Please answer the following questions so this script can generate the files needed by Pelican.

    '''.format(v=__version__))

    CONF['basedir'] = os.path.abspath(ask('Where do you want to create your new Web site ?', answer=str, default=args.path))
    CONF['sitename'] = ask('How will you call your Web site ?', answer=str, default=args.title)
    CONF['author'] = ask('Who will be the author of this Web site ?', answer=str, default=args.author)
    CONF['lang'] = ask('What will be the default language  of this Web site ?', str, args.lang or CONF['lang'], 2)

    CONF['with_pagination'] = ask('Do you want to enable article pagination ?', bool, bool(CONF['default_pagination']))

    if CONF['with_pagination']:
        CONF['default_pagination'] = ask('So how many articles per page do you want ?', int, CONF['default_pagination'])
    else:
        CONF['default_pagination'] = False

    mkfile = ask('Do you want to generate a Makefile to easily manage your website ?', bool, True)

    if mkfile:
        if ask('Do you want to upload your website using FTP ?', answer=bool, default=False):
            CONF['ftp_host'] = ask('What is the hostname of your FTP server ?', str, CONF['ftp_host'])
            CONF['ftp_user'] = ask('What is your username on this server ?', str, CONF['ftp_user'])
            CONF['ftp_traget_dir'] = ask('Where do you want to put your website on this server ?', str, CONF['ftp_target_dir'])

        if ask('Do you want to upload your website using SSH ?', answer=bool, default=False):
            CONF['ssh_host'] = ask('What is the hostname of your SSH server ?', str, CONF['ssh_host'])
            CONF['ssh_user'] = ask('What is your username on this server ?', str, CONF['ssh_user'])
            CONF['ssh_traget_dir'] = ask('Where do you want to put your website on this server ?', str, CONF['ssh_target_dir'])

        if ask('Do you want to upload your website using Dropbox ?', answer=bool, default=False):
            CONF['dropbox_dir'] = ask('Where is your Dropbox directory ?', str, CONF['dropbox_dir'])

    try:
        os.makedirs(os.path.join(CONF['basedir'], 'src'))
    except OSError, e:
        print('Error: {0}'.format(e))

    try:
        os.makedirs(os.path.join(CONF['basedir'], 'output'))
    except OSError, e:
        print('Error: {0}'.format(e))

    conf = string.Template(TEMPLATES['pelican.conf.py'])
    try:
        with open(os.path.join(CONF['basedir'], 'pelican.conf.py'), 'w') as fd:
            fd.write(conf.safe_substitute(CONF))
            fd.close()
    except OSError, e:
        print('Error: {0}'.format(e))

    if mkfile:
        Makefile = string.Template(TEMPLATES['Makefile'])

        try:
            with open(os.path.join(CONF['basedir'], 'Makefile'), 'w') as fd:
                fd.write(Makefile.safe_substitute(CONF))
                fd.close()
        except OSError, e:
            print('Error: {0}'.format(e))

    print('Done. Your new project is available at %s' % CONF['basedir'])

if __name__ == '__main__':
    main()