#!/usr/bin/env perl
use
open
qw(:std :utf8)
;
my
$text
;
if
(
@ARGV
) {
my
$file
=
shift
@ARGV
;
open
(
my
$fh
,
'<'
,
$file
) or
die
$!;
local
$/;
$text
= <
$fh
>;
close
$fh
;
}
else
{
local
$/;
$text
= <>;
}
$text
=~ s/\n(?:\s*)\n/\n\n/g;
$text
=~ s/\\/\\e/g;
$text
=~ s/ /\\~/g;
$text
=~ s/^\./\\&./mg;
$text
=~ s|^\h*\*\h*\*\h*\*.*\n+|.Bf -c -f xhtml\n<hr />\n.Ef\n|m;
$text
=~ s/^(.*)\n----+\n/.Ss
"$1"
/gm;
$text
=~ s/^(.*)\n====+\n/.Sh
"$1"
/gm;
$text
=~ s/^
$text
=~ s/^
$text
=~ s/^(-.*?)\n\s*(?!-)\n/.Bl -t item\n$1\n.El\n/gsm;
$text
=~ s/^-/.It\n/gm;
$text
=~ s/\n\n(?=[\p{Alphabetic}])/\n.P\n/g;
$text
=~ s/^\s*%\s*(.*)\n%\s*(.*)\n%\s*(.*)\n/
my
$t
=
""
;
if
($1) {
$t
=
qq{.X set document-title "$1"\n}
;
}
if
($2) {
$t
.=
qq{.X set document-author "$2"\n}
;
}
if
($3) {
$t
.=
qq{.X set document-date "$3"\n}
;
}
$t
;
/xeg;
$text
=~ s|!\[(.*?)\]\((.*?)\)\n?|
my
$t
= $1;
my
$l
= $2;
$t
=~ s/\n+/ /g;
qq{\n.Im "$l" "$t"\n}
;
|xsge;
$text
=~ s|\[(.*?)\]\((.*?)\)([\.\?,;:!])?\n?|
my
$t
= $1;
my
$l
= $2;
my
$p
= $3;
$t
=~ s/\n+/ /g;
if
(
$p
) {
$t
=
qq{\n.Lk "$l" "$t" $p\n}
;
}
else
{
$t
=
qq{\n.Lk "$l" "$t"\n}
;
}
$t
;
|xsge;
$text
=~ s/(?<!\\)\*\*(.+?)(?<!\\)\*\*\s*([\.\?,;:!])?\n?/
my
$t
= $1;
my
$p
= $2;
$t
=~ s|\n+| |g;
if
(
$p
) {
qq{\n.Sm -t strong "$t" $p\n}
}
else
{
qq{\n.Sm -t strong "$t"\n}
}
/exgs;
$text
=~ s/(?<!\\)\*(.+?)(?<!\\)\*\s*([\.\?,;:!])?\n?/
my
$t
= $1;
my
$p
= $2;
$t
=~ s|\n+| |g;
if
(
$p
) {
qq{\n.Sm "$t" $p\n}
}
else
{
qq{\n.Sm "$t"\n}
}
/exgs;
$text
=~ s/(?<!\\)`(.+?)(?<!\\)`\s*([\.\?,;:!])?\n?/
my
$t
= $1;
my
$p
= $2;
$t
=~ s|\n+| |g;
if
(
$p
) {
qq{\n.Sm -t code "$t" $p\n}
}
else
{
qq{\n.Sm -t code "$t"\n}
}
/xegs;
$text
=~ s/^~~~+\N*\n(.*?)\n~~~+/.Bd -t literal\n$1\n.Ed/msg;
$text
=~ s/^```+\N*\n(.*?)\n```+/.Bd -t literal\n$1\n.Ed/msg;
$text
=~ s/^\s*//gm;
$text
=~ s/[ \t]*$//m;
print
$text
;