-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpoole.py
666 lines (531 loc) · 21.3 KB
/
poole.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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
#!/usr/bin/python
# =============================================================================
#
# Poole - A damn simple static website generator.
# Copyright (C) 2009 Oben Sonne <[email protected]>
#
# This file is part of Poole.
#
# Poole is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Poole is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Poole. If not, see <http://www.gnu.org/licenses/>.
#
# =============================================================================
from __future__ import with_statement
import codecs
import glob
import imp
import optparse
import os
import os.path
from os.path import join as opj
from os.path import exists as opx
import re
import shutil
import StringIO
import sys
import traceback
import urlparse
from SimpleHTTPServer import SimpleHTTPRequestHandler
from BaseHTTPServer import HTTPServer
try:
import markdown
except ImportError:
print("error: need python-markdown, get it from "
"http://www.freewisdom.org/projects/python-markdown/Installation")
sys.exit(1)
# =============================================================================
# init site
# =============================================================================
EXAMPLE_FILES = {
"page.html": """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset={{ __encoding__ }}" />
<title>poole - {{ page["title"] }}</title>
<meta name="description" content="{{ page.get("description", "a poole site") }}" />
<meta name="keywords" content="{{ page.get("keywords", "poole") }}" />
<link rel="stylesheet" type="text/css" href="poole.css" />
</head>
<body>
<div id="box">
<div id="header">
<h1>a poole site</h1>
<h2>{{ page["title"] }}</h2>
</div>
<div id="menu">
<!--%
mpages = [p for p in pages if "menu-position" in p]
mpages.sort(key=lambda p: int(p["menu-position"]))
entry = '<span class="%s"><a href="%s">%s</a></span>'
for p in mpages:
style = p["title"] == page["title"] and "current" or ""
print(entry % (style, p["url"], p["title"]))
%-->
</div>
<div id="content">{{ __content__ }}</div>
</div>
<div id="footer">
Built with <a href="http://bitbucket.org/obensonne/poole">Poole</a>
·
Licensed as <a href="http://creativecommons.org/licenses/by-sa/3.0">CC-SA</a>
·
<a href="http://validator.w3.org/check?uri=referer">Validate me</a>
</div>
</body>
</html>
""",
# -----------------------------------------------------------------------------
opj("input", "index.md"): """
title: home
menu-position: 0
---
## Welcome to Poole
In Poole you write your pages in [markdown][md]. It's easier to write
markdown than HTML.
Poole is made for simple websites you just want to get done, without installing
a bunch of requirements and without learning a template engine.
In a build, Poole copies every file from the *input* directory to the *output*
directory. During that process every markdown file (ending with *md*, *mkd*,
*mdown* or *markdown*) is converted to HTML using the project's `page.html`
as a skeleton.
[md]: http://daringfireball.net/projects/markdown/
""",
# -----------------------------------------------------------------------------
opj("input", "logic.md"): """
menu-position: 4
---
Poole has basic support for content generation using Python code inlined in
page files. This is everything but a clear separation of logic and content but
for simple sites this is just a pragmatic way to get things done fast.
For instance the menu on this page is generated by some inlined Python code in
the project's `page.html` file.
Just ignore this feature if you don't need it :)
Content generation by inlined Python code is good to add some zest to your
site. If you use it a lot, you better go with more sophisticated site
generators like [Hyde](http://ringce.com/hyde).
""",
# -----------------------------------------------------------------------------
opj("input", "layout.md"): """
menu-position: 3
---
Every page of a poole site is based on *one global template file*, `page.html`.
All you need to adjust the site layout is to
* edit the page template `page.html` and
* extend or edit the style file `input/poole.css`.
""",
opj("input", "blog.md"): """
menu-position: 10
---
Poole has basic blog support. If an input page's file name has a structure like
`page-title.YYYY-MM-DD.post-title.md`, e.g. `blog.201010-02-27.read_this.md`,
Poole recognizes the date and post title and sets them as attributes of the
page. These attributes can then be used to generate a list of blog posts:
<!--%
from datetime import datetime
posts = [p for p in pages if "post" in p] # get all blog post pages
posts.sort(key=lambda p: p.get("date"), reverse=True) # sort post pages by date
for p in posts:
date = datetime.strptime(p.date, "%Y-%m-%d").strftime("%B %d, %Y")
print " * **[%s](%s)** - %s" % (p.post, p.url, date) # markdown list item
%-->
Have a look into `input/blog.md` to see how it works. Feel free to adjust it
to your needs.
""",
# -----------------------------------------------------------------------------
opj("input", "blog.2010-02-22.Doctors_in_my_penguin.md") : """
---
## {{ page["post"] }}
*Posted at
<!--%
from datetime import datetime
print datetime.strptime(page["date"], "%Y-%m-%d").strftime("%B %d, %Y")
%-->*
There is a bank in my eel, your argument is invalid.
More nonsense at <http://automeme.net/>.
""",
# -----------------------------------------------------------------------------
opj("input", "blog.2010-03-01.I_ate_all the pokemans.md"): """
## {{ page["post"] }}
*Posted at <!--{ page["date"] }-->.*
What *are* interior crocodile alligators? We just don't know.
More nonsense at <http://automeme.net/>.
""",
# -----------------------------------------------------------------------------
opj("input", "poole.css"): """
body {
font-family: sans;
width: 800px;
margin: 1em auto;
color: #2e3436;
}
div#box {
border: solid #2e3436 1px;
}
div#header, div#menu, div#content, div#footer {
padding: 1em;
}
div#menu {
background-color: #2e3436;
padding: 0.6em 0 0.6em 0;
}
#menu span {
background-color: #2e3436;
font-weight: bold;
padding: 0.6em;
}
#menu span.current {
background-color: #555753;
}
#menu a {
color: #fefefc;
text-decoration: none;
}
div#footer {
color: gray;
text-align: center;
font-size: small;
}
div#footer a {
color: gray;
text-decoration: none;
}
pre {
border: dotted black 1px;
background: #eeeeec;
font-size: small;
padding: 1em;
}
"""
}
def init(project, opts):
"""Initialize a site project."""
if not opx(project):
os.makedirs(project)
if os.listdir(project):
print("error : project dir %s is not empty, abort" % project)
sys.exit(1)
os.mkdir(opj(project, "input"))
os.mkdir(opj(project, opts.output_dir))
for fname, content in EXAMPLE_FILES.items():
with open(opj(project, fname), 'w') as fp:
fp.write(content)
print("success: initialized project")
# =============================================================================
# build site
# =============================================================================
MKD_PATT = r'\.(?:md|mkd|mdown|markdown)$'
class Page(dict):
"""Abstraction of a source page."""
_re_eom = re.compile(r'^---+ *\r?\n?$')
_re_vardef = re.compile(r'^([^\n:=]+?)[:=]((?:.|\n )*)', re.MULTILINE)
_sec_macros = "macros"
_modmacs = None
def __init__(self, templ, fname, strip, opts):
"""Create a new page.
@param templ: template dictionary
@param fname: full path to page input file
@param strip: portion of path to strip from `fname` for deployment
@param opts: command line options
"""
super(Page, self).__init__()
self.update(templ)
self["url"] = re.sub(MKD_PATT, ".html", fname)
self["url"] = self["url"][len(strip):].lstrip(os.path.sep)
self["url"] = self["url"].replace(os.path.sep, "/")
self["fname"] = fname
with codecs.open(fname, 'r', opts.input_enc) as fp:
self.raw = fp.readlines()
# split raw content into macro definitions and real content
vardefs = ""
self.source = ""
for line in self.raw:
if not vardefs and self._re_eom.match(line):
vardefs = self.source
self.source = "" # only macro defs until here, reset source
else:
self.source += line
for key, val in self._re_vardef.findall(vardefs):
key = key.strip()
val = val.strip()
val = re.sub(r' *\n +', ' ', val) # clean out line continueation
self[key] = val
basename = os.path.basename(fname)
fpatt = r'(.+?)(?:\.([0-9]+-[0-9]+-[0-9]+)(?:\.(.*))?)?%s' % MKD_PATT
title, date, post = re.match(fpatt, basename).groups()
title = title.replace("_", " ")
post = post and post.replace("_", " ") or None
self["title"] = self.get("title", title)
if date and "date" not in self: self["date"] = date
if post and "post" not in self: self["post"] = post
self.html = ""
def __getattribute__(self, name):
try:
return super(Page, self).__getattribute__(name)
except AttributeError, e:
if name in self:
return self[name]
raise e
def prepare_output(macmod):
if hasattr(macmod, "prepare_output"):
return macmod.prepare_output()
print "Using built-in prepare."
project = macmod.project
dir_in = macmod.input
dir_out = macmod.output
# prepare output directory
project_path = os.path.realpath(project)
for fod in glob.glob(opj(dir_out, "*")):
# don't remove the sources
"""
real_fod = os.path.realpath(fod)
if real_fod == project_path or real_fod.startswith(project_path):
print real_fod, project_path
continue
"""
if os.path.isdir(fod):
print "-dir", fod
shutil.rmtree(fod)
else:
os.remove(fod)
if not opx(dir_out):
os.mkdir(dir_out)
# -----------------------------------------------------------------------------
def build(project, opts):
"""Build a site project."""
# -------------------------------------------------------------------------
# utilities
# -------------------------------------------------------------------------
def abort_iex(page, itype, inline, exc):
"""Abort because of an exception in inlined Python code."""
print("error : Python %s in %s failed" % (itype, page.fname))
print((" %s raising the exception " % itype).center(79, "-"))
print(inline)
print(" exception ".center(79, "-"))
print(exc)
sys.exit(1)
# -------------------------------------------------------------------------
# regex patterns and replacements
# -------------------------------------------------------------------------
regx_escp = re.compile(r'\\((?:(?:<|<)!--|{)(?:{|%))') # escaped code
repl_escp = r'\1'
regx_rurl = re.compile(r'(?<=(?:(?:\n| )src|href)=")([^#/&%].*?)(?=")')
repl_rurl = lambda m: urlparse.urljoin(opts.base_url, m.group(1))
regx_eval = re.compile(r'(?<!\\)(?:(?:<!--|{){)(.*?)(?:}(?:-->|}))', re.S)
def repl_eval(m):
"""Replace a Python expression block by its evaluation."""
expr = m.group(1)
try:
repl = eval(expr, macros.copy())
except:
abort_iex(page, "expression", expr, traceback.format_exc())
else:
if not isinstance(repl, basestring): # e.g. numbers
repl = unicode(repl)
elif not isinstance(repl, unicode):
repl = repl.decode("utf-8")
return repl
regx_exec = re.compile(r'(?<!\\)(?:(?:<!--|{)%)(.*?)(?:%(?:-->|}))', re.S)
def repl_exec(m):
"""Replace a block of Python statements by their standard output."""
stmt = m.group(1).replace("\r\n", "\n")
# base indentation
ind_lvl = len(re.findall(r'^(?: *\n)*( *)', stmt, re.MULTILINE)[0])
ind_rex = re.compile(r'^ {0,%d}' % ind_lvl, re.MULTILINE)
stmt = ind_rex.sub('', stmt)
# execute
sys.stdout = StringIO.StringIO()
try:
exec stmt in macros.copy()
except:
sys.stdout = sys.__stdout__
abort_iex(page, "statements", stmt, traceback.format_exc())
else:
repl = sys.stdout.getvalue()[:-1] # remove last line break
sys.stdout = sys.__stdout__
if not isinstance(repl, unicode):
repl = repl.decode(opts.input_enc)
return repl
# -------------------------------------------------------------------------
# preparations
# -------------------------------------------------------------------------
dir_in = opj(project, "input")
dir_out = opj(project, opts.output_dir)
page_html = opj(project, "page.html")
# check required files and folders
for pelem in (page_html, dir_in, dir_out):
if not opx(pelem):
print("error : %s does not exist, looks like project has not been "
"initialized, abort" % pelem)
sys.exit(1)
# macro module
class nomod: pass # dummy module, something we can set attributes on
fname = opj(opts.project, "macros.py")
macros = {"__encoding__": opts.output_enc}
macmod = opx(fname) and imp.load_source("macros", fname) or nomod
setattr(macmod, "options", opts)
setattr(macmod, "project", project)
setattr(macmod, "input", dir_in)
setattr(macmod, "output", dir_out)
for attr in dir(macmod):
if not attr.startswith("_"):
macros[attr] = getattr(macmod, attr)
prepare_output(macmod)
pages = []
for _fn in dir(macmod):
if _fn.startswith("init_"):
getattr(macmod, _fn)(pages)
# -------------------------------------------------------------------------
# process input files
# -------------------------------------------------------------------------
page_global = macros.get("page", {})
for cwd, dirs, files in os.walk(dir_in.decode(opts.filename_enc)):
cwd_site = cwd[len(dir_in):].lstrip(os.path.sep)
for sdir in dirs[:]:
if re.search(opts.ignore, opj(cwd_site, sdir)):
dirs.remove(sdir)
else:
tmp = opj(dir_out, cwd_site, sdir)
if not os.path.exists(tmp):
os.mkdir(tmp)
for f in files:
if re.search(opts.ignore, opj(cwd_site, f)):
pass
elif re.search(MKD_PATT, f):
page = Page(page_global, opj(cwd, f), dir_in, opts)
pages.append(page)
else:
shutil.copy(opj(cwd, f), opj(dir_out, cwd_site))
pages.sort(key=lambda p: int(p.get("sval", "0")))
macros["pages"] = pages
macmod.pages = pages
# -------------------------------------------------------------------------
# run pre-convert hooks in macro module (named 'once' before)
# -------------------------------------------------------------------------
hooks = [a for a in dir(macmod) if re.match(r'hook_preconvert_|once_', a)]
for fn in sorted(hooks):
getattr(macmod, fn)()
# -------------------------------------------------------------------------
# convert pages (markdown to HTML)
# -------------------------------------------------------------------------
for page in sorted(pages, key=lambda p: p["url"]):
print("info : convert %s" % page.fname)
# replace expressions and statements in page source
macmod.page = page
macros["page"] = page
out = regx_eval.sub(repl_eval, page.source)
out = regx_exec.sub(repl_exec, out)
# convert to HTML
page.html = markdown.Markdown(extensions=opts.md_ext).convert(out)
# -------------------------------------------------------------------------
# run post-convert hooks in macro module
# -------------------------------------------------------------------------
hooks = [a for a in dir(macmod) if a.startswith("hook_postconvert_")]
for fn in sorted(hooks):
getattr(macmod, fn)()
# -------------------------------------------------------------------------
# render complete HTML pages
# -------------------------------------------------------------------------
with codecs.open(opj(project, "page.html"), 'r', opts.input_enc) as fp:
skeleton = fp.read()
hooks = [a for a in dir(macmod) if a.startswith("hook_html_")]
for page in sorted(pages, key=lambda p: p["url"]):
print("info : render %s" % page.url)
# replace expressions and statements in page.html
macmod.page = page
macros["page"] = page
macros["__content__"] = page.html
out = regx_eval.sub(repl_eval, skeleton)
out = regx_exec.sub(repl_exec, out)
# un-escape escaped python code blocks
out = regx_escp.sub(repl_escp, out)
# make relative links absolute
out = regx_rurl.sub(repl_rurl, out)
# apply final hooks
for fn in hooks:
out = getattr(macmod, fn)(out, page)
# write HTML page
fname = page.fname.replace(dir_in, dir_out)
fname = re.sub(MKD_PATT, ".html", fname)
with codecs.open(fname, 'w', opts.output_enc) as fp:
fp.write(out)
print("success: built project")
# =============================================================================
# serve site
# =============================================================================
def serve(project, opts):
"""Temporary serve a site project."""
root = opj(project, opts.output_dir)
if not os.listdir(project):
print("error : output dir is empty (build project first!), abort")
sys.exit(1)
os.chdir(root)
server = HTTPServer(('', opts.port), SimpleHTTPRequestHandler)
server.serve_forever()
# =============================================================================
# options
# =============================================================================
def options():
"""Parse and validate command line arguments."""
usage = ("Usage: %prog --init [path/to/project]\n"
" %prog --build [OPTIONS] [path/to/project]\n"
" %prog --serve [OPTIONS] [path/to/project]\n"
"\n"
" Project path is optional, '.' is used as default.")
op = optparse.OptionParser(usage=usage)
op.add_option("-i" , "--init", action="store_true", default=False,
help="init project")
op.add_option("-b" , "--build", action="store_true", default=False,
help="build project")
op.add_option("-s" , "--serve", action="store_true", default=False,
help="serve project")
og = optparse.OptionGroup(op, "Build options")
og.add_option("", "--base-url", default="/", metavar="URL",
help="base url for relative links (default: /)")
og.add_option("" , "--ignore", default=r"^\.|~$", metavar="REGEX",
help="input files to ignore (default: '^\.|~$')")
og.add_option("" , "--md-ext", default=[], metavar="EXT",
action="append", help="enable a markdown extension")
og.add_option("", "--input-enc", default="utf-8", metavar="ENC",
help="encoding of input pages (default: utf-8)")
og.add_option("", "--output-enc", default="utf-8", metavar="ENC",
help="encoding of output pages (default: utf-8)")
og.add_option("", "--filename-enc", default="utf-8", metavar="ENC",
help="encoding of file names (default: utf-8)")
og.add_option("", "--output-dir", default="output", metavar="DIR",
help="output directory (default: ./output)")
op.add_option_group(og)
og = optparse.OptionGroup(op, "Serve options")
og.add_option("" , "--port", default=8080,
metavar="PORT", type="int",
help="port for serving (default: 8080)")
op.add_option_group(og)
opts, args = op.parse_args()
if opts.init + opts.build + opts.serve < 1:
op.print_help()
op.exit()
opts.project = args and args[0] or "."
return opts
# =============================================================================
# main
# =============================================================================
def main():
opts = options()
sys.path.insert(0, os.getcwd())
if opts.init:
init(opts.project, opts)
if opts.build:
build(opts.project, opts)
if opts.serve:
serve(opts.project, opts)
if __name__ == '__main__':
main()