Directory: | ./ |
---|---|
File: | tmp_project/PhoenixTex2Html/src/PLatexObj/parser_utils.cpp |
Date: | 2025-03-24 18:12:43 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 123 | 129 | 95.3% |
Branches: | 194 | 212 | 91.5% |
Line | Branch | Exec | Source |
---|---|---|---|
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 | |||
10 | ///fonction qui converti une chaîne de caractères en html (elle remplace les caractères spéciaux) | ||
11 | /** @param str : string que l'on veut convertir en Html | ||
12 | * @return string convertie en html | ||
13 | */ | ||
14 | 19002 | PString convertStrToHtml(const PString & str){ | |
15 |
2/2✓ Branch 2 taken 19002 times.
✓ Branch 5 taken 19002 times.
|
38004 | PString buffer(str.replace("&", "&")); |
16 |
4/4✓ Branch 1 taken 19002 times.
✓ Branch 4 taken 19002 times.
✓ Branch 7 taken 19002 times.
✓ Branch 10 taken 19002 times.
|
19002 | buffer = buffer.replace("<", "<"); |
17 |
4/4✓ Branch 1 taken 19002 times.
✓ Branch 4 taken 19002 times.
✓ Branch 7 taken 19002 times.
✓ Branch 10 taken 19002 times.
|
19002 | buffer = buffer.replace(">", ">"); |
18 |
4/4✓ Branch 1 taken 19002 times.
✓ Branch 4 taken 19002 times.
✓ Branch 7 taken 19002 times.
✓ Branch 10 taken 19002 times.
|
19002 | buffer = buffer.replace("\\\"\\\\\\\"", "\"\\\""); |
19 |
4/4✓ Branch 1 taken 19002 times.
✓ Branch 4 taken 19002 times.
✓ Branch 7 taken 19002 times.
✓ Branch 10 taken 19002 times.
|
19002 | buffer = buffer.replace("\\\\\\\"\\\"", "\\\"\""); |
20 |
4/4✓ Branch 1 taken 19002 times.
✓ Branch 4 taken 19002 times.
✓ Branch 7 taken 19002 times.
✓ Branch 10 taken 19002 times.
|
19002 | buffer = buffer.replace("\"", """); |
21 |
4/4✓ Branch 1 taken 19002 times.
✓ Branch 4 taken 19002 times.
✓ Branch 7 taken 19002 times.
✓ Branch 10 taken 19002 times.
|
19002 | buffer = buffer.replace("\\dots{}", "..."); |
22 |
4/4✓ Branch 1 taken 19002 times.
✓ Branch 4 taken 19002 times.
✓ Branch 7 taken 19002 times.
✓ Branch 10 taken 19002 times.
|
19002 | buffer = buffer.replace("\\'e", "é"); |
23 |
4/4✓ Branch 1 taken 19002 times.
✓ Branch 4 taken 19002 times.
✓ Branch 7 taken 19002 times.
✓ Branch 10 taken 19002 times.
|
19002 | buffer = buffer.replace("\\'", ""); |
24 |
4/4✓ Branch 1 taken 19002 times.
✓ Branch 4 taken 19002 times.
✓ Branch 7 taken 19002 times.
✓ Branch 10 taken 19002 times.
|
19002 | buffer = buffer.replace("~", "~"); |
25 |
4/4✓ Branch 1 taken 19002 times.
✓ Branch 4 taken 19002 times.
✓ Branch 7 taken 19002 times.
✓ Branch 10 taken 19002 times.
|
19002 | buffer = buffer.replace("\\and", "&"); |
26 | // buffer = buffer.replace("\\\\", "<br />"); | ||
27 |
4/4✓ Branch 1 taken 19002 times.
✓ Branch 4 taken 19002 times.
✓ Branch 7 taken 19002 times.
✓ Branch 10 taken 19002 times.
|
19002 | buffer = buffer.replace("\\,", ""); |
28 |
4/4✓ Branch 1 taken 19002 times.
✓ Branch 4 taken 19002 times.
✓ Branch 7 taken 19002 times.
✓ Branch 10 taken 19002 times.
|
19002 | buffer = buffer.replace("\\", "\"); |
29 | 19002 | return buffer; | |
30 | } | ||
31 | |||
32 | ///fonction qui converti une chaîne de caractères en html (elle remplace les caractères spéciaux) | ||
33 | /** @param ch : char que l'on veut convertir en Html | ||
34 | * @return string convertie en html | ||
35 | */ | ||
36 | 20362 | PString convertCharToHtml(char ch){ | |
37 |
5/6✓ Branch 0 taken 9 times.
✓ Branch 1 taken 28 times.
✓ Branch 2 taken 305 times.
✓ Branch 3 taken 48 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 19972 times.
|
20362 | switch(ch){ |
38 | 9 | case '\\': | |
39 |
1/1✓ Branch 1 taken 9 times.
|
9 | return "\"; |
40 | 28 | case '&': | |
41 |
1/1✓ Branch 1 taken 28 times.
|
28 | return "&"; |
42 | 305 | case '<': | |
43 |
1/1✓ Branch 1 taken 305 times.
|
305 | return "<"; |
44 | 48 | case '>': | |
45 |
1/1✓ Branch 1 taken 48 times.
|
48 | return ">"; |
46 | ✗ | case '~': | |
47 | ✗ | return "~"; | |
48 | 19972 | default: | |
49 | 19972 | break; | |
50 | } | ||
51 |
1/1✓ Branch 1 taken 19972 times.
|
39944 | PString tmp(""); |
52 |
1/1✓ Branch 1 taken 19972 times.
|
19972 | tmp += ch; |
53 |
1/1✓ Branch 1 taken 19972 times.
|
19972 | return tmp; |
54 | } | ||
55 | |||
56 | ///Get the parser env | ||
57 | /** @param vecParser : list of all parser | ||
58 | * @param nameParser : name of the parser to get | ||
59 | * @return PParserEnv | ||
60 | */ | ||
61 | 307 | PParserEnv getParserEnv(const PVecParserEnv & vecParser, const PString & nameParser){ | |
62 |
1/1✓ Branch 1 taken 307 times.
|
307 | const std::vector<PParserEnv> & vecPars = vecParser.getVecEnv(); |
63 |
1/2✓ Branch 4 taken 3248 times.
✗ Branch 5 not taken.
|
3248 | for(std::vector<PParserEnv>::const_iterator it(vecPars.begin()); it != vecPars.end(); ++it){ |
64 |
3/3✓ Branch 2 taken 3248 times.
✓ Branch 5 taken 307 times.
✓ Branch 6 taken 2941 times.
|
3248 | if(it->getName() == nameParser){ |
65 |
1/1✓ Branch 2 taken 307 times.
|
307 | return *it; |
66 | } | ||
67 | } | ||
68 | ✗ | PParserEnv out; | |
69 | ✗ | return out; | |
70 | } | ||
71 | |||
72 | ///Increment current char position | ||
73 | /** @param[out] fileParser : file parser | ||
74 | * @param[out] textObj : string to be used to store text | ||
75 | */ | ||
76 | 20362 | void incrementCurrentChar(PFileParser & fileParser, PString & textObj){ | |
77 | //If nothing is known I need to save the current char in the MACRO TEXT | ||
78 | 20362 | char ch = fileParser.getCurrentCh(); | |
79 |
1/1✓ Branch 2 taken 20362 times.
|
20362 | textObj += convertCharToHtml(ch); |
80 | 20362 | fileParser.getNextChar(); | |
81 | 20362 | } | |
82 | |||
83 | ///Play the text latex obj | ||
84 | /** @param[out] parent : parent string | ||
85 | * @param[out] textObj : string to deal with text | ||
86 | */ | ||
87 | 3200 | void playTextLatexObj(PString & parent, PString & textObj){ | |
88 |
2/2✓ Branch 1 taken 2664 times.
✓ Branch 2 taken 536 times.
|
3200 | if(textObj != ""){ |
89 | 2664 | parent += textObj; | |
90 | 2664 | textObj = ""; | |
91 | } | ||
92 | 3200 | } | |
93 | |||
94 | |||
95 | ///Parse the keyword of the given parser | ||
96 | /** @param[out] out : output string | ||
97 | * @param[out] textObj : string of text | ||
98 | * @param[out] fileParser : file parser | ||
99 | * @param context : highlighting parser | ||
100 | * @return true if keyword has been found | ||
101 | */ | ||
102 | 488018 | bool parser_parseKeyword(PString & out, PString & textObj, PFileParser & fileParser, const PContext & context){ | |
103 |
4/4✓ Branch 1 taken 488018 times.
✓ Branch 4 taken 488018 times.
✓ Branch 7 taken 488018 times.
✓ Branch 10 taken 488018 times.
|
488018 | PString strBegin(context.getBegin()), strEnd(context.getEnd()); |
104 |
2/2✓ Branch 1 taken 488018 times.
✓ Branch 4 taken 488018 times.
|
488018 | PString strNotAfterBegin(context.getNotAfterBegin()); |
105 |
2/2✓ Branch 1 taken 488018 times.
✓ Branch 4 taken 488018 times.
|
488018 | PString strNotBeforeBegin(context.getNotBeforeBegin()); |
106 |
2/2✓ Branch 1 taken 488018 times.
✓ Branch 4 taken 488018 times.
|
488018 | PString strNotBeforeEnd(context.getNotBeforeEnd()); |
107 | |||
108 | 488018 | bool parseUntilEnd(false), isFind(false); | |
109 |
3/3✓ Branch 1 taken 488018 times.
✓ Branch 3 taken 327004 times.
✓ Branch 4 taken 161014 times.
|
488018 | if(strBegin != ""){ |
110 |
1/1✓ Branch 1 taken 327004 times.
|
327004 | fileParser.pushPosition(); |
111 |
7/10✓ Branch 1 taken 1587 times.
✓ Branch 2 taken 325417 times.
✓ Branch 4 taken 1587 times.
✓ Branch 6 taken 1587 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 1587 times.
✓ Branch 11 taken 1587 times.
✗ Branch 12 not taken.
✓ Branch 13 taken 327004 times.
✗ Branch 14 not taken.
|
327004 | if(strNotBeforeBegin == "" || (strNotBeforeBegin != "" && !fileParser.isMatch(strNotBeforeBegin))){ |
112 |
3/3✓ Branch 1 taken 327004 times.
✓ Branch 3 taken 869 times.
✓ Branch 4 taken 326135 times.
|
327004 | if(fileParser.isMatch(strBegin)){ |
113 |
2/3✓ Branch 1 taken 869 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 869 times.
|
869 | if(strNotAfterBegin != ""){ |
114 | ✗ | parseUntilEnd = !fileParser.isMatch(strNotAfterBegin); | |
115 | }else{ | ||
116 | 869 | parseUntilEnd = true; | |
117 | } | ||
118 | } | ||
119 | } | ||
120 |
2/2✓ Branch 0 taken 869 times.
✓ Branch 1 taken 326135 times.
|
327004 | if(parseUntilEnd){ |
121 |
2/3✓ Branch 1 taken 869 times.
✓ Branch 3 taken 869 times.
✗ Branch 4 not taken.
|
869 | if(strEnd != ""){ |
122 |
1/1✓ Branch 1 taken 869 times.
|
869 | playTextLatexObj(out, textObj); |
123 |
1/1✓ Branch 1 taken 869 times.
|
869 | PString content(""); |
124 |
3/3✓ Branch 1 taken 869 times.
✓ Branch 3 taken 141 times.
✓ Branch 4 taken 728 times.
|
869 | if(strNotBeforeEnd != ""){ |
125 | // content = fileParser.getUntilKeyWithoutPaternRecurseExclude(strEnd, strBegin, strNotBeforeEnd); | ||
126 |
2/2✓ Branch 1 taken 141 times.
✓ Branch 4 taken 141 times.
|
141 | content = fileParser.getUntilKeyWithoutPaternExclude(strEnd, strNotBeforeEnd); |
127 | }else{ | ||
128 | // content = fileParser.getUntilKeyWithoutPaternRecurse(strEnd, strBegin); | ||
129 |
2/2✓ Branch 1 taken 728 times.
✓ Branch 4 taken 728 times.
|
728 | content = fileParser.getUntilKeyWithoutPatern(strEnd); |
130 | } | ||
131 |
10/10✓ Branch 1 taken 869 times.
✓ Branch 4 taken 869 times.
✓ Branch 7 taken 869 times.
✓ Branch 10 taken 869 times.
✓ Branch 13 taken 869 times.
✓ Branch 16 taken 869 times.
✓ Branch 19 taken 869 times.
✓ Branch 22 taken 869 times.
✓ Branch 25 taken 869 times.
✓ Branch 28 taken 869 times.
|
869 | out += "<span class=\""+context.getName()+"\">" + convertStrToHtml(strBegin + content + strEnd) + "</span>"; |
132 | 869 | isFind = true; | |
133 | 869 | }else{ | |
134 | ✗ | fileParser.popPosition(); | |
135 | } | ||
136 | }else{ | ||
137 |
1/1✓ Branch 1 taken 326135 times.
|
326135 | fileParser.popPosition(); |
138 | } | ||
139 | } | ||
140 |
1/1✓ Branch 1 taken 488018 times.
|
488018 | const PVecString & vecKeyword = context.getVecKeyword(); |
141 |
2/2✓ Branch 1 taken 138128 times.
✓ Branch 2 taken 349890 times.
|
488018 | if(vecKeyword.size() != 0lu){ |
142 |
1/1✓ Branch 1 taken 138128 times.
|
138128 | PString matchString(fileParser.isMatchToken(vecKeyword)); |
143 |
3/3✓ Branch 1 taken 138128 times.
✓ Branch 3 taken 1690 times.
✓ Branch 4 taken 136438 times.
|
138128 | if(matchString != ""){ |
144 |
1/1✓ Branch 1 taken 1690 times.
|
1690 | playTextLatexObj(out, textObj); |
145 |
7/7✓ Branch 1 taken 1690 times.
✓ Branch 4 taken 1690 times.
✓ Branch 7 taken 1690 times.
✓ Branch 10 taken 1690 times.
✓ Branch 13 taken 1690 times.
✓ Branch 16 taken 1690 times.
✓ Branch 19 taken 1690 times.
|
1690 | out += "<span class=\""+context.getName()+"\">" + convertStrToHtml(matchString) + "</span>"; |
146 | 1690 | isFind = true; | |
147 | } | ||
148 | 138128 | } | |
149 |
2/2✓ Branch 1 taken 488018 times.
✓ Branch 4 taken 488018 times.
|
488018 | const PVecParseStep & vecStep = context.getSeq().getVecStep(); |
150 |
2/2✓ Branch 1 taken 22886 times.
✓ Branch 2 taken 465132 times.
|
488018 | if(vecStep.size() != 0lu){ |
151 |
2/2✓ Branch 1 taken 22886 times.
✓ Branch 4 taken 22886 times.
|
22886 | PString res(fileParser.isMatch(context.getSeq())); |
152 |
3/3✓ Branch 1 taken 22886 times.
✓ Branch 3 taken 334 times.
✓ Branch 4 taken 22552 times.
|
22886 | if(res != ""){ |
153 |
1/1✓ Branch 1 taken 334 times.
|
334 | playTextLatexObj(out, textObj); |
154 |
7/7✓ Branch 1 taken 334 times.
✓ Branch 4 taken 334 times.
✓ Branch 7 taken 334 times.
✓ Branch 10 taken 334 times.
✓ Branch 13 taken 334 times.
✓ Branch 16 taken 334 times.
✓ Branch 19 taken 334 times.
|
334 | out += "<span class=\""+context.getName()+"\">" + convertStrToHtml(res) + "</span>"; |
155 | 334 | isFind = true; | |
156 | } | ||
157 | 22886 | } | |
158 | |||
159 | 488018 | return isFind; | |
160 | 488018 | } | |
161 | |||
162 | |||
163 | ///Parse the keyword of the given parser | ||
164 | /** @param[out] out : output string | ||
165 | * @param[out] textObj : string of text | ||
166 | * @param[out] fileParser : file parser | ||
167 | * @param parser : highlighting parser | ||
168 | * @return true if keyword has been found | ||
169 | */ | ||
170 | 27348 | bool parser_parseKeyword(PString & out, PString & textObj, PFileParser & fileParser, const PParserEnv & parser){ | |
171 |
1/1✓ Branch 1 taken 27348 times.
|
27348 | const PVecContext & vecContext = parser.getVecContext(); |
172 | 27348 | PVecContext::const_iterator it(vecContext.begin()); | |
173 | 27348 | bool isSearch(true); | |
174 |
6/6✓ Branch 2 taken 490856 times.
✓ Branch 3 taken 24510 times.
✓ Branch 4 taken 488018 times.
✓ Branch 5 taken 2838 times.
✓ Branch 6 taken 488018 times.
✓ Branch 7 taken 27348 times.
|
515366 | while(it != vecContext.end() && isSearch){ |
175 |
1/1✓ Branch 2 taken 488018 times.
|
488018 | isSearch = !parser_parseKeyword(out, textObj, fileParser, *it); |
176 | 488018 | ++it; | |
177 | } | ||
178 | 27348 | return !isSearch; | |
179 | } | ||
180 | |||
181 | ///Do the highlighting of the input text | ||
182 | /** @param strContent : string content | ||
183 | * @param parser : parser to be used | ||
184 | * @return string with balise | ||
185 | */ | ||
186 | 307 | PString parser_makeHighlighting(const PString & strContent, const PParserEnv & parser){ | |
187 |
2/2✓ Branch 1 taken 307 times.
✓ Branch 4 taken 307 times.
|
307 | PString out(""), tmpText(""); |
188 |
1/1✓ Branch 1 taken 307 times.
|
307 | PFileParser fileParser; |
189 |
1/1✓ Branch 1 taken 307 times.
|
307 | fileParser.setEscapeChar('\0'); |
190 |
2/2✓ Branch 1 taken 307 times.
✓ Branch 4 taken 307 times.
|
307 | fileParser.setWhiteSpace(""); |
191 |
1/1✓ Branch 1 taken 307 times.
|
307 | fileParser.setFileContent(strContent); |
192 | |||
193 |
3/3✓ Branch 1 taken 27655 times.
✓ Branch 3 taken 27348 times.
✓ Branch 4 taken 307 times.
|
27655 | while(!fileParser.isEndOfFile()){ |
194 |
3/3✓ Branch 1 taken 27348 times.
✓ Branch 3 taken 24455 times.
✓ Branch 4 taken 2893 times.
|
27348 | if(parser_parseKeyword(out, tmpText, fileParser, parser)){} |
195 | else{ | ||
196 |
2/2✓ Branch 1 taken 24455 times.
✓ Branch 4 taken 24455 times.
|
24455 | PString plainText(fileParser.getStrComposedOf(parser.getPlainText())); |
197 |
2/2✓ Branch 1 taken 20362 times.
✓ Branch 2 taken 4093 times.
|
24455 | if(plainText == ""){ |
198 |
1/1✓ Branch 1 taken 20362 times.
|
20362 | incrementCurrentChar(fileParser, tmpText); |
199 | }else{ | ||
200 |
1/1✓ Branch 1 taken 4093 times.
|
4093 | tmpText += plainText; |
201 | } | ||
202 | 24455 | } | |
203 | } | ||
204 |
1/1✓ Branch 1 taken 307 times.
|
307 | playTextLatexObj(out, tmpText); |
205 | 614 | return out; | |
206 | 307 | } | |
207 | |||
208 | ///Get the last PLatexObj of the given one | ||
209 | /** @param obj : main object to use | ||
210 | * @return pointer to the last latex object | ||
211 | */ | ||
212 | 52 | PLatexObj* getLastPLatexObj(PLatexObj & obj){ | |
213 | 52 | PLatexType::PLatexType type = obj.getType(); | |
214 | 52 | PVecLatexObj & vecObj = obj.getVecContent(); | |
215 |
2/2✓ Branch 1 taken 22 times.
✓ Branch 2 taken 30 times.
|
52 | if(vecObj.size() != 0lu){ |
216 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 21 times.
|
22 | if(type == PLatexType::CAPTION){return &obj;} |
217 | 21 | PLatexObj* outPtr = NULL; | |
218 | 21 | PVecLatexObj::reverse_iterator it(vecObj.rbegin()); | |
219 |
6/7✓ Branch 0 taken 31 times.
✓ Branch 1 taken 21 times.
✓ Branch 4 taken 31 times.
✓ Branch 6 taken 31 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 31 times.
✓ Branch 9 taken 21 times.
|
52 | while(outPtr == NULL && it != vecObj.rend()){ |
220 |
1/1✓ Branch 2 taken 31 times.
|
31 | outPtr = getLastPLatexObj(*it); |
221 | 31 | ++it; | |
222 | } | ||
223 | 21 | return outPtr; | |
224 | }else{ //No other content | ||
225 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 20 times.
|
30 | if(type == PLatexType::TEXT){return NULL;} //Pure text cannot recieved label |
226 | 20 | else{return &obj;} | |
227 | } | ||
228 | } | ||
229 | |||
230 | |||
231 |