#!/usr/bin/env python3 """A simple blog generator with Pandoc as backend.""" # TODO: auto retouch: prompt for git commit amend after touching # (display commit message to avoid amending the wrong commit) # pylint: disable=too-many-lines import argparse from contextlib import contextmanager import copy import curses import datetime import email.utils import fileinput import io import http.client import http.server import multiprocessing import os import re import shutil import signal import string import subprocess import sys import tempfile import time import urllib.parse import blessed import bs4 import colorama import dateutil.parser import dateutil.tz import lxml.etree as ET from bs4 import UnicodeDammit from pprint import pprint import requests import toml from rss import * from utils import utils from config.config import * from generators import generators from cli import cli def edit_existing_post(args): selector = utils.PostSelector(blessed.Terminal(), utils.list_posts()) selection = selector.select() if selection: print(selection) cli.edit_post_with_editor(selection) else: return 1 def main(): """CLI interface.""" description = "Simple blog generator in Python with Pandoc as backend." parser = argparse.ArgumentParser(description=description) subparsers = parser.add_subparsers(dest="action") subparsers.required = True parser_new_post = subparsers.add_parser( "new_post", aliases=["n", "new"], description="Create a new post with metadata pre-filled.") parser_new_post.add_argument("title", help="title of the new post") parser_new_post.set_defaults(func=cli.new_post_cli) parser_new_post = subparsers.add_parser( "touch", aliases=["t", "tou"], description="""Touch an existing post, i.e., update its timestamp to current time. Why is this ever useful? Well, the timestamp filled in by new_post is the time of creation, but one might spend several hours after the creation of the file to finish the post. Sometimes the post is even created on one day and finished on another (say created at 11pm and finished at 1am). Therefore, one may want to retouch the timestamp before publishing.""") parser_new_post.add_argument("filename", help="path or basename of the source file, " "e.g., 2015-05-05-new-blog-new-start.md") parser_new_post.set_defaults(func=cli.touch_cli) parser_generate = subparsers.add_parser( "generate", aliases=["g", "gen"], description="Generate new or changed objects.") parser_generate.set_defaults(func=cli.generate) parser_regenerate = subparsers.add_parser( "regenerate", aliases=["r", "regen"], description="Regenerate the entire blog afresh.") parser_regenerate.set_defaults(func=cli.regenerate) parser_new_post = subparsers.add_parser( "preview", aliases=["p", "pre"], description="Serve the blog locally and auto regenerate upon changes.") parser_new_post.set_defaults(func=cli.preview) parser_new_post = subparsers.add_parser( "deploy", aliases=["d", "dep"], description="Deploy build/ to origin/master without regenerating.") parser_new_post.set_defaults(func=cli.deploy) parser_new_post = subparsers.add_parser( "gen_deploy", aliases=["gd", "gendep"], description="Rebuild entire blog and deploy build/ to origin/master.") parser_new_post.set_defaults(func=cli.gen_deploy) parser_new_post = subparsers.add_parser( "edit", aliases=["e", "ed"], description="Bring up post selector to select post for editing.") parser_new_post.set_defaults(func=edit_existing_post) with utils.init_colorama(): args = parser.parse_args() returncode = args.func(args) exit(returncode) if __name__ == '__main__': main()