Skip to content

Commit 54f489b

Browse files
committed
Hack together a script for testing HTML output
1 parent 397f434 commit 54f489b

File tree

2 files changed

+125
-0
lines changed

2 files changed

+125
-0
lines changed

spec/tools/bemuse.1

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
.Dd October 15, 2019
2+
.Dt BEMUSE 1
3+
.Os
4+
.Sh NAME
5+
.Nm bemuse
6+
.Nd quickly convert a Muse document to HTML
7+
.
8+
.Sh SYNOPSIS
9+
.Nm
10+
.Op Ar files
11+
.Pp
12+
.Nm
13+
.Op < Ar stdin
14+
.
15+
.Sh DESCRIPTION
16+
.Nm
17+
formats a list of Muse
18+
.Ar files
19+
using the
20+
.Xr Text::Amuse 3pm
21+
module from
22+
.Xr cpan 1 .
23+
Output is basic HTML with
24+
.Li .html
25+
replacing the file's
26+
.Li .muse
27+
extension.
28+
.
29+
.Pp
30+
If there are no files passed on the command-line,
31+
.Nm
32+
reads from stdin and writes to standard output.
33+
.
34+
.Sh FILES
35+
.Pa TEMP_FILE_LOL\.tmp\~
36+
Created in the executable's directory when reading from stdin.
37+
A limitation of
38+
.Xr Text::Amuse 3pm
39+
is that a filename is required to instantiate a new document.
40+
The aforementioned filename \(em unlikely to be in-use by the filesystem \(em
41+
is removed once the output has been generated.
42+
.
43+
.Sh EXIT STATUS
44+
.Ex -std
45+
.
46+
.Sh EXAMPLES
47+
Write converted HTML to stdout:
48+
.Bd -literal
49+
.Nm < Ar input
50+
.Ed
51+
.
52+
.Pp
53+
Create
54+
.Ar file.html :
55+
.Bd -literal
56+
.Nm Ar file.muse
57+
.Ed
58+
.
59+
.Sh SEE ALSO
60+
.Xr perl 1 ,
61+
.Xr Text::Amuse 3pm

spec/tools/bemuse.pl

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/usr/bin/env perl
2+
use strict;
3+
use warnings;
4+
use autodie;
5+
use v5.14;
6+
use utf8;
7+
$| = 1;
8+
9+
use warnings qw< FATAL utf8 >;
10+
use open qw< :std :utf8 >;
11+
use feature qw< say unicode_strings >;
12+
use File::Spec::Functions qw< rel2abs >;
13+
use Text::Amuse;
14+
15+
die <<END if(-t and $#ARGV < 0);
16+
bemuse: quickly convert a Muse document to HTML
17+
18+
Usage:
19+
bemuse < input # Write converted HTML to stdout
20+
bemuse file.muse # Create file.html
21+
END
22+
23+
24+
my $execDir = Cwd::abs_path rel2abs("../", "$0");
25+
my $tempFile = undef;
26+
27+
@ARGV = do {{
28+
# HACK: Text::Amuse(3pm) says:
29+
#
30+
# “Please note that you can't pass a string.
31+
# Build a wrapper going through a temporary
32+
# file if you need to pass strings.”
33+
#
34+
# Well, okay then…
35+
$tempFile = "$execDir/TEMP_FILE_LOL.tmp~";
36+
open(my $fh, "> :encoding(UTF-8)", $tempFile);
37+
while(<STDIN>){ print $fh $_; }
38+
close($fh);
39+
($tempFile);
40+
}} unless $ARGV[0];
41+
42+
END {
43+
if(defined($tempFile)){
44+
unlink $tempFile or die $@;
45+
}
46+
close STDOUT;
47+
}
48+
49+
for my $file (@ARGV) {
50+
my $document = Text::Amuse->new(file => $file) or die $@;
51+
my $html = $document->as_html;
52+
53+
# Reading (or read, rather) from standard input
54+
if(defined($tempFile) && !-t){
55+
$html =~ s/^\R+|\R+$//g;
56+
say $html;
57+
}
58+
else{
59+
(my $outputFile = $ARGV) =~ s/\.\Kmuse$/html/i;
60+
open(my $fh, "> :encoding(UTF-8)", $outputFile) or die $@;
61+
print $fh $html;
62+
close $fh or die $@;
63+
}
64+
}

0 commit comments

Comments
 (0)