Editorial Workflows

Tablify

public 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: A workflow for converting csv tables into multimarkdown tables for conversion to HTML. With a monospaced font, the table should have the proper alignment.
The Python script at the heart of this one uses the bottom row to determine whether the column is text and should be left aligned, or numbers and should be right aligned.

Shared by: d.mcni.ch

Comments: Comment Feed (RSS)

There are no comments yet.

+ Add Comment

Workflow Preview
Selected Text ?
Entire Line(s)
OFF
Empty Selection Output
  • No Output
  • All Text
  • Closest Word
Folded Text
  • Include
  • Replace with:
Run CSV2MMD ?
Source Code
#coding: utf-8
import workflow

csv = workflow.get_input()

import re

raw_lines = csv.splitlines()
rows = []
alignment = ''
headings = raw_lines[0].split(', ')
max_cell_length = []
for column in headings:
  max_cell_length.append(1)
for raw_line in raw_lines:
  cells = raw_line.split(', ')
  n = 0
  for cell in cells:
    if len(cell) > max_cell_length[n]:
      max_cell_length[n] = len(cell)
    n = n+1
for raw_line in raw_lines:
  cells = raw_line.split(', ')
  n = 0
  for cell in cells:
    if len(cell) < max_cell_length[n]:
      cells[n] = cell + (max_cell_length[n]-len(cell))*' '
    n = n+1
  row = '| ' + ' | '.join(cells) + ' |'
  rows.append(row)
n=0
for cell in cells:
  if re.match('^\d+\s*$',cell):
    alignment = alignment + '|-'+ max_cell_length[n]*'-' + ':'
  else:
    alignment = alignment + '|:-' + max_cell_length[n]*'-'
  n = n+1
header = rows[0]
body = rows[1:]

table = header + '\n' + alignment + '|\n' + '\n'.join(body)

workflow.set_output(table)
Replace Selected Text ?
Replacement Text
Input