Editorial Workflows: New Post

In my previous post about Editorial workflows I demonstrated some simple workflows that didn’t rely on any Python scripting. In this post I’d like to follow up with one of the more complicated Python-based workflows that I’m using for The Angry Drunk.

Since my first post last week there have been a few interesting developments in the world of Editorial. First, just a few hours after I published my post, Federico Viticci announced that he was expanding his Siracusa-class review of Editorial into an eBook. I haven’t had a chance to explore the book yet but if the review is anything to go by, the book should be well worth the price.

Second, the developer of Editorial, Ole Zorn announced a searchable workflow archive.. This is awesome news for those of us interested in developing workflows.

Now on to the workflow.

The workflow I’m going to discuss there is Create New Post. This workflow creates a new Markdown document at the root of Editorial’s local storage with a title and headers based on user input.

This workflow, in its unaltered form, is probably useless to anyone but me. However it could easily be adapted to work with any blogging system that is powered by specifically formatted text files. This workflow is inspired by, and heavily influenced by Gabe Weatherhead’s Create New Time Stamped Files workflows. Here’s how it works:

First the user is prompted to select from a list of post types. The selection is converted to lowercase and saved to a variable postType. Then the user is prompted for a post title, which is saved to the postTitle variable. Next comes a conditional block. If the postType variable is link the user is prompted for a linked-list URL. Of course this is also saved to a variable. Finally the user is prompted to chose a post status from a list, which is converted to lower case and saved to the postStatus variable.

Next a Python script is run.

#coding: utf-8
import workflow
import editor
import datetime

#gather variables
post_title = workflow.get_variable('postTitle')
post_type = workflow.get_variable('postType')
post_eurl = workflow.get_variable('eurl')
post_status = workflow.get_variable('postStatus')

#generate filename
file_name = datetime.datetime.now().strftime('%Y-%m-%d')+'-'+post_title.lower().replace(' ','-')+'.md'

#generate post title and date
post_title_case = post_title.title()
post_date = datetime.datetime.now().strftime('%Y-%m-%d %H:%M')

#generate post metadata
if post_type == 'link':
    post_header = 'Title: '+post_title_case+'\nAuthor: The Angry Drunk\nDate: '+post_date+'\nPostType: '+post_type+'\neurl: '+post_eurl+'\nStatus: '+post_status+'\n\n\n'
    post_header = 'Title: '+post_title_case+'\nAuthor: The Angry Drunk\nDate: '+post_date+'\nPostType: '+post_type+'\nStatus: '+post_status+'\n\n\n'

#generate a file with the generated file name and content

#return the filename to the workflow.

The script begins by importing the workflow, editor, and datetime python modules. workflow and editor contain functionality for integration with Editorial workflows and the editor itself. datetime is self-explanatory.

The script then gathers the variables from the workflow via workflow.get_variable() and assigns them to internal variables. Then the script takes the post title, appends the current date, replaces all spaces with dashes and adds “.md” to the end. This code doesn’t check that the resulting filename is valid on a given OS but it will do for most simple titles.

Next the script uses the post title and today’s date to generate the Post Title and Post Date fields used in my Pelican setup. At this point an if/else block generates the header text, including an external link if the post type is “link”.

Then the script uses the editor.set_file_contents() statement to create a file at the root of the local storage1 with the specified filename and contents. The file name is also exported to the output of the action for the next step in the workflow.

Finally, the workflow opens the file that was just created. I enabled the “pause” setting on this action because without it, the workflow often was trying to open the file before it was completely created. The pause setting avoids that issue.

  1. I could write the file directly to Dropbox storage, but that precludes the workflow from functioning while offline.