Skip to content

Commit

Permalink
Add Asciidoctor module and refactor command utils
Browse files Browse the repository at this point in the history
Remove the preference for `asciidoctor` from Text::Markup::Asciidoc and
put it in a new parser, Text::Markup::Asciidoctor. Like
Text::Markup::CommonMark, this module is not auto-loaded by Text::Markup
when it recognizes an AsciiDoc document; it still defaults to
Text::Markup::Asciidoc for backward compatibility. But when the user
uses the module, it will be used in preference to
Text::Markup::Asciidoc.

This makes three parsers that use external command line utilities, so
extract the functions used to find and execute those utilities into a
new module, Text::Markup::Cmd. This reduces a ton of duplicate code and
simplifies adding new CLI-oriented parsers in the future.

It also simplifies the testing, as there is no longer magic behavior in
the Asciidoc and Markdown modules, so each can be tested individually.
  • Loading branch information
theory committed Sep 10, 2023
1 parent 388e6b0 commit 48b2da6
Show file tree
Hide file tree
Showing 10 changed files with 326 additions and 197 deletions.
24 changes: 7 additions & 17 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,7 @@ jobs:
cpanm -v --notest --no-man-pages Module::Build Archive::Tar ExtUtils::PL2Bat
cpanm -v --notest --no-man-pages --installdeps --with-develop .
pip3 install docutils asciidoc
# Testing default implmementations, ecluding AsciiDoctor and CommonMark.
- name: Test
env:
AUTHOR_TESTING: 1
RELEASE_TESTING: 1
TEXT_MARKUP_TEST_ALL: 1
TEXT_MARKUP_SKIP_COMMONMARK: 1
TEXT_MARKUP_TEST_ASCIIDOC: asciidoc
run: prove -lv

- name: Install Asciidoctor
run: gem install asciidoctor
gem install asciidoctor
- name: Brew CommonMark
if: runner.os == 'macOS'
Expand All @@ -59,12 +47,14 @@ jobs:
sudo apt-get install libcmark-dev
cpanm -v --notest --no-man-pages CommonMark
# Run author tests and test CommonMark and the output of asciidoctor.
- name: Test Overrides
# Testing default implmementations, excluding AsciiDoctor and CommonMark.
- name: Test
env:
TEXT_MARKUP_TEST_ASCIIDOC: asciidoctor
AUTHOR_TESTING: 1
RELEASE_TESTING: 1
TEXT_MARKUP_TEST_ALL: 1
TEXT_MARKUP_SKIP_COMMONMARK: ${{ matrix.os.name == 'windows' }}
run: prove -lv t
run: prove -lv

# Make sure we can build the distribution bundle.
- name: Test Distro
Expand Down
9 changes: 7 additions & 2 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
Revision history for Perl extension Text-Markup.

0.30
- The Asciidoc renderer now prefers `asciidoctor` if it's available, and
but falls back on `asciidoc` when it's not.
- Removed very old custom paths to `docutils` (reST) and `asciidoc`
binaries on Windows. Users must ensure that the proper commands are
in the PATH, instead.
- Added Text::Markdown::Asciidoctor, which uses the `asciidoctor` command,
as a updated alternative to Text::Markdown::Asciidoc, which uses the
legacy `asciidoc` command. Text::Markdown will not use this formatter by
default, but when explicitly loaded will be used instead of
Text::Markup::Asciidoc.
- Added Text::Markdown::CommonMark, which uses the cmark library, via the
CommonMark module. Text::Markdown will not use this formatter by
default, but when explicitly loaded will be used instead of
Text::Markup::Markdown.
- De-duped the utilities for managing external commands from the Rest and
Asciidoc modules into Text::Markdown::Cmd.

0.25 2023-09-04T22:20:13Z
- Fixed the importation of Pod::Simple::XHTML, Text::Trac, Text::Textile,
Expand Down
84 changes: 10 additions & 74 deletions lib/Text/Markup/Asciidoc.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3,56 +3,15 @@ package Text::Markup::Asciidoc;
use 5.8.1;
use strict;
use warnings;
use File::Spec;
use constant WIN32 => $^O eq 'MSWin32';
use Symbol 'gensym';
use IPC::Open3;
use Text::Markup::Cmd;
use utf8;

our $VERSION = '0.30';

