132 lines
5.1 KiB
Python
132 lines
5.1 KiB
Python
# Generate Main blog list for [[file:content/blogs/blog-index.org]]
|
|
# The following code block looks at all org files(except [[file:content/blogs/blog-index.org]] ) in the [[file:content/blogs/]] directory and finds there title, creation date, and last modified date and then creates an org table, then this table is stored as a string in the blog_table variable
|
|
# # begin_src python :results output raw :exports results
|
|
# #+name: blog-table-generator
|
|
|
|
# [[file:../main.org::blog-table-generator][blog-table-generator]]
|
|
import os
|
|
from datetime import datetime
|
|
|
|
# path of the blog directory
|
|
blogs_directory = "./content/blogs"
|
|
|
|
|
|
# get a list of all things in blogs_directory
|
|
blog_list = os.listdir(blogs_directory)
|
|
max_lines_to_scan = 10
|
|
|
|
#Only include files which are .org and do no include the main Blog-list.org file which serves as an index
|
|
b = []
|
|
for i in range(0,len(blog_list)):
|
|
if blog_list[i].endswith(".org") and blog_list[i] != 'blog-index.org':
|
|
b.append(blog_list[i])
|
|
blog_list=b
|
|
|
|
#Sort blog files by last modified time
|
|
# blog_list.sort(key=lambda x: os.path.getmtime(blogs_directory+x),reverse=True)
|
|
|
|
# variable to store output table
|
|
blog_table = ""
|
|
|
|
#prints the heading of the list.
|
|
blog_table += ("|-\n")
|
|
blog_table += ("|Title|Last Modified|Created|\n")
|
|
|
|
titles = []
|
|
title_found = False
|
|
creation_dates = []
|
|
creation_found = False
|
|
last_modified_found = False
|
|
last_modified_dates = []
|
|
|
|
# finds the title, last modified time and creation time
|
|
for blog_no in range(0,len(blog_list)):
|
|
with open(blogs_directory +'/'+ blog_list[blog_no], "r") as file:
|
|
creation_found = False
|
|
last_modified_found = False
|
|
title_found = False
|
|
for line_no in range(max_lines_to_scan):
|
|
try:
|
|
line = str(next(file))
|
|
except Exception as e:
|
|
break
|
|
if (line.lower()).startswith('#+title') and title_found == False:
|
|
titles.append((line.split(':',1)[1]).strip())
|
|
title_found = True
|
|
elif (line.lower()).startswith('#+created') and creation_found == False:
|
|
creation_dates.append((line.split(':',1)[1]).strip())
|
|
creation_found = True
|
|
elif (line.lower()).startswith('#+last_modified') and last_modified_found == False:
|
|
last_modified_dates.append((line.split(':',1)[1]).strip())
|
|
last_modified_found = True
|
|
if title_found == True and last_modified_found == True and creation_found == True:
|
|
break
|
|
if last_modified_found == False:
|
|
last_modified_dates.append('unknown')
|
|
if creation_found == False:
|
|
creation_dates.append('unknown')
|
|
if title_found == False:
|
|
titles.append('unknown')
|
|
blog_table += ("|-\n")
|
|
|
|
def sort_multiple_lists(*args):
|
|
"""This function sorts multiple lists according to the sorting of the first list and returns the sorted lists as a tuple"""
|
|
|
|
#Extract the firstlist from the argumnts and find its length
|
|
first_list = args[0]
|
|
list_length = len(first_list)
|
|
|
|
# Group all corresponding elements into a list of tuples
|
|
tuples_list = list(zip(*args))
|
|
|
|
# Sort the tuples according to there first elements
|
|
sorted_tuples_list = sorted(tuples_list,key = lambda x: first_list.index(x[0]),reverse = False)
|
|
|
|
# Extract the sorted lists and return them
|
|
return(list(zip(*sorted_tuples_list)))
|
|
|
|
|
|
# Sort all lists according to creation_date
|
|
sorted_creation_dates,sorted_titles,sorted_last_modified_dates,sorted_blog_list = sort_multiple_lists(creation_dates,titles,last_modified_dates,blog_list)
|
|
|
|
for blog_no in range(0,len(blog_list)):
|
|
blog_table += ("|"+ '[[file:'+ sorted_blog_list[blog_no]+ ']' + '['+sorted_titles[blog_no] + ']]'+ '|' +sorted_last_modified_dates[blog_no] + '|' + sorted_creation_dates[blog_no] + '|\n')
|
|
# blog-table-generator ends here
|
|
|
|
# Write the blog table into [[file:content/blogs/blog-index.org]]
|
|
|
|
# [[file:../main.org::*Write the blog table into \[\[file:content/blogs/blog-index.org\]\]][Write the blog table into [[file:content/blogs/blog-index.org]]:1]]
|
|
def write_output_to_org_file(file_path, name, output):
|
|
erase_until_newline_after_name(file_path)
|
|
with open(file_path, 'r') as file:
|
|
data = file.readlines()
|
|
|
|
with open(file_path, 'w') as file:
|
|
for line in data:
|
|
if '#+name: ' in line and name in line:
|
|
file.write(line)
|
|
file.write(output)
|
|
else:
|
|
file.write(line)
|
|
|
|
def erase_until_newline_after_name(filename):
|
|
with open(filename, 'r') as file:
|
|
lines = file.readlines()
|
|
|
|
with open(filename, 'w') as file:
|
|
erase = False
|
|
for line in lines:
|
|
if "#+name" in line:
|
|
erase = True
|
|
file.write(line)
|
|
if erase:
|
|
if line == '\n':
|
|
erase = False
|
|
file.write(line)
|
|
else:
|
|
file.write(line)
|
|
|
|
write_output_to_org_file( "content/blogs/blog-index.org", "blog-table-complete", blog_table)
|
|
|
|
print("Blog table generated successfully")
|
|
# Write the blog table into [[file:content/blogs/blog-index.org]]:1 ends here
|