def test_safe(self): assert helpers.safe('"') == '"' assert helpers.safe("") == '""' assert helpers.safe("*Foo") == '"*Foo"' assert helpers.safe('{"Foo') == "'{\"Foo'" assert helpers.safe('"Foo" Bar') == '"Foo" Bar' assert helpers.safe("'bout \" and '") == '"\'bout " and \'"' assert helpers.safe("Hello World") == "Hello World" assert helpers.safe("Hello: World") == '"Hello: World"' assert helpers.safe("Hello's World") == "Hello's World" assert helpers.safe('Hello "World"') == 'Hello "World"'
def test_safe(self): assert helpers.safe('"') == '"' assert helpers.safe('') == '""' assert helpers.safe('*Foo') == '"*Foo"' assert helpers.safe('{"Foo') == '\'{"Foo\'' assert helpers.safe('"Foo" Bar') == '"Foo" Bar' assert helpers.safe("'bout \" and '") == "\"'bout \" and '\"" assert helpers.safe('Hello World') == 'Hello World' assert helpers.safe('Hello: World') == '"Hello: World"' assert helpers.safe('Hello\'s World') == 'Hello\'s World' assert helpers.safe('Hello "World"') == 'Hello "World"'
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 create(path, **kwargs): with open(path, 'w') as fp: fp.write('---\n') for k, v in kwargs.iteritems(): if isinstance(v, basestring): v = safe(v) fp.write('%s: %s\n' % (k, v)) fp.write('---\n')
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 = 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)
from acrylamid import log, readers, commands from acrylamid.errors import AcrylamidException from acrylamid.compat import string_types from acrylamid.tasks import task, argument from acrylamid.utils import force_unicode as u from acrylamid.helpers import safe, event try: input = raw_input except NameError: pass yaml, rst, md = \ lambda title, date: u"---\ntitle: %s\ndate: %s\n---\n\n" % (safe(title), date), \ lambda title, date: u"%s\n" % title + "="*len(title) + '\n\n' + ":date: %s\n\n" % date, \ lambda title, date: u"Title: %s\nDate: %s\n\n" % (title, date) formats = {'.md': md, '.mkdown': md, '.rst': rst, '.rest': rst} @task('new', [argument("title", nargs="*", default=None)], help="create a new entry") 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)
import tempfile import subprocess import shutil from os.path import join, dirname, isfile from datetime import datetime from acrylamid import log, readers, commands from acrylamid.errors import AcrylamidException from acrylamid.tasks import task, argument from acrylamid.utils import force_unicode as u from acrylamid.helpers import safe, event yaml, rst, md = \ lambda title, date: u"---\ntitle: %s\ndate: %s\n---\n\n" % (safe(title), date), \ lambda title, date: u"%s\n" % title + "="*len(title) + '\n\n' + ":date: %s\n\n" % date, \ lambda title, date: u"Title: %s\nDate: %s\n\n" % (title, date) formats = {'.md': md, '.mkdown': md, '.rst': rst, '.rest': rst} @task('new', [argument("title", nargs="*", default=None)], help="create a new entry") 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)
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 safe(self): assert helpers.safe('"') == '"' assert helpers.safe('') == '""' assert helpers.safe('*Foo') == '"*Foo"' assert helpers.safe('{"Foo') == '\'{"Foo\'' assert helpers.safe('"Foo" Bar') == '"Foo" Bar' assert helpers.safe("'bout \" and '") == "\"'bout \" and '\"" assert helpers.safe('Hello World') == 'Hello World' assert helpers.safe('Hello: World') == '"Hello: World"' assert helpers.safe('Hello\'s World') == 'Hello\'s World' assert helpers.safe('Hello "World"') == 'Hello "World"' assert helpers.safe('[foo][bar] Baz') == '"[foo][bar] Baz"'