Fix header indentation, update version/copyright.
[scripts.git] / mmm / mmm.pl
... / ...
CommitLineData
1#! /usr/bin/perl -W
2#
3# mmm Version 0.2 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-2016 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
14my $VERSION = "0.2";
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";
59printf (' boundary="%s"'."\n",$Boundary);
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;
85 $hex .= sprintf("%x", rand 16) for 1..20;
86 return ( "----------" . $hex);
87}
88
89sub 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
98sub ShowPOD {
99 exec('perldoc', $0);
100 exit(100);
101};
This page took 0.009134 seconds and 4 git commands to generate.