2
0
mirror of https://github.com/boostorg/website.git synced 2026-01-29 08:02:20 +00:00

Website: Replace templite files with simple python scripts.

[SVN r78676]
This commit is contained in:
Daniel James
2012-05-27 12:48:30 +00:00
parent d785b0295d
commit 9212e29bc2
11 changed files with 20 additions and 104 deletions

View File

@@ -21,4 +21,3 @@
<li><a href="http://sourceforge.net/projects/boost/files/boost/1.49.0/">Download</a></li>
<li><a href="/doc/libs/1_49_0/">Documentation</a></li>
</ul>

View File

@@ -1051,4 +1051,3 @@
<ul class="menu">
<li><a href="/users/history/old_versions.html">Details</a></li>
</ul>

View File

@@ -50,4 +50,3 @@
</div>
<div class="clear"></div>

View File

@@ -32,10 +32,10 @@ settings = {
}
},
'index-pages' : {
'generated/download-items.html' : 'site-tools/templates/download-template.html',
'generated/history-items.html' : 'site-tools/templates/history-template.html',
'generated/news-items.html' : 'site-tools/templates/news-template.html',
'generated/home-items.html' : 'site-tools/templates/index-src.html'
'generated/download-items.html' : 'site-tools/templates/download-template.py',
'generated/history-items.html' : 'site-tools/templates/history-template.py',
'generated/news-items.html' : 'site-tools/templates/news-template.py',
'generated/home-items.html' : 'site-tools/templates/index-template.py'
},
# See boost_site.pages for matches pattern syntax.
#

View File

@@ -4,7 +4,7 @@
# (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
import os, sys, subprocess, glob, re, time, xml.dom.minidom, codecs
import boost_site.templite, boost_site.pages, boost_site.boostbook_parser, boost_site.util
import boost_site.pages, boost_site.boostbook_parser, boost_site.util
from boost_site.settings import settings
################################################################################
@@ -54,7 +54,7 @@ def update_quickbook(refresh = False):
}
for index_page in settings['index-pages']:
boost_site.templite.write_template(
boost_site.util.write_py_template(
index_page,
settings['index-pages'][index_page],
index_page_variables)

View File

@@ -1,87 +0,0 @@
# Templite
#
# Taken from:
## {{{ http://code.activestate.com/recipes/496702/ (r3)
#
# Modified to use unicode, and added convenience method.
#
# Licensed under the PSF License
import re
# TODO: Integrate with main class?
def write_template(location, template, params):
template_file = open(template, 'r')
try:
template_format = template_file.read().decode('utf-8')
finally:
template_file.close()
t = Templite(template_format)
output = t(params)
output_file = open(location, 'w')
try:
output_file.write(output.encode('utf-8'))
finally:
output_file.close()
class Templite(object):
delimiter = re.compile(r"\$\{(.*?)\}\$", re.DOTALL)
def __init__(self, template):
self.tokens = self.compile(template)
@classmethod
def from_file(cls, file):
"""
loads a template from a file. `file` can be either a string, specifying
a filename, or a file-like object, supporting read() directly
"""
if isinstance(file, basestring):
file = open(file)
return cls(file.read())
@classmethod
def compile(cls, template):
tokens = []
for i, part in enumerate(cls.delimiter.split(template)):
if i % 2 == 0:
if part:
tokens.append((False, part.replace("$\\{", "${")))
else:
if not part.strip():
continue
lines = part.replace("}\\$", "}$").splitlines()
margin = min(len(l) - len(l.lstrip()) for l in lines if l.strip())
realigned = "\n".join(l[margin:] for l in lines)
code = compile(realigned, "<templite %r>" % (realigned[:20],), "exec")
tokens.append((True, code))
return tokens
def render(__self, __namespace = None, **kw):
"""
renders the template according to the given namespace.
__namespace - a dictionary serving as a namespace for evaluation
**kw - keyword arguments which are added to the namespace
"""
namespace = {}
if __namespace: namespace.update(__namespace)
if kw: namespace.update(kw)
def emitter(*args):
for a in args: output.append(unicode(a))
def fmt_emitter(fmt, *args):
output.append(fmt % args)
namespace["emit"] = emitter
namespace["emitf"] = fmt_emitter
output = []
for is_code, value in __self.tokens:
if is_code:
eval(value, namespace)
else:
output.append(value)
return "".join(output)
# shorthand
__call__ = render

View File

@@ -42,3 +42,17 @@ def write_template(dst_path, template_path, data):
output = re.sub(r' +$', '', output, flags = re.M)
out = open(dst_path, "w")
out.write(output.encode('utf-8'))
def write_py_template(dst_path, template_path, data):
data['emit'] = Emitter()
exec(open(template_path).read(), {}, data)
out = open(dst_path, 'w')
out.write(data['emit'].output.encode('utf-8'))
class Emitter:
def __init__(self):
self.output = ''
def __call__(self, x):
self.output += x

View File

@@ -1,4 +1,3 @@
${
from boost_site.util import htmlencode
emit('<ul class="toc">\n');
@@ -30,4 +29,3 @@ for x in downloads:
emit('<a href="%s">Documentation</a>' % htmlencode(entry.documentation))
emit('</li>\n')
emit('</ul>\n')
}$

View File

@@ -1,4 +1,3 @@
${
from boost_site.util import htmlencode
title = 'Boost Version History'
@@ -24,4 +23,3 @@ for entry in entries:
emit('<a href="%s">Documentation</a>' % htmlencode(entry.documentation))
emit('</li>\n')
emit('</ul>\n')
}$

View File

@@ -1,4 +1,3 @@
${
from boost_site.util import htmlencode
# TODO: This is duplicated from other places, should only be set once?
@@ -53,4 +52,3 @@ emit('<p><a href="/users/news/">More News...</a> (<a href=feed/news.rss">RSS</a>
emit('</div>\n\n')
emit('<div class="clear"></div>\n')
}$

View File

@@ -1,4 +1,3 @@
${
from boost_site.util import htmlencode
entries = pages.match_pages(['feed/news/*.qbk', 'feed/history/*.qbk|released'], 5)
@@ -31,4 +30,3 @@ for entry in entries:
emit('<a href="%s">Documentation</a>' % htmlencode(entry.documentation))
emit('</li>\n')
emit('</ul>')
}$