def run(conf, env, options): """Subcommand: new -- create a new blog entry the easy way. Either run ``acrylamid new My fresh new Entry`` or interactively via ``acrylamid new`` and the file will be created using the preferred permalink format.""" # we need the actual defaults values commands.initialize(conf, env) ext = conf.get('content_extension', '.txt') fd, tmp = tempfile.mkstemp(suffix=ext, dir='.cache/') editor = os.getenv('VISUAL') if os.getenv('VISUAL') else os.getenv('EDITOR') tt = formats.get((conf.get('metastyle') == 'native', ext), yaml) if not options.title: options.title = raw_input("Entry's title: ") title = (' '.join(options.title)) if not PY3: title = title.decode('utf-8') with io.open(fd, 'w') as f: f.write(tt(title, datetime.now().strftime(conf['date_format']))) entry = readers.Entry(tmp, conf) p = join(conf['content_dir'], dirname(entry.permalink)[1:]) try: os.makedirs(p.rsplit('/', 1)[0]) except OSError: pass filepath = p + ext if isfile(filepath): raise AcrylamidException('Entry already exists %r' % filepath) shutil.move(tmp, filepath) event.create(filepath) if datetime.now().hour == 23 and datetime.now().minute > 45: log.info("notice consider editing entry.date-day after you passed mignight!") if log.level() >= log.WARN: return try: if editor: retcode = subprocess.call([editor, filepath]) elif sys.platform == 'darwin': retcode = subprocess.call(['open', filepath]) else: retcode = subprocess.call(['xdg-open', filepath]) except OSError: raise AcrylamidException('Could not launch an editor') # XXX process detaches... m( if retcode < 0: raise AcrylamidException('Child was terminated by signal %i' % -retcode) if os.stat(filepath)[6] == 0: raise AcrylamidException('File is empty!')
def new(conf, env, title, prompt=True): """Subcommand: new -- create a new blog entry the easy way. Either run ``acrylamid new My fresh new Entry`` or interactively via ``acrylamid new`` and the file will be created using the preferred permalink format.""" # we need the actual defaults values initialize(conf, env) fd, tmp = tempfile.mkstemp(suffix='.txt', dir='.cache/') editor = os.getenv('VISUAL') if os.getenv('VISUAL') else os.getenv('EDITOR') if not title: title = raw_input("Entry's title: ") title = escape(title) with io.open(fd, 'w') as f: f.write(u'---\n') f.write(u'title: %s\n' % title) f.write(u'date: %s\n' % datetime.now().strftime(conf['date_format'])) f.write(u'---\n\n') entry = readers.Entry(tmp, conf) p = join(conf['content_dir'], dirname(entry.permalink)[1:]) try: os.makedirs(p.rsplit('/', 1)[0]) except OSError: pass filepath = p + '.txt' if isfile(filepath): raise AcrylamidException('Entry already exists %r' % filepath) shutil.move(tmp, filepath) event.create(filepath) if datetime.now().hour == 23 and datetime.now().minute > 45: log.info("notice consider editing entry.date-day after you passed mignight!") if not prompt: return try: if editor: retcode = subprocess.call([editor, filepath]) elif sys.platform == 'darwin': retcode = subprocess.call(['open', filepath]) else: retcode = subprocess.call(['xdg-open', filepath]) except OSError: raise AcrylamidException('Could not launch an editor') # XXX process detaches... m( if retcode < 0: raise AcrylamidException('Child was terminated by signal %i' % -retcode) if os.stat(filepath)[6] == 0: raise AcrylamidException('File is empty!')
def init(env, options): """Subcommand: init -- create the base structure of an Acrylamid blog or restore individual files and folders. If the destination directory is empty, it will create a new blog. If the destination directory is not empty it won't overwrite anything unless you supply -f, --force to re-initialize the whole theme. If you need to restore a single file, run $ cd blog && acrylamid init theme/main.html """ if not options.engine: try: import jinja2 options.engine = "jinja2" except ImportError: options.engine = "mako" theme, files = rollout(options.theme, options.engine) # if destination is part of theme, restore it! for src, dest in resolve(options, theme, files): if dest.endswith(options.dest): if ( not isfile(options.dest) or options.overwrite or raw_input("re-initialize %s ? [yn]: " % options.dest) == "y" ): write(src, options.dest) log.info("re-initialized " + options.dest) return if isfile(options.dest): raise AcrylamidException("%s already exists!" % options.dest) if isdir(options.dest) and len(os.listdir(options.dest)) > 0 and not options.overwrite: if raw_input("Destination directory not empty! Continue? [yn]: ") != "y": sys.exit(1) for src, dest in resolve(options, theme, files): if not isdir(dirname(dest)): os.makedirs(dirname(dest)) if options.overwrite or not exists(dest): write(src, dest) event.create(dest) else: event.skip(dest) log.info("Created your fresh new blog at %r. Enjoy!", options.dest)
def create(defaults, item): global USED_WORDPRESS fd, tmp = tempfile.mkstemp(suffix='.txt') with io.open(fd, 'w', encoding='utf-8') as f: f.write(u'---\n') f.write(u'title: %s\n' % safe(item['title'])) if item.get('author') != defaults.get('author'): f.write(u'author: %s\n' % (item.get('author') or defaults.get('author'))) f.write(u'date: %s\n' % item['date'].strftime(conf['date_format'])) #f.write(u'filter: %s\n' % item['filter']) if 'draft' in item: f.write(u'draft: %s\n' % item['draft']) if 'tags' in item: f.write(u'tags: [%s]\n' % ', '.join(item['tags'])) if item.get('description'): f.write(u'description: %s\n' % item['description']) if 'permalink' in item: f.write(u'permalink: %s\n' % item['permalink']) if item.get('type', 'entry') != 'entry': f.write(u'type: %s\n' % item['type']) for arg in options.args: f.write(arg.strip() + u'\n') f.write(u'---\n\n') # this are fixes for WordPress because they don't save HTML but a # stupid mixed-in form of HTML making it very difficult to get either HTML # or reStructuredText/Markdown if USED_WORDPRESS and item['filter'] == 'markdown': item['content'] = item['content'].replace("\n ", " \n") elif USED_WORDPRESS and item['filter'] == 'rst': item['content'] = item['content'].replace('\n ', '\n\n') f.write(item['content'] + u'\n') entry = Entry(tmp, conf) p = join(conf['content_dir'], dirname(entry.permalink)[1:]) try: os.makedirs(p.rsplit('/', 1)[0]) except OSError: pass filepath = p + '.txt' if isfile(filepath) and not options.force: raise AcrylamidException('Entry already exists %r' % filepath) shutil.move(tmp, filepath) event.create('import', filepath)
def create(defaults, item): global USED_WORDPRESS fd, tmp = tempfile.mkstemp(suffix='.txt') with io.open(fd, 'w', encoding='utf-8') as f: f.write(u'---\n') f.write(u'title: %s\n' % safe(item['title'])) if item.get('author') != defaults.get('author'): f.write(u'author: %s\n' % (item.get('author') or defaults.get('author'))) f.write(u'date: %s\n' % item['date'].strftime(conf['date_format'])) #f.write(u'filter: %s\n' % item['filter']) if 'draft' in item: f.write(u'draft: %s\n' % item['draft']) if 'tags' in item: f.write(u'tags: [%s]\n' % ', '.join(item['tags'])) if item.get('description'): f.write(u'description: %s\n' % item['description']) if 'permalink' in item: f.write(u'permalink: %s\n' % item['permalink']) if item.get('type', 'entry') != 'entry': f.write(u'type: %s\n' % item['type']) for arg in options.args: f.write(arg.strip() + u'\n') f.write(u'---\n\n') # this are fixes for WordPress because they don't save HTML but a # stupid mixed-in form of HTML making it very difficult to get either HTML # or reStructuredText/Markdown if USED_WORDPRESS and item['filter'] == 'markdown': item['content'] = item['content'].replace("\n ", " \n") elif USED_WORDPRESS and item['filter'] == 'rst': item['content'] = item['content'].replace('\n ', '\n\n') f.write(item['content']+u'\n') entry = Entry(tmp, conf) p = join(conf['content_dir'], dirname(entry.permalink)[1:]) try: os.makedirs(p.rsplit('/', 1)[0]) except OSError: pass filepath = p + '.txt' if isfile(filepath) and not options.force: raise AcrylamidException('Entry already exists %r' % filepath) shutil.move(tmp, filepath) event.create('import', filepath)
def create(defaults, title, date, author, content, fmt, permalink=None, tags=None): global USED_WORDPRESS fd, tmp = tempfile.mkstemp(suffix='.txt') title = safe(title) with io.open(fd, 'w') as f: f.write(u'---\n') f.write(u'title: %s\n' % title) if author != defaults.get('author', None): f.write(u'author: %s\n' % author) f.write(u'date: %s\n' % date.strftime(conf['date_format'])) f.write(u'filter: [%s, ]\n' % fmt) if tags: f.write(u'tags: [%s]\n' % ', '.join(tags)) if permalink: f.write(u'permalink: %s\n' % permalink) for arg in options.args: f.write(arg.strip() + u'\n') f.write(u'---\n\n') # this are fixes for WordPress because they don't save HTML but a # stupid mixed-in form of HTML making it very difficult to get either HTML # or reStructuredText/Markdown if USED_WORDPRESS and fmt == 'markdown': content = content.replace("\n ", " \n") elif USED_WORDPRESS and fmt == 'rst': content = content.replace('\n ', '\n\n') f.write(content+u'\n') entry = Entry(tmp, conf) p = join(conf['content_dir'], dirname(entry.permalink)[1:]) try: os.makedirs(p.rsplit('/', 1)[0]) except OSError: pass filepath = p + '.txt' if isfile(filepath) and not options.force: raise AcrylamidException('Entry already exists %r' % filepath) shutil.move(tmp, filepath) event.create(filepath)
def create(defaults, title, date, author, content, fmt, permalink=None, tags=None): global USED_WORDPRESS fd, tmp = tempfile.mkstemp(suffix='.txt') title = escape(title) with io.open(fd, 'w') as f: f.write(u'---\n') f.write(u'title: %s\n' % title) if author != defaults.get('author', None): f.write(u'author: %s\n' % author) f.write(u'date: %s\n' % date.strftime(conf['date_format'])) f.write(u'filter: [%s, ]\n' % fmt) if tags: f.write(u'tags: [%s]\n' % ', '.join(tags)) if permalink: f.write(u'permalink: %s\n' % permalink) for arg in options.args: f.write(arg.strip() + u'\n') f.write(u'---\n\n') # this are fixes for WordPress because they don't save HTML but a # stupid mixed-in form of HTML making it very difficult to get either HTML # or reStructuredText/Markdown if USED_WORDPRESS and fmt == 'markdown': content = content.replace("\n ", " \n") elif USED_WORDPRESS and fmt == 'rst': content = content.replace('\n ', '\n\n') f.write(content+u'\n') entry = Entry(tmp, conf) p = join(conf['content_dir'], dirname(entry.permalink)[1:]) try: os.makedirs(p.rsplit('/', 1)[0]) except OSError: pass filepath = p + '.txt' if isfile(filepath) and not options.force: raise AcrylamidException('Entry already exists %r' % filepath) shutil.move(tmp, filepath) event.create(filepath)
def create(defaults, title, date, author, content, fmt, permalink=None, tags=None): global USED_WORDPRESS fd, tmp = tempfile.mkstemp(suffix=".txt") title = safe(title) with io.open(fd, "w") as f: f.write(u"---\n") f.write(u"title: %s\n" % title) if author != defaults.get("author", None): f.write(u"author: %s\n" % author) f.write(u"date: %s\n" % date.strftime(conf["date_format"])) f.write(u"filter: [%s, ]\n" % fmt) if tags: f.write(u"tags: [%s]\n" % ", ".join(tags)) if permalink: f.write(u"permalink: %s\n" % permalink) for arg in options.args: f.write(arg.strip() + u"\n") f.write(u"---\n\n") # this are fixes for WordPress because they don't save HTML but a # stupid mixed-in form of HTML making it very difficult to get either HTML # or reStructuredText/Markdown if USED_WORDPRESS and fmt == "markdown": content = content.replace("\n ", " \n") elif USED_WORDPRESS and fmt == "rst": content = content.replace("\n ", "\n\n") f.write(content + u"\n") entry = Entry(tmp, conf) p = join(conf["content_dir"], dirname(entry.permalink)[1:]) try: os.makedirs(p.rsplit("/", 1)[0]) except OSError: pass filepath = p + ".txt" if isfile(filepath) and not options.force: raise AcrylamidException("Entry already exists %r" % filepath) shutil.move(tmp, filepath) event.create(filepath)
def run(conf, env, options): """Subcommand: new -- create a new blog entry the easy way. Either run ``acrylamid new My fresh new Entry`` or interactively via ``acrylamid new`` and the file will be created using the preferred permalink format.""" # we need the actual default values commands.initialize(conf, env) # config content_extension originally defined as string, not a list extlist = conf.get('content_extension', ['.txt']) if isinstance(extlist, string_types): ext = extlist else: ext = extlist[0] fd, tmp = tempfile.mkstemp(suffix=ext, dir='.cache/') editor = os.getenv('VISUAL') if os.getenv('VISUAL') else os.getenv( 'EDITOR') tt = formats.get(ext, yaml) if options.title: title = u(' '.join(options.title)) else: title = u(input("Entry's title: ")) with io.open(fd, 'w', encoding='utf-8') as f: f.write(tt(title, datetime.now().strftime(conf['date_format']))) entry = readers.Entry(tmp, conf) p = join(conf['content_dir'], splitext(entry.permalink.strip('/'))[0]) try: os.makedirs(p.rsplit('/', 1)[0]) except OSError: pass filepath = p + ext if isfile(filepath): raise AcrylamidException('Entry already exists %r' % filepath) shutil.move(tmp, filepath) event.create('new', filepath) if datetime.now().hour == 23 and datetime.now().minute > 45: log.info( "notice don't forget to update entry.date-day after mignight!") if log.level() >= log.WARN: return try: if editor: retcode = subprocess.call(shlex.split(editor) + [filepath]) elif sys.platform == 'darwin': retcode = subprocess.call(['open', filepath]) else: retcode = subprocess.call(['xdg-open', filepath]) except OSError: raise AcrylamidException('Could not launch an editor') # XXX process detaches... m( if retcode < 0: raise AcrylamidException('Child was terminated by signal %i' % -retcode) if os.stat(filepath)[6] == 0: raise AcrylamidException('File is empty!')
def run(conf, env, options): """Subcommand: new -- create a new blog entry the easy way. Either run ``acrylamid new My fresh new Entry`` or interactively via ``acrylamid new`` and the file will be created using the preferred permalink format.""" # we need the actual default values commands.initialize(conf, env) # config content_extension originally defined as string, not a list extlist = conf.get('content_extension',['.txt']) if isinstance(extlist, string_types): ext = extlist else: ext = extlist[0] fd, tmp = tempfile.mkstemp(suffix=ext, dir='.cache/') editor = os.getenv('VISUAL') if os.getenv('VISUAL') else os.getenv('EDITOR') tt = formats.get(ext, yaml) if options.title: title = u(' '.join(options.title)) else: title = u(input("Entry's title: ")) with io.open(fd, 'w', encoding='utf-8') as f: f.write(tt(title, datetime.now().strftime(conf['date_format']))) entry = readers.Entry(tmp, conf) p = join(conf['content_dir'], splitext(entry.permalink.strip('/'))[0]) try: os.makedirs(p.rsplit('/', 1)[0]) except OSError: pass filepath = p + ext if isfile(filepath): raise AcrylamidException('Entry already exists %r' % filepath) shutil.move(tmp, filepath) event.create('new', filepath) if datetime.now().hour == 23 and datetime.now().minute > 45: log.info("notice don't forget to update entry.date-day after mignight!") if log.level() >= log.WARN: return try: if editor: retcode = subprocess.call(shlex.split(editor) + [filepath]) elif sys.platform == 'darwin': retcode = subprocess.call(['open', filepath]) else: retcode = subprocess.call(['xdg-open', filepath]) except OSError: raise AcrylamidException('Could not launch an editor') # XXX process detaches... m( if retcode < 0: raise AcrylamidException('Child was terminated by signal %i' % -retcode) if os.stat(filepath)[6] == 0: raise AcrylamidException('File is empty!')
def new(conf, env, title, prompt=True): """Subcommand: new -- create a new blog entry the easy way. Either run ``acrylamid new My fresh new Entry`` or interactively via ``acrylamid new`` and the file will be created using the preferred permalink format.""" # we need the actual defaults values initialize(conf, env) fd, tmp = tempfile.mkstemp(suffix='.txt', dir='.cache/') editor = os.getenv('VISUAL') if os.getenv('VISUAL') else os.getenv( 'EDITOR') if not title: title = raw_input("Entry's title: ") title = safe(title) with io.open(fd, 'w') as f: f.write(u'---\n') f.write(u'title: %s\n' % title) f.write(u'date: %s\n' % datetime.now().strftime(conf['date_format'])) f.write(u'---\n\n') entry = readers.Entry(tmp, conf) p = join(conf['content_dir'], dirname(entry.permalink)[1:]) try: os.makedirs(p.rsplit('/', 1)[0]) except OSError: pass filepath = p + '.txt' if isfile(filepath): raise AcrylamidException('Entry already exists %r' % filepath) shutil.move(tmp, filepath) event.create(filepath) if datetime.now().hour == 23 and datetime.now().minute > 45: log.info( "notice consider editing entry.date-day after you passed mignight!" ) if not prompt: return try: if editor: retcode = subprocess.call([editor, filepath]) elif sys.platform == 'darwin': retcode = subprocess.call(['open', filepath]) else: retcode = subprocess.call(['xdg-open', filepath]) except OSError: raise AcrylamidException('Could not launch an editor') # XXX process detaches... m( if retcode < 0: raise AcrylamidException('Child was terminated by signal %i' % -retcode) if os.stat(filepath)[6] == 0: raise AcrylamidException('File is empty!')
def init(env, options): """Subcommand: init -- create the base structure of an Acrylamid blog or restore individual files and folders. If the destination directory is empty, it will create a new blog. If the destination directory is not empty it won't overwrite anything unless you supply -f, --force to re-initialize the whole theme. If you need to restore single files, remove the existing file and run:: $ acrylamid init path/to/blog/ and all missing files are automatically re-created.""" if not options.engine: try: import jinja2 options.engine = "jinja2" except ImportError: options.engine = "mako" root = options.dest theme, files = rollout(options.theme, options.engine) # remember whether we are restore an existing theme restore = isfile(join(root, "conf.py")) if isfile(root): raise AcrylamidException("%s already exists!" % root) if isdir(root) and len(os.listdir(root)) > 0 and not options.overwrite: if not restore and raw_input("Destination directory not empty! Continue? [yn]: ") != "y": sys.exit(1) if "conf.py" not in files: conf = string.Template(defaults.copy("conf.py").read()) files["conf.py"] = io.BytesIO(conf.substitute(engine=options.engine, theme=theme).encode("utf-8")) for dest, items in files.iteritems(): dest = join(root, dest) if not isdir(dirname(dest)): os.makedirs(dirname(dest)) if isinstance(items, (basestring, io.IOBase)): items = [items] for obj in items: if hasattr(obj, "read"): path = join(dirname(dest), basename(obj.name)) if dest.endswith("/") else dest if options.overwrite or not exists(path): with io.open(path, "wb") as fp: fp.write(obj.read()) event.create(path) else: event.skip(path) else: src = join(dirname(defaults.__file__), options.theme, options.engine, obj) if options.overwrite or not exists(join(dest, basename(src))): shutil.copy(src, dest) event.create(dest if basename(dest) else join(dest, obj)) else: event.skip(dest) if not restore: log.info("Created your fresh new blog at %r. Enjoy!", root)