From 71edacd9da10889bc928c9c3f4cccf366bd4774b Mon Sep 17 00:00:00 2001 From: Brian Fraser Date: Wed, 7 Nov 2012 09:35:32 -0300 Subject: [PATCH] Replaced util/pod2rst-fixed with util/pod2rst-fixed, which includes the fixes in util/fix-pod2rst-output, but also uses the link callback to fix 927955, and also tentatively resolves 898665 --- util/fix-pod2rst-output | 68 --------------------------- util/pod2rst-fixed | 100 ++++++++++++++++++++++++++++++++++++++++ util/write-user-docs | 13 +----- 3 files changed, 102 insertions(+), 79 deletions(-) delete mode 100755 util/fix-pod2rst-output create mode 100755 util/pod2rst-fixed diff --git a/util/fix-pod2rst-output b/util/fix-pod2rst-output deleted file mode 100755 index f618fb38..00000000 --- a/util/fix-pod2rst-output +++ /dev/null @@ -1,68 +0,0 @@ -#!/usr/bin/env perl - -use strict; -use warnings FATAL => 'all'; -use English qw(-no_match_vars); -use constant PTDEBUG => $ENV{PTDEBUG} || 0; - -my $file = shift @ARGV; -die "I need a file" unless $file; -die "$file is not a file" unless -f $file; -open my $fh, '<', $file or die "Cannot open $file: $OS_ERROR"; - -my $tool = shift @ARGV; -die "I need a tool" unless $tool; - -print ".. program:: $tool\n\n", - ('=' x (length($tool) + 11)), "\n", - ":program:`$tool`\n", - ('=' x (length($tool) + 11)), "\n\n"; - -$INPUT_RECORD_SEPARATOR = ''; - -my $in_code_block = 0; -my $section = ''; - -while (my $para = <$fh>) { - next if $para =~ m/^\.\. highlight:: perl/; - - $in_code_block = $para =~ m/^\s{2,}/ ? 1 : 0; - - if ($para =~ m/^\*{2,}\n([\w\s,-]+)\n\*{2,}$/m) { - print "$1\n", - ('=' x length $1), - "\n\n"; - $section = $1; - } - elsif ($para =~ m/^Usage: /) { - $para =~ s/^Usage: //; - print "Usage\n", - "-----\n\n", - "::\n\n", - " $para"; - } - elsif ($para =~ m/^Examples:/) { - print "Examples\n", - "--------\n\n"; - } - else { - $para =~ s/\.\. code-block:: perl/.. code-block:: bash/mg; - $para =~ s/`+$tool`+/$tool/g; - $para =~ s/([^\/])$tool/$1:program:`$tool`/g unless $in_code_block; - $para =~ s/^$tool/:program:`$tool`/gm; - $para =~ s/^--(\S+)$/.. option:: --$1/mg; - $para =~ s/"--(\S+)"/:option:`--$1`/g; - $para =~ s/\\\*/*/g; - $para =~ s/\\ //g; - $para =~ s/^[ ]+$//mg; - $para =~ s/^\n\n/\n/mg; - $para =~ s/code-block:: bash(\s+)CREATE/code-block:: sql$1CREATE/sg; - if ( ($section || '') eq 'OUTPUT' ) { - $para =~ s/^([A-Z_]+)\n\n/$1\n/; - } - print $para; - } -} - -close $fh; -exit; diff --git a/util/pod2rst-fixed b/util/pod2rst-fixed new file mode 100755 index 00000000..3f14b8d4 --- /dev/null +++ b/util/pod2rst-fixed @@ -0,0 +1,100 @@ +#!/usr/bin/env perl +use 5.010; # for \K +use strict; +use warnings; +use English qw(-no_match_vars); + +use IO::File; + +use File::Basename qw(basename); +use Pod::POM::View::Restructured; + +my $input_file = shift @ARGV or die "Need an input file"; + +my $tool = basename($input_file); + +open my $in_fh, q{<:encoding(UTF-8)}, $input_file + or die "Cannot open $input_file: $!"; + +open my $out_fh, q{>}, \my $out; + +my $conv = Pod::POM::View::Restructured->new(); +my $output = $conv->convert_file($in_fh, undef, $out_fh, { link => \&format_links }); + +close $in_fh; +close $out_fh; + +if (!defined($output)) { + die "Failed to convert!"; +} + +my $header = + ".. program:: $tool\n\n" . + ('=' x (length($tool) + 11)) . "\n" . + ":program:`$tool`\n" . + ('=' x (length($tool) + 11)) . "\n\n"; + +open my $in, q{<:encoding(UTF-8)}, \$out; + +local $INPUT_RECORD_SEPARATOR = ''; + +my $in_code_block = 0; +my $section = ''; + +my $fixed_output = ''; + +while (my $para = <$in>) { + next if $para =~ m/^\.\. highlight:: perl/; + + $in_code_block = $para =~ m/^\s{2,}/ ? 1 : 0; + + if ($para =~ m/^\*{2,}\n([\w\s,-]+)\n\*{2,}$/m) { + $fixed_output .= "$1\n" . + ('=' x length $1) . + "\n\n"; + $section = $1; + } + elsif ($para =~ m/^Usage: /) { + $para =~ s/^Usage: //; + $fixed_output .= "Usage\n" . + "-----\n\n" . + "::\n\n" . + " $para"; + } + elsif ($para =~ m/^Examples:/) { + $fixed_output .= "Examples\n" . + "--------\n\n"; + } + else { + $para =~ s/\.\. code-block:: perl/.. code-block:: bash/mg; + $para =~ s/`+$tool`+/$tool/g; + $para =~ s/([^\/])$tool/$1:program:`$tool`/g unless $in_code_block; + $para =~ s/^$tool/:program:`$tool`/gm; + $para =~ s/^--(\S+)$/.. option:: --$1/mg; + $para =~ s/"--(\S+)"/:option:`--$1`/g; + $para =~ s/\\\*/*/g; + $para =~ s/\\ //g; + $para =~ s/^[ ]+$//mg; + $para =~ s/^\n\n/\n/mg; + $para =~ s/code-block:: bash(\s+)CREATE/code-block:: sql$1CREATE/sg; + $para =~ s/\Q.. option:: \E\K(.+)/\Q$1/g; + if ( ($section || '') eq 'OUTPUT' ) { + $para =~ s/^([A-Z_]+)\n\n/$1\n/; + } + $fixed_output .= $para; + } +} + +close $in; + +print $header . $fixed_output; + +sub format_links { + if ( my ($label, $url) = split /\|/, $_[0] ) { + return $url, $label; + } + else { + local $conv->{callbacks}{link}; + return $conv->view_seq_link(@_) + } +} \ No newline at end of file diff --git a/util/write-user-docs b/util/write-user-docs index 72e2cd78..b1f7a870 100755 --- a/util/write-user-docs +++ b/util/write-user-docs @@ -70,18 +70,9 @@ write_rst() { warn "$file does not exist" return fi - local tool=$(basename $file) - cat $file | pod2rst > $RST_DIR/$tool.orig.rst - if [ $? -eq 0 ]; then - $BRANCH/util/fix-pod2rst-output $RST_DIR/$tool.orig.rst $tool \ - > $RST_DIR/$tool.rst - echo "Wrote $RST_DIR/$tool.rst" - else - warn "Error writing $RST_DIR/tool.rst" - fi - - rm $RST_DIR/$tool.orig.rst + $BRANCH/util/pod2rst-fixed $file > $RST_DIR/$tool.rst + echo "Wrote $RST_DIR/$tool.rst" } # Parse the head1 sections from percona-toolkit.pod and write them as