index.md


this page is content from a text file rendered by django
if you find an easier or faster way to implement this please let me know!
this is part of a bigger plan, some ideas I stumbled into years ago - more here
later I will extend this app so you can optionally detect languages run the code in files
for multiple purposes; troubleshooting, testing, or when prototyping django apps
more minimalist blog pages

the idea is you can extend this easily, allowing multiple users to extend the content using modern editors with plugin automation, or pipelines running various conversion apps like pandoc to automatically deploy html, navigation is created automatically

the code

url routing

  • when you view blog/ you see a list of your folders
  • when you click a folder it displays the files in that folder
  • when you click a file it displays the contents of the file

"""                                                                                                                                                
mdblog urls                                                                                                                                    
"""                                                                                                                                                
                                                                                                                                                     
from django.urls import path, include                                                                                                              
from . import views                                                                                                                                
                                                                                                                                                   
urlpatterns = [
    path('', views.blog, name='blog'),
    path('<folder>/', views.view_folder),
    path('<folder>/<file>', views.view_file)
]     


views


from django.shortcuts import render
from django.http import HttpResponse 
import glob
import os

MDBLOGFOLDER = '/path/to/textfiles/' #normally you'd import this

MDPAGES = glob.glob(MDBLOGFOLDER + '/*/')

def blog(request):
    """return blog folders"""
    directories = next(os.walk(MDBLOGFOLDER))[1]
    return render(request, 'mdblog/directories.html', {'directories':directories})

def file_get_contents(filename):
    """contents of one file"""
    with open(filename) as f:
        return f.read()

def view_folder(request,folder):
    """list files in one folder"""
    foldername = folder
    folder = MDBLOGFOLDER + folder
    html = r"" % folder
    html += ""
    if os.path.isdir(folder):
        onlyfiles = [f for f in os.listdir(folder) if os.path.isfile(os.path.join(folder, f))]
        for f in onlyfiles:
            html += ("br\n")
            html += "a class=\"mdl-button \" href=%s%s/a" % (f, f)
    else:
        html += "
none " return render(request, 'mdblog/folder.html', { 'html': html, 'folder': folder, }) def view_file(request, folder, file): """show one file""" html = "" html += 'h3%s/h % file contents = file_get_contents(MDBLOGFOLDER + '/' + folder + '/' + file) html_end = "" return render(request, 'mdblog/file.html', { 'html':html, 'html_end':html_end, 'contents': contents})

note: I had to remove some html markup so the codeblock didn't break