Combine markdown files and convert to HTML in a Swift script.

Swift 3

Sam Burnstone recently wrote about how to convert a simple shell script to Swift. Here’s the same shell script rewritten using SwiftShell and FileSmith.

The shell script

It reads all the markdown files in the “chapters” directory, joins them together using each filename as a heading and converts everything to HTML. This is actually a very nice and clear* shell script, I especially like how the “for” loop is piped directly into the shell command so I did the same in the SwiftShell version.

* except for “%.*” in line 7, which leaves out the file extension


I think this is more readable and more explicit about what is going on, though a lot wordier. I especially like how each “try” makes it clear where things can go wrong. Also this is mostly standard Swift and Foundation code, the only parts from SwiftShell are “open” and “run”.

Note that top-level Swift code which can throw does not need to be enclosed in a “do … catch” clause, but the resulting error messages printed to standard error are quite ugly and hard to read. The exit method from SwiftShell makes them prettier.

  • 2016-11-02: updated to Swift 3.
  • 2017_04_28: updated to SwiftShell 3.0 and added FileSmith.