All Blog Posts

1. Overview

2. Write a python script that prints out all files as a result

Modified from Stack Overflow Answer on How do I list all files of a directory. I set the path to the local path since that is where all of the blogs posts are stored. This should filter out any directories that may exist in the future.

from os import listdir
from os.path import isfile, join

mypath = "./"

onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]

## enter length of directory!
print("- [+] List completed - # of files: {}".format(len(onlyfiles))) 

- [+] List completed - # of files: 69

3. Take this list and filter on for *.html files

While the 'l' in html may not be the only extention that ends with 'l' in existence. It is good enough for my personal blog. The risk of a potential inclusion with another extension that ends in 'l' is acceptable. This is similar to the risk I'm accepting with only parsing for dates at the beginning, and exluding this file starting with 'a'.

htmlList = []
for line in onlyfiles:
  if (line[-1] == 'l') and (line[0] != 'a'):
    htmlList.append(line)

print("- [+] # of HTML files: {}".format(len(htmlList)))
- [+] # of HTML files: 30

4. Convert html files to org links

blogPostLinks = []  
for line in htmlList:
  getDate = line[:10]
  getTitle = line[11:-5]
  cleanTitle = getTitle.replace("-", " ")
  orgLink = "- [[file:./" + line + "][" + getDate + " " + cleanTitle + "]]"
  blogPostLinks.append(orgLink)

print("- [+] Converted {} files into {} valid links".format(len(htmlList), len(blogPostLinks)))
- [+] Converted 30 files into 30 valid links

5. The Final Code

Note, the actual code block below has been modified to show the text that the header will execute. You should be able to recreate using the visual header below, and the code below, along with all of the code block (except Overview) above, named how they are named between the <<import-here>> in order.

#+BEGIN _SRC python :results output raw :exports results :noweb yes :tangle yes

<<file-list>>
<<html-filter>>
<<org-links>>
for line in blogPostLinks:
  print(line)

Then what it would look like if from if you exported the above blog into a webpage with :exports code instead of the current header of :exports results

from os import listdir
from os.path import isfile, join

mypath = "./"

onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]

## enter length of directory!
print("- [+] List completed - # of files: {}".format(len(onlyfiles))) 

htmlList = []
for line in onlyfiles:
  if (line[-1] == 'l') and (line[0] != 'a'):
    htmlList.append(line)

print("- [+] # of HTML files: {}".format(len(htmlList)))
blogPostLinks = []  
for line in htmlList:
  getDate = line[:10]
  getTitle = line[11:-5]
  cleanTitle = getTitle.replace("-", " ")
  orgLink = "- [[file:./" + line + "][" + getDate + " " + cleanTitle + "]]"
  blogPostLinks.append(orgLink)

print("- [+] Converted {} files into {} valid links".format(len(htmlList), len(blogPostLinks)))
for line in blogPostLinks:
  print(line)

Date: 2023-01-09

Author: Russell Brinson