From 795bb8b03a4a61a92c855173a54148315d62214e Mon Sep 17 00:00:00 2001
From: Jurgen Leschner <jldec@github.com>
Date: Mon, 22 Feb 2016 16:06:23 -0500
Subject: [PATCH] init

---
 LICENSE.md    | 22 ++++++++++++++++++++++
 README.md     | 36 +++++++++++++++++++++++++++++++++++-
 tomd          | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 tomd_post.awk | 10 ++++++++++
 tomd_pre.awk  | 31 +++++++++++++++++++++++++++++++
 5 files changed, 153 insertions(+), 1 deletion(-)
 create mode 100644 LICENSE.md
 create mode 100755 tomd
 create mode 100755 tomd_post.awk
 create mode 100755 tomd_pre.awk

diff --git a/LICENSE.md b/LICENSE.md
new file mode 100644
index 0000000..3568bc9
--- /dev/null
+++ b/LICENSE.md
@@ -0,0 +1,22 @@
+(The MIT License)
+
+Copyright (c) 2015-2016 Jürgen Leschner -- github.com/jldec
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+'Software'), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\ No newline at end of file
diff --git a/README.md b/README.md
index c002ad3..aca900d 100644
--- a/README.md
+++ b/README.md
@@ -1,2 +1,36 @@
 # tomd
-Shell script for upgrading textile posts to markdown.
+
+### Automated conversion from .textile to .md with pandoc
+
+Users with many textile files in their Jekyll Pages site should consider using [pandoc](http://pandoc.org), a utility for converting between different markup formats. E.g. for converting from textile to markdown:
+
+```sh
+pandoc --wrap=preserve -f textile -t markdown_github <foo.textile >foo.md
+```
+
+##### limitations
+Unfortunately pandoc may change content in unwanted ways when it encounters:
+
+- YAML frontmatter at the top of .textile files
+- `{% highlight %}` blocks
+- `<notextile>` blocks
+
+To get around these limitations, this `tomd` shell script calls [awk](http://www.grymoire.com/Unix/Awk.html) and [sed](http://www.grymoire.com/Unix/Sed.html) to filter out those sections and then re-insert them after pandoc has converted the rest the file.
+
+##### to run `tomd`
+Copy `tomd` and the 2 awk scripts from this repo into your jekyll project folder (or a subdirectory if you prefer)
+
+Then invoke with `./tomd`.
+
+```
+$ ./tomd
+checking for sed, awk, and pandoc
+/usr/bin/sed
+awk version 20070501
+pandoc 1.16.0.2
+looking for .textile files in _posts moving them to _old_posts
+226 textile files converted
+```
+
+#### NOTE
+The `tomd` script has been run successfully on OSX on a couple of repos - YMMV.
diff --git a/tomd b/tomd
new file mode 100755
index 0000000..bc1a739
--- /dev/null
+++ b/tomd
@@ -0,0 +1,55 @@
+#!/bin/sh
+
+# shell script to convert textile content to markdown using sed, awk and pandoc.
+# copy this file and tomd_pre.awk and tomd_post.awk into your local jekyll project folder
+# then `chmod +x tomd` and invoke with `tomd [_posts] [_old_posts]`
+# remember to keep a backup of everything, and USE THIS AT YOUR OWN RISK.
+
+set -e
+
+# make sure the following exist
+echo "checking for sed, awk, and pandoc"
+which sed | sed
+awk --version
+pandoc -v | sed '1 q'
+
+# script dir
+DIR=`dirname $0`
+
+# posts dir
+if [ $1 ] ; then
+  POSTSDIR=$1
+else
+  POSTSDIR=_posts
+fi
+
+# archive dir
+if [ $2 ] ; then
+  OLDDIR=$1
+else
+  OLDDIR=_old_posts
+fi
+
+echo "looking for .textile files in $POSTSDIR moving them to $OLDDIR"
+mkdir -p -v $OLDDIR
+
+# find all the textile files
+find $POSTSDIR -name \*.textile | sed 's/\.textile$//' >tomd_files.txt
+
+while read foo; do
+
+  # save YAML header
+  sed -n '1,/^---/ p' $foo.textile >tomd_head.txt
+
+  # textile (minus header) | tomd_pre | pandoc | tomd_post
+  sed '1,/^---/ d' $foo.textile | awk -f $DIR/tomd_pre.awk | pandoc -B tomd_head.txt -f textile -t markdown_github | awk -f $DIR/tomd_post.awk >$foo.md
+
+  # archive
+  mv $foo.textile $OLDDIR
+
+done <tomd_files.txt
+
+echo `wc -l <tomd_files.txt` textile files converted
+
+# clean up
+rm -f tomd_files.txt tomd_head.txt tomd-include-*.txt
diff --git a/tomd_post.awk b/tomd_post.awk
new file mode 100755
index 0000000..312bb95
--- /dev/null
+++ b/tomd_post.awk
@@ -0,0 +1,10 @@
+# awk post-processor - after pandoc
+# re-creates !include sections extracted by tomd_pre.awk
+
+/^!include tomd-include-/ {
+  while ((getline line < $2) > 0) { print line }
+  close($2);
+  next;
+}
+
+{ print }
diff --git a/tomd_pre.awk b/tomd_pre.awk
new file mode 100755
index 0000000..c77dfa4
--- /dev/null
+++ b/tomd_pre.awk
@@ -0,0 +1,31 @@
+# tomd awk pre-processor (before pandoc)
+# redirects {% highlight %} sections to numbered files
+# and passes through <notextile> blocks
+
+BEGIN { nme = "tomd-include-"; cnt = 0; }
+
+/{% +highlight/ {
+  including = "yes";
+  incfile = (nme cnt++ ".txt");
+  print ("!include " incfile);
+}
+
+/{% +endhighlight/ {
+  including = "";
+  print >incfile;
+  next;
+}
+
+/^<notextile>/ {
+  including = "yes";
+  incfile = (nme cnt++ ".txt");
+  print ("!include " incfile);
+  next;
+}
+
+/^<\/notextile>/ {
+  including = "";
+  next;
+}
+
+{ if(including) { print >incfile } else { print }}
\ No newline at end of file
