|
| 1 | +import os |
| 2 | +import shutil |
| 3 | +from flask import Flask, render_template |
| 4 | +import docutils.core |
| 5 | +from datetime import datetime |
| 6 | + |
| 7 | +app = Flask(__name__) |
| 8 | + |
| 9 | +def rst_to_html(rst_content): |
| 10 | + """Convert RST content to HTML.""" |
| 11 | + overrides = { |
| 12 | + 'input_encoding': 'unicode', |
| 13 | + 'output_encoding': 'unicode', |
| 14 | + 'report_level': 5, # Suppress all messages |
| 15 | + 'halt_level': 5, # Don't halt on any level |
| 16 | + } |
| 17 | + return docutils.core.publish_string( |
| 18 | + source=rst_content, |
| 19 | + writer_name='html', |
| 20 | + settings_overrides=overrides |
| 21 | + ) |
| 22 | + |
| 23 | +def get_content(filename): |
| 24 | + """Read and convert RST content from file.""" |
| 25 | + with open(os.path.join('content', filename), 'r', encoding='utf-8') as f: |
| 26 | + return rst_to_html(f.read()) |
| 27 | + |
| 28 | +def build_page(template, content_file, output_file): |
| 29 | + """Build a single page.""" |
| 30 | + with app.app_context(): |
| 31 | + content = get_content(content_file) |
| 32 | + html = render_template(template, |
| 33 | + content=content, |
| 34 | + current_year=datetime.now().year) |
| 35 | + |
| 36 | + # Ensure the directory exists |
| 37 | + os.makedirs(os.path.dirname(output_file), exist_ok=True) |
| 38 | + |
| 39 | + with open(output_file, 'w', encoding='utf-8') as f: |
| 40 | + f.write(html) |
| 41 | + |
| 42 | +def main(): |
| 43 | + # Create output directory |
| 44 | + if os.path.exists('_site'): |
| 45 | + shutil.rmtree('_site') |
| 46 | + os.makedirs('_site') |
| 47 | + |
| 48 | + # Build pages |
| 49 | + build_page('index.html', 'index.rst', '_site/index.html') |
| 50 | + build_page('publications.html', 'publications.rst', '_site/publications/index.html') |
| 51 | + build_page('experience.html', 'experience.rst', '_site/experience/index.html') |
| 52 | + |
| 53 | + # Copy static files |
| 54 | + shutil.copytree('static', '_site/static', dirs_exist_ok=True) |
| 55 | + |
| 56 | +if __name__ == '__main__': |
| 57 | + main() |
0 commit comments