PhoenixLecture  2.0.0
Set of tools to make lectures
platexobj_formulae.cpp
Go to the documentation of this file.
1 /***************************************
2  Auteur : Pierre Aubert
3  Mail : pierre.aubert@lapp.in2p3.fr
4  Licence : CeCILL-C
5 ****************************************/
6 
7 #include <sys/stat.h>
8 #include <sys/types.h>
9 #include <fstream>
10 
11 #include "data_all.h"
12 #include "convertToString.h"
13 #include "platexobj_menu.h"
14 
15 #include "platexobj_formulae.h"
16 
18 
22  if(!data_load(fileName, mapFormula)){return;}
23 }
24 
26 
28 void platexobj_saveFormulaeMap(const PMapFormula & mapFormula){
30  if(!data_save(fileName, mapFormula)){return;}
31 }
32 
33 
35 
41 bool platexobj_htmlStrLatexFile(const PString & fileName, const PString & latexEnv, const PString & mathContent, const PString & fontColor){
42  std::ofstream fs;
43  fs.open(fileName.c_str());
44  if(!fs.is_open()) return false;
45  if(latexEnv == "$"){
46  fs << "\\documentclass[8pt]{article}\n";
47  }else{
48  fs << "\\documentclass[12pt]{article}\n";
49  }
50  fs << "\\usepackage[T1]{fontenc}\n\\usepackage[utf8]{inputenc}\n\\usepackage{lmodern}\n\n\n";
51  fs << "\\usepackage{calc}\n\n\n\\usepackage{amssymb}\n\\usepackage{color}\n\\usepackage{amsfonts}\n\\usepackage{bbm}\n\\pagestyle{empty}\n\n\n";
52  fs << "\\usepackage{amsmath}\n\\usepackage{esint}\n\n";
53 // saveListLatexFunction(fs, ptListLatexFunc);
54 // saveListLatexEnv(fs, ptListLatexEnv);
55  fs << "\\begin{document}\n\n";
56  fs << "\\def\\sgn{\\mathop{\\mathgroup\\symoperators sgn}\\nolimits}\n";
57  fs << fontColor << " ";
58  if(latexEnv == "$"){
59  fs << "$" << mathContent << "$\n";
60  }else{
61  fs << "\\begin{"<<latexEnv<<"}" << mathContent << "\\end{"<<latexEnv<<"}\n";
62  }
63  fs << "\\end{document}\n";
64  fs.close();
65  return true;
66 }
67 
69 
72 bool lauchLatexToPng(const PString & fileNamePng){
73  PString command(CMAKE_INSTALL_PREFIX "/share/PhoenixTex2Html/SCRIPTS/createTexDisplayMathpng.sh "+fileNamePng);
74 
75  if(system(command.c_str()) != 0){
76  std::cerr << "lauchLatexToPng : can't call 'latex' to create file" << std::endl;
77  return false;
78  }
79  return true;
80 }
81 
83 
89 PString platexObj_createImageWithMathTex(size_t objId, const PString & latexEnv, const PString & texContent, const PString & fontColor){
90  PString objIdStr(valueToString(objId));
91  PString fileName("_tmp_file.tex");
92  if(!platexobj_htmlStrLatexFile(fileName, latexEnv, texContent, fontColor)){
93  std::cerr << "platexObj_createImageWithMathTex : can't save latex file '"<<fileName<<"' for object " << objId << std::endl;
94  return "";
95  }
96  PPath outputDir(LATEX_IMAGE_OUTPUT_DIR);
97  if(!outputDir.createDirectory()){
98  std::cerr << "platexObj_createImageWithMathTex : can't create directory '" << outputDir << "'" << std::endl;
99  return "";
100  }
101  PString fileNamePng(outputDir+"/imgMath"+objIdStr+".png");
102  if(!lauchLatexToPng(fileNamePng)){
103  std::cerr << "platexObj_createImageWithMathTex : can't save png file '"<<fileNamePng<<"' for object " << objId << std::endl;
104  return "";
105  }
106  remove(fileName.c_str());
107  return fileNamePng;
108 }
109 
111 
117 PString platexObj_createFormulaTex(POutoutMode & outputMode, size_t objId, const PString & latexEnv, const PString & texContent){
118  PMapFormula & mapFormula = outputMode.mapFormula;
119  //Let's check if we already know the content
120  PMapFormula::iterator it(mapFormula.find(texContent));
121  if(it != mapFormula.end()){ //We already know this formula
122  //Now we have to check if the file exists
123  if(PPath(it->second).isFileExist()){
124  return it->second;
125  }
126  }
127  PString fontColor("");
128  if(outputMode.currentStyle == "dark"){
129  fontColor = "\\pagecolor{black}\\color{white}";
130  }
131  //We do not know this formula, or the file of this formula does not exist
132  PString outputFileName(platexObj_createImageWithMathTex(objId, latexEnv, texContent, fontColor));
133  mapFormula[texContent] = outputFileName;
134  return outputFileName;
135 }
136 
137 
139 
144 PString platexobj_htmlStrLatex(POutoutMode & outputMode, const PLatexObj & obj, const PString & latexEnv){
145  PString body("");
146  if(outputMode.isMathJaxMode){
147  body += "$$\\begin{"+latexEnv+"}"+obj.getText()+"\\end{"+latexEnv+"}$$\n";
148  }else{
149  PString fileNamePng(platexObj_createFormulaTex(outputMode, obj.getId(), latexEnv, obj.getText()));
150  body += "<div class=\"formula\" /><img src=\"" + fileNamePng + "\" alt=\"nothing\" /></div>";
151  }
152  return body;
153 }
154 
156 
161  PString body("");
162  if(outputMode.isMathJaxMode){
163  body += "$"+obj.getText()+"$\n";
164  }else{
165  PString fileNamePng(platexObj_createFormulaTex(outputMode, obj.getId(), "$", obj.getText()));
166  body += "<span class=\"inlineformula\" /><img src=\"" + fileNamePng + "\" alt=\""+obj.getText()+"\" /></span>";
167  }
168  return body;
169 }
170 
172 
175 void platexobj_update_theme(POutoutMode & outputMode, const PPath & themeBaseFile){
176  dico_create_nested_call(outputMode.themeCall, themeBaseFile.loadFileContent(), "${", "}");
177 }
178 
180 
184 void platexobj_theme_set_var(POutoutMode & outputMode, const PString & varName, const PString & value){
185  outputMode.mapVar["${" + varName + "}"] = value;
186  if(value == ""){
187  outputMode.mapVar["${IS_" + varName + "_BEGIN}"] = "<!-- ";
188  outputMode.mapVar["${IS_" + varName + "_END}"] = " -->";
189  }else{
190  outputMode.mapVar["${IS_" + varName + "_BEGIN}"] = "";
191  outputMode.mapVar["${IS_" + varName + "_END}"] = "";
192  }
193 }
194 
196 
199  outputMode.mapVar["${STYLE}"] = outputMode.currentStyle;
200  if(outputMode.isBookTheme){
201  platexobj_theme_set_var(outputMode, "AUTHOR_EMAIL", outputMode.bookMail);
202  platexobj_theme_set_var(outputMode, "GITLAB_URL", outputMode.bookGitlabUrl);
203  platexobj_theme_set_var(outputMode, "GITLAB_MASTER_URL", outputMode.bookMasterProjectUrl);
204  outputMode.mapVar["${BOOK_TABLE_OF_CONTENT}"] = "";
205  }
206  PString bibliography("");
207  if(outputMode.isBibliography){
208  bibliography = "bibliography.html";
209  }
210  platexobj_theme_set_var(outputMode, "BIBLIOGRAPHY", bibliography);
211  PString remoteMathjaxFile(""), localMathjax("");
212  if(outputMode.isRemoteMathjax){
213  remoteMathjaxFile = "load-mathjax.js";
214  }else{
215  if(outputMode.isMathJaxMode){
216  localMathjax = "MathJax.js";
217  }
218  }
219  platexobj_theme_set_var(outputMode, "REMOTE_MATHJAX", remoteMathjaxFile);
220  platexobj_theme_set_var(outputMode, "LOCAL_MATHJAX", localMathjax);
221 }
222 
224 
236 void platexobj_theme_update_var(POutoutMode & outputMode, const PString & pageLink, const PString & pageTitle,
237  const PString & prevSecLink, const PString & prevSecTitle,
238  const PString & nextSecLink, const PString & nextSecTitle,
239  const PString & parentSecLink, const PString & parentSecTitle,
240  const PPath & currentSourceFile, size_t currentSourceLine)
241 {
242  outputMode.mapVar["${PAGE_LINK}"] = pageLink;
243  outputMode.mapVar["${PAGE_TITLE}"] = pageTitle;
244  outputMode.mapVar["${PARENT_SECTION_TITLE}"] = parentSecTitle;
245  platexobj_theme_set_var(outputMode, "PARENT_SECTION", parentSecLink);
246  outputMode.mapVar["${PREV_PAGE_TITLE}"] = prevSecTitle;
247  platexobj_theme_set_var(outputMode, "PREV_PAGE", prevSecLink);
248  outputMode.mapVar["${NEXT_PAGE_TITLE}"] = nextSecTitle;
249  platexobj_theme_set_var(outputMode, "NEXT_PAGE", nextSecLink);
250 
251  outputMode.mapVar["${CURRENT_SOURCE_FILE}"] = currentSourceFile.makeAbsolute().simplify().getUnderPath(PPath(outputMode.bookGitlabUrl).getFileName());
252  outputMode.mapVar["${CURRENT_SOURCE_LINE}"] = valueToString(currentSourceLine);
253 
254 // outputMode.mapVar["${MAIN_TITLE}"] = "";
255 // outputMode.mapVar["${SUB_MENU}"] = "";
256 
257  if(outputMode.isBookTheme){
258  outputMode.mapVar["${BOOK_TABLE_OF_CONTENT}"] = platexobj_menuToHtml(outputMode.fullMenu, pageLink, outputMode.isBibliography);
259  }
260 }
261 
263 
267  PString body("");
268  dico_replace_nested_call(body, outputMode.themeCall, outputMode.mapVar, "${", "}");
269  return body;
270 }
271 
272 
#define LATEX_IMAGE_OUTPUT_DIR
Definition: PLatexObj_def.h:11
Describe a latex object.
Definition: PLatexObj.h:40
long unsigned int getId() const
Gets the id of the PLatexObj.
Definition: PLatexObj.cpp:284
const PString & getText() const
Gets the text of the PLatexObj.
Definition: PLatexObj.cpp:312
Path of a directory or a file.
Definition: PPath.h:17
bool createDirectory(mode_t mode=0755) const
Create the current directory.
Definition: PPath.cpp:331
PPath getUnderPath(const PString &pathPart) const
Get path which is under the given pathPart ('some/dir/path' with 'dir' will return 'path')
Definition: PPath.cpp:360
PPath makeAbsolute() const
Make an absolute path of the given path.
Definition: PPath.cpp:572
PPath simplify() const
Remove extra dots from the path.
Definition: PPath.cpp:533
bool isFileExist() const
Say if the current file path does exist.
Definition: PPath.cpp:139
PString loadFileContent() const
Get the file content in a PString.
Definition: PPath.cpp:382
PPath getFileName() const
Get the name of the file, from last char to /.
Definition: PPath.cpp:172
Extends the std::string.
Definition: PString.h:16
std::string valueToString(const T &val)
Convert a type into a string.
bool data_load(FILE *iter, T &data)
Load data from a message.
Definition: data_file.h:39
bool data_save(FILE *iter, const T &data)
Save data in a message.
Definition: data_file.h:18
void dico_replace_nested_call(PString &out, const PNestedCall &call, const PMapKnownVar &mapKeyVariable, const PString &varBegin, const PString &varEnd)
Replace the nested call by the variables in map.
void dico_create_nested_call(PNestedCall &call, const PString &baseStr, const PString &varBegin, const PString &varEnd)
Create the nested calls of the input base string.
bool lauchLatexToPng(const PString &fileNamePng)
Lauch latex to create png file.
void platexobj_theme_init_var(POutoutMode &outputMode)
Initialise basic variables of POutoutMode.
PString platexobj_theme_createPage(POutoutMode &outputMode)
Create the page from variables in POutoutMode.
void platexobj_theme_update_var(POutoutMode &outputMode, const PString &pageLink, const PString &pageTitle, const PString &prevSecLink, const PString &prevSecTitle, const PString &nextSecLink, const PString &nextSecTitle, const PString &parentSecLink, const PString &parentSecTitle, const PPath &currentSourceFile, size_t currentSourceLine)
Update var of POutoutMode for current section.
PString platexobj_htmlStrLatex(POutoutMode &outputMode, const PLatexObj &obj, const PString &latexEnv)
Convert a PLatexObj into a string.
void platexobj_theme_set_var(POutoutMode &outputMode, const PString &varName, const PString &value)
Set a variable in the map of variable for theme (will take account the IS_XXX_BEGIN accordingly if th...
PString platexobj_htmlStrInlineMath(POutoutMode &outputMode, const PLatexObj &obj)
Convert a PLatexObj into a string.
PString platexObj_createFormulaTex(POutoutMode &outputMode, size_t objId, const PString &latexEnv, const PString &texContent)
Create an image with a latex formula.
void platexobj_loadFormulaeMap(PMapFormula &mapFormula)
Load the map file of all the formulae if it exists.
PString platexObj_createImageWithMathTex(size_t objId, const PString &latexEnv, const PString &texContent, const PString &fontColor)
Create an image with a latex formula.
void platexobj_update_theme(POutoutMode &outputMode, const PPath &themeBaseFile)
Update the theme of the generated website.
void platexobj_saveFormulaeMap(const PMapFormula &mapFormula)
Save the map file of the formulae to avoid extra latex call.
bool platexobj_htmlStrLatexFile(const PString &fileName, const PString &latexEnv, const PString &mathContent, const PString &fontColor)
Save the latex file to be used to create a picture of the given formula.
std::map< PString, PString > PMapFormula
Map of the formulae generated by the ptex2html which can be reused by other PLatexObj which have the ...
#define FORMULAE_RECOVER_FILE
Define the name of the file which contains the formulae and the path to the created file.
PString platexobj_menuToHtml(const PFullMenu &menu, const PString &currentLink, bool isBibliography)
Convert a PFullMenu to html.
Output mode of the html backend.
bool isMathJaxMode
True to use the MathJax backend, false to use the latex backend.
PFullMenu fullMenu
Full menu for book theme.
PString bookGitlabUrl
Gitlab url of the current sources of the project.
PMapFormula mapFormula
Map of the formula which are already saved as png files.
PMapKnownVar mapVar
Map of output var which are use to fill the output themes.
bool isBookTheme
True to activate the book theme.
PString bookMasterProjectUrl
Master project url in book mode.
PString bookMail
Mail to be used to contact authors in book mode.
bool isBibliography
True if the generated web site has a bibliography, false otherwise.
bool isRemoteMathjax
True to activate mathjax but in remote mode (so no need to have mathjax in the generated site)
PString currentStyle
Current style to be used for the generated pages of the site.
PNestedCall themeCall
Parsed theme to be used.