Last active
December 25, 2015 14:10
-
-
Save showa-yojyo/67722462d9eb731cd4ed to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
""" mkrefresh.py -- Generate redirecting HTML files. | |
""" | |
from jinja2 import Environment | |
import re | |
from glob import iglob | |
URL='https://showa-yojyo.github.io/notebook/' | |
HTML_TEMPLATE = '''\ | |
<!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="ja"> | |
<head> | |
<head> | |
<title>{{ title }}</title> | |
<meta http-equiv="Content-type" content="text/html; charset=utf-8" /> | |
<meta http-equiv="Content-style-type" content="text/css" /> | |
<meta http-equiv="refresh" content="{{ seconds }}; URL={{ url }}" /> | |
<meta name="author" content="showa_yojyo@hotmail.com" /> | |
<link rel="stylesheet" href="_static/prefab.css" type="text/css" /> | |
</head> | |
<body> | |
<div class="document"> | |
<div class="documentwrapper"> | |
<div class="body"> | |
<p>This document, {{ title }}, has moved to the following location: <br /> | |
<a href="{{ url }}">{{ url }}</a></p> | |
</div> | |
</div> | |
<div class="clearer"></div> | |
</div> | |
</body> | |
</html> | |
''' | |
def main(): | |
"""The main function. | |
Returns: | |
None. | |
""" | |
env = Environment(autoescape=False) | |
templ = env.from_string(HTML_TEMPLATE) | |
for i in iglob('*.html'): | |
with open(i, mode='r', encoding='utf-8') as source: | |
text = source.read() | |
title = re.findall(r'<title>+(.*)</title>+', text, re.I | re.M) | |
# Overwrite the original file that Sphinx generated. | |
with open(i, mode='w', encoding='utf-8') as dest: | |
print(templ.render( | |
seconds=30, | |
title=title[0], | |
url="{url}{html}".format(url=URL, html=i),), file=dest) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment