2015-03-16 11:37:24 +00:00
|
|
|
#!/usr/local/apps/perl/current/bin/perl -I/usr/local/lib/metaps/perl
|
|
|
|
|
|
|
|
use Getopt::Long;
|
|
|
|
use strict;
|
|
|
|
|
|
|
|
#--------------------------------------------------
|
2015-11-12 11:16:59 +00:00
|
|
|
#
|
2015-03-16 11:37:24 +00:00
|
|
|
#--------------------------------------------------
|
|
|
|
|
|
|
|
#The root page of examples in confluence!!!
|
|
|
|
my $rootPage="API examples";
|
|
|
|
|
2015-11-11 13:47:36 +00:00
|
|
|
my @langKeys=("f","p","c");
|
2015-03-16 11:37:24 +00:00
|
|
|
|
|
|
|
my %langName = ("f" => "Fortran 90",
|
|
|
|
"c" => "C",
|
|
|
|
"p" => "Python");
|
|
|
|
|
|
|
|
my %langType = ("f" => "none",
|
|
|
|
"c" => "cpp",
|
|
|
|
"p" => "python");
|
|
|
|
|
|
|
|
my %langSuffix = ("f" => "f90",
|
|
|
|
"c" => "c",
|
|
|
|
"p" => "py");
|
|
|
|
|
|
|
|
my %langDir = ("f" => "F90",
|
|
|
|
"c" => "C",
|
|
|
|
"p" => "python");
|
|
|
|
|
2015-11-26 09:31:36 +00:00
|
|
|
my %langComment = ("f" => "!",
|
|
|
|
"c" => "*",
|
|
|
|
"p" => "#");
|
|
|
|
|
2015-03-16 11:37:24 +00:00
|
|
|
|
2015-11-12 11:16:59 +00:00
|
|
|
my $CONF=$ENV{CONF};
|
2015-11-12 09:42:46 +00:00
|
|
|
die "Env var CONF should point to confluence.sh script" if ( $CONF eq "" || ! -f "$CONF" );
|
2015-11-11 13:47:36 +00:00
|
|
|
|
2015-03-16 11:37:24 +00:00
|
|
|
my $inRootDir;
|
2015-11-12 09:42:46 +00:00
|
|
|
my $confSpace="ECC";
|
2015-03-16 11:37:24 +00:00
|
|
|
|
|
|
|
#---------------------------------
|
|
|
|
# Read arguments
|
|
|
|
#---------------------------------
|
|
|
|
|
2015-11-12 09:42:46 +00:00
|
|
|
GetOptions("inDir=s" => \$inRootDir,
|
|
|
|
"confSpace=s" => \$confSpace)
|
2015-03-16 11:37:24 +00:00
|
|
|
or die("Error in command line arguments\n");
|
2015-11-12 11:16:59 +00:00
|
|
|
|
2015-11-12 09:42:46 +00:00
|
|
|
print "confluence space=".$confSpace."\n";
|
|
|
|
|
2015-03-16 11:37:24 +00:00
|
|
|
#----------------------------------
|
|
|
|
# Dirs
|
|
|
|
#----------------------------------
|
|
|
|
|
|
|
|
#The root dir of the examples
|
|
|
|
$inRootDir="../examples";
|
|
|
|
|
|
|
|
#The genareted html files are stored here
|
|
|
|
my $htmlDir=$ENV{TMPDIR}."/res_html";
|
|
|
|
|
|
|
|
|
|
|
|
my %allExamples=read_examples();
|
|
|
|
|
|
|
|
#----------------------------------------
|
|
|
|
# Preparations
|
|
|
|
#----------------------------------------
|
|
|
|
|
|
|
|
unless ( -d $htmlDir ) {
|
|
|
|
mkdir $htmlDir
|
|
|
|
}
|
|
|
|
|
|
|
|
#---------------------------------------------------
|
|
|
|
# Loop for the code types (e.g. grib, bufr, etc.)
|
|
|
|
#---------------------------------------------------
|
|
|
|
|
|
|
|
foreach my $cType (keys %allExamples) {
|
2015-11-12 11:16:59 +00:00
|
|
|
|
2015-03-16 11:37:24 +00:00
|
|
|
print "----------------------------------------\n";
|
|
|
|
print " Processing examples for: $cType\n";
|
|
|
|
print "----------------------------------------\n";
|
2015-11-12 11:16:59 +00:00
|
|
|
|
2015-03-16 11:37:24 +00:00
|
|
|
#Get the examples
|
2015-11-12 11:16:59 +00:00
|
|
|
my @examples=@{$allExamples{$cType}};
|
|
|
|
|
2015-03-16 11:37:24 +00:00
|
|
|
if($#examples+1 == 0) {
|
|
|
|
die "No examples are defined!\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
#---------------------------------------------------
|
|
|
|
# Create a parent page for the examples of this type
|
|
|
|
#---------------------------------------------------
|
|
|
|
|
|
|
|
my $parentPage=parentPageTitle($cType);
|
|
|
|
makeParentPage($rootPage,$parentPage,$htmlDir,$cType,@examples);
|
2015-11-12 11:16:59 +00:00
|
|
|
|
2015-03-16 11:37:24 +00:00
|
|
|
#--------------------------------
|
|
|
|
# Loop for the examples
|
|
|
|
#--------------------------------
|
2015-11-12 11:16:59 +00:00
|
|
|
|
2015-03-16 11:37:24 +00:00
|
|
|
foreach my $name (@examples) {
|
2015-11-12 11:16:59 +00:00
|
|
|
|
2015-03-16 11:37:24 +00:00
|
|
|
my $fOut=$htmlDir."/".$cType."_".$name.".html";
|
2015-11-12 11:16:59 +00:00
|
|
|
open(OUT,">$fOut") or die "$fOut: $!";
|
|
|
|
|
2015-03-16 11:37:24 +00:00
|
|
|
#Description
|
|
|
|
print OUT "<h3>Description</h3>";
|
|
|
|
print OUT "This example shows: <i>".getDescription($name)."</i>\n";
|
2015-11-12 11:16:59 +00:00
|
|
|
|
2015-03-16 11:37:24 +00:00
|
|
|
#Create a tab container
|
|
|
|
print OUT "<h3>Source code</h3>";
|
|
|
|
print OUT "<ac:structured-macro ac:name=\"auitabs\"><ac:parameter ac:name=\"direction\">horizontal</ac:parameter><ac:rich-text-body>";
|
2015-11-12 11:16:59 +00:00
|
|
|
|
2015-03-16 11:37:24 +00:00
|
|
|
#Add a tab for each language
|
|
|
|
foreach my $i (@langKeys) {
|
2015-11-12 11:16:59 +00:00
|
|
|
|
2015-03-16 11:37:24 +00:00
|
|
|
my $tabName=$langName{$i};
|
|
|
|
my $fName=$name.".".$langSuffix{$i};
|
|
|
|
my $fPath=$inRootDir."/".$langDir{$i}."/".$fName;
|
|
|
|
my $lType=$langType{$i};
|
2015-11-12 11:16:59 +00:00
|
|
|
|
2015-03-16 11:37:24 +00:00
|
|
|
#Read sourcode into html format
|
|
|
|
my $c=getSourceCode($fName,$fPath,$lType);
|
2015-11-12 11:16:59 +00:00
|
|
|
|
|
|
|
#If there is a source creates a tab for it
|
|
|
|
if(length($c) > 0) {
|
|
|
|
print OUT "<ac:structured-macro ac:name=\"auitabspage\"><ac:parameter ac:name=\"title\">".$tabName."</ac:parameter>
|
2015-03-16 11:37:24 +00:00
|
|
|
<ac:rich-text-body>".$c."</ac:rich-text-body></ac:structured-macro>";
|
2015-11-12 11:16:59 +00:00
|
|
|
}
|
2015-03-16 11:37:24 +00:00
|
|
|
}
|
2015-11-12 11:16:59 +00:00
|
|
|
|
2015-03-16 11:37:24 +00:00
|
|
|
#Close tab container
|
2015-11-12 11:16:59 +00:00
|
|
|
print OUT "</ac:rich-text-body></ac:structured-macro>";
|
2015-03-16 11:37:24 +00:00
|
|
|
|
|
|
|
close OUT;
|
2015-11-12 11:16:59 +00:00
|
|
|
|
|
|
|
#Upload the file to confluence
|
2015-03-16 11:37:24 +00:00
|
|
|
my $pageTitle=examplePageTitle($name);
|
2015-11-12 11:16:59 +00:00
|
|
|
loadToConf($fOut,$pageTitle,$parentPage);
|
2015-03-16 11:37:24 +00:00
|
|
|
}
|
2015-11-12 11:16:59 +00:00
|
|
|
}
|
2015-03-16 11:37:24 +00:00
|
|
|
|
|
|
|
#===========================================================
|
|
|
|
#===========================================================
|
|
|
|
#
|
2015-11-12 11:16:59 +00:00
|
|
|
# FUNCTIONS
|
2015-03-16 11:37:24 +00:00
|
|
|
#
|
|
|
|
#===========================================================
|
|
|
|
#===========================================================
|
|
|
|
|
|
|
|
#====================================================
|
|
|
|
#
|
|
|
|
# Functions related to the examples
|
|
|
|
#
|
|
|
|
#====================================================
|
|
|
|
|
|
|
|
#------------------------------------------
|
|
|
|
# Read in example list
|
|
|
|
#------------------------------------------
|
|
|
|
|
|
|
|
sub read_examples {
|
|
|
|
|
|
|
|
my %res=();
|
|
|
|
my $fIn="examples.par";
|
|
|
|
open(IN,"<$fIn") or die "$fIn: $!";
|
|
|
|
|
|
|
|
my $line;
|
|
|
|
my $actType;
|
|
|
|
while (defined ($line = <IN>)) {
|
|
|
|
chomp($line);
|
|
|
|
if ($line =~ /.*#/) {next;}
|
|
|
|
|
|
|
|
#print $line."\n";
|
2015-11-12 11:16:59 +00:00
|
|
|
|
2015-03-16 11:37:24 +00:00
|
|
|
$line =~ s/\"//g;
|
2015-11-12 11:16:59 +00:00
|
|
|
|
2015-03-16 11:37:24 +00:00
|
|
|
my ($a)=($line =~ /(\S+)\s*\(/);
|
|
|
|
if($a and $a !~ /\!/) {
|
|
|
|
$actType=$a;
|
|
|
|
#print "type: ".$a."\n";
|
|
|
|
#$res{$actType};
|
|
|
|
next;
|
|
|
|
}
|
2015-11-12 11:16:59 +00:00
|
|
|
|
2015-03-16 11:37:24 +00:00
|
|
|
if( $line =~ /\)/) {
|
|
|
|
$actType="";
|
|
|
|
next;
|
|
|
|
}
|
2015-11-12 11:16:59 +00:00
|
|
|
|
|
|
|
if(length($actType) > 0) {
|
2015-03-16 11:37:24 +00:00
|
|
|
my ($b) =($line =~ /(\S+)/);
|
|
|
|
#print "ex: ".$b."\n";
|
|
|
|
if($b) {
|
|
|
|
push(@{$res{$actType}},$b);
|
|
|
|
}
|
2015-11-12 11:16:59 +00:00
|
|
|
}
|
2015-03-16 11:37:24 +00:00
|
|
|
}
|
2015-11-12 11:16:59 +00:00
|
|
|
|
2015-03-16 11:37:24 +00:00
|
|
|
close IN;
|
2015-11-12 11:16:59 +00:00
|
|
|
|
2015-03-16 11:37:24 +00:00
|
|
|
return %res;
|
|
|
|
}
|
|
|
|
|
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
# Genarate the page title for the parent page for a given type of examples
|
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
sub parentPageTitle {
|
2015-11-12 11:16:59 +00:00
|
|
|
|
2015-03-16 11:37:24 +00:00
|
|
|
my ($codeType) = @_;
|
2015-11-12 11:16:59 +00:00
|
|
|
return $codeType." examples";
|
|
|
|
}
|
2015-03-16 11:37:24 +00:00
|
|
|
|
|
|
|
#--------------------------------------------------------
|
|
|
|
# Create parent page for a given type of examples
|
|
|
|
#--------------------------------------------------------
|
|
|
|
|
|
|
|
sub makeParentPage {
|
|
|
|
my ($rootPage,$pageTitle,$outDir,$cType,@xmp) = @_;
|
2015-11-12 11:16:59 +00:00
|
|
|
|
2015-03-16 11:37:24 +00:00
|
|
|
my $f=$outDir."/parent_".$cType."html";
|
2015-11-12 11:16:59 +00:00
|
|
|
|
|
|
|
open(OUT,">$f") or die "$f: $!";
|
|
|
|
|
2015-03-16 11:37:24 +00:00
|
|
|
my $str="<table><tbody>";
|
|
|
|
$str=$str."<tr><th>Example</th><th>Description</th></tr>";
|
2015-11-12 11:16:59 +00:00
|
|
|
|
2015-03-16 11:37:24 +00:00
|
|
|
foreach my $xm (@xmp) {
|
|
|
|
$str=$str."<tr><td>".linkToPage($xm,examplePageTitle($xm))."</td><td>".getDescription($xm)."</td></tr>";
|
2015-11-12 11:16:59 +00:00
|
|
|
}
|
|
|
|
$str=$str."</tbody></table>";
|
|
|
|
|
2015-03-16 11:37:24 +00:00
|
|
|
print OUT $str;
|
2015-11-12 11:16:59 +00:00
|
|
|
|
2015-03-16 11:37:24 +00:00
|
|
|
close OUT;
|
2015-11-12 11:16:59 +00:00
|
|
|
|
2015-03-16 11:37:24 +00:00
|
|
|
#Load it to confluence
|
|
|
|
loadToConf($f,$pageTitle,$rootPage);
|
|
|
|
}
|
|
|
|
|
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
# Genarate the page title for a given example
|
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
sub examplePageTitle {
|
2015-11-12 11:16:59 +00:00
|
|
|
|
2015-03-16 11:37:24 +00:00
|
|
|
my ($exName) = @_;
|
2015-11-12 11:16:59 +00:00
|
|
|
return $exName;
|
|
|
|
}
|
|
|
|
|
2015-03-16 11:37:24 +00:00
|
|
|
#-----------------------------------------------------------
|
2015-11-12 11:16:59 +00:00
|
|
|
# Get the file path for an example with the given language
|
2015-03-16 11:37:24 +00:00
|
|
|
#-----------------------------------------------------------
|
|
|
|
|
2015-11-12 11:16:59 +00:00
|
|
|
sub examplePath {
|
2015-03-16 11:37:24 +00:00
|
|
|
my ($name,$lang) = @_;
|
|
|
|
return $inRootDir."/".$langDir{$lang}."/".$name.".".$langSuffix{$lang};
|
|
|
|
}
|
|
|
|
|
|
|
|
#-----------------------------------------------------------
|
2015-11-12 11:16:59 +00:00
|
|
|
# Check if the file for a given example exists.
|
2015-03-16 11:37:24 +00:00
|
|
|
#-----------------------------------------------------------
|
|
|
|
|
|
|
|
sub hasExample {
|
2015-11-12 11:16:59 +00:00
|
|
|
|
2015-03-16 11:37:24 +00:00
|
|
|
my ($name,$lang) = @_;
|
|
|
|
my $f=examplePath($name,$lang);
|
|
|
|
if( -e $f ) {return 1};
|
2015-11-12 11:16:59 +00:00
|
|
|
|
2015-03-16 11:37:24 +00:00
|
|
|
print "$f no!!!\n";
|
2015-11-12 11:16:59 +00:00
|
|
|
|
2015-03-16 11:37:24 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#------------------------------------------------------------
|
|
|
|
# Generate confluence formatted html string form a source file
|
|
|
|
#------------------------------------------------------------
|
|
|
|
|
|
|
|
sub getSourceCode {
|
2015-11-12 11:16:59 +00:00
|
|
|
|
2015-03-16 11:37:24 +00:00
|
|
|
my ($title,$fPath,$lang) = @_;
|
|
|
|
my $codeTxt;
|
2015-11-12 11:16:59 +00:00
|
|
|
|
2015-03-16 11:37:24 +00:00
|
|
|
print $fPath."\n";
|
2015-11-12 11:16:59 +00:00
|
|
|
|
2015-03-16 11:37:24 +00:00
|
|
|
my $str;
|
2015-11-12 11:16:59 +00:00
|
|
|
|
2015-03-16 11:37:24 +00:00
|
|
|
if($lang eq "") {
|
|
|
|
$codeTxt=fortran_file_to_string($fPath);
|
|
|
|
#return "<p /><code><ac:macro ac:name=\"html\"><ac:plain-text-body><![CDATA[".$codeTxt."]]></ac:plain-text-body></ac:macro></code>";
|
|
|
|
return "<p /><pre>".$codeTxt."</pre>";
|
2015-11-12 11:16:59 +00:00
|
|
|
} else {
|
|
|
|
open(IN,"<$fPath") or return ""; # die "$fPath: $!";
|
|
|
|
$codeTxt=join("", <IN>);
|
2015-03-16 11:37:24 +00:00
|
|
|
close IN;
|
|
|
|
}
|
2015-11-12 11:16:59 +00:00
|
|
|
|
2015-03-16 11:37:24 +00:00
|
|
|
$str="
|
|
|
|
<ac:structured-macro ac:name=\"code\">
|
|
|
|
<ac:parameter ac:name=\"title\">".$title."</ac:parameter>
|
|
|
|
<ac:parameter ac:name=\"linenumbers\">false</ac:parameter>
|
|
|
|
<ac:parameter ac:name=\"language\">".$lang."</ac:parameter>
|
|
|
|
<ac:plain-text-body><![CDATA[".$codeTxt."]]></ac:plain-text-body>
|
|
|
|
</ac:structured-macro>";
|
2015-11-12 11:16:59 +00:00
|
|
|
|
2015-03-16 11:37:24 +00:00
|
|
|
return $str;
|
|
|
|
}
|
|
|
|
|
|
|
|
#--------------------------------------------------------
|
|
|
|
# Extract the description of a given example
|
|
|
|
#--------------------------------------------------------
|
|
|
|
|
|
|
|
sub getDescription {
|
2015-11-12 11:16:59 +00:00
|
|
|
|
2015-03-16 11:37:24 +00:00
|
|
|
my ($name) = @_;
|
2015-11-26 09:31:36 +00:00
|
|
|
my $res;
|
|
|
|
|
|
|
|
foreach my $lang (@langKeys) {
|
|
|
|
|
|
|
|
if(hasExample($name,$lang) == 1 ) {
|
|
|
|
|
|
|
|
my $f=examplePath($name,$lang);
|
|
|
|
|
|
|
|
my $found=0;
|
|
|
|
open (IN,"<$f") or return "";
|
|
|
|
while (defined (my $line = <IN>)) {
|
|
|
|
my $comment=$langComment{$lang};
|
|
|
|
|
|
|
|
if($comment ne "*") {
|
|
|
|
$line =~ s/\Q${comment}/\*/g;
|
2015-11-12 11:16:59 +00:00
|
|
|
}
|
2015-11-26 09:31:36 +00:00
|
|
|
|
|
|
|
#First row
|
|
|
|
if($found==0) {
|
|
|
|
if($line =~ /\*\s*Description:/) {
|
|
|
|
$found=1;
|
|
|
|
my ($r)=($line =~ /\*\s*Description:\s*(.+)/);
|
|
|
|
if(length($r) > 2) {
|
|
|
|
$res=$r;
|
|
|
|
}
|
|
|
|
}
|
2015-03-16 11:37:24 +00:00
|
|
|
} else {
|
2015-11-26 09:31:36 +00:00
|
|
|
my ($r)=($line =~ /\*\s+(.+)/);
|
|
|
|
if(length($r) > 2) {
|
|
|
|
$res=$res." ".$r;
|
|
|
|
} else {
|
|
|
|
last;
|
|
|
|
}
|
2015-11-12 11:16:59 +00:00
|
|
|
}
|
2015-03-16 11:37:24 +00:00
|
|
|
}
|
2015-11-12 11:16:59 +00:00
|
|
|
|
2015-11-26 09:31:36 +00:00
|
|
|
if($res) {
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-03-16 11:37:24 +00:00
|
|
|
#print $f."\n";
|
2015-11-12 11:16:59 +00:00
|
|
|
#print "descr: ".$res."\n";
|
2015-03-16 11:37:24 +00:00
|
|
|
}
|
2015-11-12 11:16:59 +00:00
|
|
|
|
|
|
|
return $res;
|
|
|
|
}
|
2015-03-16 11:37:24 +00:00
|
|
|
|
|
|
|
#====================================================
|
|
|
|
#
|
2015-11-12 11:16:59 +00:00
|
|
|
# Generic functions related to confluence
|
2015-03-16 11:37:24 +00:00
|
|
|
#
|
|
|
|
#====================================================
|
|
|
|
|
|
|
|
#--------------------------------
|
|
|
|
# Upload a page to confluence
|
|
|
|
#--------------------------------
|
|
|
|
|
|
|
|
sub loadToConf {
|
2015-11-12 11:16:59 +00:00
|
|
|
|
2015-03-16 11:37:24 +00:00
|
|
|
my ($fHtml,$title,$parentPage) = @_;
|
2015-11-12 09:42:46 +00:00
|
|
|
system("$CONF -a addPage --noConvert --replace --space \"$confSpace\" --title \"$title\" --parent \"$parentPage\" --file $fHtml");
|
2015-11-12 11:16:59 +00:00
|
|
|
}
|
2015-03-16 11:37:24 +00:00
|
|
|
|
|
|
|
#---------------------------------------
|
|
|
|
# Create link to a page
|
|
|
|
#---------------------------------------
|
|
|
|
|
|
|
|
sub linkToPage {
|
2015-11-12 11:16:59 +00:00
|
|
|
|
2015-03-16 11:37:24 +00:00
|
|
|
my ($page,$label) = @_;
|
2015-11-12 11:16:59 +00:00
|
|
|
|
2015-03-16 11:37:24 +00:00
|
|
|
my $str="<ac:link>
|
|
|
|
<ri:page ri:content-title=\"".$page."\"/>
|
|
|
|
<ac:plain-text-link-body>
|
|
|
|
<![CDATA[".$label."]]>
|
|
|
|
</ac:plain-text-link-body>
|
|
|
|
</ac:link>";
|
|
|
|
|
|
|
|
return $str;
|
2015-11-12 11:16:59 +00:00
|
|
|
}
|
2015-03-16 11:37:24 +00:00
|
|
|
|
|
|
|
sub makeInfo {
|
2015-11-12 11:16:59 +00:00
|
|
|
|
2015-03-16 11:37:24 +00:00
|
|
|
my ($text) = @_;
|
|
|
|
return "<ac:structured-macro ac:name=\"info\"><ac:parameter ac:name=\"icon\">false</ac:parameter>".
|
|
|
|
"<ac:rich-text-body>".$text."</ac:rich-text-body></ac:structured-macro>";
|
2015-11-12 11:16:59 +00:00
|
|
|
}
|