1
0
mirror of https://github.com/pgbackrest/pgbackrest.git synced 2024-12-14 10:13:05 +02:00
pgbackrest/doc/lib/BackRestDoc/Common/DocRender.pm

516 lines
15 KiB
Perl
Raw Normal View History

2015-09-03 00:55:04 +02:00
####################################################################################################################################
# DOC RENDER MODULE
####################################################################################################################################
package BackRestDoc::Common::DocRender;
2015-09-03 00:55:04 +02:00
use strict;
use warnings FATAL => qw(all);
use Carp qw(confess);
use Exporter qw(import);
our @EXPORT = qw();
use File::Basename qw(dirname);
use lib dirname($0) . '/../lib';
use BackRest::Common::Log;
use BackRest::Common::String;
####################################################################################################################################
# Operation constants
####################################################################################################################################
use constant OP_DOC_RENDER => 'DocRender';
use constant OP_DOC_RENDER_PROCESS => OP_DOC_RENDER . '->process';
use constant OP_DOC_RENDER_PROCESS_TAG => OP_DOC_RENDER . '->processTag';
use constant OP_DOC_RENDER_PROCESS_TEXT => OP_DOC_RENDER . '->processText';
use constant OP_DOC_RENDER_NEW => OP_DOC_RENDER . '->new';
use constant OP_DOC_RENDER_SAVE => OP_DOC_RENDER . '->save';
# use HTML::HTML5::Builder qw[:standard JQUERY];
2015-09-03 00:55:04 +02:00
####################################################################################################################################
# Render tags for various output types
####################################################################################################################################
my $oRenderTag =
{
'markdown' =>
{
'b' => ['**', '**'],
'i' => ['_', '_'],
'bi' => ['_**', '**_'],
'ul' => ["\n", "\n"],
'ol' => ["\n", "\n"],
2015-09-03 00:55:04 +02:00
'li' => ['- ', "\n"],
'id' => ['`', '`'],
'file' => ['`', '`'],
'path' => ['`', '`'],
'cmd' => ['`', '`'],
'param' => ['`', '`'],
'setting' => ['`', '`'],
'code' => ['`', '`'],
'code-block' => ['```', '```'],
'exe' => [undef, ''],
'backrest' => [undef, ''],
'postgres' => ['PostgreSQL', '']
},
'text' =>
{
'b' => ['', ''],
'i' => ['', ''],
'bi' => ['', ''],
'ul' => ["\n", "\n"],
'ol' => ["\n", "\n"],
'li' => ['* ', "\n"],
'id' => ['', ''],
'file' => ['', ''],
'path' => ['', ''],
'cmd' => ['', ''],
'br-option' => ['', ''],
'param' => ['', ''],
'setting' => ['', ''],
'code' => ['', ''],
'code-block' => ['', ''],
'exe' => [undef, ''],
'backrest' => [undef, ''],
'postgres' => ['PostgreSQL', '']
},
2015-09-03 00:55:04 +02:00
'html' =>
{
'b' => ['<b>', '</b>'],
'i' => ['<i>', '</i>'],
'bi' => ['<i><b>', '</b></i>'],
'ul' => ['<ul>', '</ul>'],
'ol' => ['<ol>', '</ol>'],
'li' => ['<li>', '</li>'],
'id' => ['<span class="id">', '</span>'],
'file' => ['<span class="file">', '</span>'],
'path' => ['<span class="path">', '</span>'],
'cmd' => ['<span class="cmd">', '</span>'],
'user' => ['<span class="user">', '</span>'],
'br-option' => ['<span class="br-option">', '</span>'],
'br-setting' => ['<span class="br-setting">', '</span>'],
'pg-option' => ['<span class="pg-option">', '</span>'],
'pg-setting' => ['<span class="pg-setting">', '</span>'],
'code' => ['<id>', '</id>'],
'code-block' => ['<code-block>', '</code-block>'],
'exe' => ['<id>', '</id>'],
'setting' => ['<span class="br-setting">', '</span>'], # !!! This will need to be fixed
'backrest' => [undef, ''],
'postgres' => ['<span class="postgres">PostgreSQL</span>', '']
2015-09-03 00:55:04 +02:00
}
};
####################################################################################################################################
# CONSTRUCTOR
####################################################################################################################################
sub new
{
my $class = shift; # Class name
# Create the class hash
my $self = {};
bless $self, $class;
# Assign function parameters, defaults, and log debug info
(
my $strOperation,
$self->{strType},
$self->{strProjectName},
$self->{strExeName}
) =
logDebugParam
(
OP_DOC_RENDER_NEW, \@_,
{name => 'strType'},
{name => 'strProjectName'},
{name => 'strExeName'}
);
$$oRenderTag{markdown}{backrest}[0] = $self->{strProjectName};
$$oRenderTag{markdown}{exe}[0] = $self->{strExeName};
$$oRenderTag{text}{backrest}[0] = $self->{strProjectName};
$$oRenderTag{text}{exe}[0] = $self->{strExeName};
$$oRenderTag{html}{backrest}[0] = "<span class=\"backrest\">$self->{strProjectName}</span>";
2015-09-03 00:55:04 +02:00
# Return from function and log return values if any
return logDebugReturn
(
$strOperation,
{name => 'self', value => $self}
);
}
####################################################################################################################################
# process
#
# Render the document
####################################################################################################################################
sub process
{
my $self = shift;
# Assign function parameters, defaults, and log debug info
my
(
$strOperation,
$oDoc,
$iDepth,
$bChildList,
) =
logDebugParam
(
OP_DOC_RENDER_PROCESS, \@_,
{name => 'oDoc', trace => true},
2015-09-03 00:55:04 +02:00
{name => 'iDepth', default => 1, trace => true},
{name => 'bChildList', default => true, trace => true}
);
my $strType = $self->{strType};
my $strProjectName = $self->{strProjectName};
my $strBuffer = "";
my $bList = $oDoc->nameGet() =~ /.*-bullet-list$/;
2015-09-03 00:55:04 +02:00
$bChildList = defined($bChildList) ? $bChildList : false;
my $iChildDepth = $iDepth;
my @stryMonth = ('January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December');
if ($strType eq 'markdown' || $strType eq 'text')
2015-09-03 00:55:04 +02:00
{
if (defined($oDoc->paramGet('id', false)))
2015-09-03 00:55:04 +02:00
{
my @stryToken = split('-', $oDoc->nameGet());
2015-09-03 00:55:04 +02:00
my $strTitle = @stryToken == 0 ? '[unknown]' : $stryToken[@stryToken - 1];
$strBuffer = ('#' x $iDepth) . ' `' . $oDoc->paramGet('id') . '` ' . $strTitle;
2015-09-03 00:55:04 +02:00
}
# Try to get the title param from the element (!!! this is the old style and should be removed)
my $strTitle = $oDoc->paramGet('title', false);
if (!defined($strTitle))
{
$strTitle = $oDoc->paramGet('subtitle', false);
}
# If not found then get the title element
if (!defined($strTitle) && defined($oDoc->nodeGet('title', false)))
{
$strTitle = $self->processText($oDoc->nodeGet('title')->textGet());
}
if (defined($strTitle))
2015-09-03 00:55:04 +02:00
{
$strBuffer = ('#' x $iDepth) . ' ';
if (defined($oDoc->paramGet('version', false)))
2015-09-03 00:55:04 +02:00
{
$strBuffer .= 'v' . $oDoc->paramGet('version') . ': ';
2015-09-03 00:55:04 +02:00
}
$strBuffer .= ($iDepth == 1 ? "${strProjectName}<br/>" : '') . $strTitle;
2015-09-03 00:55:04 +02:00
if (defined($oDoc->paramGet('date', false)))
2015-09-03 00:55:04 +02:00
{
my $strDate = $oDoc->paramGet('date');
2015-09-03 00:55:04 +02:00
if ($strDate !~ /^(XXXX-XX-XX)|([0-9]{4}-[0-9]{2}-[0-9]{2})$/)
{
confess "invalid date ${strDate}";
}
if ($strDate =~ /^X/)
{
$strBuffer .= "\n__No Release Date Set__";
}
else
{
$strBuffer .= "\n__Released " . $stryMonth[(substr($strDate, 5, 2) - 1)] . ' ' .
(substr($strDate, 8, 2) + 0) . ', ' . substr($strDate, 0, 4) . '__';
}
}
}
if ($strBuffer ne "")
{
$iChildDepth++;
}
if (defined($oDoc->nodeGet('summary', false)))
{
if ($strBuffer ne "")
{
$strBuffer .= "\n\n";
}
if ($bChildList)
{
$strBuffer .= '* ';
}
$strBuffer .= $self->processText($oDoc->nodeGet('summary')->textGet());
}
if (defined($oDoc->textGet(false)))
2015-09-03 00:55:04 +02:00
{
if ($strBuffer ne "")
{
$strBuffer .= "\n\n";
}
if ($bChildList)
{
$strBuffer .= '* ';
}
$strBuffer .= $self->processText($oDoc->textGet());
2015-09-03 00:55:04 +02:00
}
if ($strBuffer ne "" && $iDepth != 1 && !$bList)
{
$strBuffer = "\n\n" . $strBuffer;
}
}
else
{
confess "unknown type ${strType}";
}
my $bFirst = true;
foreach my $oChild ($oDoc->nodeList(undef, false))
2015-09-03 00:55:04 +02:00
{
if ($oChild->nameGet() ne 'summary' && $oChild->nameGet() ne 'title')
2015-09-03 00:55:04 +02:00
{
if ($strType eq 'markdown' || $strType eq 'text')
{
}
else
{
confess "unknown type ${strType}";
}
2015-09-03 00:55:04 +02:00
$strBuffer .= $self->process($oChild, $iChildDepth, $bList);
}
2015-09-03 00:55:04 +02:00
}
if ($iDepth == 1)
{
if ($strType eq 'markdown' || $strType eq 'text')
2015-09-03 00:55:04 +02:00
{
$strBuffer .= "\n";
}
else
{
confess "unknown type ${strType}";
}
}
# Return from function and log return values if any
return logDebugReturn
(
$strOperation,
{name => 'strBuffer', value => $strBuffer, trace => true}
);
}
####################################################################################################################################
# processTag
####################################################################################################################################
sub processTag
{
my $self = shift;
# Assign function parameters, defaults, and log debug info
my
(
$strOperation,
$oTag
) =
logDebugParam
(
OP_DOC_RENDER_PROCESS_TAG, \@_,
{name => 'oTag', trace => true}
);
my $strBuffer = "";
my $strType = $self->{strType};
my $strTag = $oTag->nameGet();
2015-09-03 00:55:04 +02:00
if (!defined($strTag))
2015-09-03 00:55:04 +02:00
{
use Data::Dumper;
confess Dumper($oTag);
2015-09-03 00:55:04 +02:00
}
if ($strTag eq 'link')
2015-09-03 00:55:04 +02:00
{
my $strUrl = $oTag->paramGet('url', false);
if ($strType eq 'markdown')
{
if (!defined($strUrl))
{
$strUrl = '{[backrest-url-base]}/' . $oTag->paramGet('page');
}
$strBuffer = '[' . $oTag->valueGet() . '](' . $strUrl . ')';
}
elsif ($strType eq 'html')
{
if (!defined($strUrl))
{
$strUrl = $oTag->paramGet('page');
}
$strBuffer = '<a href="' . $strUrl . '">' . $oTag->valueGet() . '</a>';
}
else
{
confess "tag link not valid for type ${strType}";
}
2015-09-03 00:55:04 +02:00
}
else
2015-09-03 00:55:04 +02:00
{
my $strStart = $$oRenderTag{$strType}{$strTag}[0];
my $strStop = $$oRenderTag{$strType}{$strTag}[1];
if (!defined($strStart) || !defined($strStop))
2015-09-03 00:55:04 +02:00
{
confess "invalid type ${strType} or tag ${strTag}";
2015-09-03 00:55:04 +02:00
}
$strBuffer .= $strStart;
if ($strTag eq 'p' || $strTag eq 'title' || $strTag eq 'li' || $strTag eq 'code-block' || $strTag eq 'summary')
{
$strBuffer .= $self->processText($oTag);
}
elsif (defined($oTag->valueGet()))
{
$strBuffer .= $oTag->valueGet();
}
else
{
foreach my $oSubTag ($oTag->nodeList(undef, false))
{
$strBuffer .= $self->processTag($oSubTag);
}
}
$strBuffer .= $strStop;
}
2015-09-03 00:55:04 +02:00
# Return from function and log return values if any
return logDebugReturn
(
$strOperation,
{name => 'strBuffer', value => $strBuffer, trace => true}
);
}
####################################################################################################################################
# processText
####################################################################################################################################
sub processText
{
my $self = shift;
# Assign function parameters, defaults, and log debug info
my
(
$strOperation,
$oText
) =
logDebugParam
(
OP_DOC_RENDER_PROCESS_TEXT, \@_,
{name => 'oText', trace => true}
);
my $strType = $self->{strType};
my $strBuffer = '';
2015-09-03 00:55:04 +02:00
foreach my $oNode ($oText->nodeList(undef, false))
2015-09-03 00:55:04 +02:00
{
if (ref(\$oNode) eq "SCALAR")
2015-09-03 00:55:04 +02:00
{
$strBuffer .= $oNode;
}
else
{
$strBuffer .= $self->processTag($oNode);
2015-09-03 00:55:04 +02:00
}
}
#
# if ($strType eq 'html')
# {
# # $strBuffer =~ s/^\s+|\s+$//g;
#
# $strBuffer =~ s/\n/\<br\/\>\n/g;
# }
# if ($strType eq 'markdown')
# {
# $strBuffer =~ s/^\s+|\s+$//g;
$strBuffer =~ s/ +/ /g;
$strBuffer =~ s/^ //smg;
# }
2015-09-03 00:55:04 +02:00
# Return from function and log return values if any
return logDebugReturn
(
$strOperation,
{name => 'strBuffer', value => $strBuffer, trace => true}
);
}
####################################################################################################################################
# save
####################################################################################################################################
sub save
{
my $self = shift;
# Assign function parameters, defaults, and log debug info
my
(
$strOperation,
$strFileName,
$strBuffer
) =
logDebugParam
(
OP_DOC_RENDER_SAVE, \@_,
{name => 'strFileName', trace => true},
{name => 'strBuffer', trace => true}
);
# Open the file
my $hFile;
open($hFile, '>', $strFileName)
or confess &log(ERROR, "unable to open ${strFileName}");
# Write the buffer
my $iBufferOut = syswrite($hFile, $strBuffer);
# Report any errors
if (!defined($iBufferOut) || $iBufferOut != length($strBuffer))
{
confess "unable to write '${strBuffer}'" . (defined($!) ? ': ' . $! : '');
}
# Close the file
close($hFile);
# Return from function and log return values if any
return logDebugReturn($strOperation);
}
1;