-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmmm.pl
More file actions
101 lines (85 loc) · 2.42 KB
/
mmm.pl
File metadata and controls
101 lines (85 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#! /usr/bin/perl -W
#
# mmm Version 0.3 by Thomas Hochstein
#
# Create a MIME multipart/alternative part, containing
# text/plain (in Markdowen) and text/html, from a
# Markdown file.
#
# Copyright (c) 2015-2016 Thomas Hochstein <thh@thh.name>
#
# It can be redistributed and/or modified under the same terms under
# which Perl itself is published.
my $VERSION = "0.3";
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..25;
return ( "--MMM" . $hex);
}
sub ShowVersion {
print "mmm v$VERSION\n";
print "MIME multipart/alternative from Markdown - MMM\n";
print "Copyright (c) 2015 Thomas Hochstein <thh\@thh.name>\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);
};