#!/usr/bin/python ''' ttbp: tilde town blogging platform (also known as the feels engine) a console-based blogging program developed for tilde.town copyright (c) 2016 ~endorphant (endorphant@tilde.town) core.py: this is a core handler for some ttbp standalone/output functions Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. the complete codebase is available at: https://github.com/modgethanc/ttbp ''' import os import time import subprocess import re import mistune import json import chatter SOURCE = os.path.join("/home", "endorphant", "projects", "ttbp", "bin") USER = os.path.basename(os.path.expanduser("~")) PATH = os.path.join("/home", USER, ".ttbp") LIVE = "http://tilde.town/~" WWW = os.path.join(PATH, "www") CONFIG = os.path.join(PATH, "config") DATA = os.path.join(PATH, "entries") FEED = os.path.join(SOURCE, "www", "index.html") DOCS = os.path.join(SOURCE, "www", "help.html") NOPUB = os.path.join(CONFIG, "nopub") SETTINGS = {} HEADER = "" FOOTER = "" FILES = [] def load(ttbprc={}): ''' get all them globals set up!! ''' global HEADER global FOOTER global SETTINGS HEADER = open(os.path.join(CONFIG, "header.txt")).read() FOOTER = open(os.path.join(CONFIG, "footer.txt")).read() SETTINGS = ttbprc load_files() def reload_ttbprc(ttbprc={}): ''' reloads new ttbprc into current session ''' global SETTINGS SETTINGS = ttbprc def load_files(): ''' file loader * reads user's nopub file * loads all valid filenames that are not excluded in nopub to global files list ''' global FILES FILES = [] for filename in os.listdir(DATA): if nopub(filename): link = os.path.join(WWW, os.path.splitext(os.path.basename(filename))[0]+".html") if os.path.exists(link): subprocess.call(["rm", link]) continue filename = os.path.join(DATA, filename) if os.path.isfile(filename) and valid(filename): FILES.append(filename) FILES.sort() FILES.reverse() ## html outputting def write(outurl="default.html"): ''' main page renderer * takes everything currently in FILES and writes a single non-paginated html file * calls write_page() on each file to make permalinks ''' outfile = open(os.path.join(WWW, outurl), "w") outfile.write("\n\n") for line in HEADER: outfile.write(line) outfile.write("\n") for filename in FILES: write_page(filename) for line in write_entry(filename): outfile.write(line) outfile.write("\n") for line in FOOTER: outfile.write(line) outfile.close() return os.path.join(LIVE+USER,os.path.basename(os.path.realpath(WWW)),outurl) def write_page(filename): ''' permalink generator * makes a page out of a single entry for permalinking, using filename/date as url ''' outurl = os.path.join(WWW, "".join(parse_date(filename))+".html") outfile = open(outurl, "w") outfile.write("\n\n") for line in HEADER: outfile.write(line) outfile.write("\n") for line in write_entry(filename): outfile.write(line) outfile.write("\n") for line in FOOTER: outfile.write(line) outfile.close() return outurl def write_entry(filename): ''' entry text generator * dump given file into entry format by parsing file as markdown * return as list of strings ''' date = parse_date(filename) entry = [ "\t\t
\n", "\t\t" ] raw = [] rawfile = open(os.path.join(DATA, filename), "r") for line in rawfile: raw.append(line) rawfile.close() entry.append("\t\t\t"+mistune.markdown("".join(raw), escape=False, hard_wrap=False)) #for line in raw: #entry.append(line+"\t\t\t") #if line == "\n": # entry.append("
\n\t\t\t") #entry.append("
\n") entry.append("\t\t\t\n") entry.append("\n\t\t""") ## docs outfile.write("""\