from django.template import Template, RequestContext from django.http import HttpResponse, Http404 from django.conf import settings from django.shortcuts import render from os import path def articleVars(fn): file, url = file_for_url(fn) vars = {} with open(file, 'r') as f: l = f.readline().strip() while l: ll = l.split(':', 1) h = ll[0].strip() if len(ll) > 1: v = ll[1].strip() else: v = "" vars[h.lower()] = v l = f.readline().strip() return vars def loadArticle(request, url, f): vars = {'url': url} l = f.readline().strip() while l: ll = l.split(':', 1) h = ll[0].strip() if len(ll) > 1: v = ll[1].strip() else: v = "" if h.lower() == "display": return showarticle(request, v) vars[h.lower()] = v l = f.readline().strip() vars['article_text'] = f.read() if vars.get('previousfile'): f = vars['previousfile'] print(f) vars['previous_url'] = '/' + f prev = articleVars(f) vars['previous_article'] = prev.get('title', '') if vars.get('nextfile'): f = vars['nextfile'] vars['next_url'] = '/' + f nxt = articleVars(f) vars['next_article'] = nxt.get('title', '') return render(request, 'article.html', vars) def file_for_url(file): if file.startswith('/'): file = file[1:] full_path = path.join(settings.DOC_ROOT, file) if not path.exists(full_path): raise Http404 if path.isdir(full_path): full_path = path.join(full_path, 'index') return full_path, '/' + file def showarticle(request, file): full_path, url = file_for_url(file) with open(full_path, 'r') as f: return loadArticle(request, url, f)