Add mmm.pl
authorThomas Hochstein <thh@inter.net>
Fri, 1 May 2015 21:03:48 +0000 (23:03 +0200)
committerThomas Hochstein <thh@inter.net>
Fri, 1 May 2015 21:03:48 +0000 (23:03 +0200)
Signed-off-by: Thomas Hochstein <thh@inter.net>
README.md
mmm/mmm.pl [new file with mode: 0644]

index 85e327c..1576a8f 100644 (file)
--- a/README.md
+++ b/README.md
@@ -13,3 +13,12 @@ to the format of the Wordpress plugin
 
 Usage: `footnotes --to mmd|wp [--file $inputfile]`
 
+### mmm.pl
+
+`mmm` (*MIME multipart/alternative from Markdown*) can
+create a MIME `multipart/alternative` mail, containing a
+`text/plain` part (in Markdowen) and a `text/html` part, from
+a Markdown file. Other mail headers (From:, To:, Subject:, ...)
+can be prepended from a template file.
+
+Usage: `mmm [--file $inputfile] [--headers $templatefile]`
diff --git a/mmm/mmm.pl b/mmm/mmm.pl
new file mode 100644 (file)
index 0000000..1a322a5
--- /dev/null
@@ -0,0 +1,101 @@
+#! /usr/bin/perl -W
+#
+# mmm Version 0.1 by Thomas Hochstein
+#
+# Create a MIME multipart/alternative part, containing
+# text/plain (in Markdowen) and text/html, from a
+# Markdown file.
+#
+# Copyright (c) 2015 Thomas Hochstein <thh@inter.net>
+#
+# It can be redistributed and/or modified under the same terms under 
+# which Perl itself is published.
+
+my $VERSION = "0.1";
+
+use strict;
+use Getopt::Long qw(GetOptions);
+use Text::Markdown;
+
+# read commandline option(s)
+my ($OptInFile,$OptHeaderFile);
+GetOptions ('f|file=s'    => \$OptInFile,
+            't|headers=s' => \$OptHeaderFile,
+            'H|help'      => \&ShowPOD,
+            'V|version'   => \&ShowVersion) or exit 1;
+
+# read input from STDIN or --file
+# unset $/
+undef $/;
+my $markdown;
+if ($OptInFile) {
+  open(FILE, "< $OptInFile") or die "Can't open $OptInFile: $!";
+  $markdown = <FILE>;
+  close(FILE);
+} else {
+  $markdown = <>;
+}
+# read header template
+my $headers;
+if ($OptHeaderFile) {
+  open(HEADERS, "< $OptHeaderFile") or die "Can't open $OptHeaderFile: $!";
+  $headers = <HEADERS>;
+  close(HEADERS);
+  # remove all trailing newlines
+  $headers =~ s/\n+$//;
+}
+# reset $/
+$/ = "\n";
+
+# convert markdown to html
+my $html = Text::Markdown::Markdown($markdown);
+
+# output
+print  "$headers\n" if ($OptHeaderFile);
+
+my $Boundary = &GenBoundary;
+print  "MIME-Version: 1.0\n";
+print  "Content-Type: multipart/alternative;\n";
+printf ('boundary="%s"'."\n",$Boundary);
+print  "\n";
+
+print  "This is a multi-part message in MIME format.\n";
+
+printf ("--%s\n",$Boundary);
+print  "Content-Type: text/plain; charset=ISO-8859-15\n";
+print  "Content-Transfer-Encoding: 8bit\n";
+print  "\n";
+print  "$markdown\n";
+
+printf ("--%s\n",$Boundary);
+print  "Content-Type: text/html; charset=ISO-8859-15\n";
+print  "Content-Transfer-Encoding: 8bit\n";
+print  "X-Creator: Markdown/1.0.1\n";
+print  "\n";
+print  "$html\n";
+
+printf ("--%s--\n",$Boundary);
+
+exit(0);
+
+################################################################################
+
+sub GenBoundary {
+  my $hex;
+  $hex .= sprintf("%x", rand 16) for 1..20;
+  return ( "----------" . $hex);
+}
+
+sub ShowVersion {
+  print "mmm v$VERSION\n";
+  print "MIME multipart/alternative from Markdown - MMM\n";
+  print "Copyright (c) 2014 Thomas Hochstein <thh\@inter.net>\n";
+  print "This program is free software; you may redistribute it ".
+        "and/or modify it under the same terms as Perl itself.\n";
+  exit(100);
+};
+
+sub ShowPOD {
+  exec('perldoc', $0);
+  exit(100);
+};
This page took 0.012661 seconds and 4 git commands to generate.