PhoenixLecture  2.0.0
Set of tools to make lectures
parser_utils.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 "PFileParser.h"
8 #include "parser_utils.h"
9 
11 
15  PString buffer(str.replace("&", "&"));
16  buffer = buffer.replace("<", "&lt;");
17  buffer = buffer.replace(">", "&gt;");
18  buffer = buffer.replace("\\\"\\\\\\\"", "&bsol;&quot;&bsol;&bsol;&bsol;&quot;");
19  buffer = buffer.replace("\\\\\\\"\\\"", "&bsol;&bsol;&bsol;&quot;&bsol;&quot;");
20  buffer = buffer.replace("\"", "&quot;");
21  buffer = buffer.replace("\\dots{}", "...");
22  buffer = buffer.replace("\\'e", "é");
23  buffer = buffer.replace("\\'", "");
24  buffer = buffer.replace("~", "&#126;");
25  buffer = buffer.replace("\\and", "&amp;");
26 // buffer = buffer.replace("\\\\", "<br />");
27  buffer = buffer.replace("\\,", "");
28  buffer = buffer.replace("\\", "&bsol;");
29  return buffer;
30 }
31 
33 
37  switch(ch){
38  case '\\':
39  return "&bsol;";
40  case '&':
41  return "&amp;";
42  case '<':
43  return "&lt;";
44  case '>':
45  return "&gt;";
46  case '~':
47  return "&#126;";
48  default:
49  break;
50  }
51  PString tmp("");
52  tmp += ch;
53  return tmp;
54 }
55 
57 
61 PParserEnv getParserEnv(const PVecParserEnv & vecParser, const PString & nameParser){
62  const std::vector<PParserEnv> & vecPars = vecParser.getVecEnv();
63  for(std::vector<PParserEnv>::const_iterator it(vecPars.begin()); it != vecPars.end(); ++it){
64  if(it->getName() == nameParser){
65  return *it;
66  }
67  }
68  PParserEnv out;
69  return out;
70 }
71 
73 
76 void incrementCurrentChar(PFileParser & fileParser, PString & textObj){
77  //If nothing is known I need to save the current char in the MACRO TEXT
78  char ch = fileParser.getCurrentCh();
79  textObj += convertCharToHtml(ch);
80  fileParser.getNextChar();
81 }
82 
84 
87 void playTextLatexObj(PString & parent, PString & textObj){
88  if(textObj != ""){
89  parent += textObj;
90  textObj = "";
91  }
92 }
93 
94 
96 
102 bool parser_parseKeyword(PString & out, PString & textObj, PFileParser & fileParser, const PContext & context){
103  PString strBegin(context.getBegin()), strEnd(context.getEnd());
104  PString strNotAfterBegin(context.getNotAfterBegin());
105  PString strNotBeforeBegin(context.getNotBeforeBegin());
106  PString strNotBeforeEnd(context.getNotBeforeEnd());
107 
108  bool parseUntilEnd(false), isFind(false);
109  if(strBegin != ""){
110  fileParser.pushPosition();
111  if(strNotBeforeBegin == "" || (strNotBeforeBegin != "" && !fileParser.isMatch(strNotBeforeBegin))){
112  if(fileParser.isMatch(strBegin)){
113  if(strNotAfterBegin != ""){
114  parseUntilEnd = !fileParser.isMatch(strNotAfterBegin);
115  }else{
116  parseUntilEnd = true;
117  }
118  }
119  }
120  if(parseUntilEnd){
121  if(strEnd != ""){
122  playTextLatexObj(out, textObj);
123  PString content("");
124  if(strNotBeforeEnd != ""){
125 // content = fileParser.getUntilKeyWithoutPaternRecurseExclude(strEnd, strBegin, strNotBeforeEnd);
126  content = fileParser.getUntilKeyWithoutPaternExclude(strEnd, strNotBeforeEnd);
127  }else{
128 // content = fileParser.getUntilKeyWithoutPaternRecurse(strEnd, strBegin);
129  content = fileParser.getUntilKeyWithoutPatern(strEnd);
130  }
131  out += "<span class=\""+context.getName()+"\">" + convertStrToHtml(strBegin + content + strEnd) + "</span>";
132  isFind = true;
133  }else{
134  fileParser.popPosition();
135  }
136  }else{
137  fileParser.popPosition();
138  }
139  }
140  const PVecString & vecKeyword = context.getVecKeyword();
141  if(vecKeyword.size() != 0lu){
142  PString matchString(fileParser.isMatchToken(vecKeyword));
143  if(matchString != ""){
144  playTextLatexObj(out, textObj);
145  out += "<span class=\""+context.getName()+"\">" + convertStrToHtml(matchString) + "</span>";
146  isFind = true;
147  }
148  }
149  const PVecString & vecDelimiter= context.getVecDelimiter();
150  if(vecDelimiter.size() != 0lu){
151  PString matchString(fileParser.isMatch(vecDelimiter));
152  if(matchString != ""){
153  playTextLatexObj(out, textObj);
154  out += "<span class=\""+context.getName()+"\">" + convertStrToHtml(matchString) + "</span>";
155  isFind = true;
156  }
157  }
158  const PVecParseStep & vecStep = context.getSeq().getVecStep();
159  if(vecStep.size() != 0lu){
160  PString res(fileParser.isMatch(context.getSeq()));
161  if(res != ""){
162  playTextLatexObj(out, textObj);
163  out += "<span class=\""+context.getName()+"\">" + convertStrToHtml(res) + "</span>";
164  isFind = true;
165  }
166  }
167 
168  return isFind;
169 }
170 
171 
173 
179 bool parser_parseKeyword(PString & out, PString & textObj, PFileParser & fileParser, const PParserEnv & parser){
180  const PVecContext & vecContext = parser.getVecContext();
181  PVecContext::const_iterator it(vecContext.begin());
182  bool isSearch(true);
183  while(it != vecContext.end() && isSearch){
184  isSearch = !parser_parseKeyword(out, textObj, fileParser, *it);
185  ++it;
186  }
187  return !isSearch;
188 }
189 
191 
195 PString parser_makeHighlighting(const PString & strContent, const PParserEnv & parser){
196  PString out(""), tmpText("");
197  PFileParser fileParser;
198  fileParser.setEscapeChar('\0');
199  fileParser.setWhiteSpace("");
200  fileParser.setFileContent(strContent);
201 
202  while(!fileParser.isEndOfFile()){
203  if(parser_parseKeyword(out, tmpText, fileParser, parser)){}
204  else{
205  PString plainText(fileParser.getStrComposedOf(parser.getPlainText()));
206  if(plainText == ""){
207  incrementCurrentChar(fileParser, tmpText);
208  }else{
209  tmpText += plainText;
210  }
211  }
212  }
213  playTextLatexObj(out, tmpText);
214  return out;
215 }
216 
218 
222  PLatexType::PLatexType type = obj.getType();
223  PVecLatexObj & vecObj = obj.getVecContent();
224  if(vecObj.size() != 0lu){
225  if(type == PLatexType::CAPTION){return &obj;}
226  PLatexObj* outPtr = NULL;
227  PVecLatexObj::reverse_iterator it(vecObj.rbegin());
228  while(outPtr == NULL && it != vecObj.rend()){
229  outPtr = getLastPLatexObj(*it);
230  ++it;
231  }
232  return outPtr;
233  }else{ //No other content
234  if(type == PLatexType::TEXT){return NULL;} //Pure text cannot recieved label
235  else{return &obj;}
236  }
237 }
238 
239 
std::vector< PParseStep > PVecParseStep
Definition: PParseSeqDef.h:12
std::vector< PString > PVecString
Definition: PString.h:99
Context for parser environement.
Definition: PLatexObj.h:308
const std::vector< PString > & getVecDelimiter() const
Gets the vecDelimiter of the PContext.
Definition: PLatexObj.cpp:1392
const PString & getNotBeforeBegin() const
Gets the notBeforeBegin of the PContext.
Definition: PLatexObj.cpp:1336
const PString & getBegin() const
Gets the begin of the PContext.
Definition: PLatexObj.cpp:1308
const PParseSeq & getSeq() const
Gets the seq of the PContext.
Definition: PLatexObj.cpp:1406
const PString & getEnd() const
Gets the end of the PContext.
Definition: PLatexObj.cpp:1322
const PString & getNotBeforeEnd() const
Gets the notBeforeEnd of the PContext.
Definition: PLatexObj.cpp:1364
const std::vector< PString > & getVecKeyword() const
Gets the vecKeyword of the PContext.
Definition: PLatexObj.cpp:1378
const PString & getName() const
Gets the name of the PContext.
Definition: PLatexObj.cpp:1294
const PString & getNotAfterBegin() const
Gets the notAfterBegin of the PContext.
Definition: PLatexObj.cpp:1350
classe qui permet de parser des fichiers texte en renvoyant les tokens les uns après les autres
Definition: PFileParser.h:20
PString getUntilKeyWithoutPaternExclude(const PString &patern, const PString &strNotBeforeEndPatern)
Parse a string until the patern is found, only if it has not strNotBeforeEndPatern before it.
PString getUntilKeyWithoutPatern(const PString &patern)
Renvoie la chaine de caractère du caractère courant jusqu'à patern exclu.
void setEscapeChar(char escapeChar)
Sets the escape character of the PFileParser.
Definition: PFileParser.cpp:58
PString getStrComposedOf(const PString &charset)
Get string composed of the characters in the string charset.
void setWhiteSpace(const PString &whiteSpace)
Initialise la liste des caractères blancs.
Definition: PFileParser.cpp:35
bool isMatchToken(const PString &patern)
Says if the patern match with the current caracters of the PFileParser but treats the string as a tok...
bool isMatch(const PString &patern)
Says if the patern match with the current caracters of the PFileParser.
char getCurrentCh() const
Renvoie le caractère courant.
void popPosition()
Get to the last saved position of the PFileParser in the current file.
Definition: PFileParser.cpp:99
void setFileContent(const PString &fileContent)
Set the file content.
Definition: PFileParser.cpp:50
void pushPosition()
Remember the current position of the PFileParser in the current file.
Definition: PFileParser.cpp:93
bool isEndOfFile() const
Dit si on est à la fin du fichier.
Definition: PFileParser.cpp:88
char getNextChar()
Fonction qui renvoie le prochain caractère du fichier courant.
Describe a latex object.
Definition: PLatexObj.h:38
const std::vector< PLatexObj > & getVecContent() const
Gets the vecContent of the PLatexObj.
Definition: PLatexObj.cpp:408
const PLatexType::PLatexType & getType() const
Gets the type of the PLatexObj.
Definition: PLatexObj.cpp:296
const std ::vector< PParseStep > & getVecStep() const
Get the variable p_vecStep.
Definition: PParseSeq.cpp:256
Parser environement.
Definition: PLatexObj.h:366
const PString & getPlainText() const
Gets the plainText of the PParserEnv.
Definition: PLatexObj.cpp:1506
const std::vector< PContext > & getVecContext() const
Gets the vecContext of the PParserEnv.
Definition: PLatexObj.cpp:1520
Extends the std::string.
Definition: PString.h:16
PString replace(const PString &pattern, const PString &replaceStr) const
Replace a PString into an other PString.
Definition: PString.cpp:267
Vector of the environements.
Definition: PLatexObj.h:394
const std::vector< PParserEnv > & getVecEnv() const
Gets the vecEnv of the PVecParserEnv.
Definition: PLatexObj.cpp:1582
PLatexType
Type of the PLatexObj.
Definition: PLatexType.h:12
PString parser_makeHighlighting(const PString &strContent, const PParserEnv &parser)
Do the highlighting of the input text.
PLatexObj * getLastPLatexObj(PLatexObj &obj)
Get the last PLatexObj of the given one.
void playTextLatexObj(PString &parent, PString &textObj)
Play the text latex obj.
bool parser_parseKeyword(PString &out, PString &textObj, PFileParser &fileParser, const PContext &context)
Parse the keyword of the given parser.
PString convertCharToHtml(char ch)
fonction qui converti une chaîne de caractères en html (elle remplace les caractères spéciaux)
PParserEnv getParserEnv(const PVecParserEnv &vecParser, const PString &nameParser)
Get the parser env.
PString convertStrToHtml(const PString &str)
fonction qui converti une chaîne de caractères en html (elle remplace les caractères spéciaux)
void incrementCurrentChar(PFileParser &fileParser, PString &textObj)
Increment current char position.
std::vector< PContext > PVecContext
Vector of context.
std::vector< PLatexObj > PVecLatexObj
Vector of obj.