Small include script for static sites generation

At the moment static sites are becoming more and more popular. So sites you generate in your file system and then publish it via ftp, shell or firebase cli for example.

Most such site generators have their own reasonable system that you should study first and certain rules plugins etc..

While they don't really do anything other than take certain bits of text from different places and merge them into one page.

In this way I have also made a small generator for ourselves with which I have already built up a number of static sites. It works well with small sites and basically sites. Think about certain site with about 1000 pages, If more then it becomes complicated to search and edit pages without a database. If you need thousands of pages, You can generate pages or information from database in file system in a similar way but using Django commands and Django templates reder_to_string for example but the principle can remain the same.

#!/usr/bin/python

import sys, getopt, os
import re
import glob

class Include:
tag = ""
replacement = ""

def get_replacement(tag):
pattern = re.compile("{% include '([^']*)' %}")
match = re.match(pattern, tag)
content = open(match.group(1), "r").read()
return content

def include(content):
pattern = re.compile("{% include '[^']*' %}")
includes = re.findall(pattern, content)
for include in includes:
io = Include()
io.tag = include
io.replacement = get_replacement(include)
content = content.replace(io.tag, io.replacement)
return content


def main(argv):
inputfiles = glob.glob("input/*.html")
for infile in inputfiles:
head, tail = os.path.split(infile)
content = open(infile, "r").read()
content = include(content)
outfile = open(tail, "w")
outfile.write(content)
outfile.close()


if __name__ == "__main__":
main(sys.argv[1:])

 


 



An example input file that may look something like this.

<html>
<head>
{% include 'include/tail-meta.html' %}

<style>
{% include 'static/output.css' %}
</style>

<title>Contact telefoonnummer</title>
<link rel="canonical" href="https://.today/contact.html">
<meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
<meta name="description" content=" telefoonnummer, betrouwbaar, review verzenden, 

.today opmerking verzenden" />
<meta name="keywords" content="telefoonnummer, betrouwbaar, review verzenden, 

.today opmerking verzenden" />
<meta property="og:title" content="Contact .today telefoonnummer" />
<meta property="og:image" content="/static/images/Social-Media.png" />
<meta property="og:description" content=" telefoonnummer, betrouwbaar, review verzenden, .today opmerking verzenden" />
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "Webpage",
"url": "https://.today/contact.html",
"name": "Contact .today telefoonnummer",
"headline": "Contact .today telefoonnummer",
"description": " telefoonnummer, betrouwbaar, review verzenden, 

.today opmerking verzenden",
"mainEntityOfPage": {
"@type": "WebPage",
"@id": "http://

.today/contact.html"
},
"publisher": {
"@type": "Organization",
"name": ".today",
"logo": {
 

Comments