Logo coherent WaveBurst  
Library Reference Guide
Logo
cwb_mkhtml_file.C
Go to the documentation of this file.
1 /*
2 # Copyright (C) 2019 Gabriele Vedovato
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17 
18 
19 // convert file into html
20 #include <stdio.h>
21 #include <unistd.h>
22 #include <iostream>
23 #include <string>
24 #include <algorithm>
25 #include <vector>
26 
27 void cwb_mkhtml_file(TString file_name, TString options="") {
28 
29  // get title
30  TString option_title = CWB::Toolbox::getParameter(options,"--title");
31  option_title.ReplaceAll("#"," ");
32 
33  // get subtitle
34  TString option_subtitle = CWB::Toolbox::getParameter(options,"--subtitle");
35  option_subtitle.ReplaceAll("#"," ");
36 
37  // get multi, if multi=='true' the plots are presented with 2 columns format
38  TString option_multi = CWB::Toolbox::getParameter(options,"--multi");
39  option_multi.ToUpper();
40 
41  if(file_name=="") {
42  cout << "cwb_mkhtml_file.C - Error : empty file name !!!" << endl;exit(1);
43  }
44  if(file_name.Contains("..")) {
45  cout << "cwb_mkhtml_file.C - Error : file_name must not contains '..' !!!" << endl;exit(1);
46  }
47  if(file_name==".") file_name=gSystem->WorkingDirectory();
48  // check if file_name is a directory
49  Long_t id,size=0,flags,mt;
50  int estat = gSystem->GetPathInfo(file_name.Data(),&id,&size,&flags,&mt);
51  // create an html file with the list of png plots contained in the file_name dir
52  if(flags&2 || file_name==".") { // is a directory
53  TString odir = file_name;
54  // get the list of png files
55  vector<TString> pngList = CWB::Toolbox::getFileListFromDir(odir, ".png");
56  if(!pngList.size()) {
57  cout << "cwb_mkhtml_file.C - Error : no *.png files found !!!" << endl;exit(1);
58  }
59  // sort list
60  pngList = CWB::Toolbox::sortStrings(pngList);
61 
62  TString texiName=odir+"/png_html_index.texi";
63  bool overwrite=CWB::Toolbox::checkFile(texiName,true);
64  if(!overwrite) gSystem->Exit(0);
65  ofstream out;
66  out.open(texiName,ios::out);
67  out << "@c include texi macros" << endl;
68  out << "@include macros.texi" << endl;
69  out << "@include mathjax.texi" << endl << endl;
70  out << "@*" << endl;
71  if(option_title!="") {
72  out << "@center @txtfont{"<<"<br>"<<option_title<<"<br>"<<", blue, h1}" << endl;
73  if(option_subtitle!="") out << "@center @txtcolor{"<<option_subtitle.Data()<<",black}" << endl;
74  out << "@*" << endl;
75  out << "@drawline" << endl;
76  }
77  out << "@*" << endl;
78  if(option_multi=="TRUE") {
79  for(int i=0;i<pngList.size();i++) {
80  TString name=pngList[i];name.ReplaceAll(".png","");
81  name=gSystem->BaseName(name);
82  if(i%2==0) {
83  out << "@multitable @columnfractions .5 .5" << endl;
84  out << "@item @center @txtfont{"<<i+1<<", red, h2}" << endl;
85  out << "@center @displayimage{../.,"<<name<<",470}"<<endl;
86  } else {
87  out << "@tab @center @txtfont{"<<i+1<<", red, h2}" << endl;
88  out << "@center @displayimage{../.,"<<name<<",470}"<<endl;
89  out << "@end multitable" << endl;
90  }
91  }
92  if(pngList.size()%2==1) out << "@end multitable" << endl;
93  } else {
94  for(int i=0;i<pngList.size();i++) {
95  TString name=pngList[i];name.ReplaceAll(".png","");
96  name=gSystem->BaseName(name);
97  out << "@center @txtfont{"<<i+1<<", red, h2}" << endl;
98  out << "@center @displayimage{../.,"<<name<<",1000}"<<endl;
99  }
100  }
101  out.close();
102  // convert texi into html
103  TString cwb_scripts = TString(gSystem->Getenv("CWB_SCRIPTS"));
104  TString exec_cmd = TString::Format("%s/cwb_mkhtml.csh %s wheader;rm %s",
105  cwb_scripts.Data(),texiName.Data(),texiName.Data());
106  int ret=gSystem->Exec(exec_cmd);
107  if(ret) {
108  cout << "cwb_mkhtml_file.C : Error while executing cwb_mkhtml png_html_index.texi !!!" << endl;
109  exit(1);
110  }
111 
112  // Change default html title: CWB Display -> CWB Report
113  TString indexName=texiName;
114  indexName.ReplaceAll(".texi","/index.html");
115  char cmd[1024];
116  sprintf(cmd,"sed -i 's/<title>CWB Display/<title>CWB Report/g' %s",indexName.Data());
117  gSystem->Exec(cmd);
118 
119  gSystem->Exit(0);
120  }
121 
122  if(!file_name.EndsWith(".C")) {
123  } else if(!file_name.EndsWith(".cc")) {
124  } else if(!file_name.EndsWith(".hh")) {
125  } else if(!file_name.EndsWith(".c")) {
126  } else if(!file_name.EndsWith(".h")) {
127  } else {
128  cout << "cwb_mkhtml_file.C - Error : file name must ends with .C/.c/.h/.cc/.hh !!!" << endl;exit(1);
129  }
130  // if file_name is not an absolute path add working dir path
131  if(!file_name.BeginsWith("/")) {
132  TString tmp = file_name;
133  file_name = TString(gSystem->WorkingDirectory())+"/"+tmp;
134  }
135 
136  // create output html dir
137  TString html_dir = file_name;
138  html_dir.ReplaceAll(".C","");
139  html_dir.ReplaceAll(".cc","");
140  html_dir.ReplaceAll(".hh","");
141  html_dir.ReplaceAll(".c","");
142  html_dir.ReplaceAll(".h","");
143  CWB::Toolbox::mkDir(html_dir,true);
144 
145  THtml html;
146  html.SetEtcDir(gSystem->ExpandPathName("$HOME_WAT/html/etc/html"));
147  html.SetProductName("CWB");
148  TString html_input_dir=html_dir;
149  html.SetInputDir(html_input_dir.Data());
150 
151  char cmd[1024];
152  sprintf(cmd,"cp %s/html/etc/html/ROOT.css %s/",gSystem->ExpandPathName("$HOME_WAT"),html_dir.Data());
153  gSystem->Exec(cmd);
154  sprintf(cmd,"cp %s/html/etc/html/ROOT.js %s/",gSystem->ExpandPathName("$HOME_WAT"),html_dir.Data());
155  gSystem->Exec(cmd);
156 
157  // redirect stderr to /dev/null to getrid of messages produced by html.Convert
158  fpos_t poserr; fflush(stderr); fgetpos(stderr, &poserr);
159  int fderr = dup(fileno(stderr)); freopen("/dev/null", "w", stderr);
160  // redirect stdout to /dev/null to getrid of messages produced by html.Convert
161  fpos_t posout; fflush(stdout); fgetpos(stdout, &posout);
162  int fdout = dup(fileno(stdout)); freopen("/dev/null", "w", stdout);
163 
164  // convert file to html
165  char title[1024];
166  sprintf(title,"<h2 class=\"convert\" align=\"center\"> %s </h2>",file_name.Data());
167  html.Convert(file_name.Data(),"",html_dir,"", 0, title);
168 
169  // restore the stderr output
170  fflush(stderr); dup2(fderr, fileno(stderr)); close(fderr);
171  clearerr(stderr); fsetpos(stderr, &poserr);
172  // restore the stdout output
173  fflush(stdout); dup2(fdout, fileno(stdout)); close(fdout);
174  clearerr(stdout); fsetpos(stdout, &posout);
175 
176  TString html_file = html_dir+"/"+gSystem->BaseName(file_name)+".html";
177  cout << endl << "created html file : " << html_file << endl << endl;
178 
179  gSystem->Exit(0);
180 }
TString cwb_scripts
static vector< TString > getFileListFromDir(TString dir_name, TString endString="", TString beginString="", TString containString="", bool fast=false)
Definition: Toolbox.cc:5108
par [0] name
TString("c")
ofstream out
Definition: cwb_merge.C:214
char odir[1024]
Long_t flags
Long_t size
i drho i
static bool checkFile(TString fName, bool question=false, TString message="")
Definition: Toolbox.cc:4670
double * tmp
Definition: testWDM_5.C:31
static vector< TString > sortStrings(vector< TString > ilist)
Definition: Toolbox.cc:6678
char options[256]
char title[256]
Definition: SSeriesExample.C:1
static TString getParameter(TString options, TString param="")
Definition: Toolbox.cc:6727
static void mkDir(TString dir, bool question=false, bool remove=true)
Definition: Toolbox.cc:4714
void cwb_mkhtml_file(TString file_name, TString options="")
char cmd[1024]
int estat
sprintf(tfres,"(1/%g)x(%g) (sec)x(Hz)", 2 *df, df)
Long_t mt
bool overwrite
Definition: cwb_dump_inj.C:100
Long_t id
in close()
exit(0)