So first, the good news, I have my meds sorted out and it seems like I’m back on track. I’m sure I’ll talk more about it on Overtired, and will post more of the story here after I’m sure things have leveled out. I’m primarily directing my energy toward BitWriter (the nvALT update), but wanted a morning project, thus…
This tool converts CSV style input to MultiMarkdown tables. It’s based on Christopher Brandow’s Swift utility that I linked a little bit back. I took the idea and rebuilt it in Ruby, added handling for a few things I considered missing, and turned it into a Markdown Service Tool.
The Service lets you use simple CSV syntax for creating MultiMarkdown tables. Comma-separated cells, and alignment row is built based on Christopher’s syntax using “<”, “>”, and “=” (left, right, center).
This version allows the use of double quotes to include a comma within a cell, handles “cleaning” (justifying spaces based on alignment), and adds a few extra syntax characters.
Here’s a rundown:
Use CSV formatting, with cells separated by commas and newlines defining end of row.
Data 1, Data 2, Data 3
Header row
The first row is required and defines column headers.
Header 1, Header 2, Header 3
Data 1, Data 2, Data 3
Alignment row
A second row containing only <
, >
, =
, and commas (whitespace ignored) can be added to force column aligment. If this row isn’t found, one will be created for you with a default “center” value for every column.
Header 1, Header 2, Header 3
<,<,=
Data 1, Data 2, Data 3
An alignment row must contain the same number of cells as the longest row. Because this tool can handle figuring out how to pad rows of different lengths, it’s forgiving in every area except this row.
Table caption
A line beginning with ==
at the beginning or the end of the CSV input will create a table caption.
== The title of my table
header 1, header 2
a,b
Column spanning
To make a cell span multiple columns, use a single >
in the next cell(s)
For example:
1,2,3,4,5
one,>,two,>,>
creates:
| 1 | 2 | 3 | 4 | 5 |
| :---: | :---: | :---: | :---: | :---: |
| one || two |||
Which MultiMarkdown will interpret as “one” being two columns wide, “two” being three columns.
Notes
- Two consecutive commas in a row creates a blank cell
-
To use a comma within cell data, surround the contents in double quotes
one, "and, two", three
- Whitespace surrounding cell data is ignored
- Empty (blank) rows are stripped
- To force an empty row, use a single comma on a line
- To force a single-cell (full span) empty row, use » alone on the line
Example input
==Title
one, "two, and a half", three and a quarter
>, =, =, =, <
four, five,,, six
>>
hell, >, hath no, fury
Outputs:
| one | two, and a half | three and a quarter | | |
| ----: | :-------------: | :-----------------: | :---: | :---- |
| four | five | | | six |
|||||
| hell || hath no | fury | |
[Title]
Which looks like:
Download the service as part of the Markdown Service Tools package. I’ve also thrown the script up as a Gist, as messy as it may be.
Markdown Service Tools v3.0.3
Download Markdown Service Tools v3.0.3
The Markdown Service Tools are a collection of macOS Services designed to make creating Markdown-formatted text that much easier. Services work in almos any macOS application.
Published 01/09/14.
Updated 09/14/20. Changelog