blog

Personal blog
git clone git://git.laack.co/blog.git
Log | Files | Refs

indexer.py (1149B)


      1 import subprocess
      2 
      3 results = subprocess.run(["bash" , "-c",  "stat posts/* --format '%n %W'"], capture_output=True)
      4 stdout = results.stdout.decode('utf-8')
      5 
      6 lines = stdout.split("\n")
      7 
      8 class File:
      9     def __init__(self, file_name, time):
     10         self.file_name = file_name
     11         self.time = time
     12 
     13         if len(file_name.split(".")) > 1:
     14             self.extension = file_name.split(".")[-1]
     15         else:
     16             self.extension = ""
     17 
     18         with open(file_name, "r") as file:
     19             self.title = file.readline().replace("#", "")
     20 
     21     def __lt__(self, other):
     22         return self.time < other.time
     23 
     24     def __str__(self):
     25         return f"- [https://blog.laack.co/{str(self.file_name)}]({self.title.strip()})\n"
     26 
     27 files = []
     28 
     29 for line in lines:
     30     if line:
     31         split = line.split(" ")
     32         file = File(split[0], int(split[1]))
     33         files.append(file)
     34 
     35 
     36 files.sort()
     37 file_out = ""
     38 
     39 for file in files:
     40     if file.extension == "md" and file.file_name != "posts/index.md":
     41         file_out += str(file)
     42 
     43 site = f"""
     44 
     45 
     46 ## Most Recent Blog Posts
     47 
     48 {file_out}
     49 """
     50 
     51 with open("posts/index.md", "w") as file:
     52     file.write(site)
     53