# Find Asciidoc.
sub _find_cli {
my @names = (
(map {
(WIN32 ? ("$_.exe", "$_.bat") : ($_))
} qw(asciidoctor asciidoc)),
'asciidoc.py',
);
my $cli;
EXE: {
for my $exe (@names) {
for my $p (File::Spec->path) {
my $path = File::Spec->catfile($p, $exe);
next unless -f $path && -x $path;
$cli = $path;
last EXE;
}
}
}

unless ($cli) {
use Carp;
my $sep = WIN32 ? ';' : ':';
my $list = join(', ', @names[0..$#names-1]) . ", or $names[-1]";
Carp::croak(
"Cannot find $list in path " . join $sep => File::Spec->path
);
}

# Make sure it looks like it will work.
my $output = gensym;
my $pid = open3 undef, $output, $output, $cli, '--version';
waitpid $pid, 0;
if ($?) {
use Carp;
local $/;
Carp::croak( qq{$cli will not execute\n}, <$output> );
}
return $cli;
}

my $ASCIIDOC = _find_cli;
my $ASCIIDOC = find_cmd([
(map { (WIN32 ? ("$_.exe", "$_.bat") : ($_)) } qw(asciidoc)),
'asciidoc.py',
], '--version');

# Arguments to pass to asciidoc.
# Restore --safe if Asciidoc ever fixes it with the XHTML back end.
Expand All @@ -66,7 +25,7 @@ my @OPTIONS = qw(
sub parser {
my ($file, $encoding, $opts) = @_;
my $html = do {
my $fh = _fh(
my $fh = open_pipe(
$ASCIIDOC, @OPTIONS,
'--attribute' => "encoding=$encoding",
$file
Expand All @@ -92,27 +51,6 @@ $html
};
}

# Stolen from SVN::Notify.
sub _fh {
if (WIN32) {
my $cmd = q{"} . join(q{" "}, @_) . q{"|};
open my $fh, $cmd or die "Cannot fork: $!\n";
return $fh;
}

my $pid = open my $fh, '-|';
die "Cannot fork: $!\n" unless defined $pid;

if ($pid) {
# Parent process, return the file handle.
return $fh;
} else {
# Child process. Execute the commands.
exec @_ or die "Cannot exec $_[0]: $!\n";
# Not reached.
}
}

1;
__END__
Expand All @@ -129,11 +67,9 @@ Text::Markup::Asciidoc - Asciidoc parser for Text::Markup
=head1 Description
This is the L<Asciidoc|https://asciidoc.org/> parser for L<Text::Markup>. It
depends on the C<asciidoctor> command-line application; see the
L<installation docs|https://asciidoctor.org/#installation> for details, or
use the command C<gem install asciidoctor>. It falls back on the
L<legacy C<asciidoc>|https://asciidoc-py.github.io> processor if
C<asciidoctor> is not available.
depends on the L<C<asciidoc>|https://asciidoc-py.github.io> command-line
application. See the L<installation docs|https://asciidoc-py.github.io/INSTALL.html>
for details, or use the command C<pip3 install asciidoc>.
Text::Markup::Asciidoc recognizes files with the following extensions as
Asciidoc:
Expand All @@ -158,7 +94,7 @@ David E. Wheeler <david@justatheory.com>
=head1 Copyright and License
Copyright (c) 2012-2019 David E. Wheeler. Some Rights Reserved.
Copyright (c) 2012-2023 David E. Wheeler. Some Rights Reserved.
This module is free software; you can redistribute it and/or modify it under
the same terms as Perl itself.
Expand Down
113 changes: 113 additions & 0 deletions lib/Text/Markup/Asciidoctor.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package Text::Markup::Asciidoctor;

use 5.8.1;
use strict;
use warnings;
use Text::Markup::Cmd;
use utf8;

our $VERSION = '0.30';

# Replace Text::Markup::Asciidoc.
Text::Markup->register( asciidoc => qr{a(?:sc(?:iidoc)?|doc)?} );

# Find Asciidoc.
my $ASCIIDOC = find_cmd([
(map { (WIN32 ? ("$_.exe", "$_.bat") : ($_)) } qw(asciidoctor)),
'asciidoctor.py',
], '--version');

# Arguments to pass to asciidoc.
# Restore --safe if Asciidoc ever fixes it with the XHTML back end.
# https://groups.google.com/forum/#!topic/asciidoc/yEr5PqHm4-o
my @OPTIONS = qw(
--no-header-footer
--out-file -
--attribute newline=\\n
);

sub parser {
my ($file, $encoding, $opts) = @_;
my $html = do {
my $fh = open_pipe(
$ASCIIDOC, @OPTIONS,
'--attribute' => "encoding=$encoding",
$file
);

binmode $fh, ":encoding($encoding)";
local $/;
<$fh>;
};

# Make sure we have something.
return unless $html =~ /\S/;
utf8::encode $html;
return $html if $opts->{raw};
return qq{<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
$html
</body>
</html>
};
}
1;
__END__
=head1 Name
Text::Markup::Asciidoc - Asciidoc parser for Text::Markup
=head1 Synopsis
use Text::Markup::Asciidoctor;
my $html = Text::Markup->new->parse(file => 'hello.adoc');
my $raw_asciidoc = Text::Markup->new->parse(file => 'hello.adoc', raw => 1 );
=head1 Description
This is the L<Asciidoc|https://asciidoc.org/> parser for L<Text::Markup>. It
depends on the C<asciidoctor> command-line application; see the
L<installation docs|https://asciidoctor.org/#installation> for details, or
use the command C<gem install asciidoctor>. Note that L<Text::Markup> does
not load this module by default, but when loaded manually will replace
Text::Markup::Asciidoc as preferred Asciidoc parser.
Text::Markup::Asciidoctor reads in the file (relying on a
L<BOM|https://www.unicode.org/unicode/faq/utf_bom.html#BOM>), hands it off to
L<C<asciidoctor>|https://asciidoctor.org> for parsing, and then returns the
generated HTML as an encoded UTF-8 string with an C<http-equiv="Content-Type">
element identifying the encoding as UTF-8.
Text::Markup::Asciidoctor recognizes files with the following extensions as
Asciidoc:
=over
=item F<.asciidoc>
=item F<.asc>
=item F<.adoc>
=back
Normally this parser returns the output of C<asciidoctor> wrapped in a minimal
HTML page skeleton. If you would prefer to just get the exact output returned
by C<asciidoctor>, you can pass in a true value for the C<raw> option.
=head1 Author
David E. Wheeler <david@justatheory.com>
=head1 Copyright and License
Copyright (c) 2012-2023 David E. Wheeler. Some Rights Reserved.
This module is free software; you can redistribute it and/or modify it under
the same terms as Perl itself.
=cut
Loading

0 comments on commit 48b2da6

Please sign in to comment.