Change mail address.
[scripts.git] / mmm / mmm.pl
CommitLineData
afd47875
TH
1#! /usr/bin/perl -W
2#
eb086b34 3# mmm Version 0.3 by Thomas Hochstein
afd47875
TH
4#
5# Create a MIME multipart/alternative part, containing
6# text/plain (in Markdowen) and text/html, from a
7# Markdown file.
8#
7a57a65e 9# Copyright (c) 2015-2016 Thomas Hochstein <thh@thh.name>
afd47875
TH
10#
11# It can be redistributed and/or modified under the same terms under
12# which Perl itself is published.
13
eb086b34 14my $VERSION = "0.3";
afd47875
TH
15
16use strict;
17use Getopt::Long qw(GetOptions);
18use Text::Markdown;
19
20# read commandline option(s)
21my ($OptInFile,$OptHeaderFile);
22GetOptions ('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 $/
29undef $/;
30my $markdown;
31if ($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
39my $headers;
40if ($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
51my $html = Text::Markdown::Markdown($markdown);
52
53# output
54print "$headers\n" if ($OptHeaderFile);
55
56my $Boundary = &GenBoundary;
57print "MIME-Version: 1.0\n";
58print "Content-Type: multipart/alternative;\n";
17254774 59printf (' boundary="%s"'."\n",$Boundary);
afd47875
TH
60print "\n";
61
62print "This is a multi-part message in MIME format.\n";
63
64printf ("--%s\n",$Boundary);
65print "Content-Type: text/plain; charset=ISO-8859-15\n";
66print "Content-Transfer-Encoding: 8bit\n";
67print "\n";
68print "$markdown\n";
69
70printf ("--%s\n",$Boundary);
71print "Content-Type: text/html; charset=ISO-8859-15\n";
72print "Content-Transfer-Encoding: 8bit\n";
73print "X-Creator: Markdown/1.0.1\n";
74print "\n";
75print "$html\n";
76
77printf ("--%s--\n",$Boundary);
78
79exit(0);
80
81################################################################################
82
83sub GenBoundary {
84 my $hex;
eb086b34
TH
85 $hex .= sprintf("%x", rand 16) for 1..25;
86 return ( "--MMM" . $hex);
afd47875
TH
87}
88
89sub ShowVersion {
90 print "mmm v$VERSION\n";
91 print "MIME multipart/alternative from Markdown - MMM\n";
7a57a65e 92 print "Copyright (c) 2015 Thomas Hochstein <thh\@thh.name>\n";
afd47875
TH
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
98sub ShowPOD {
99 exec('perldoc', $0);
100 exit(100);
101};
This page took 0.014805 seconds and 4 git commands to generate.