Majorendian Code

Home Tutorials Sheets Code Projects Files

factorial.pl

Tail-Recursive factorial function in perl


statics.py

A simple python-based static web generation tool I used to generate this website

It has na '.ifile' directive which I use to include the script itself in this here webzone

#!/usr/bin/python3

# BSD 3-Clause License
# Copyright (c) 2021, Ernest Deák
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
#    list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its
#    contributors may be used to endorse or promote products derived from
#    this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
#
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import re
import statics_config

def contents(filename):
  with open(filename) as fp:
    return fp.read()

class Page (object):
  standardKeys = {
    ".iheader" : contents("header.html"),
    ".ipages" : contents("pages.html"),
    ".ifooter" : contents("footer.html"),
    ".idefaultjs" : ""
  }
  def __init__(self, pagename, templatefile, contentfile, re={}):
    self.name = pagename
    self.template = contents(templatefile)
    self.keys = re
    self.content = contents(contentfile)
    self.text = None

  def replaceKeys(self, extrakeys={}):
    for k,v in Page.standardKeys.items():
      self.template = self.template.replace(k, v)

    for k,v in self.keys.items():
      self.template = self.template.replace(k, v)

    for k,v in extrakeys.items():
      self.template = self.template.replace(k, v)


    self.text = self.template.replace(".icontent", self.content)

    # Regular expressions last
    # so that we can use them even in our
    # content text

    for m in re.finditer(r"(\s*\.ifile )(\S+)\n", self.text):
      if(m):
        if(m.group(0)):
          print("Found .ifile directive, attempting to include file:", m.group(2))
          self.text = self.text.replace(
              m.group(1) + m.group(2), contents(m.group(2)))

    # Multi-language support via regex
    for m in re.finditer(r"\s*%(\w\w){(.+?)}%\n", self.text, flags=re.S):
      if m:
        print("Found language directive:",m.group(1))
        self.text = self.text.replace(
            "%"+m.group(1)+"{"+m.group(2)+"}%", "
"+m.group(2)+"
") def getContent(self): if(not self.text): raise Exception("Template has not been processed") return self.text class Content (object): def __init__(self, name, subpage=False, template="page.html.tmpl"): self.name = name self.path = "./" if(type(subpage) == str): self.path = "./" + subpage + "/" self.in_path = self.path + name.lower() + ".in.html" self.out_path = self.path + name.lower() + ".html" self.content = contents(self.in_path) self.page = Page(name, template, self.in_path, { "$subtitle" : self.name, }) self.page.replaceKeys() self.content = self.page.getContent() def writePage(self): print("Writting page",self.name,"as",self.out_path) with open(self.out_path,"w") as fp: fp.write(self.content) print("Done") if __name__ == "__main__": import os # Template construction for C in statics_config.pages: C.writePage() print("Moving file",C.out_path,"into repository") os.system("mv "+C.out_path+" repo/") # Configuration actions if(statics_config.gitmessage): print("Commiting changes to repo") os.system("cd repo/ && git commit -a -m '"+statics_config.gitmessage+"'") if(statics_config.gitpush): print("Pushing changes to upstream") os.system("cd repo/ && git push")

statics_config.py

Accompanying config file for statics.py

# Define pages
from statics import Content
pages = [
  Content("Index",False,"index.html.tmpl"),
  Content("Code")
]
# Define posts
posts = []
# Wether or not we should push to github after commit
gitpush=True
# Message to commit
gitmessage="github.io page update"
# gitmessage=False