INFO
This is no longer in use. All code is stored on GitHub instead.
Generate HTML from Source Code
Pandoc doesn’t support code files, so I’m reusing the same template but the replacement is done through standard Python functionality.
def compile_code(source_path, destination_path):
with open(source_path, 'r') as f:
content = html.escape(f.read())
with open(TEMPLATE, 'r') as f:
template = f.read()
content_with_html_tags = f'<pre><code>{content}</code></pre>'
template_with_body = template.replace('$body$', content_with_html_tags)
with open(destination_path, 'w') as f:
f.write(template_with_body)
Note that it’s important to add html.escape
when reading the file. The code snippet for this functionality would already cause problems for example, because it contains HTML tags (pre
and code
).
Generate index file for source code
When navigating through a code repository, it’s useful to have an overview of all available source files. Locally this file doesn’t exist because the code editor provides an overview of all files. When this website is deployed online, there is no such overview. An index file is generated on the fly to give an overview of all files in a single code repository.
The implementation for generating HTML files uses the type SourceFile
, which represents the original file that must be transformed into HTML. For the index file that is generated on the fly, I have introduced the concept of a VirtualSourceFile
, which is a source file that does not have that ‘original’ file on disk.
With this concept, the index file can be treated like any other file.