1 /******************************************************************************
3 * Copyright (C) 1997-2014 by Dimitri van Heesch.
5 * Permission to use, copy, modify, and distribute this software and its
6 * documentation under the terms of the GNU General Public License is hereby
7 * granted. No representations are made about the suitability of this software
8 * for any purpose. It is provided "as is" without express or implied warranty.
9 * See the GNU General Public License for more details.
11 * Documents produced by Doxygen are derivative works derived from the
12 * input used in their production; they are not affected by this license.
16 /* Note: part of the code below is inspired by libupskirt written by
17 * Natacha Porté. Original copyright message follows:
19 * Copyright (c) 2008, Natacha Porté
21 * Permission to use, copy, modify, and distribute this software for any
22 * purpose with or without fee is hereby granted, provided that the above
23 * copyright notice and this permission notice appear in all copies.
25 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
26 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
28 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
29 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
30 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
31 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
37 #include <qfileinfo.h>
45 #include "commentscan.h"
48 #include "commentcnv.h"
55 // is character at position i in data part of an identifier?
57 ((data[i]>='a' && data[i]<='z') || \
58 (data[i]>='A' && data[i]<='Z') || \
59 (data[i]>='0' && data[i]<='9')) \
61 // is character at position i in data allowed before an emphasis section
62 #define isOpenEmphChar(i) \
63 (data[i]=='\n' || data[i]==' ' || data[i]=='\'' || data[i]=='<' || \
64 data[i]=='{' || data[i]=='(' || data[i]=='[' || data[i]==',' || \
65 data[i]==':' || data[i]==';')
67 // is character at position i in data an escape that prevents ending an emphasis section
68 // so for example *bla (*.txt) is cool*
69 #define ignoreCloseEmphChar(i) \
70 (data[i]=='(' || data[i]=='{' || data[i]=='[' || data[i]=='<' || \
71 data[i]=='=' || data[i]=='+' || data[i]=='-' || data[i]=='\\' || \
78 LinkRef(const QCString &l,const QCString &t) : link(l), title(t) {}
83 typedef int (*action_t)(GrowBuf &out,const char *data,int offset,int size);
85 enum Alignment { AlignNone, AlignLeft, AlignCenter, AlignRight };
90 static QDict<LinkRef> g_linkRefs(257);
91 static action_t g_actions[256];
92 static Entry *g_current;
93 static QCString g_fileName;
96 // In case a markdown page starts with a level1 header, that header is used
97 // as a title of the page, in effect making it a level0 header, so the
98 // level of all other sections needs to be corrected as well.
99 // This flag is TRUE if corrections are needed.
100 //static bool g_correctSectionLevel;
105 const int codeBlockIndent = 4;
107 static void processInline(GrowBuf &out,const char *data,int size);
109 // escape characters that have a special meaning later on.
110 static QCString escapeSpecialChars(const QCString &s)
112 if (s.isEmpty()) return "";
120 case '<': growBuf.addStr("\\<"); break;
121 case '>': growBuf.addStr("\\>"); break;
122 case '\\': growBuf.addStr("\\\\"); break;
123 case '@': growBuf.addStr("\\@"); break;
124 default: growBuf.addChar(c); break;
128 return growBuf.get();
131 static void convertStringFragment(QCString &result,const char *data,int size)
134 result.resize(size+1);
135 memcpy(result.data(),data,size);
136 result.at(size)='\0';
139 /** helper function to convert presence of left and/or right alignment markers
140 * to a alignment value
142 static Alignment markersToAlignment(bool leftMarker,bool rightMarker)
144 //printf("markerToAlignment(%d,%d)\n",leftMarker,rightMarker);
145 if (leftMarker && rightMarker)
153 else if (rightMarker)
164 // Check if data contains a block command. If so returned the command
165 // that ends the block. If not an empty string is returned.
166 // Note When offset>0 character position -1 will be inspected.
168 // Checks for and skip the following block commands:
169 // {@code .. { .. } .. }
176 // \verbatim..\endverbatim
177 // \latexonly..\endlatexonly
178 // \htmlonly..\endhtmlonly
179 // \xmlonly..\endxmlonly
180 // \rtfonly..\endrtfonly
181 // \manonly..\endmanonly
182 static QCString isBlockCommand(const char *data,int offset,int size)
184 bool openBracket = offset>0 && data[-1]=='{';
185 bool isEscaped = offset>0 && (data[-1]=='\\' || data[-1]=='@');
186 if (isEscaped) return QCString();
189 while (end<size && (data[end]>='a' && data[end]<='z')) end++;
190 if (end==1) return QCString();
192 convertStringFragment(blockName,data+1,end-1);
193 if (blockName=="code" && openBracket)
197 else if (blockName=="dot" ||
200 blockName=="verbatim" ||
201 blockName=="latexonly" ||
202 blockName=="htmlonly" ||
203 blockName=="xmlonly" ||
204 blockName=="rtfonly" ||
205 blockName=="manonly" ||
206 blockName=="docbookonly"
209 return "end"+blockName;
211 else if (blockName=="f" && end<size)
217 else if (data[end]=='[')
221 else if (data[end]=='}')
229 /** looks for the next emph char, skipping other constructs, and
230 * stopping when either it is found, or we are at the end of a paragraph.
232 static int findEmphasisChar(const char *data, int size, char c, int c_size)
238 while (i<size && data[i]!=c && data[i]!='`' &&
239 data[i]!='\\' && data[i]!='@' &&
241 //printf("findEmphasisChar: data=[%s] i=%d c=%c\n",data,i,data[i]);
243 // not counting escaped chars or characters that are unlikely
244 // to appear as the end of the emphasis char
245 if (i>0 && ignoreCloseEmphChar(i-1))
252 // get length of emphasis token
254 while (i+len<size && data[i+len]==c)
261 if (len!=c_size || (i<size-len && isIdChar(i+len))) // to prevent touching some_underscore_identifier
266 return i; // found it
270 // skipping a code span
274 while (i<size && data[i]=='`') snb++,i++;
276 // find same pattern to end the span
278 while (i<size && enb<snb)
280 if (data[i]=='`') enb++;
281 if (snb==1 && data[i]=='\'') break; // ` ended by '
285 else if (data[i]=='@' || data[i]=='\\')
286 { // skip over blocks that should not be processed
287 QCString endBlockName = isBlockCommand(data+i,i,size-i);
288 if (!endBlockName.isEmpty())
291 int l = endBlockName.length();
294 if ((data[i]=='\\' || data[i]=='@') && // command
295 data[i-1]!='\\' && data[i-1]!='@') // not escaped
297 if (qstrncmp(&data[i+1],endBlockName,l)==0)
305 else if (i<size-1 && isIdChar(i+1)) // @cmd, stop processing, see bug 690385
314 else if (data[i]=='\n') // end * or _ at paragraph boundary
317 while (i<size && data[i]==' ') i++;
318 if (i>=size || data[i]=='\n') return 0; // empty line -> paragraph
320 else // should not get here!
329 /** process single emphasis */
330 static int processEmphasis1(GrowBuf &out, const char *data, int size, char c)
334 /* skipping one symbol if coming from emph3 */
335 if (size>1 && data[0]==c && data[1]==c) { i=1; }
339 len = findEmphasisChar(data+i, size-i, c, 1);
340 if (len==0) return 0;
342 if (i>=size) return 0;
344 if (i+1<size && data[i+1]==c)
349 if (data[i]==c && data[i-1]!=' ' && data[i-1]!='\n')
352 processInline(out,data,i);
360 /** process double emphasis */
361 static int processEmphasis2(GrowBuf &out, const char *data, int size, char c)
367 len = findEmphasisChar(data+i, size-i, c, 2);
373 if (i+1<size && data[i]==c && data[i+1]==c && i && data[i-1]!=' ' &&
377 out.addStr("<strong>");
378 processInline(out,data,i);
379 out.addStr("</strong>");
387 /** Parsing tripple emphasis.
388 * Finds the first closing tag, and delegates to the other emph
390 static int processEmphasis3(GrowBuf &out, const char *data, int size, char c)
396 len = findEmphasisChar(data+i, size-i, c, 3);
403 /* skip whitespace preceded symbols */
404 if (data[i]!=c || data[i-1]==' ' || data[i-1]=='\n')
409 if (i+2<size && data[i+1]==c && data[i+2]==c)
411 out.addStr("<em><strong>");
412 processInline(out,data,i);
413 out.addStr("</strong></em>");
416 else if (i+1<size && data[i+1]==c)
418 // double symbol found, handing over to emph1
419 len = processEmphasis1(out, data-2, size+2, c);
431 // single symbol found, handing over to emph2
432 len = processEmphasis2(out, data-1, size+1, c);
446 /** Process ndash and mdashes */
447 static int processNmdash(GrowBuf &out,const char *data,int off,int size)
449 // precondition: data[0]=='-'
452 if (i<size && data[i]=='-') // found --
456 if (i<size && data[i]=='-') // found ---
460 if (i<size && data[i]=='-') // found ----
464 if (count==2 && (off<8 || qstrncmp(data-8,"operator",8)!=0)) // -- => ndash
466 out.addStr("–");
469 else if (count==3) // --- => ndash
471 out.addStr("—");
474 // not an ndash or mdash
478 /** Process quoted section "...", can contain one embedded newline */
479 static int processQuoted(GrowBuf &out,const char *data,int,int size)
483 while (i<size && data[i]!='"' && nl<2)
485 if (data[i]=='\n') nl++;
488 if (i<size && data[i]=='"' && nl<2)
490 out.addStr(data,i+1);
493 // not a quoted section
497 /** Process a HTML tag. Note that <pre>..</pre> are treated specially, in
498 * the sense that all code inside is written unprocessed
500 static int processHtmlTag(GrowBuf &out,const char *data,int offset,int size)
502 if (offset>0 && data[-1]=='\\') return 0; // escaped <
504 // find the end of the html tag
507 // compute length of the tag name
508 while (i<size && isIdChar(i)) i++,l++;
510 convertStringFragment(tagName,data+1,i-1);
511 if (tagName.lower()=="pre") // found <pre> tag
513 bool insideStr=FALSE;
517 if (!insideStr && c=='<') // potential start of html tag
519 if (data[i+1]=='/' &&
520 tolower(data[i+2])=='p' && tolower(data[i+3])=='r' &&
521 tolower(data[i+4])=='e' && tolower(data[i+5])=='>')
522 { // found </pre> tag, copy from start to end of tag
523 out.addStr(data,i+6);
524 //printf("found <pre>..</pre> [%d..%d]\n",0,i+6);
528 else if (insideStr && c=='"')
530 if (data[i-1]!='\\') insideStr=FALSE;
539 else // some other html tag
543 if (data[i]=='/' && i<size-1 && data[i+1]=='>') // <bla/>
545 //printf("Found htmlTag={%s}\n",QCString(data).left(i+2).data());
546 out.addStr(data,i+2);
549 else if (data[i]=='>') // <bla>
551 //printf("Found htmlTag={%s}\n",QCString(data).left(i+1).data());
552 out.addStr(data,i+1);
555 else if (data[i]==' ') // <bla attr=...
558 bool insideAttr=FALSE;
561 if (!insideAttr && data[i]=='"')
565 else if (data[i]=='"' && data[i-1]!='\\')
569 else if (!insideAttr && data[i]=='>') // found end of tag
571 //printf("Found htmlTag={%s}\n",QCString(data).left(i+1).data());
572 out.addStr(data,i+1);
580 //printf("Not a valid html tag\n");
584 static int processEmphasis(GrowBuf &out,const char *data,int offset,int size)
586 if ((offset>0 && !isOpenEmphChar(-1)) || // invalid char before * or _
587 (size>1 && data[0]!=data[1] && !isIdChar(1)) || // invalid char after * or _
588 (size>2 && data[0]==data[1] && !isIdChar(2))) // invalid char after ** or __
595 if (size>2 && data[1]!=c) // _bla or *bla
597 // whitespace cannot follow an opening emphasis
598 if (data[1]==' ' || data[1]=='\n' ||
599 (ret = processEmphasis1(out, data+1, size-1, c)) == 0)
605 if (size>3 && data[1]==c && data[2]!=c) // __bla or **bla
607 if (data[2]==' ' || data[2]=='\n' ||
608 (ret = processEmphasis2(out, data+2, size-2, c)) == 0)
614 if (size>4 && data[1]==c && data[2]==c && data[3]!=c) // ___bla or ***bla
616 if (data[3]==' ' || data[3]=='\n' ||
617 (ret = processEmphasis3(out, data+3, size-3, c)) == 0)
626 static int processLink(GrowBuf &out,const char *data,int,int size)
631 int contentStart,contentEnd,linkStart,titleStart,titleEnd;
632 bool isImageLink = FALSE;
638 if (size<2 || data[1]!='[')
647 // find the matching ]
650 if (data[i-1]=='\\') // skip escaped characters
653 else if (data[i]=='[')
657 else if (data[i]==']')
662 else if (data[i]=='\n')
665 if (nl>1) return 0; // only allow one newline in the content
669 if (i>=size) return 0; // premature end of comment -> no link
671 convertStringFragment(content,data+contentStart,contentEnd-contentStart);
672 //printf("processLink: content={%s}\n",content.data());
673 if (!isImageLink && content.isEmpty()) return 0; // no link text
677 while (i<size && data[i]==' ') i++;
678 if (i<size && data[i]=='\n') // one newline allowed here
681 // skip more whitespace
682 while (i<size && data[i]==' ') i++;
685 bool explicitTitle=FALSE;
686 if (i<size && data[i]=='(') // inline link
689 while (i<size && data[i]==' ') i++;
690 if (i<size && data[i]=='<') i++;
693 while (i<size && data[i]!='\'' && data[i]!='"' && data[i]!=')')
702 if (i>=size || data[i]=='\n') return 0;
703 convertStringFragment(link,data+linkStart,i-linkStart);
704 link = link.stripWhiteSpace();
705 //printf("processLink: link={%s}\n",link.data());
706 if (link.isEmpty()) return 0;
707 if (link.at(link.length()-1)=='>') link=link.left(link.length()-1);
710 if (data[i]=='\'' || data[i]=='"')
716 while (i<size && data[i]!=')')
730 // search back for closing marker
731 while (titleEnd>titleStart && data[titleEnd]==' ') titleEnd--;
732 if (data[titleEnd]==c) // found it
734 convertStringFragment(title,data+titleStart,titleEnd-titleStart);
735 //printf("processLink: title={%s}\n",title.data());
744 else if (i<size && data[i]=='[') // reference link
750 while (i<size && data[i]!=']')
759 if (i>=size) return 0;
761 convertStringFragment(link,data+linkStart,i-linkStart);
762 //printf("processLink: link={%s}\n",link.data());
763 link = link.stripWhiteSpace();
764 if (link.isEmpty()) // shortcut link
769 LinkRef *lr = g_linkRefs.find(link.lower());
774 //printf("processLink: ref: link={%s} title={%s}\n",link.data(),title.data());
776 else // reference not found!
778 //printf("processLink: ref {%s} do not exist\n",link.lower().data());
783 else if (i<size && data[i]!=':' && !content.isEmpty()) // minimal link ref notation [some id]
785 LinkRef *lr = g_linkRefs.find(content.lower());
786 //printf("processLink: minimal link {%s} lr=%p",content.data(),lr);
794 else if (content=="TOC")
809 static QRegExp re("^[@\\]ref ");
810 if (isToc) // special case for [TOC]
812 if (g_current) g_current->stat=TRUE;
814 else if (isImageLink)
818 if (link.find("@ref ")!=-1 || link.find("\\ref ")!=-1 ||
819 (fd=findFileDef(Doxygen::imageNameDict,link,ambig)))
820 // assume doxygen symbol link or local image link
822 out.addStr("@image html ");
823 out.addStr(link.mid(fd ? 0 : 5));
824 if (!explicitTitle && !content.isEmpty())
830 else if ((content.isEmpty() || explicitTitle) && !title.isEmpty())
839 out.addStr("<img src=\"");
841 out.addStr("\" alt=\"");
844 if (!title.isEmpty())
846 out.addStr(" title=\"");
847 out.addStr(substitute(title.simplifyWhiteSpace(),"\"","""));
855 SrcLangExt lang = getLanguageFromFileName(link);
857 if ((lp=link.find("@ref "))!=-1 || (lp=link.find("\\ref "))!=-1 || lang==SrcLangExt_Markdown)
858 // assume doxygen symbol link
860 if (lp==-1) // link to markdown page
866 if (explicitTitle && !title.isEmpty())
876 else if (link.find('/')!=-1 || link.find('.')!=-1 || link.find('#')!=-1)
878 out.addStr("<a href=\"");
881 if (!title.isEmpty())
883 out.addStr(" title=\"");
884 out.addStr(substitute(title.simplifyWhiteSpace(),"\"","""));
888 out.addStr(content.simplifyWhiteSpace());
891 else // avoid link to e.g. F[x](y)
893 //printf("no link for '%s'\n",link.data());
900 /** '`' parsing a code span (assuming codespan != 0) */
901 static int processCodeSpan(GrowBuf &out, const char *data, int /*offset*/, int size)
903 int end, nb = 0, i, f_begin, f_end;
905 /* counting the number of backticks in the delimiter */
906 while (nb<size && data[nb]=='`')
911 /* finding the next delimiter */
914 for (end=nb; end<size && i<nb && nl<2; end++)
920 else if (data[end]=='\n')
930 if (i < nb && end >= size)
932 return 0; // no matching delimiter
934 if (nl==2) // too many newlines inside the span
939 // trimming outside whitespaces
941 while (f_begin < end && data[f_begin]==' ')
946 while (f_end > nb && data[f_end-1]==' ')
951 if (nb==1) // check for closing ' followed by space within f_begin..f_end
956 if (data[i]=='\'' && !isIdChar(i+1)) // reject `some word' and not `it's cool`
963 //printf("found code span '%s'\n",QCString(data+f_begin).left(f_end-f_begin).data());
968 QCString codeFragment;
969 convertStringFragment(codeFragment,data+f_begin,f_end-f_begin);
971 //out.addStr(convertToHtml(codeFragment,TRUE));
972 out.addStr(escapeSpecialChars(codeFragment));
979 static int processSpecialCommand(GrowBuf &out, const char *data, int offset, int size)
982 QCString endBlockName = isBlockCommand(data,offset,size);
983 if (!endBlockName.isEmpty())
985 int l = endBlockName.length();
988 if ((data[i]=='\\' || data[i]=='@') && // command
989 data[i-1]!='\\' && data[i-1]!='@') // not escaped
991 if (qstrncmp(&data[i+1],endBlockName,l)==0)
993 //printf("found end at %d\n",i);
994 out.addStr(data,i+1+l);
1001 if (size>1 && data[0]=='\\')
1004 if (c=='[' || c==']' || c=='*' || c=='+' || c=='-' ||
1005 c=='!' || c=='(' || c==')' || c=='.' || c=='`' || c=='_')
1007 if (c=='-' && size>3 && data[2]=='-' && data[3]=='-') // \---
1009 out.addStr(&data[1],3);
1012 else if (c=='-' && size>2 && data[2]=='-') // \--
1014 out.addStr(&data[1],2);
1017 out.addStr(&data[1],1);
1024 static void processInline(GrowBuf &out,const char *data,int size)
1027 action_t action = 0;
1030 while (end<size && ((action=g_actions[(uchar)data[end]])==0)) end++;
1031 out.addStr(data+i,end-i);
1032 if (end>=size) break;
1034 end = action(out,data+i,i,size-i);
1047 /** returns whether the line is a setext-style hdr underline */
1048 static int isHeaderline(const char *data, int size)
1051 while (i<size && data[i]==' ') i++;
1053 // test of level 1 header
1056 while (i<size && data[i]=='=') i++,c++;
1057 while (i<size && data[i]==' ') i++;
1058 return (c>1 && (i>=size || data[i]=='\n')) ? 1 : 0;
1060 // test of level 2 header
1063 while (i<size && data[i]=='-') i++,c++;
1064 while (i<size && data[i]==' ') i++;
1065 return (c>1 && (i>=size || data[i]=='\n')) ? 2 : 0;
1070 /** returns TRUE if this line starts a block quote */
1071 static bool isBlockQuote(const char *data,int size,int indent)
1074 while (i<size && data[i]==' ') i++;
1075 if (i<indent+codeBlockIndent) // could be a quotation
1077 // count >'s and skip spaces
1079 while (i<size && (data[i]=='>' || data[i]==' '))
1081 if (data[i]=='>') level++;
1084 // last characters should be a space or newline,
1085 // so a line starting with >= does not match
1086 return level>0 && i<size && ((data[i-1]==' ') || data[i]=='\n');
1088 else // too much indentation -> code block
1092 //return i<size && data[i]=='>' && i<indent+codeBlockIndent;
1095 /** returns end of the link ref if this is indeed a link reference. */
1096 static int isLinkRef(const char *data,int size,
1097 QCString &refid,QCString &link,QCString &title)
1099 //printf("isLinkRef data={%s}\n",data);
1100 // format: start with [some text]:
1102 while (i<size && data[i]==' ') i++;
1103 if (i>=size || data[i]!='[') return 0;
1106 while (i<size && data[i]!='\n' && data[i]!=']') i++;
1107 if (i>=size || data[i]!=']') return 0;
1108 convertStringFragment(refid,data+refIdStart,i-refIdStart);
1109 if (refid.isEmpty()) return 0;
1110 //printf(" isLinkRef: found refid='%s'\n",refid.data());
1112 if (i>=size || data[i]!=':') return 0;
1115 // format: whitespace* \n? whitespace* (<url> | url)
1116 while (i<size && data[i]==' ') i++;
1117 if (i<size && data[i]=='\n')
1120 while (i<size && data[i]==' ') i++;
1122 if (i>=size) return 0;
1124 if (i<size && data[i]=='<') i++;
1126 while (i<size && data[i]!=' ' && data[i]!='\n') i++;
1128 if (i<size && data[i]=='>') i++;
1129 if (linkStart==linkEnd) return 0; // empty link
1130 convertStringFragment(link,data+linkStart,linkEnd-linkStart);
1131 //printf(" isLinkRef: found link='%s'\n",link.data());
1132 if (link=="@ref" || link=="\\ref")
1135 while (i<size && data[i]!='\n' && data[i]!='"') i++;
1137 convertStringFragment(refArg,data+argStart,i-argStart);
1143 // format: (whitespace* \n? whitespace* ( 'title' | "title" | (title) ))?
1145 while (i<size && data[i]==' ') i++;
1146 if (i<size && data[i]=='\n')
1150 while (i<size && data[i]==' ') i++;
1154 //printf("end of isLinkRef while looking for title! i=%d\n",i);
1155 return i; // end of buffer while looking for the optional title
1159 if (c=='\'' || c=='"' || c=='(') // optional title present?
1161 //printf(" start of title found! char='%c'\n",c);
1163 if (c=='(') c=')'; // replace c by end character
1165 // search for end of the line
1166 while (i<size && data[i]!='\n') i++;
1169 // search back to matching character
1171 while (end>titleStart && data[end]!=c) end--;
1174 convertStringFragment(title,data+titleStart,end-titleStart);
1176 //printf(" title found: '%s'\n",title.data());
1178 while (i<size && data[i]==' ') i++;
1179 //printf("end of isLinkRef: i=%d size=%d data[i]='%c' eol=%d\n",
1180 // i,size,data[i],eol);
1181 if (i>=size) return i; // end of buffer while ref id was found
1182 else if (eol) return eol; // end of line while ref id was found
1183 return 0; // invalid link ref
1186 static int isHRuler(const char *data,int size)
1189 if (size>0 && data[size-1]=='\n') size--; // ignore newline character
1190 while (i<size && data[i]==' ') i++;
1191 if (i>=size) return 0; // empty line
1193 if (c!='*' && c!='-' && c!='_')
1195 return 0; // not a hrule character
1202 n++; // count rule character
1204 else if (data[i]!=' ')
1206 return 0; // line contains non hruler characters
1210 return n>=3; // at least 3 characters needed for a hruler
1213 static QCString extractTitleId(QCString &title)
1215 //static QRegExp r1("^[a-z_A-Z][a-z_A-Z0-9\\-]*:");
1216 static QRegExp r2("\\{#[a-z_A-Z][a-z_A-Z0-9\\-]*\\}");
1218 int i = r2.match(title,0,&l);
1219 if (i!=-1 && title.mid(i+l).stripWhiteSpace().isEmpty()) // found {#id} style id
1221 QCString id = title.mid(i+2,l-3);
1222 title = title.left(i);
1223 //printf("found id='%s' title='%s'\n",id.data(),title.data());
1226 //printf("no id found in title '%s'\n",title.data());
1231 static int isAtxHeader(const char *data,int size,
1232 QCString &header,QCString &id)
1235 int level = 0, blanks=0;
1237 // find start of header text and determine heading level
1238 while (i<size && data[i]==' ') i++;
1239 if (i>=size || data[i]!='#')
1243 while (i<size && level<6 && data[i]=='#') i++,level++;
1244 while (i<size && data[i]==' ') i++,blanks++;
1245 if (level==1 && blanks==0)
1247 return 0; // special case to prevent #someid seen as a header (see bug 671395)
1250 // find end of header text
1252 while (end<size && data[end]!='\n') end++;
1253 while (end>i && (data[end-1]=='#' || data[end-1]==' ')) end--;
1256 convertStringFragment(header,data+i,end-i);
1257 id = extractTitleId(header);
1258 if (!id.isEmpty()) // strip #'s between title and id
1260 i=header.length()-1;
1261 while (i>=0 && (header.at(i)=='#' || header.at(i)==' ')) i--;
1262 header=header.left(i+1);
1268 static int isEmptyLine(const char *data,int size)
1273 if (data[i]=='\n') return TRUE;
1274 if (data[i]!=' ') return FALSE;
1280 #define isLiTag(i) \
1281 (data[(i)]=='<' && \
1282 (data[(i)+1]=='l' || data[(i)+1]=='L') && \
1283 (data[(i)+2]=='i' || data[(i)+2]=='I') && \
1286 // compute the indent from the start of the input, excluding list markers
1287 // such as -, -#, *, +, 1., and <li>
1288 static int computeIndentExcludingListMarkers(const char *data,int size)
1294 bool listMarkerSkipped=FALSE;
1296 (data[i]==' ' || // space
1297 (!listMarkerSkipped && // first list marker
1298 (data[i]=='+' || data[i]=='-' || data[i]=='*' || // unordered list char
1299 (data[i]=='#' && i>0 && data[i-1]=='-') || // -# item
1300 (isDigit=(data[i]>='1' && data[i]<='9')) || // ordered list marker?
1301 (isLi=(i<size-3 && isLiTag(i))) // <li> tag
1307 if (isDigit) // skip over ordered list marker '10. '
1310 while (j<size && ((data[j]>='0' && data[j]<='9') || data[j]=='.'))
1312 if (data[j]=='.') // should be end of the list marker
1314 if (j<size-1 && data[j+1]==' ') // valid list marker
1316 listMarkerSkipped=TRUE;
1321 else // not a list marker
1331 i+=3; // skip over <li>
1333 listMarkerSkipped=TRUE;
1335 else if (data[i]=='-' && i<size-2 && data[i+1]=='#' && data[i+2]==' ')
1337 listMarkerSkipped=TRUE; // only a single list marker is accepted
1341 else if (data[i]!=' ' && i<size-1 && data[i+1]==' ')
1342 { // case "- " or "+ " or "* "
1343 listMarkerSkipped=TRUE; // only a single list marker is accepted
1345 if (data[i]!=' ' && !listMarkerSkipped)
1351 //printf("{%s}->%d\n",QCString(data).left(size).data(),indent);
1355 static bool isFencedCodeBlock(const char *data,int size,int refIndent,
1356 QCString &lang,int &start,int &end,int &offset)
1358 // rules: at least 3 ~~~, end of the block same amount of ~~~'s, otherwise
1363 while (i<size && data[i]==' ') indent++,i++;
1364 if (indent>=refIndent+4) return FALSE; // part of code block
1365 while (i<size && data[i]=='~') startTildes++,i++;
1366 if (startTildes<3) return FALSE; // not enough tildes
1367 if (i<size && data[i]=='{') i++; // skip over optional {
1369 while (i<size && (data[i]!='\n' && data[i]!='}' && data[i]!=' ')) i++;
1370 convertStringFragment(lang,data+startLang,i-startLang);
1371 while (i<size && data[i]!='\n') i++; // proceed to the end of the line
1379 while (i<size && data[i]=='~') endTildes++,i++;
1380 while (i<size && data[i]==' ') i++;
1381 if (i==size || data[i]=='\n')
1384 return endTildes==startTildes;
1392 static bool isCodeBlock(const char *data,int offset,int size,int &indent)
1394 //printf("<isCodeBlock(offset=%d,size=%d,indent=%d)\n",offset,size,indent);
1395 // determine the indent of this line
1398 while (i<size && data[i]==' ') indent0++,i++;
1400 if (indent0<codeBlockIndent)
1402 //printf(">isCodeBlock: line is not indented enough %d<4\n",indent0);
1405 if (indent0>=size || data[indent0]=='\n') // empty line does not start a code block
1407 //printf("only spaces at the end of a comment block\n");
1414 // search back 3 lines and remember the start of lines -1 and -2
1417 if (data[i-offset-1]=='\n') nl_pos[nl++]=i-offset;
1421 // if there are only 2 preceding lines, then line -2 starts at -offset
1422 if (i==0 && nl==2) nl_pos[nl++]=-offset;
1423 //printf(" nl=%d\n",nl);
1425 if (nl==3) // we have at least 2 preceding lines
1427 //printf(" positions: nl_pos=[%d,%d,%d] line[-2]='%s' line[-1]='%s'\n",
1428 // nl_pos[0],nl_pos[1],nl_pos[2],
1429 // QCString(data+nl_pos[1]).left(nl_pos[0]-nl_pos[1]-1).data(),
1430 // QCString(data+nl_pos[2]).left(nl_pos[1]-nl_pos[2]-1).data());
1432 // check that line -1 is empty
1433 if (!isEmptyLine(data+nl_pos[1],nl_pos[0]-nl_pos[1]-1))
1438 // determine the indent of line -2
1439 indent=computeIndentExcludingListMarkers(data+nl_pos[2],nl_pos[1]-nl_pos[2]);
1441 //printf(">isCodeBlock local_indent %d>=%d+4=%d\n",
1442 // indent0,indent2,indent0>=indent2+4);
1443 // if the difference is >4 spaces -> code block
1444 return indent0>=indent+codeBlockIndent;
1446 else // not enough lines to determine the relative indent, use global indent
1448 // check that line -1 is empty
1449 if (nl==1 && !isEmptyLine(data-offset,offset-1))
1453 //printf(">isCodeBlock global indent %d>=%d+4=%d nl=%d\n",
1454 // indent0,indent,indent0>=indent+4,nl);
1455 return indent0>=indent+codeBlockIndent;
1459 /** Finds the location of the table's contains in the string \a data.
1460 * Only one line will be inspected.
1461 * @param[in] data pointer to the string buffer.
1462 * @param[in] size the size of the buffer.
1463 * @param[out] start offset of the first character of the table content
1464 * @param[out] end offset of the last character of the table content
1465 * @param[out] columns number of table columns found
1466 * @returns The offset until the next line in the buffer.
1468 int findTableColumns(const char *data,int size,int &start,int &end,int &columns)
1472 // find start character of the table line
1473 while (i<size && data[i]==' ') i++;
1474 if (i<size && data[i]=='|' && data[i]!='\n') i++,n++; // leading | does not count
1477 // find end character of the table line
1478 while (i<size && data[i]!='\n') i++;
1481 while (i>0 && data[i]==' ') i--;
1482 if (i>0 && data[i-1]!='\\' && data[i]=='|') i--,n++; // trailing or escaped | does not count
1485 // count columns between start and end
1490 while (i<=end) // look for more column markers
1492 if (data[i]=='|' && (i==0 || data[i-1]!='\\')) columns++;
1493 if (columns==1) columns++; // first | make a non-table into a two column table
1497 if (n==2 && columns==0) // table row has | ... |
1501 //printf("findTableColumns(start=%d,end=%d,columns=%d) eol=%d\n",
1502 // start,end,columns,eol);
1506 /** Returns TRUE iff data points to the start of a table block */
1507 static bool isTableBlock(const char *data,int size)
1511 // the first line should have at least two columns separated by '|'
1512 int i = findTableColumns(data,size,start,end,cc0);
1513 if (i>=size || cc0<1)
1515 //printf("isTableBlock: no |'s in the header\n");
1520 int ret = findTableColumns(data+i,size-i,start,end,cc1);
1522 // separator line should consist of |, - and : and spaces only
1525 if (data[j]!=':' && data[j]!='-' && data[j]!='|' && data[j]!=' ')
1527 //printf("isTableBlock: invalid character '%c'\n",data[j]);
1528 return FALSE; // invalid characters in table separator
1532 if (cc1!=cc0) // number of columns should be same as previous line
1537 i+=ret; // goto next line
1539 ret = findTableColumns(data+i,size-i,start,end,cc2);
1541 //printf("isTableBlock: %d\n",cc1==cc2);
1545 static int writeTableBlock(GrowBuf &out,const char *data,int size)
1548 int columns,start,end,cc;
1550 i = findTableColumns(data,size,start,end,columns);
1552 out.addStr("<table>");
1554 // write table header, in range [start..end]
1557 int headerStart = start;
1558 int headerEnd = end;
1560 // read cell alignments
1561 int ret = findTableColumns(data+i,size-i,start,end,cc);
1563 Alignment *columnAlignment = new Alignment[columns];
1565 bool leftMarker=FALSE,rightMarker=FALSE;
1566 bool startFound=FALSE;
1572 if (data[j]==':') { leftMarker=TRUE; startFound=TRUE; }
1573 if (data[j]=='-') startFound=TRUE;
1574 //printf(" data[%d]=%c startFound=%d\n",j,data[j],startFound);
1576 if (data[j]=='-') rightMarker=FALSE;
1577 else if (data[j]==':') rightMarker=TRUE;
1578 if (j<=end+i && (data[j]=='|' && (j==0 || data[j-1]!='\\')))
1582 columnAlignment[k] = markersToAlignment(leftMarker,rightMarker);
1583 //printf("column[%d] alignment=%d\n",k,columnAlignment[k]);
1594 columnAlignment[k] = markersToAlignment(leftMarker,rightMarker);
1595 //printf("column[%d] alignment=%d\n",k,columnAlignment[k]);
1597 // proceed to next line
1601 for (k=0;k<columns;k++)
1604 switch (columnAlignment[k])
1606 case AlignLeft: out.addStr(" align=\"left\""); break;
1607 case AlignRight: out.addStr(" align=\"right\""); break;
1608 case AlignCenter: out.addStr(" align=\"center\""); break;
1609 case AlignNone: break;
1612 while (m<=headerEnd && (data[m]!='|' || (m>0 && data[m-1]=='\\')))
1614 out.addChar(data[m++]);
1618 out.addStr("\n</th>\n");
1620 // write table cells
1623 int ret = findTableColumns(data+i,size-i,start,end,cc);
1624 //printf("findTableColumns cc=%d\n",cc);
1625 if (cc!=columns) break; // end of table
1636 switch (columnAlignment[k])
1638 case AlignLeft: out.addStr(" align=\"left\""); break;
1639 case AlignRight: out.addStr(" align=\"right\""); break;
1640 case AlignCenter: out.addStr(" align=\"center\""); break;
1641 case AlignNone: break;
1645 if (j<=end+i && (data[j]=='|' && (j==0 || data[j-1]!='\\')))
1652 out.addChar(data[j]);
1658 // proceed to next line
1662 out.addStr("</table> ");
1664 delete[] columnAlignment;
1669 void writeOneLineHeaderOrRuler(GrowBuf &out,const char *data,int size)
1674 if (isHRuler(data,size))
1676 out.addStr("<hr>\n");
1678 else if ((level=isAtxHeader(data,size,header,id)))
1680 //if (level==1) g_correctSectionLevel=FALSE;
1681 //if (g_correctSectionLevel) level--;
1683 if (level<5 && !id.isEmpty())
1685 SectionInfo::SectionType type = SectionInfo::Anchor;
1688 case 1: out.addStr("@section ");
1689 type=SectionInfo::Section;
1691 case 2: out.addStr("@subsection ");
1692 type=SectionInfo::Subsection;
1694 case 3: out.addStr("@subsubsection ");
1695 type=SectionInfo::Subsubsection;
1697 default: out.addStr("@paragraph ");
1698 type=SectionInfo::Paragraph;
1705 SectionInfo *si = Doxygen::sectionDict->find(header);
1708 if (si->lineNr != -1)
1710 warn(g_fileName,g_lineNr,"multiple use of section label '%s', (first occurrence: %s, line %d)",header.data(),si->fileName.data(),si->lineNr);
1714 warn(g_fileName,g_lineNr,"multiple use of section label '%s', (first occurrence: %s)",header.data(),si->fileName.data());
1719 si = new SectionInfo(g_fileName,g_lineNr,id,header,type,level);
1722 g_current->anchors->append(si);
1724 Doxygen::sectionDict->append(header,si);
1731 out.addStr("\\anchor "+id+"\n");
1733 hTag.sprintf("h%d",level);
1734 out.addStr("<"+hTag+">");
1736 out.addStr("</"+hTag+">\n");
1739 else // nothing interesting -> just output the line
1741 out.addStr(data,size);
1745 static int writeBlockQuote(GrowBuf &out,const char *data,int size)
1753 // find end of this line
1755 while (end<=size && data[end-1]!='\n') end++;
1759 // compute the quoting level
1760 while (j<end && (data[j]==' ' || data[j]=='>'))
1762 if (data[j]=='>') { level++; indent=j+1; }
1763 else if (j>0 && data[j-1]=='>') indent=j+1;
1766 if (j>0 && data[j-1]=='>' &&
1767 !(j==size || data[j]=='\n')) // disqualify last > if not followed by space
1772 if (level>curLevel) // quote level increased => add start markers
1774 for (l=curLevel;l<level;l++)
1776 out.addStr("<blockquote>\n");
1779 else if (level<curLevel) // quote level descreased => add end markers
1781 for (l=level;l<curLevel;l++)
1783 out.addStr("</blockquote>\n");
1787 if (level==0) break; // end of quote block
1788 // copy line without quotation marks
1789 out.addStr(data+indent,end-indent);
1790 // proceed with next line
1793 // end of comment within blockquote => add end markers
1794 for (l=0;l<curLevel;l++)
1796 out.addStr("</blockquote>\n");
1801 static int writeCodeBlock(GrowBuf &out,const char *data,int size,int refIndent)
1804 //printf("writeCodeBlock: data={%s}\n",QCString(data).left(size).data());
1805 out.addStr("@verbatim\n");
1809 // find end of this line
1811 while (end<=size && data[end-1]!='\n') end++;
1814 while (j<end && data[j]==' ') j++,indent++;
1815 //printf("j=%d end=%d indent=%d refIndent=%d tabSize=%d data={%s}\n",
1816 // j,end,indent,refIndent,Config_getInt("TAB_SIZE"),QCString(data+i).left(end-i-1).data());
1817 if (j==end-1) // empty line
1822 else if (indent>=refIndent+codeBlockIndent) // enough indent to contine the code block
1824 while (emptyLines>0) // write skipped empty lines
1830 // add code line minus the indent
1831 out.addStr(data+i+refIndent+codeBlockIndent,end-i-refIndent-codeBlockIndent);
1834 else // end of code block
1839 out.addStr("@endverbatim\n");
1840 while (emptyLines>0) // write skipped empty lines
1846 //printf("i=%d\n",i);
1850 // start searching for the end of the line start at offset \a i
1851 // keeping track of possible blocks that need to to skipped.
1852 static void findEndOfLine(GrowBuf &out,const char *data,int size,
1853 int &pi,int&i,int &end)
1855 // find end of the line
1858 while (end<=size && data[end-1]!='\n')
1860 // while looking for the end of the line we might encounter a block
1861 // that needs to be passed unprocessed.
1862 if ((data[end-1]=='\\' || data[end-1]=='@') && // command
1863 (end<=1 || (data[end-2]!='\\' && data[end-2]!='@')) // not escaped
1866 QCString endBlockName = isBlockCommand(data+end-1,end-1,size-(end-1));
1868 if (!endBlockName.isEmpty())
1870 int l = endBlockName.length();
1871 for (;end<size-l-1;end++) // search for end of block marker
1873 if ((data[end]=='\\' || data[end]=='@') &&
1874 data[end-1]!='\\' && data[end-1]!='@'
1877 if (qstrncmp(&data[end+1],endBlockName,l)==0)
1879 if (pi!=-1) // output previous line if available
1881 //printf("feol out={%s}\n",QCString(data+pi).left(i-pi).data());
1882 out.addStr(data+pi,i-pi);
1884 // found end marker, skip over this block
1885 //printf("feol.block out={%s}\n",QCString(data+i).left(end+l+1-i).data());
1886 out.addStr(data+i,end+l+1-i);
1888 i=end+l+1; // continue after block
1896 else if (nb==0 && data[end-1]=='<' && end<size-6 &&
1897 (end<=1 || (data[end-2]!='\\' && data[end-2]!='@'))
1900 if (tolower(data[end])=='p' && tolower(data[end+1])=='r' &&
1901 tolower(data[end+2])=='e' && data[end+3]=='>') // <pre> tag
1903 if (pi!=-1) // output previous line if available
1905 out.addStr(data+pi,i-pi);
1907 // output part until <pre>
1908 out.addStr(data+i,end-1-i);
1909 // output part until </pre>
1910 i = end-1 + processHtmlTag(out,data+end-1,end-1,size-end+1);
1920 else if (nb==0 && data[end-1]=='`')
1922 while (end<=size && data[end-1]=='`') end++,nb++;
1924 else if (nb>0 && data[end-1]=='`')
1927 while (end<=size && data[end-1]=='`') end++,enb++;
1935 //printf("findEndOfLine pi=%d i=%d end=%d {%s}\n",pi,i,end,QCString(data+i).left(end-i).data());
1938 static void writeFencedCodeBlock(GrowBuf &out,const char *data,const char *lng,
1939 int blockStart,int blockEnd)
1941 QCString lang = lng;
1942 if (!lang.isEmpty() && lang.at(0)=='.') lang=lang.mid(1);
1943 out.addStr("@code");
1944 if (!lang.isEmpty())
1946 out.addStr("{"+lang+"}");
1948 out.addStr(data+blockStart,blockEnd-blockStart);
1950 out.addStr("@endcode");
1953 static QCString processQuotations(const QCString &s,int refIndent)
1956 const char *data = s.data();
1957 int size = s.length();
1958 int i=0,end=0,pi=-1;
1959 int blockStart,blockEnd,blockOffset;
1963 findEndOfLine(out,data,size,pi,i,end);
1964 // line is now found at [i..end)
1968 if (isFencedCodeBlock(data+pi,size-pi,refIndent,lang,blockStart,blockEnd,blockOffset))
1970 writeFencedCodeBlock(out,data+pi,lang,blockStart,blockEnd);
1976 else if (isBlockQuote(data+pi,i-pi,refIndent))
1978 i = pi+writeBlockQuote(out,data+pi,size-pi);
1985 //printf("quote out={%s}\n",QCString(data+pi).left(i-pi).data());
1986 out.addStr(data+pi,i-pi);
1992 if (pi!=-1 && pi<size) // deal with the last line
1994 if (isBlockQuote(data+pi,size-pi,refIndent))
1996 writeBlockQuote(out,data+pi,size-pi);
2000 out.addStr(data+pi,size-pi);
2005 //printf("Process quotations\n---- input ----\n%s\n---- output ----\n%s\n------------\n",
2006 // s.data(),out.get());
2011 static QCString processBlocks(const QCString &s,int indent)
2014 const char *data = s.data();
2015 int size = s.length();
2016 int i=0,end=0,pi=-1,ref,level;
2017 QCString id,link,title;
2018 int blockIndent = indent;
2020 // get indent for the first line
2023 while (end<=size && data[end-1]!='\n')
2025 if (data[end-1]==' ') sp++;
2029 #if 0 // commented out, since starting with a comment block is probably a usage error
2030 // see also http://stackoverflow.com/q/20478611/784672
2032 // special case when the documentation starts with a code block
2033 // since the first line is skipped when looking for a code block later on.
2034 if (end>codeBlockIndent && isCodeBlock(data,0,end,blockIndent))
2036 i=writeCodeBlock(out,data,size,blockIndent);
2042 // process each line
2045 findEndOfLine(out,data,size,pi,i,end);
2046 // line is now found at [i..end)
2048 //printf("findEndOfLine: pi=%d i=%d end=%d\n",pi,i,end);
2052 int blockStart,blockEnd,blockOffset;
2054 blockIndent = indent;
2055 //printf("isHeaderLine(%s)=%d\n",QCString(data+i).left(size-i).data(),level);
2056 if ((level=isHeaderline(data+i,size-i))>0)
2058 //if (level==1) g_correctSectionLevel=FALSE;
2059 //if (g_correctSectionLevel) level--;
2060 //printf("Found header at %d-%d\n",i,end);
2061 while (pi<size && data[pi]==' ') pi++;
2063 convertStringFragment(header,data+pi,i-pi-1);
2064 id = extractTitleId(header);
2065 //printf("header='%s' is='%s'\n",header.data(),id.data());
2066 if (!header.isEmpty())
2070 out.addStr(level==1?"@section ":"@subsection ");
2075 SectionInfo *si = Doxygen::sectionDict->find(header);
2078 if (si->lineNr != -1)
2080 warn(g_fileName,g_lineNr,"multiple use of section label '%s', (first occurrence: %s, line %d)",header.data(),si->fileName.data(),si->lineNr);
2084 warn(g_fileName,g_lineNr,"multiple use of section label '%s', (first occurrence: %s)",header.data(),si->fileName.data());
2089 si = new SectionInfo(g_fileName,g_lineNr,id,header,
2090 level==1 ? SectionInfo::Section : SectionInfo::Subsection,level);
2093 g_current->anchors->append(si);
2095 Doxygen::sectionDict->append(header,si);
2100 out.addStr(level==1?"<h1>":"<h2>");
2102 out.addStr(level==1?"\n</h1>\n":"\n</h2>\n");
2107 out.addStr("<hr>\n");
2114 else if ((ref=isLinkRef(data+pi,size-pi,id,link,title)))
2116 //printf("found link ref: id='%s' link='%s' title='%s'\n",
2117 // id.data(),link.data(),title.data());
2118 g_linkRefs.insert(id.lower(),new LinkRef(link,title));
2123 else if (isFencedCodeBlock(data+pi,size-pi,indent,lang,blockStart,blockEnd,blockOffset))
2125 //printf("Found FencedCodeBlock lang='%s' start=%d end=%d code={%s}\n",
2126 // lang.data(),blockStart,blockEnd,QCString(data+pi+blockStart).left(blockEnd-blockStart).data());
2127 writeFencedCodeBlock(out,data+pi,lang,blockStart,blockEnd);
2133 else if (isCodeBlock(data+i,i,end-i,blockIndent))
2135 // skip previous line (it is empty anyway)
2136 i+=writeCodeBlock(out,data+i,size-i,blockIndent);
2141 else if (isTableBlock(data+pi,size-pi))
2143 i=pi+writeTableBlock(out,data+pi,size-pi);
2150 writeOneLineHeaderOrRuler(out,data+pi,i-pi);
2156 //printf("last line %d size=%d\n",i,size);
2157 if (pi!=-1 && pi<size) // deal with the last line
2159 if (isLinkRef(data+pi,size-pi,id,link,title))
2161 //printf("found link ref: id='%s' link='%s' title='%s'\n",
2162 // id.data(),link.data(),title.data());
2163 g_linkRefs.insert(id.lower(),new LinkRef(link,title));
2167 writeOneLineHeaderOrRuler(out,data+pi,size-pi);
2175 static QCString extractPageTitle(QCString &docs,QCString &id)
2178 // first first non-empty line
2180 const char *data = docs.data();
2182 int size=docs.size();
2183 while (i<size && (data[i]==' ' || data[i]=='\n'))
2185 if (data[i]=='\n') ln++;
2188 if (i>=size) return "";
2190 while (end1<size && data[end1-1]!='\n') end1++;
2191 //printf("i=%d end1=%d size=%d line='%s'\n",i,end1,size,docs.mid(i,end1-i).data());
2192 // first line from i..end1
2196 // second line form end1..end2
2198 while (end2<size && data[end2-1]!='\n') end2++;
2199 if (isHeaderline(data+end1,size-end1))
2201 convertStringFragment(title,data+i,end1-i-1);
2204 docs=lns+docs.mid(end2);
2205 id = extractTitleId(title);
2206 //printf("extractPageTitle(title='%s' docs='%s' id='%s')\n",title.data(),docs.data(),id.data());
2210 if (i<end1 && isAtxHeader(data+i,end1-i,title,id)>0)
2212 docs=docs.mid(end1);
2214 //printf("extractPageTitle(title='%s' docs='%s' id='%s')\n",title.data(),docs.data(),id.data());
2218 static QCString detab(const QCString &s,int &refIndent)
2220 static int tabSize = Config_getInt("TAB_SIZE");
2222 int size = s.length();
2223 const char *data = s.data();
2226 const int maxIndent=1000000; // value representing infinity
2227 int minIndent=maxIndent;
2233 case '\t': // expand tab
2235 int stop = tabSize - (col%tabSize);
2236 //printf("expand at %d stop=%d\n",col,stop);
2238 while (stop--) out.addChar(' ');
2241 case '\n': // reset colomn counter
2245 case ' ': // increment column counter
2249 default: // non-whitespace => update minIndent
2251 if (c<0 && i<size) // multibyte sequence
2253 out.addChar(data[i++]); // >= 2 bytes
2254 if (((uchar)c&0xE0)==0xE0 && i<size)
2256 out.addChar(data[i++]); // 3 bytes
2258 if (((uchar)c&0xF0)==0xF0 && i<size)
2260 out.addChar(data[i++]); // 4 byres
2263 if (col<minIndent) minIndent=col;
2267 if (minIndent!=maxIndent) refIndent=minIndent; else refIndent=0;
2269 //printf("detab refIndent=%d\n",refIndent);
2273 //---------------------------------------------------------------------------
2275 QCString processMarkdown(const QCString &fileName,const int lineNr,Entry *e,const QCString &input)
2277 static bool init=FALSE;
2280 // setup callback table for special characters
2281 g_actions[(unsigned int)'_']=processEmphasis;
2282 g_actions[(unsigned int)'*']=processEmphasis;
2283 g_actions[(unsigned int)'`']=processCodeSpan;
2284 g_actions[(unsigned int)'\\']=processSpecialCommand;
2285 g_actions[(unsigned int)'@']=processSpecialCommand;
2286 g_actions[(unsigned int)'[']=processLink;
2287 g_actions[(unsigned int)'!']=processLink;
2288 g_actions[(unsigned int)'<']=processHtmlTag;
2289 g_actions[(unsigned int)'-']=processNmdash;
2290 g_actions[(unsigned int)'"']=processQuoted;
2294 g_linkRefs.setAutoDelete(TRUE);
2297 g_fileName = fileName;
2300 if (input.isEmpty()) return input;
2303 // for replace tabs by spaces
2304 QCString s = detab(input,refIndent);
2305 //printf("======== DeTab =========\n---- output -----\n%s\n---------\n",s.data());
2306 // then process quotation blocks (as these may contain other blocks)
2307 s = processQuotations(s,refIndent);
2308 //printf("======== Quotations =========\n---- output -----\n%s\n---------\n",s.data());
2309 // then process block items (headers, rules, and code blocks, references)
2310 s = processBlocks(s,refIndent);
2311 //printf("======== Blocks =========\n---- output -----\n%s\n---------\n",s.data());
2312 // finally process the inline markup (links, emphasis and code spans)
2313 processInline(out,s,s.length());
2315 Debug::print(Debug::Markdown,0,"======== Markdown =========\n---- input ------- \n%s\n---- output -----\n%s\n---------\n",input.data(),out.get());
2319 //---------------------------------------------------------------------------
2321 QCString markdownFileNameToId(const QCString &fileName)
2323 QCString baseFn = stripFromPath(QFileInfo(fileName).absFilePath().utf8());
2324 int i = baseFn.findRev('.');
2325 if (i!=-1) baseFn = baseFn.left(i);
2326 QCString baseName = substitute(substitute(baseFn," ","_"),"/","_");
2327 return "md_"+baseName;
2330 void MarkdownFileParser::parseInput(const char *fileName,
2331 const char *fileBuf,
2333 bool /*sameTranslationUnit*/,
2334 QStrList & /*filesInSameTranslationUnit*/)
2336 Entry *current = new Entry;
2337 current->lang = SrcLangExt_Markdown;
2338 current->fileName = fileName;
2339 current->docFile = fileName;
2340 current->docLine = 1;
2341 QCString docs = fileBuf;
2343 QCString title=extractPageTitle(docs,id).stripWhiteSpace();
2344 QCString titleFn = QFileInfo(fileName).baseName().utf8();
2345 QCString fn = QFileInfo(fileName).fileName().utf8();
2346 static QCString mdfileAsMainPage = Config_getString("USE_MDFILE_AS_MAINPAGE");
2347 if (id.isEmpty()) id = markdownFileNameToId(fileName);
2348 if (title.isEmpty()) title = titleFn;
2349 if (!mdfileAsMainPage.isEmpty() &&
2350 (fn==mdfileAsMainPage || // name reference
2351 QFileInfo(fileName).absFilePath()==
2352 QFileInfo(mdfileAsMainPage).absFilePath()) // file reference with path
2355 docs.prepend("@mainpage\n");
2357 else if (id=="mainpage" || id=="index")
2359 docs.prepend("@mainpage "+title+"\n");
2363 docs.prepend("@page "+id+" "+title+"\n");
2368 // even without markdown support enabled, we still
2369 // parse markdown files as such
2370 bool markdownEnabled = Doxygen::markdownSupport;
2371 Doxygen::markdownSupport = TRUE;
2375 while (parseCommentBlock(
2382 FALSE, // javadoc autobrief
2383 FALSE, // inBodyDocs
2390 QCString docFile = current->docFile;
2391 root->addSubEntry(current);
2392 current = new Entry;
2393 current->lang = SrcLangExt_Markdown;
2394 current->docFile = docFile;
2395 current->docLine = lineNr;
2400 root->addSubEntry(current);
2404 Doxygen::markdownSupport = markdownEnabled;
2405 //g_correctSectionLevel = FALSE;
2408 void MarkdownFileParser::parseCode(CodeOutputInterface &codeOutIntf,
2409 const char *scopeName,
2410 const QCString &input,
2412 bool isExampleBlock,
2413 const char *exampleName,
2417 bool inlineFragment,
2418 MemberDef *memberDef,
2419 bool showLineNumbers,
2420 Definition *searchCtx,
2424 ParserInterface *pIntf = Doxygen::parserManager->getParser("*.cpp");
2428 codeOutIntf,scopeName,input,lang,isExampleBlock,exampleName,
2429 fileDef,startLine,endLine,inlineFragment,memberDef,showLineNumbers,
2430 searchCtx,collectXRefs);
2434 void MarkdownFileParser::resetCodeParserState()
2436 ParserInterface *pIntf = Doxygen::parserManager->getParser("*.cpp");
2439 pIntf->resetCodeParserState();
2443 void MarkdownFileParser::parsePrototype(const char *text)
2445 ParserInterface *pIntf = Doxygen::parserManager->getParser("*.cpp");
2448 pIntf->parsePrototype(text);