Help generation: add the ability to generate an image from inlined Scilab code
[scilab.git] / scilab / modules / helptools / sci_gateway / cpp / sci_buildDocv2.cpp
1 /*
2  *  Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  *  Copyright (C) 2008 - INRIA - Sylvestre LEDRU
4  *  Copyright (C) 2011 - Scilab Enterprises - Sylvestre LEDRU
5  *
6  *  This file must be used under the terms of the CeCILL.
7  *  This source file is licensed as described in the file COPYING, which
8  *  you should have received as part of this distribution.  The terms
9  *  are also available at
10  *  http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
11  *
12  */
13 /*--------------------------------------------------------------------------*/
14 #include "SciDocMain.hxx"
15 #include "GiwsException.hxx"
16 #include "BuildDocObject.hxx"
17
18 extern "C"
19 {
20     /*--------------------------------------------------------------------------*/
21 #include <string.h>
22 #include "api_scilab.h"
23 #include "gw_helptools.h"
24 #include "Scierror.h"
25 #include "setgetSCIpath.h"
26 #include "localization.h"
27 #include "setgetlanguage.h"
28 #include "getScilabJavaVM.h"
29 #include "MALLOC.h"
30 #ifdef _MSC_VER
31 #include "ConvertSlash.h"
32 #endif
33     /*--------------------------------------------------------------------------*/
34 #define PATHTOCSS "/modules/helptools/css/javahelp.css"
35 #define PATHTOBUILDDOC "/modules/helptools/build/doc/scilab_%s_help/"
36 #define PATHTOMASTERXML "/modules/helptools/master_%s_help.xml"
37 #define DEFAULTEXPORT "JH"
38 #ifdef _MSC_VER
39     static void __slashToAntislash(std::string * in)
40     {
41         size_t found = in->rfind("/");
42
43         while (found != std::string::npos)
44         {
45             in->replace(found, 1, "\\");
46             found = in->rfind("/");
47         }
48     }
49 #endif
50     /*--------------------------------------------------------------------------*/
51     int sci_buildDocv2(char *fname, unsigned long l)
52     {
53         std::string exportFormat;
54         std::string SciPath = getSCIpath(); /* Scilab path */
55         std::string masterXML;  /* Which file contains all the doc stuff */
56         std::string masterXMLTMP;
57         std::string outputDirectory;    /* Working directory */
58         std::string outputDirectoryTMP;
59         std::string language;
60         std::string styleSheet; /* the CSS */
61         char * fileToExec = NULL;
62         SciErr sciErr;
63         int *piAddr = NULL;
64         int iRet = 0;
65
66         CheckRhs(0, 4);
67         CheckLhs(1, 1);
68
69         styleSheet = SciPath + PATHTOCSS;
70
71         if (Rhs < 1)
72         {
73             exportFormat = DEFAULTEXPORT;
74         }
75         else
76         {
77             char *pstData = NULL;
78
79             sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddr);
80             if (sciErr.iErr)
81             {
82                 printError(&sciErr, 0);
83                 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
84                 return 0;
85             }
86
87             if (!isStringType(pvApiCtx, piAddr))
88             {
89                 Scierror(999, _("%s: Wrong type for input argument #%d: Single string expected.\n"), fname, 1);
90                 return 0;
91                 // Wrong type string
92             }
93
94             iRet = getAllocatedSingleString(pvApiCtx, piAddr, &pstData);
95             if (iRet)
96             {
97                 freeAllocatedSingleString(pstData);
98                 return iRet;
99             }
100             exportFormat = std::string(pstData);
101             freeAllocatedSingleString(pstData);
102
103         }
104
105         if (Rhs < 3)            /* Language not provided */
106         {
107             language = getlanguage();
108         }
109         else
110         {
111             char *pstData = NULL;
112
113             sciErr = getVarAddressFromPosition(pvApiCtx, 3, &piAddr);
114             if (sciErr.iErr)
115             {
116                 printError(&sciErr, 0);
117                 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 3);
118                 return 0;
119             }
120
121             if (!isStringType(pvApiCtx, piAddr))
122             {
123                 Scierror(999, _("%s: Wrong type for input argument #%d: Single string expected.\n"), fname, 3);
124                 return 0;
125                 // Wrong type string
126             }
127
128             if (!isScalar(pvApiCtx, piAddr))
129             {
130                 language = getlanguage();
131             }
132             else
133             {
134                 iRet = getAllocatedSingleString(pvApiCtx, piAddr, &pstData);
135                 if (iRet)
136                 {
137                     freeAllocatedSingleString(pstData);
138                     return iRet;
139                 }
140                 language = std::string(pstData);
141                 freeAllocatedSingleString(pstData);
142             }
143
144         }
145
146         if (Rhs < 2)
147         {
148             /* Update the path with the localization */
149             masterXMLTMP = std::string("/modules/helptools/master_") + language + std::string("_help.xml");
150             masterXML = SciPath + masterXMLTMP;
151         }
152         else
153         {
154             char *pstData = NULL;
155
156             sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddr);
157             if (sciErr.iErr)
158             {
159                 printError(&sciErr, 0);
160                 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2);
161                 return 0;
162             }
163             if (!isStringType(pvApiCtx, piAddr))
164             {
165                 Scierror(999, _("%s: Wrong type for input argument #%d: Single string expected.\n"), fname, 2);
166                 return 0;
167                 // Wrong type string
168             }
169
170             iRet = getAllocatedSingleString(pvApiCtx, piAddr, &pstData);
171             if (iRet)
172             {
173                 freeAllocatedSingleString(pstData);
174                 return iRet;
175             }
176             masterXML = std::string(pstData);
177             freeAllocatedSingleString(pstData);
178         }
179
180         if (Rhs == 4)
181         {
182             char *pstData = NULL;
183
184             sciErr = getVarAddressFromPosition(pvApiCtx, 4, &piAddr);
185             if (sciErr.iErr)
186             {
187                 printError(&sciErr, 0);
188                 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 4);
189                 return 0;
190             }
191             if (!isStringType(pvApiCtx, piAddr))
192             {
193                 Scierror(999, _("%s: Wrong type for input argument #%d: Single string expected.\n"), fname, 4);
194                 return 0;
195                 // Wrong type string
196             }
197
198             iRet = getAllocatedSingleString(pvApiCtx, piAddr, &pstData);
199             if (iRet)
200             {
201                 freeAllocatedSingleString(pstData);
202                 return iRet;
203             }
204             outputDirectory = std::string(pstData) + std::string("/scilab_") + language + std::string("_help/");
205             freeAllocatedSingleString(pstData);
206
207         }
208         else                    /* Scilab help */
209         {
210             /* Update the path with the localization */
211             if (exportFormat != "jar-only")
212             {
213                 outputDirectoryTMP =
214                     std::string("/modules/helptools/") + std::string(exportFormat) + std::string("/scilab_") + language + std::string("_help/");
215             }
216             else
217             {
218                 outputDirectoryTMP =
219                     std::string("/modules/helptools/") + std::string("javaHelp") + std::string("/scilab_") + language + std::string("_help/");
220             }
221
222             outputDirectory = SciPath + outputDirectoryTMP;
223         }
224
225 #ifdef _MSC_VER
226         __slashToAntislash(&outputDirectory);
227         __slashToAntislash(&styleSheet);
228         __slashToAntislash(&masterXML);
229 #endif
230
231         try
232         {
233             if (exportFormat == "javaHelp" || exportFormat == "html" || exportFormat == "chm" || exportFormat == "web")
234             {
235                 org_scilab_modules_helptools::SciDocMain * doc = new org_scilab_modules_helptools::SciDocMain(getScilabJavaVM());
236
237                 if (doc->setOutputDirectory((char *)outputDirectory.c_str()))
238                 {
239                     doc->setWorkingLanguage((char *)language.c_str());
240                     doc->setExportFormat((char *)exportFormat.c_str());
241                     doc->setIsToolbox(Rhs == 4);
242                     fileToExec = doc->process((char *)masterXML.c_str(), (char *)styleSheet.c_str());
243                 }
244                 else
245                 {
246                     Scierror(999, _("%s: Could find or create the working directory %s.\n"), fname, outputDirectory.c_str());
247                     return FALSE;
248                 }
249                 if (doc != NULL)
250                 {
251                     delete doc;
252                 }
253
254             }
255             else if (exportFormat == "jar-only")
256             {
257                 org_scilab_modules_helptools::SciDocMain::generateJavahelp(getScilabJavaVM(), (char *)outputDirectory.c_str(), (char *)language.c_str());
258             }
259             else
260             {
261                 org_scilab_modules_helptools::BuildDocObject * doc = new org_scilab_modules_helptools::BuildDocObject(getScilabJavaVM());
262
263                 if (doc->setOutputDirectory((char *)outputDirectory.c_str()))
264                 {
265                     doc->setWorkingLanguage((char *)language.c_str());
266                     doc->setExportFormat((char *)exportFormat.c_str());
267                     doc->process((char *)masterXML.c_str(), (char *)styleSheet.c_str());
268                 }
269                 else
270                 {
271                     Scierror(999, _("%s: Could find or create the working directory %s.\n"), fname, outputDirectory.c_str());
272                     return FALSE;
273                 }
274                 if (doc != NULL)
275                 {
276                     delete doc;
277                 }
278
279             }
280         }
281         catch (GiwsException::JniException ex)
282         {
283             Scierror(999, _("%s: Error while building documentation: %s.\n"), fname, ex.getJavaDescription().c_str());
284             Scierror(999, _("%s: Execution Java stack: %s.\n"), fname, ex.getJavaStackTrace().c_str());
285             Scierror(999,
286                      _
287                      ("If Scilab is started in a chroot, you might want to try to set the two environment variables: SCI_DISABLE_TK=1 SCI_JAVA_ENABLE_HEADLESS=1\n"));
288             return FALSE;
289         }
290
291         if (fileToExec)
292         {
293             createMatrixOfString(pvApiCtx, Rhs + 1, 1, 1, &fileToExec);
294             delete [] fileToExec;
295         }
296         else
297         {
298             createEmptyMatrix(pvApiCtx, Rhs + 1);
299         }
300
301         LhsVar(1) = Rhs + 1;
302         PutLhsVar();
303
304         return 0;
305     }
306     /*--------------------------------------------------------------------------*/
307 }