Help generation: add the ability to generate an image from inlined Scilab code
[scilab.git] / scilab / modules / helptools / src / java / org / scilab / modules / helptools / SciDocMain.java
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2010 - Calixte DENIZET
4  *
5  * This file must be used under the terms of the CeCILL.
6  * This source file is licensed as described in the file COPYING, which
7  * you should have received as part of this distribution.  The terms
8  * are also available at
9  * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
10  *
11  */
12
13 package org.scilab.modules.helptools;
14
15 import java.io.File;
16
17 import org.scilab.modules.commons.ScilabCommonsUtils;
18 import org.scilab.modules.commons.ScilabConstants;
19 import org.scilab.modules.gui.utils.ScilabSwingUtilities;
20 import org.scilab.modules.helptools.external.HTMLMathMLHandler;
21 import org.scilab.modules.helptools.external.HTMLScilabHandler;
22 import org.scilab.modules.helptools.external.HTMLSVGHandler;
23 import org.scilab.modules.helptools.image.ScilabImageConverter;
24
25 /**
26  * Class to convert DocBook to HTML
27  * @author Calixte DENIZET
28  */
29 public final class SciDocMain {
30
31     private static final String SCI = ScilabConstants.SCI.getPath();
32
33     private String outputDirectory;
34     private String language;
35     private String format;
36     private String template;
37     private String version;
38     private String imagedir;
39     private String[] sciprim;
40     private String[] scimacro;
41     private boolean isToolbox;
42
43     /**
44      * Set the directory where files must be exported
45      * Note that directory is created
46      *
47      * @param outputDirectoryectory The path to the directory
48      * @return If the directory exists
49      */
50     public boolean setOutputDirectory(String outputDirectory) {
51         File directory = new File(outputDirectory);
52         if (!directory.isDirectory()) {
53             if (!directory.mkdirs()) {
54                 return false;
55             }
56         }
57         this.outputDirectory = outputDirectory;
58         return true;
59     }
60
61     /**
62      * Defines the language
63      *
64      * @param language the language (xx_XX ex: en_US, fr_FR)
65      */
66     public void setWorkingLanguage(String language) {
67         this.language = language;
68     }
69
70     /**
71      * @param isToolbox must be true if we compile a toolbox doc
72      */
73     public void setIsToolbox(boolean isToolbox) {
74         this.isToolbox = isToolbox;
75     }
76
77     /**
78      * Defines the export format
79      * @param format the format (among the list CHM, HTML, PDF, JH, PS)
80      */
81     public void setExportFormat(String format) {
82         this.format = format;
83     }
84
85     /* Stylesheet is useless and just kept to keep the consistency with
86      * builddoc V1 */
87     public String process(String sourceDoc, String styleSheet)  {
88         SciDocConfiguration conf = new SciDocConfiguration();
89         template = conf.getTemplate(format.toLowerCase());
90         /* TODO: make this file generated at build time of Scilab */
91         sciprim = conf.getBuiltins();
92         scimacro = conf.getMacros();
93         version = conf.getVersion();
94         imagedir = ".";//the path must be relative to outputDirectory
95         String fileToExec = null;
96
97         if (!new File(sourceDoc).isFile()) {
98             System.err.println("Could not find master document: " + sourceDoc);
99             return null;
100         }
101
102         if (!new File(template).isFile()) {
103             System.err.println("Could not find template document: " + template);
104             return null;
105         }
106
107         try {
108             DocbookTagConverter converter = null;
109             String urlBase = null;
110
111             if (format.equalsIgnoreCase("javahelp")) {
112                 converter = new JavaHelpDocbookTagConverter(sourceDoc, outputDirectory, sciprim, scimacro, template, version, imagedir, isToolbox, "scilab://");
113             } else {
114                 if (isToolbox) {
115                     urlBase = conf.getWebSiteURL() + language + "/";
116                 }
117                 if (format.equalsIgnoreCase("html") || format.equalsIgnoreCase("web")) {
118                     converter = new HTMLDocbookTagConverter(sourceDoc, outputDirectory, sciprim, scimacro, template, version, imagedir, isToolbox, urlBase);
119                 } else if (format.equalsIgnoreCase("chm")) {
120                     converter = new CHMDocbookTagConverter(sourceDoc, outputDirectory, sciprim, scimacro, template, version, imagedir, conf.getWebSiteURL(), isToolbox, urlBase, language);
121                 }
122             }
123
124             converter.registerExternalXMLHandler(HTMLMathMLHandler.getInstance(outputDirectory, imagedir));
125             converter.registerExternalXMLHandler(HTMLSVGHandler.getInstance(outputDirectory, imagedir));
126             converter.registerExternalXMLHandler(HTMLScilabHandler.getInstance(outputDirectory, imagedir));
127             converter.convert();
128
129             fileToExec = ScilabImageConverter.getInstance().getFileWithScilabCode();
130
131             ScilabCommonsUtils.copyFile(new File(SCI + "/modules/helptools/data/css/scilab_code.css"), new File(outputDirectory + "/scilab_code.css"));
132             ScilabCommonsUtils.copyFile(new File(SCI + "/modules/helptools/data/css/xml_code.css"), new File(outputDirectory + "/xml_code.css"));
133             ScilabCommonsUtils.copyFile(new File(SCI + "/modules/helptools/data/css/c_code.css"), new File(outputDirectory + "/c_code.css"));
134             ScilabCommonsUtils.copyFile(new File(SCI + "/modules/helptools/data/css/style.css"), new File(outputDirectory + "/style.css"));
135             ScilabCommonsUtils.copyFile(new File(ScilabSwingUtilities.findIcon("media-playback-start")), new File(outputDirectory + "/ScilabExecute.png"));
136             ScilabCommonsUtils.copyFile(new File(ScilabSwingUtilities.findIcon("accessories-text-editor")), new File(outputDirectory + "/ScilabEdit.png"));
137             ScilabCommonsUtils.copyFile(new File(ScilabSwingUtilities.findIcon("dialog-information")), new File(outputDirectory + "/ScilabNote.png"));
138             ScilabCommonsUtils.copyFile(new File(ScilabSwingUtilities.findIcon("dialog-warning")), new File(outputDirectory + "/ScilabWarning.png"));
139             ScilabCommonsUtils.copyFile(new File(ScilabSwingUtilities.findIcon("dialog-warning")), new File(outputDirectory + "/ScilabCaution.png"));
140             ScilabCommonsUtils.copyFile(new File(ScilabSwingUtilities.findIcon("dialog-information")), new File(outputDirectory + "/ScilabTip.png"));
141             ScilabCommonsUtils.copyFile(new File(ScilabSwingUtilities.findIcon("emblem-important")), new File(outputDirectory + "/ScilabImportant.png"));
142             if (format.equalsIgnoreCase("javahelp")) {
143                 if (!isToolbox) {
144                     ScilabCommonsUtils.copyFile(new File(SCI + "/modules/helptools/data/pages/error.html"), new File(outputDirectory + "/ScilabErrorPage.html"));
145                     File homepage = new File(SCI + "/modules/helptools/data/pages/homepage-" + language + ".html");
146                     if (!homepage.isFile()) {
147                         /* could not find the localized homepage. Switch to english */
148                         homepage = new File(SCI + "/modules/helptools/data/pages/homepage-en_US.html");
149                     }
150                     ScilabCommonsUtils.copyFile(homepage, new File(outputDirectory + "/ScilabHomePage.html"));
151
152                     File homepageImage = new File(SCI + "/modules/helptools/data/pages/ban-" + language + ".png");
153                     if (!homepageImage.isFile()) {
154                         homepageImage = new File(SCI + "/modules/helptools/data/pages/ban-en_US.png");
155                     }
156                     ScilabCommonsUtils.copyFile(homepageImage, new File(outputDirectory + "/ban_en_US.png"));
157                 }
158                 if (fileToExec == null) {
159                     generateJavahelp(outputDirectory, language);
160                 }
161             }
162
163         } catch (Exception e) {
164             System.err.println("An error occured during the conversion:\n");
165             e.printStackTrace();
166         }
167
168         return fileToExec;
169     }
170
171     public static void generateJavahelp(String outputDirectory, String language) {
172         BuildJavaHelp.buildJavaHelp(outputDirectory, language);
173     }
174 }