@@ -462,6 +462,8 @@ struct commentscanYY_state
462462 OutputContext inContext; // are we inside the brief, details or xref part
463463 bool briefEndsAtDot = FALSE ; // does the brief description stop at a dot?
464464 QCString formulaText; // Running text of a formula
465+ QCString formulaPreText; // Start command of a formula
466+ QCString formulaPostText; // End command of a formula
465467 QCString formulaEnv; // environment name
466468 int formulaNewLines = 0 ; // amount of new lines in the formula
467469 QCString *pOutputString = nullptr ; // pointer to string to which the output is appended.
@@ -993,30 +995,38 @@ STopt [^\n@\\]*
993995 }
994996<Comment >{B }* {CMD }" f{" [^ } \n ]+ " }" (" {" ? ) { // start of a formula with custom environment
995997 setOutput (yyscanner,OutputDoc);
996- yyextra->formulaText =" \\ begin" ;
998+ yyextra->formulaText =" " ;
999+ yyextra->formulaPreText =" \\ begin" ;
1000+ yyextra->formulaPostText =" " ;
9971001 yyextra->formulaEnv =QCString (yytext).stripWhiteSpace ().mid (2 );
9981002 if (yyextra->formulaEnv .at (yyextra->formulaEnv .length ()-1 )==' {' )
9991003 {
10001004 // remove trailing open brace
10011005 yyextra->formulaEnv =yyextra->formulaEnv .left (yyextra->formulaEnv .length ()-1 );
10021006 }
1003- yyextra->formulaText +=yyextra->formulaEnv ;
1007+ yyextra->formulaPreText +=yyextra->formulaEnv ;
10041008 yyextra->formulaNewLines =0 ;
10051009 BEGIN (ReadFormulaLong);
10061010 }
10071011<Comment >{B }* {CMD }" f$" { // start of a inline formula
1008- yyextra->formulaText =" $" ;
1012+ yyextra->formulaText =" " ;
1013+ yyextra->formulaPreText =" $" ;
1014+ yyextra->formulaPostText =" " ;
10091015 yyextra->formulaNewLines =0 ;
10101016 BEGIN (ReadFormulaShort);
10111017 }
10121018<Comment >{B }* {CMD }" f(" { // start of a inline formula
10131019 yyextra->formulaText =" " ;
1020+ yyextra->formulaPreText =" " ;
1021+ yyextra->formulaPostText =" " ;
10141022 yyextra->formulaNewLines =0 ;
10151023 BEGIN (ReadFormulaRound);
10161024 }
10171025<Comment >{B }* {CMD }" f[" { // start of a block formula
10181026 setOutput (yyscanner,OutputDoc);
1019- yyextra->formulaText =" \\ [" ;
1027+ yyextra->formulaText =" " ;
1028+ yyextra->formulaPreText =" \\ [" ;
1029+ yyextra->formulaPostText =" " ;
10201030 yyextra->formulaNewLines =0 ;
10211031 BEGIN (ReadFormulaLong);
10221032 }
@@ -1201,7 +1211,7 @@ STopt [^\n@\\]*
12011211 /* -------------- Rules for handling formulas ---------------- */
12021212
12031213<ReadFormulaShort ,ReadFormulaShortSection >{CMD }" f$" { // end of inline formula
1204- yyextra->formulaText +=" $" ;
1214+ yyextra->formulaPostText +=" $" ;
12051215 QCString form = addFormula (yyscanner);
12061216 addOutput (yyscanner," " +form);
12071217 if (YY_START == ReadFormulaShort)
@@ -1228,13 +1238,13 @@ STopt [^\n@\\]*
12281238 }
12291239 }
12301240<ReadFormulaLong >{CMD }" f]" { // end of block formula
1231- yyextra->formulaText +=" \\ ]" ;
1241+ yyextra->formulaPostText +=" \\ ]" ;
12321242 addOutput (yyscanner," " +addFormula (yyscanner));
12331243 BEGIN (Comment);
12341244 }
12351245<ReadFormulaLong >{CMD }" f}" { // end of custom env formula
1236- yyextra->formulaText +=" \\ end" ;
1237- yyextra->formulaText +=yyextra->formulaEnv ;
1246+ yyextra->formulaPostText +=" \\ end" ;
1247+ yyextra->formulaPostText +=yyextra->formulaEnv ;
12381248 addOutput (yyscanner," " +addFormula (yyscanner));
12391249 BEGIN (Comment);
12401250 }
@@ -1840,12 +1850,16 @@ STopt [^\n@\\]*
18401850 BEGIN ( Comment );
18411851 }
18421852<SectionTitle >{B }* {CMD }" f$" {
1843- yyextra->formulaText =" $" ;
1853+ yyextra->formulaText =" " ;
1854+ yyextra->formulaPreText =" $" ;
1855+ yyextra->formulaPostText =" " ;
18441856 yyextra->formulaNewLines =0 ;
18451857 BEGIN (ReadFormulaShortSection);
18461858 }
18471859<SectionTitle >{B }* {CMD }" f(" { // start of a inline formula
18481860 yyextra->formulaText =" " ;
1861+ yyextra->formulaPreText =" " ;
1862+ yyextra->formulaPostText =" " ;
18491863 yyextra->formulaNewLines =0 ;
18501864 BEGIN (ReadFormulaRoundSection);
18511865 }
@@ -4102,6 +4116,18 @@ static void addXRefItem(yyscan_t yyscanner,
41024116}
41034117
41044118// -----------------------------------------------------------------------------
4119+ inline bool qqisspace (char c)
4120+ { return c==' ' || c==' \t ' ; }
4121+
4122+ QCString qStripEndWhiteSpace (QCString rep)
4123+ {
4124+ size_t sl = rep.length ();
4125+ if (sl==0 || (!qqisspace (rep[sl-1 ]))) return rep;
4126+ size_t start=0 ,end=sl-1 ;
4127+ while (end>start && qqisspace (rep[end])) end--;
4128+ return QCString (rep.mid (start,1 +end-start));
4129+ }
4130+
41054131
41064132// Adds a formula text to the list/dictionary of formulas if it was
41074133// not already added. Returns the label of the formula.
@@ -4110,7 +4136,10 @@ static QCString addFormula(yyscan_t yyscanner)
41104136 std::unique_lock<std::mutex> lock (g_formulaMutex);
41114137 struct yyguts_t *yyg = (struct yyguts_t *)yyscanner;
41124138 QCString formLabel;
4113- int id = FormulaManager::instance ().addFormula (yyextra->formulaText .str ());
4139+ int cnt = 0 ;
4140+ QCString stripForm = stripLeadingAndTrailingEmptyLines (yyextra->formulaText ,cnt);
4141+ if (yyextra->formulaText .stripWhiteSpace (true ).endsWith (" \n " )) stripForm += " \n " ;
4142+ int id = FormulaManager::instance ().addFormula ((yyextra->formulaPreText + stripForm + yyextra->formulaPostText ).str ());
41144143 formLabel.sprintf (" \\ _form#%d" ,id);
41154144 for (int i=0 ;i<yyextra->formulaNewLines ;i++) formLabel+=" @_fakenl" ; // add fake newlines to
41164145 // keep the warnings
0 commit comments