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 PVecParseStep & vecStep = context.getSeq().getVecStep();
150  if(vecStep.size() != 0lu){
151  PString res(fileParser.isMatch(context.getSeq()));
152  if(res != ""){
153  playTextLatexObj(out, textObj);
154  out += "<span class=\""+context.getName()+"\">" + convertStrToHtml(res) + "</span>";
155  isFind = true;
156  }
157  }
158 
159  return isFind;
160 }
161 
162 
164 
170 bool parser_parseKeyword(PString & out, PString & textObj, PFileParser & fileParser, const PParserEnv & parser){
171  const PVecContext & vecContext = parser.getVecContext();
172  PVecContext::const_iterator it(vecContext.begin());
173  bool isSearch(true);
174  while(it != vecContext.end() && isSearch){
175  isSearch = !parser_parseKeyword(out, textObj, fileParser, *it);
176  ++it;
177  }
178  return !isSearch;
179 }
180 
182 
186 PString parser_makeHighlighting(const PString & strContent, const PParserEnv & parser){
187  PString out(""), tmpText("");
188  PFileParser fileParser;
189  fileParser.setEscapeChar('\0');
190  fileParser.setWhiteSpace("");
191  fileParser.setFileContent(strContent);
192 
193  while(!fileParser.isEndOfFile()){
194  if(parser_parseKeyword(out, tmpText, fileParser, parser)){}
195  else{
196  PString plainText(fileParser.getStrComposedOf(parser.getPlainText()));
197  if(plainText == ""){
198  incrementCurrentChar(fileParser, tmpText);
199  }else{
200  tmpText += plainText;
201  }
202  }
203  }
204  playTextLatexObj(out, tmpText);
205  return out;
206 }
207 
209 
213  PLatexType::PLatexType type = obj.getType();
214  PVecLatexObj & vecObj = obj.getVecContent();
215  if(vecObj.size() != 0lu){
216  if(type == PLatexType::CAPTION){return &obj;}
217  PLatexObj* outPtr = NULL;
218  PVecLatexObj::reverse_iterator it(vecObj.rbegin());
219  while(outPtr == NULL && it != vecObj.rend()){
220  outPtr = getLastPLatexObj(*it);
221  ++it;
222  }
223  return outPtr;
224  }else{ //No other content
225  if(type == PLatexType::TEXT){return NULL;} //Pure text cannot recieved label
226  else{return &obj;}
227  }
228 }
229 
230 
std::vector< PParseStep > PVecParseStep
Definition: PParseSeqDef.h:12
std::vector< PString > PVecString
Definition: PString.h:96
Context for parser environement.
Definition: PLatexObj.h:310
const PString & getNotBeforeBegin() const
Gets the notBeforeBegin of the PContext.
Definition: PLatexObj.cpp:1331
const PString & getBegin() const
Gets the begin of the PContext.
Definition: PLatexObj.cpp:1303
const PParseSeq & getSeq() const
Gets the seq of the PContext.
Definition: PLatexObj.cpp:1387
const PString & getEnd() const
Gets the end of the PContext.
Definition: PLatexObj.cpp:1317
const PString & getNotBeforeEnd() const
Gets the notBeforeEnd of the PContext.
Definition: PLatexObj.cpp:1359
const std::vector< PString > & getVecKeyword() const
Gets the vecKeyword of the PContext.
Definition: PLatexObj.cpp:1373
const PString & getName() const
Gets the name of the PContext.
Definition: PLatexObj.cpp:1289
const PString & getNotAfterBegin() const
Gets the notAfterBegin of the PContext.
Definition: PLatexObj.cpp:1345
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:40
const std::vector< PLatexObj > & getVecContent() const
Gets the vecContent of the PLatexObj.
Definition: PLatexObj.cpp:410
const PLatexType::PLatexType & getType() const
Gets the type of the PLatexObj.
Definition: PLatexObj.cpp:298
const std ::vector< PParseStep > & getVecStep() const
Get the variable p_vecStep.
Definition: PParseSeq.cpp:256
Parser environement.
Definition: PLatexObj.h:363
const PString & getPlainText() const
Gets the plainText of the PParserEnv.
Definition: PLatexObj.cpp:1486
const std::vector< PContext > & getVecContext() const
Gets the vecContext of the PParserEnv.
Definition: PLatexObj.cpp:1500
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:204
Vector of the environements.
Definition: PLatexObj.h:391
const std::vector< PParserEnv > & getVecEnv() const
Gets the vecEnv of the PVecParserEnv.
Definition: PLatexObj.cpp:1562
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.