Editorial Workflows

Markdown to Docx

unlisted workflow

Install Workflow...

This workflow contains at least one Python script. Only use it if you trust the person who shared this with you, and if you know exactly what it does.

I understand, install the workflow!

This is a workflow for Editorial, a Markdown and plain text editor for iOS. To download it, you need to view this page on a device that has the app installed.

Description: Workflow to convert the document currently open in the editor from markdown to docx file. It uses a variant of Caleb McDaniel's IMDtoPDF.py script, which uses Pandoc through Docverter service to conver the file. Editorial then opens the file in the console, from where it can be opened in Pages, QuickOffice, Documents, etc.

Comments: Comment Feed (RSS)

There are no comments yet.

+ Add Comment

Workflow Preview
Document Text ?
Folded Text
  • Include
  • Replace with:
Set Variable ?
Variable Name
document
Value
Input
Set Clipboard ?
URL Escape ?
Run Python Script ?
Source Code
#! /usr/bin/env python
# -*- coding: utf-8 -*-


# MDtoDOCX by Charles Bucher. Modified from iMDtoPDF.py
# I basically removed the Dropbox integration, which was better, for me, and instead allowed the console to open the final result, so I can use the built-in Open In... feature to get it into my app of choice.
# I also added a little piece suggested on the original creator's website, listed below, so instead of appending a timestamp, you are prompted to enter a title.
# The Docx extension is added in the process. Do not add it to the file name when prompted, unless you feel like being redundant.

# Give credit where credit is due:
'''
MDtoDOCX is a modified version of Original Script:
iMDtoPDF.py
by W. Caleb McDaniel
http://wcm1.web.rice.edu
'''
## This is a wrapper script for sending documents to Docverter
## for conversion using Pandoc. Typically
## Docverter calls are made with cURL; this script uses httplib.
## It is intended for use with Pythonista on iOS.
## For more information, see: http://www.docverter.com/api.html

import clipboard
import datetime
import httplib
import mimetypes
import console
import os


## Helper functions for posting multipart/form-data request
## using standard libraries. Updated to reflect changes to
## httplib in Python 2.0.
## {{{ http://code.activestate.com/recipes/146306/ (r1)
 
def post_multipart(host, selector, fields, files):
    """
    Post fields and files to an http host as multipart/form-data.
    fields is a sequence of (name, value) elements for regular form fields.
    files is a sequence of (name, filename, value) elements for data to be uploaded as files
    Return the server's response page.
    """
    content_type, body = encode_multipart_formdata(fields, files)
    h = httplib.HTTPConnection(host)
    h.putrequest('POST', selector)
    h.putheader('content-type', content_type)
    h.putheader('content-length', str(len(body)))
    h.endheaders()
    h.send(body)
    response = h.getresponse()
    output = response.read()
    return output
    # return h.file.read()
 
def encode_multipart_formdata(fields, files):
    """
    fields is a sequence of (name, value) elements for regular form fields.
    files is a sequence of (name, filename, value) elements for data to be uploaded as files
    Return (content_type, body) ready for httplib.HTTP instance
    """
    BOUNDARY = '----------ThIs_Is_tHe_bouNdaRY_$'
    CRLF = '\r\n'
    L = []
    for (key, value) in fields:
        L.append('--' + BOUNDARY)
        L.append('Content-Disposition: form-data; name="%s"' % key)
        L.append('')
        L.append(value)
    for (key, filename, value) in files:
        L.append('--' + BOUNDARY)
        L.append('Content-Disposition: form-data; name="%s"; filename="%s"' % (key, filename))
        L.append('Content-Type: %s' % get_content_type(filename))
        L.append('')
        L.append(value)
    L.append('--' + BOUNDARY + '--')
    L.append('')
    body = CRLF.join(L)
    content_type = 'multipart/form-data; boundary=%s' % BOUNDARY
    return content_type, body
 
def get_content_type(filename):
    return mimetypes.guess_type(filename)[0] or 'application/octet-stream'
 
## end of http://code.activestate.com/recipes/146306/ }}}
 
## Now let's DO this. First put some markdown text on your clipboard.
 
## Put clipboard contents in a file to send to Docverter.
input_text = clipboard.get()
f = open("docverterin.txt", "w").write(input_text)

## Use CSS to style your output. I've included some sensible defaults.
css = """
body {margin: 4em; }
p {line-height: 1.2em; text-align: justify;}
h1, h2, h3, h4 {font-weight: normal;}
sup {line-height: 0;}
hr {border: 1px #eee solid; margin-top: 2em; margin-botom: 2em; width: 70%;}
pre {white-space: pre-wrap; word-wrap: break-word;}
"""
# Use CSS3 Paged Media Module to number pages of PDF, set margins.
page_info = "@page {margin: 1in; @bottom-center{content: counter(page)}}"
c = open("docverter.css", "w").write(css + page_info)


## Set Docverter options and define fields using lists.
## Other options available at http://www.docverter.com/api.html#toc_2
fields = [("from", "markdown"), ("to", "docx"), ("css", "docverter.css")]
files = [("input_files[]", "docverterin.txt", open("docverterin.txt", "r").read()), ("other_files[]", "docverter.css", open("docverter.css","r").read())]
 
## Post to Docverter using post_multipart()
output = post_multipart("c.docverter.com", "/convert", fields, files)

# Prompt for a custom filename before writing the file
title = console.input_alert('Output filename', 'Enter title below')
outfile = title + '.docx'
o = open(outfile, "w").write(output)

# Preview resulting file in the console
# Share it to the desired app via Open In
outfile_path = os.path.abspath(outfile)
console.quicklook(outfile_path)