c45b28b9fe71d83e7a3d1ec55a0a6fb3c0aa2a53
[scripts.git] / mmm / mmm.pl
1 #! /usr/bin/perl -W
2 #
3 # mmm Version 0.1 by Thomas Hochstein
4 #
5 # Create a MIME multipart/alternative part, containing
6 # text/plain (in Markdowen) and text/html, from a
7 # Markdown file.
8 #
9 # Copyright (c) 2015 Thomas Hochstein <thh@inter.net>
10 #
11 # It can be redistributed and/or modified under the same terms under 
12 # which Perl itself is published.
13
14 my $VERSION = "0.1";
15
16 use strict;
17 use Getopt::Long qw(GetOptions);
18 use Text::Markdown;
19
20 # read commandline option(s)
21 my ($OptInFile,$OptHeaderFile);
22 GetOptions ('f|file=s'    => \$OptInFile,
23             't|headers=s' => \$OptHeaderFile,
24             'H|help'      => \&ShowPOD,
25             'V|version'   => \&ShowVersion) or exit 1;
26
27 # read input from STDIN or --file
28 # unset $/
29 undef $/;
30 my $markdown;
31 if ($OptInFile) {
32   open(FILE, "< $OptInFile") or die "Can't open $OptInFile: $!";
33   $markdown = <FILE>;
34   close(FILE);
35 } else {
36   $markdown = <>;
37 }
38 # read header template
39 my $headers;
40 if ($OptHeaderFile) {
41   open(HEADERS, "< $OptHeaderFile") or die "Can't open $OptHeaderFile: $!";
42   $headers = <HEADERS>;
43   close(HEADERS);
44   # remove all trailing newlines
45   $headers =~ s/\n+$//;
46 }
47 # reset $/
48 $/ = "\n";
49
50 # convert markdown to html
51 my $html = Text::Markdown::Markdown($markdown);
52
53 # output
54 print  "$headers\n" if ($OptHeaderFile);
55
56 my $Boundary = &GenBoundary;
57 print  "MIME-Version: 1.0\n";
58 print  "Content-Type: multipart/alternative;\n";
59 printf ('boundary="%s"'."\n",$Boundary);
60 print  "\n";
61
62 print  "This is a multi-part message in MIME format.\n";
63
64 printf ("--%s\n",$Boundary);
65 print  "Content-Type: text/plain; charset=ISO-8859-15\n";
66 print  "Content-Transfer-Encoding: 8bit\n";
67 print  "\n";
68 print  "$markdown\n";
69
70 printf ("--%s\n",$Boundary);
71 print  "Content-Type: text/html; charset=ISO-8859-15\n";
72 print  "Content-Transfer-Encoding: 8bit\n";
73 print  "X-Creator: Markdown/1.0.1\n";
74 print  "\n";
75 print  "$html\n";
76
77 printf ("--%s--\n",$Boundary);
78
79 exit(0);
80
81 ################################################################################
82
83 sub GenBoundary {
84   my $hex;
85   $hex .= sprintf("%x", rand 16) for 1..20;
86   return ( "----------" . $hex);
87 }
88
89 sub ShowVersion {
90   print "mmm v$VERSION\n";
91   print "MIME multipart/alternative from Markdown - MMM\n";
92   print "Copyright (c) 2015 Thomas Hochstein <thh\@inter.net>\n";
93   print "This program is free software; you may redistribute it ".
94         "and/or modify it under the same terms as Perl itself.\n";
95   exit(100);
96 };
97
98 sub ShowPOD {
99   exec('perldoc', $0);
100   exit(100);
101 };
This page took 0.011777 seconds and 3 git commands to generate.