Doxygen
Loading...
Searching...
No Matches
markdown.cpp
Go to the documentation of this file.
1/******************************************************************************
2 *
3 * Copyright (C) 1997-2020 by Dimitri van Heesch.
4 *
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.
10 *
11 * Documents produced by Doxygen are derivative works derived from the
12 * input used in their production; they are not affected by this license.
13 *
14 */
15
16/* Note: part of the code below is inspired by libupskirt written by
17 * Natacha Porté. Original copyright message follows:
18 *
19 * Copyright (c) 2008, Natacha Porté
20 *
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.
24 *
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.
32 */
33
34#include <stdio.h>
35
36#include <unordered_map>
37#include <functional>
38#include <atomic>
39#include <array>
40#include <string_view>
41
42#include "markdown.h"
43#include "debug.h"
44#include "util.h"
45#include "doxygen.h"
46#include "commentscan.h"
47#include "entry.h"
48#include "config.h"
49#include "message.h"
50#include "portable.h"
51#include "regex.h"
52#include "fileinfo.h"
53#include "trace.h"
54#include "anchor.h"
55#include "stringutil.h"
56
57#if !ENABLE_MARKDOWN_TRACING
58#undef AUTO_TRACE
59#undef AUTO_TRACE_ADD
60#undef AUTO_TRACE_EXIT
61#define AUTO_TRACE(...) (void)0
62#define AUTO_TRACE_ADD(...) (void)0
63#define AUTO_TRACE_EXIT(...) (void)0
64#endif
65
67{
68 explicitPage, /**< docs start with a page command */
69 explicitMainPage, /**< docs start with a mainpage command */
70 explicitOtherPage, /**< docs start with a dir / defgroup / addtogroup command */
71 notExplicit /**< docs doesn't start with either page or mainpage */
72};
73
74//-----------
75
76// is character c part of an identifier?
77static constexpr bool isIdChar(char c)
78{
79 return (c>='a' && c<='z') ||
80 (c>='A' && c<='Z') ||
81 (c>='0' && c<='9') ||
82 (static_cast<unsigned char>(c)>=0x80); // unicode characters
83}
84
85// is character allowed right at the beginning of an emphasis section
86static constexpr bool extraChar(char c)
87{
88 return c=='-' || c=='+' || c=='!' || c=='?' || c=='$' || c=='@' ||
89 c=='&' || c=='*' || c=='_' || c=='%' || c=='[' || c=='(' ||
90 c=='.' || c=='>' || c==':' || c==',' || c==';' || c=='\'' ||
91 c=='"' || c=='`' || c=='\\';
92}
93
94// is character c allowed before an emphasis section
95static constexpr bool isOpenEmphChar(char c)
96{
97 return c=='\n' || c==' ' || c=='\'' || c=='<' ||
98 c=='>' || c=='{' || c=='(' || c=='[' ||
99 c==',' || c==':' || c==';';
100}
101
102// test for non breakable space (UTF-8)
103static constexpr bool isUtf8Nbsp(char c1,char c2)
104{
105 return c1==static_cast<char>(0xc2) && c2==static_cast<char>(0xa0);
106}
107
108static constexpr bool isAllowedEmphStr(const std::string_view &data,size_t offset)
109{
110 return ((offset>0 && !isOpenEmphChar(data.data()[-1])) &&
111 (offset>1 && !isUtf8Nbsp(data.data()[-2],data.data()[-1])));
112}
113
114// is character c an escape that prevents ending an emphasis section
115// so for example *bla (*.txt) is cool*, the c=='(' prevents '*' from ending the emphasis section.
116static constexpr bool ignoreCloseEmphChar(char c,char cn)
117{
118 return c=='(' || c=='{' || c=='[' || (c=='<' && cn!='/') || c=='\\' || c=='@';
119}
120
121//----------
122
124{
125 TableCell() : colSpan(false) {}
128};
129
131{
132 Private(const QCString &fn,int line,int indent) : fileName(fn), lineNr(line), indentLevel(indent) { }
133
134 QCString processQuotations(std::string_view data,size_t refIndent);
135 QCString processBlocks(std::string_view data,size_t indent);
136 QCString isBlockCommand(std::string_view data,size_t offset);
137 size_t isSpecialCommand(std::string_view data,size_t offset);
138 size_t findEndOfLine(std::string_view data,size_t offset);
139 int processHtmlTagWrite(std::string_view data,size_t offset,bool doWrite);
140 int processHtmlTag(std::string_view data,size_t offset);
141 int processEmphasis(std::string_view data,size_t offset);
142 int processEmphasis1(std::string_view data,char c);
143 int processEmphasis2(std::string_view data,char c);
144 int processEmphasis3(std::string_view data,char c);
145 int processNmdash(std::string_view data,size_t offset);
146 int processQuoted(std::string_view data,size_t offset);
147 int processCodeSpan(std::string_view data,size_t offset);
148 int processSpecialCommand(std::string_view data,size_t offset);
149 int processLink(std::string_view data,size_t offset);
150 size_t findEmphasisChar(std::string_view, char c, size_t c_size);
151 void addStrEscapeUtf8Nbsp(std::string_view data);
152 void processInline(std::string_view data);
153 void writeMarkdownImage(std::string_view fmt, bool inline_img, bool explicitTitle,
154 const QCString &title, const QCString &content,
155 const QCString &link, const QCString &attributes,
156 const FileDef *fd);
157 int isHeaderline(std::string_view data, bool allowAdjustLevel);
158 int isAtxHeader(std::string_view data, QCString &header,QCString &id,bool allowAdjustLevel,
159 bool *pIsIdGenerated=nullptr);
160 void writeOneLineHeaderOrRuler(std::string_view data);
161 void writeFencedCodeBlock(std::string_view data, std::string_view lang,
162 size_t blockStart,size_t blockEnd);
163 size_t writeBlockQuote(std::string_view data);
164 size_t writeCodeBlock(std::string_view,size_t refIndent);
165 size_t writeTableBlock(std::string_view data);
166 QCString extractTitleId(QCString &title, int level,bool *pIsIdGenerated=nullptr);
167
168 struct LinkRef
169 {
170 LinkRef(const QCString &l,const QCString &t) : link(l), title(t) {}
173 };
174
175 std::unordered_map<std::string,LinkRef> linkRefs;
177 int lineNr = 0;
178 int indentLevel=0; // 0 is outside markdown, -1=page level
180};
181
183{
185 a[static_cast<unsigned int>('_')] = [](Markdown::Private &obj,std::string_view data,size_t offset) { return obj.processEmphasis (data,offset); };
186 a[static_cast<unsigned int>('*')] = [](Markdown::Private &obj,std::string_view data,size_t offset) { return obj.processEmphasis (data,offset); };
187 a[static_cast<unsigned int>('~')] = [](Markdown::Private &obj,std::string_view data,size_t offset) { return obj.processEmphasis (data,offset); };
188 a[static_cast<unsigned int>('`')] = [](Markdown::Private &obj,std::string_view data,size_t offset) { return obj.processCodeSpan (data,offset); };
189 a[static_cast<unsigned int>('\\')]= [](Markdown::Private &obj,std::string_view data,size_t offset) { return obj.processSpecialCommand(data,offset); };
190 a[static_cast<unsigned int>('@')] = [](Markdown::Private &obj,std::string_view data,size_t offset) { return obj.processSpecialCommand(data,offset); };
191 a[static_cast<unsigned int>('[')] = [](Markdown::Private &obj,std::string_view data,size_t offset) { return obj.processLink (data,offset); };
192 a[static_cast<unsigned int>('!')] = [](Markdown::Private &obj,std::string_view data,size_t offset) { return obj.processLink (data,offset); };
193 a[static_cast<unsigned int>('<')] = [](Markdown::Private &obj,std::string_view data,size_t offset) { return obj.processHtmlTag (data,offset); };
194 a[static_cast<unsigned int>('-')] = [](Markdown::Private &obj,std::string_view data,size_t offset) { return obj.processNmdash (data,offset); };
195 a[static_cast<unsigned int>('"')] = [](Markdown::Private &obj,std::string_view data,size_t offset) { return obj.processQuoted (data,offset); };
196 return a;
197}
199
200
201Markdown::Markdown(const QCString &fileName,int lineNr,int indentLevel)
202 : prv(std::make_unique<Private>(fileName,lineNr,indentLevel))
203{
204 using namespace std::placeholders;
205 (void)lineNr; // not used yet
206}
207
208Markdown::~Markdown() = default;
209
210void Markdown::setIndentLevel(int level) { prv->indentLevel = level; }
211
212enum class Alignment { None, Left, Center, Right };
213
214
215//---------- constants -------
216//
217static const char *g_utf8_nbsp = "\xc2\xa0"; // UTF-8 nbsp
218static const char *g_doxy_nbsp = "&_doxy_nbsp;"; // doxygen escape command for UTF-8 nbsp
219static const size_t codeBlockIndent = 4;
220
221//---------- helpers -------
222
223// test if the next characters in data represent a new line (which can be character \n or string \ilinebr).
224// returns 0 if no newline is found, or the number of characters that make up the newline if found.
225inline size_t isNewline(std::string_view data)
226{
227 // normal newline
228 if (data[0] == '\n') return 1;
229 // artificial new line from ^^ in ALIASES
230 if (literal_at(data,"\\ilinebr"))
231 {
232 return (data.size()>8 && data[8]==' ') ? 9 : 8; // also count space after \ilinebr if present
233 }
234 return 0;
235}
236
237// escape double quotes in string
239{
240 AUTO_TRACE("s={}",Trace::trunc(s));
241 if (s.isEmpty()) return s;
242 QCString result;
243 const char *p=s.data();
244 char c=0, pc='\0';
245 while ((c=*p++))
246 {
247 if (c=='"' && pc!='\\') result+='\\';
248 result+=c;
249 pc=c;
250 }
251 AUTO_TRACE_EXIT("result={}",result);
252 return result;
253}
254
255// escape characters that have a special meaning later on.
257{
258 AUTO_TRACE("s={}",Trace::trunc(s));
259 if (s.isEmpty()) return s;
260 bool insideQuote=FALSE;
261 QCString result;
262 const char *p=s.data();
263 char c=0, pc='\0';
264 while ((c=*p++))
265 {
266 switch (c)
267 {
268 case '"':
269 if (pc!='\\')
270 {
271 if (Config_getBool(MARKDOWN_STRICT))
272 {
273 result+='\\';
274 }
275 else // For Doxygen's markup style a quoted text is left untouched
276 {
277 insideQuote=!insideQuote;
278 }
279 }
280 result+=c;
281 break;
282 case '<':
283 // fall through
284 case '>':
285 if (!insideQuote)
286 {
287 result+='\\';
288 result+=c;
289 if ((p[0]==':') && (p[1]==':'))
290 {
291 result+='\\';
292 result+=':';
293 p++;
294 }
295 }
296 else
297 {
298 result+=c;
299 }
300 break;
301 case '\\': if (!insideQuote) { result+='\\'; } result+='\\'; break;
302 case '@': if (!insideQuote) { result+='\\'; } result+='@'; break;
303 // commented out next line due to regression when using % to suppress a link
304 //case '%': if (!insideQuote) { result+='\\'; } result+='%'; break;
305 case '#': if (!insideQuote) { result+='\\'; } result+='#'; break;
306 case '$': if (!insideQuote) { result+='\\'; } result+='$'; break;
307 case '&': if (!insideQuote) { result+='\\'; } result+='&'; break;
308 default:
309 result+=c; break;
310 }
311 pc=c;
312 }
313 AUTO_TRACE_EXIT("result={}",result);
314 return result;
315}
316
317/** helper function to convert presence of left and/or right alignment markers
318 * to an alignment value
319 */
320static constexpr Alignment markersToAlignment(bool leftMarker,bool rightMarker)
321{
322 if (leftMarker && rightMarker)
323 {
324 return Alignment::Center;
325 }
326 else if (leftMarker)
327 {
328 return Alignment::Left;
329 }
330 else if (rightMarker)
331 {
332 return Alignment::Right;
333 }
334 else
335 {
336 return Alignment::None;
337 }
338}
339
340/** parse the image attributes and return attributes for given format */
341static QCString getFilteredImageAttributes(std::string_view fmt, const QCString &attrs)
342{
343 AUTO_TRACE("fmt={} attrs={}",fmt,attrs);
344 StringVector attrList = split(attrs.str(),",");
345 for (const auto &attr_ : attrList)
346 {
347 QCString attr = QCString(attr_).stripWhiteSpace();
348 int i = attr.find(':');
349 if (i>0) // has format
350 {
351 QCString format = attr.left(i).stripWhiteSpace().lower();
352 if (format == fmt) // matching format
353 {
354 AUTO_TRACE_EXIT("result={}",attr.mid(i+1));
355 return attr.mid(i+1); // keep part after :
356 }
357 }
358 else // option that applies to all formats
359 {
360 AUTO_TRACE_EXIT("result={}",attr);
361 return attr;
362 }
363 }
364 return QCString();
365}
366
367// Check if data contains a block command. If so returned the command
368// that ends the block. If not an empty string is returned.
369// Note When offset>0 character position -1 will be inspected.
370//
371// Checks for and skip the following block commands:
372// {@code .. { .. } .. }
373// \dot .. \enddot
374// \code .. \endcode
375// \msc .. \endmsc
376// \f$..\f$
377// \f(..\f)
378// \f[..\f]
379// \f{..\f}
380// \verbatim..\endverbatim
381// \iliteral..\endiliteral
382// \latexonly..\endlatexonly
383// \htmlonly..\endhtmlonly
384// \xmlonly..\endxmlonly
385// \rtfonly..\endrtfonly
386// \manonly..\endmanonly
387// \startuml..\enduml
388QCString Markdown::Private::isBlockCommand(std::string_view data,size_t offset)
389{
390 QCString result;
391 AUTO_TRACE("data='{}' offset={}",Trace::trunc(data),offset);
392
393 using EndBlockFunc = QCString (*)(const std::string &,bool,char);
394
395 static constexpr auto getEndBlock = [](const std::string &blockName,bool,char) -> QCString
396 {
397 return "end"+blockName;
398 };
399 static constexpr auto getEndCode = [](const std::string &blockName,bool openBracket,char) -> QCString
400 {
401 return openBracket ? QCString("}") : "end"+blockName;
402 };
403 static constexpr auto getEndUml = [](const std::string &/* blockName */,bool,char) -> QCString
404 {
405 return "enduml";
406 };
407 static constexpr auto getEndFormula = [](const std::string &/* blockName */,bool,char nextChar) -> QCString
408 {
409 switch (nextChar)
410 {
411 case '$': return "f$";
412 case '(': return "f)";
413 case '[': return "f]";
414 case '{': return "f}";
415 }
416 return "";
417 };
418
419 // table mapping a block start command to a function that can return the matching end block string
420 static const std::unordered_map<std::string,EndBlockFunc> blockNames =
421 {
422 { "dot", getEndBlock },
423 { "code", getEndCode },
424 { "icode", getEndBlock },
425 { "msc", getEndBlock },
426 { "verbatim", getEndBlock },
427 { "iverbatim", getEndBlock },
428 { "iliteral", getEndBlock },
429 { "latexonly", getEndBlock },
430 { "htmlonly", getEndBlock },
431 { "xmlonly", getEndBlock },
432 { "rtfonly", getEndBlock },
433 { "manonly", getEndBlock },
434 { "docbookonly", getEndBlock },
435 { "startuml", getEndUml },
436 { "f", getEndFormula }
437 };
438
439 const size_t size = data.size();
440 bool openBracket = offset>0 && data.data()[-1]=='{';
441 bool isEscaped = offset>0 && (data.data()[-1]=='\\' || data.data()[-1]=='@');
442 if (isEscaped) return result;
443
444 size_t end=1;
445 while (end<size && (data[end]>='a' && data[end]<='z')) end++;
446 if (end==1) return result;
447 std::string blockName(data.substr(1,end-1));
448 auto it = blockNames.find(blockName);
449 if (it!=blockNames.end()) // there is a function assigned
450 {
451 result = it->second(blockName, openBracket, end<size ? data[end] : 0);
452 }
453 AUTO_TRACE_EXIT("result={}",result);
454 return result;
455}
456
457size_t Markdown::Private::isSpecialCommand(std::string_view data,size_t offset)
458{
459 AUTO_TRACE("data='{}' offset={}",Trace::trunc(data),offset);
460
461 using EndCmdFunc = size_t (*)(std::string_view,size_t);
462
463 static constexpr auto endOfLine = [](std::string_view data_,size_t offset_) -> size_t
464 {
465 // skip until the end of line (allowing line continuation characters)
466 char lc = 0;
467 char c = 0;
468 while (offset_<data_.size() && ((c=data_[offset_])!='\n' || lc=='\\'))
469 {
470 if (c=='\\') lc='\\'; // last character was a line continuation
471 else if (c!=' ') lc=0; // rest line continuation
472 offset_++;
473 }
474 return offset_;
475 };
476
477 static constexpr auto endOfLabels = [](std::string_view data_,size_t offset_,bool multi_) -> size_t
478 {
479 if (offset_<data_.size() && data_[offset_]==' ') // we expect a space before the label
480 {
481 char c = 0;
482 offset_++;
483 bool done=false;
484 while (!done)
485 {
486 // skip over spaces
487 while (offset_<data_.size() && data_[offset_]==' ')
488 {
489 offset_++;
490 }
491 // skip over label
492 while (offset_<data_.size() && (c=data_[offset_])!=' ' && c!=',' && c!='\\' && c!='@' && c!='\n')
493 {
494 offset_++;
495 }
496 // optionally skip over a comma separated list of labels
497 if (multi_ && offset_<data_.size() && (data_[offset_]==',' || data_[offset_]==' '))
498 {
499 size_t off = offset_;
500 while (off<data_.size() && data_[off]==' ')
501 {
502 off++;
503 }
504 if (off<data_.size() && data_[off]==',')
505 {
506 offset_ = ++off;
507 }
508 else // no next label found
509 {
510 done=true;
511 }
512 }
513 else
514 {
515 done=true;
516 }
517 }
518 return offset_;
519 }
520 return 0;
521 };
522
523 static constexpr auto endOfLabel = [](std::string_view data_,size_t offset_) -> size_t
524 {
525 return endOfLabels(data_,offset_,false);
526 };
527
528 static constexpr auto endOfLabelOpt = [](std::string_view data_,size_t offset_) -> size_t
529 {
530 size_t index=offset_;
531 if (index<data_.size() && data_[index]==' ') // skip over optional spaces
532 {
533 index++;
534 while (index<data_.size() && data_[index]==' ') index++;
535 }
536 if (index<data_.size() && data_[index]=='{') // find matching '}'
537 {
538 index++;
539 char c = 0;
540 while (index<data_.size() && (c=data_[index])!='}' && c!='\\' && c!='@' && c!='\n') index++;
541 if (index==data_.size() || data_[index]!='}') return 0; // invalid option
542 offset_=index+1; // part after {...} is the option
543 }
544 return endOfLabel(data_,offset_);
545 };
546
547 static constexpr auto endOfParam = [](std::string_view data_,size_t offset_) -> size_t
548 {
549 size_t index=offset_;
550 if (index<data_.size() && data_[index]==' ') // skip over optional spaces
551 {
552 index++;
553 while (index<data_.size() && data_[index]==' ') index++;
554 }
555 if (index<data_.size() && data_[index]=='[') // find matching ']'
556 {
557 index++;
558 char c = 0;
559 while (index<data_.size() && (c=data_[index])!=']' && c!='\n') index++;
560 if (index==data_.size() || data_[index]!=']') return 0; // invalid parameter
561 offset_=index+1; // part after [...] is the parameter name
562 }
563 return endOfLabels(data_,offset_,true);
564 };
565
566 static constexpr auto endOfRetVal = [](std::string_view data_,size_t offset_) -> size_t
567 {
568 return endOfLabels(data_,offset_,true);
569 };
570
571 static constexpr auto endOfFuncLike = [](std::string_view data_,size_t offset_,bool allowSpaces) -> size_t
572 {
573 if (offset_<data_.size() && data_[offset_]==' ') // we expect a space before the name
574 {
575 char c=0;
576 offset_++;
577 // skip over spaces
578 while (offset_<data_.size() && data_[offset_]==' ')
579 {
580 offset_++;
581 }
582 // skip over name (and optionally type)
583 while (offset_<data_.size() && (c=data_[offset_])!='\n' && (allowSpaces || c!=' ') && c!='(')
584 {
585 if (literal_at(data_.substr(offset_),"\\ilinebr ")) break;
586 offset_++;
587 }
588 if (c=='(') // find the end of the function
589 {
590 int count=1;
591 offset_++;
592 while (offset_<data_.size() && (c=data_[offset_++]))
593 {
594 if (c=='(') count++;
595 else if (c==')') count--;
596 if (count==0) return offset_;
597 }
598 }
599 return offset_;
600 }
601 return 0;
602 };
603
604 static constexpr auto endOfFunc = [](std::string_view data_,size_t offset_) -> size_t
605 {
606 return endOfFuncLike(data_,offset_,true);
607 };
608
609 static constexpr auto endOfGuard = [](std::string_view data_,size_t offset_) -> size_t
610 {
611 return endOfFuncLike(data_,offset_,false);
612 };
613
614 static const std::unordered_map<std::string,EndCmdFunc> cmdNames =
615 {
616 { "a", endOfLabel },
617 { "addindex", endOfLine },
618 { "addtogroup", endOfLabel },
619 { "anchor", endOfLabel },
620 { "b", endOfLabel },
621 { "c", endOfLabel },
622 { "category", endOfLine },
623 { "cite", endOfLabel },
624 { "class", endOfLine },
625 { "concept", endOfLine },
626 { "copybrief", endOfFunc },
627 { "copydetails", endOfFunc },
628 { "copydoc", endOfFunc },
629 { "def", endOfFunc },
630 { "defgroup", endOfLabel },
631 { "diafile", endOfLine },
632 { "dir", endOfLine },
633 { "dockbookinclude",endOfLine },
634 { "dontinclude", endOfLine },
635 { "dotfile", endOfLine },
636 { "e", endOfLabel },
637 { "elseif", endOfGuard },
638 { "em", endOfLabel },
639 { "emoji", endOfLabel },
640 { "enum", endOfLabel },
641 { "example", endOfLine },
642 { "exception", endOfLine },
643 { "extends", endOfLabel },
644 { "file", endOfLine },
645 { "fn", endOfFunc },
646 { "headerfile", endOfLine },
647 { "htmlinclude", endOfLine },
648 { "ianchor", endOfLabelOpt },
649 { "idlexcept", endOfLine },
650 { "if", endOfGuard },
651 { "ifnot", endOfGuard },
652 { "image", endOfLine },
653 { "implements", endOfLine },
654 { "include", endOfLine },
655 { "includedoc", endOfLine },
656 { "includelineno", endOfLine },
657 { "ingroup", endOfLabel },
658 { "interface", endOfLine },
659 { "latexinclude", endOfLine },
660 { "maninclude", endOfLine },
661 { "memberof", endOfLabel },
662 { "mscfile", endOfLine },
663 { "namespace", endOfLabel },
664 { "noop", endOfLine },
665 { "overload", endOfLine },
666 { "p", endOfLabel },
667 { "package", endOfLabel },
668 { "page", endOfLabel },
669 { "paragraph", endOfLabel },
670 { "param", endOfParam },
671 { "property", endOfLine },
672 { "protocol", endOfLine },
673 { "qualifier", endOfLine },
674 { "ref", endOfLabel },
675 { "refitem", endOfLine },
676 { "related", endOfLabel },
677 { "relatedalso", endOfLabel },
678 { "relates", endOfLabel },
679 { "relatesalso", endOfLabel },
680 { "retval", endOfRetVal},
681 { "rtfinclude", endOfLine },
682 { "section", endOfLabel },
683 { "skip", endOfLine },
684 { "skipline", endOfLine },
685 { "snippet", endOfLine },
686 { "snippetdoc", endOfLine },
687 { "snippetlineno", endOfLine },
688 { "struct", endOfLine },
689 { "subpage", endOfLabel },
690 { "subparagraph", endOfLabel },
691 { "subsubparagraph",endOfLabel },
692 { "subsection", endOfLabel },
693 { "subsubsection", endOfLabel },
694 { "throw", endOfLabel },
695 { "throws", endOfLabel },
696 { "tparam", endOfLabel },
697 { "typedef", endOfLine },
698 { "plantumlfile", endOfLine },
699 { "union", endOfLine },
700 { "until", endOfLine },
701 { "var", endOfLine },
702 { "verbinclude", endOfLine },
703 { "weakgroup", endOfLabel },
704 { "xmlinclude", endOfLine },
705 { "xrefitem", endOfLabel }
706 };
707
708 bool isEscaped = offset>0 && (data.data()[-1]=='\\' || data.data()[-1]=='@');
709 if (isEscaped) return 0;
710
711 const size_t size = data.size();
712 size_t end=1;
713 while (end<size && (data[end]>='a' && data[end]<='z')) end++;
714 if (end==1) return 0;
715 std::string cmdName(data.substr(1,end-1));
716 size_t result=0;
717 auto it = cmdNames.find(cmdName);
718 if (it!=cmdNames.end()) // command with parameters that should be ignored by markdown
719 {
720 // find the end of the parameters
721 result = it->second(data,end);
722 }
723 AUTO_TRACE_EXIT("result={}",result);
724 return result;
725}
726
727/** looks for the next emph char, skipping other constructs, and
728 * stopping when either it is found, or we are at the end of a paragraph.
729 */
730size_t Markdown::Private::findEmphasisChar(std::string_view data, char c, size_t c_size)
731{
732 AUTO_TRACE("data='{}' c={} c_size={}",Trace::trunc(data),c,c_size);
733 size_t i = 1;
734 const size_t size = data.size();
735
736 while (i<size)
737 {
738 while (i<size && data[i]!=c &&
739 data[i]!='\\' && data[i]!='@' &&
740 !(data[i]=='/' && data[i-1]=='<') && // html end tag also ends emphasis
741 data[i]!='\n') i++;
742 // avoid overflow (unclosed emph token)
743 if (i==size)
744 {
745 return 0;
746 }
747 //printf("findEmphasisChar: data=[%s] i=%d c=%c\n",data,i,data[i]);
748
749 // not counting escaped chars or characters that are unlikely
750 // to appear as the end of the emphasis char
751 if (ignoreCloseEmphChar(data[i-1],data[i]))
752 {
753 i++;
754 continue;
755 }
756 else
757 {
758 // get length of emphasis token
759 size_t len = 0;
760 while (i+len<size && data[i+len]==c)
761 {
762 len++;
763 }
764
765 if (len>0)
766 {
767 if (len!=c_size || (i+len<size && isIdChar(data[i+len]))) // to prevent touching some_underscore_identifier
768 {
769 i+=len;
770 continue;
771 }
772 AUTO_TRACE_EXIT("result={}",i);
773 return static_cast<int>(i); // found it
774 }
775 }
776
777 // skipping a code span
778 if (data[i]=='`')
779 {
780 int snb=0;
781 while (i < size && data[i] == '`')
782 {
783 snb++;
784 i++;
785 }
786
787 // find same pattern to end the span
788 int enb=0;
789 while (i<size && enb<snb)
790 {
791 if (data[i]=='`') enb++;
792 if (snb==1 && data[i]=='\'') break; // ` ended by '
793 i++;
794 }
795 }
796 else if (data[i]=='@' || data[i]=='\\')
797 { // skip over blocks that should not be processed
798 QCString endBlockName = isBlockCommand(data.substr(i),i);
799 if (!endBlockName.isEmpty())
800 {
801 i++;
802 size_t l = endBlockName.length();
803 while (i+l<size)
804 {
805 if ((data[i]=='\\' || data[i]=='@') && // command
806 data[i-1]!='\\' && data[i-1]!='@') // not escaped
807 {
808 if (qstrncmp(&data[i+1],endBlockName.data(),l)==0)
809 {
810 break;
811 }
812 }
813 i++;
814 }
815 }
816 else if (i+1<size && isIdChar(data[i+1])) // @cmd, stop processing, see bug 690385
817 {
818 return 0;
819 }
820 else
821 {
822 i++;
823 }
824 }
825 else if (data[i-1]=='<' && data[i]=='/') // html end tag invalidates emphasis
826 {
827 return 0;
828 }
829 else if (data[i]=='\n') // end * or _ at paragraph boundary
830 {
831 i++;
832 while (i<size && data[i]==' ') i++;
833 if (i>=size || data[i]=='\n')
834 {
835 return 0;
836 } // empty line -> paragraph
837 }
838 else // should not get here!
839 {
840 i++;
841 }
842 }
843 return 0;
844}
845
846/** process single emphasis */
847int Markdown::Private::processEmphasis1(std::string_view data, char c)
848{
849 AUTO_TRACE("data='{}' c={}",Trace::trunc(data),c);
850 size_t i = 0;
851 const size_t size = data.size();
852
853 /* skipping one symbol if coming from emph3 */
854 if (size>1 && data[0]==c && data[1]==c) { i=1; }
855
856 while (i<size)
857 {
858 size_t len = findEmphasisChar(data.substr(i), c, 1);
859 if (len==0) { return 0; }
860 i+=len;
861 if (i>=size) { return 0; }
862
863 if (i+1<size && data[i+1]==c)
864 {
865 i++;
866 continue;
867 }
868 if (data[i]==c && data[i-1]!=' ' && data[i-1]!='\n')
869 {
870 out+="<em>";
871 processInline(data.substr(0,i));
872 out+="</em>";
873 AUTO_TRACE_EXIT("result={}",i+1);
874 return static_cast<int>(i+1);
875 }
876 }
877 return 0;
878}
879
880/** process double emphasis */
881int Markdown::Private::processEmphasis2(std::string_view data, char c)
882{
883 AUTO_TRACE("data='{}' c={}",Trace::trunc(data),c);
884 size_t i = 0;
885 const size_t size = data.size();
886
887 while (i<size)
888 {
889 size_t len = findEmphasisChar(data.substr(i), c, 2);
890 if (len==0)
891 {
892 return 0;
893 }
894 i += len;
895 if (i+1<size && data[i]==c && data[i+1]==c && i && data[i-1]!=' ' && data[i-1]!='\n')
896 {
897 if (c == '~') out+="<strike>";
898 else out+="<strong>";
899 processInline(data.substr(0,i));
900 if (c == '~') out+="</strike>";
901 else out+="</strong>";
902 AUTO_TRACE_EXIT("result={}",i+2);
903 return static_cast<int>(i+2);
904 }
905 i++;
906 }
907 return 0;
908}
909
910/** Parsing triple emphasis.
911 * Finds the first closing tag, and delegates to the other emph
912 */
913int Markdown::Private::processEmphasis3(std::string_view data,char c)
914{
915 AUTO_TRACE("data='{}' c={}",Trace::trunc(data),c);
916 size_t i = 0;
917 const size_t size = data.size();
918
919 while (i<size)
920 {
921 size_t len = findEmphasisChar(data.substr(i), c, 3);
922 if (len==0)
923 {
924 return 0;
925 }
926 i+=len;
927
928 /* skip whitespace preceded symbols */
929 if (data[i]!=c || data[i-1]==' ' || data[i-1]=='\n')
930 {
931 continue;
932 }
933
934 if (i+2<size && data[i+1]==c && data[i+2]==c)
935 {
936 out+="<em><strong>";
937 processInline(data.substr(0,i));
938 out+="</strong></em>";
939 AUTO_TRACE_EXIT("result={}",i+3);
940 return static_cast<int>(i+3);
941 }
942 else if (i+1<size && data[i+1]==c)
943 {
944 // double symbol found, handing over to emph1
945 len = processEmphasis1(std::string_view(data.data()-2, size+2), c);
946 if (len==0)
947 {
948 return 0;
949 }
950 else
951 {
952 AUTO_TRACE_EXIT("result={}",len-2);
953 return static_cast<int>(len - 2);
954 }
955 }
956 else
957 {
958 // single symbol found, handing over to emph2
959 len = processEmphasis2(std::string_view(data.data()-1, size+1), c);
960 if (len==0)
961 {
962 return 0;
963 }
964 else
965 {
966 AUTO_TRACE_EXIT("result={}",len-1);
967 return static_cast<int>(len - 1);
968 }
969 }
970 }
971 return 0;
972}
973
974/** Process ndash and mdashes */
975int Markdown::Private::processNmdash(std::string_view data,size_t offset)
976{
977 AUTO_TRACE("data='{}' offset={}",Trace::trunc(data),offset);
978 const size_t size = data.size();
979 // precondition: data[0]=='-'
980 size_t i=1;
981 int count=1;
982 if (i<size && data[i]=='-') // found --
983 {
984 count++;
985 i++;
986 }
987 if (i<size && data[i]=='-') // found ---
988 {
989 count++;
990 i++;
991 }
992 if (i<size && data[i]=='-') // found ----
993 {
994 count++;
995 }
996 if (count>=2 && offset>=2 && literal_at(data.data()-2,"<!"))
997 { AUTO_TRACE_EXIT("result={}",1-count); return 1-count; } // start HTML comment
998 if (count==2 && size > 2 && data[2]=='>')
999 { return 0; } // end HTML comment
1000 if (count==3 && size > 3 && data[3]=='>')
1001 { return 0; } // end HTML comment
1002 if (count==2 && (offset<8 || !literal_at(data.data()-8,"operator"))) // -- => ndash
1003 {
1004 out+="&ndash;";
1005 AUTO_TRACE_EXIT("result=2");
1006 return 2;
1007 }
1008 else if (count==3) // --- => ndash
1009 {
1010 out+="&mdash;";
1011 AUTO_TRACE_EXIT("result=3");
1012 return 3;
1013 }
1014 // not an ndash or mdash
1015 return 0;
1016}
1017
1018/** Process quoted section "...", can contain one embedded newline */
1019int Markdown::Private::processQuoted(std::string_view data,size_t)
1020{
1021 AUTO_TRACE("data='{}'",Trace::trunc(data));
1022 const size_t size = data.size();
1023 size_t i=1;
1024 int nl=0;
1025 while (i<size && data[i]!='"' && nl<2)
1026 {
1027 if (data[i]=='\n') nl++;
1028 i++;
1029 }
1030 if (i<size && data[i]=='"' && nl<2)
1031 {
1032 out+=data.substr(0,i+1);
1033 AUTO_TRACE_EXIT("result={}",i+2);
1034 return static_cast<int>(i+1);
1035 }
1036 // not a quoted section
1037 return 0;
1038}
1039
1040/** Process a HTML tag. Note that <pre>..</pre> are treated specially, in
1041 * the sense that all code inside is written unprocessed
1042 */
1043int Markdown::Private::processHtmlTagWrite(std::string_view data,size_t offset,bool doWrite)
1044{
1045 AUTO_TRACE("data='{}' offset={} doWrite={}",Trace::trunc(data),offset,doWrite);
1046 if (offset>0 && data.data()[-1]=='\\') { return 0; } // escaped <
1047
1048 const size_t size = data.size();
1049
1050 // find the end of the html tag
1051 size_t i=1;
1052 size_t l=0;
1053 // compute length of the tag name
1054 while (i < size && isIdChar(data[i]))
1055 {
1056 i++;
1057 l++;
1058 }
1059 QCString tagName(data.substr(1,i-1));
1060 if (tagName.lower()=="pre") // found <pre> tag
1061 {
1062 bool insideStr=FALSE;
1063 while (i+6<size)
1064 {
1065 char c=data[i];
1066 if (!insideStr && c=='<') // potential start of html tag
1067 {
1068 if (data[i+1]=='/' &&
1069 tolower(data[i+2])=='p' && tolower(data[i+3])=='r' &&
1070 tolower(data[i+4])=='e' && tolower(data[i+5])=='>')
1071 { // found </pre> tag, copy from start to end of tag
1072 if (doWrite) out+=data.substr(0,i+6);
1073 //printf("found <pre>..</pre> [%d..%d]\n",0,i+6);
1074 AUTO_TRACE_EXIT("result={}",i+6);
1075 return static_cast<int>(i+6);
1076 }
1077 }
1078 else if (insideStr && c=='"')
1079 {
1080 if (data[i-1]!='\\') insideStr=FALSE;
1081 }
1082 else if (c=='"')
1083 {
1084 insideStr=TRUE;
1085 }
1086 i++;
1087 }
1088 }
1089 else // some other html tag
1090 {
1091 if (l>0 && i<size)
1092 {
1093 if (data[i]=='/' && i+1<size && data[i+1]=='>') // <bla/>
1094 {
1095 //printf("Found htmlTag={%s}\n",qPrint(QCString(data).left(i+2)));
1096 if (doWrite) out+=data.substr(0,i+2);
1097 AUTO_TRACE_EXIT("result={}",i+2);
1098 return static_cast<int>(i+2);
1099 }
1100 else if (data[i]=='>') // <bla>
1101 {
1102 //printf("Found htmlTag={%s}\n",qPrint(QCString(data).left(i+1)));
1103 if (doWrite) out+=data.substr(0,i+1);
1104 AUTO_TRACE_EXIT("result={}",i+1);
1105 return static_cast<int>(i+1);
1106 }
1107 else if (data[i]==' ') // <bla attr=...
1108 {
1109 i++;
1110 bool insideAttr=FALSE;
1111 while (i<size)
1112 {
1113 if (!insideAttr && data[i]=='"')
1114 {
1115 insideAttr=TRUE;
1116 }
1117 else if (data[i]=='"' && data[i-1]!='\\')
1118 {
1119 insideAttr=FALSE;
1120 }
1121 else if (!insideAttr && data[i]=='>') // found end of tag
1122 {
1123 //printf("Found htmlTag={%s}\n",qPrint(QCString(data).left(i+1)));
1124 if (doWrite) out+=data.substr(0,i+1);
1125 AUTO_TRACE_EXIT("result={}",i+1);
1126 return static_cast<int>(i+1);
1127 }
1128 i++;
1129 }
1130 }
1131 }
1132 }
1133 AUTO_TRACE_EXIT("not a valid html tag");
1134 return 0;
1135}
1136
1137int Markdown::Private::processHtmlTag(std::string_view data,size_t offset)
1138{
1139 AUTO_TRACE("data='{}' offset={}",Trace::trunc(data),offset);
1140 return processHtmlTagWrite(data,offset,true);
1141}
1142
1143int Markdown::Private::processEmphasis(std::string_view data,size_t offset)
1144{
1145 AUTO_TRACE("data='{}' offset={}",Trace::trunc(data),offset);
1146 const size_t size = data.size();
1147
1148 if (isAllowedEmphStr(data,offset) || // invalid char before * or _
1149 (size>1 && data[0]!=data[1] && !(isIdChar(data[1]) || extraChar(data[1]))) || // invalid char after * or _
1150 (size>2 && data[0]==data[1] && !(isIdChar(data[2]) || extraChar(data[2])))) // invalid char after ** or __
1151 {
1152 AUTO_TRACE_EXIT("invalid surrounding characters");
1153 return 0;
1154 }
1155
1156 char c = data[0];
1157 int ret = 0;
1158 if (size>2 && c!='~' && data[1]!=c) // _bla or *bla
1159 {
1160 // whitespace cannot follow an opening emphasis
1161 if (data[1]==' ' || data[1]=='\n' ||
1162 (ret = processEmphasis1(data.substr(1), c)) == 0)
1163 {
1164 return 0;
1165 }
1166 AUTO_TRACE_EXIT("result={}",ret+1);
1167 return ret+1;
1168 }
1169 if (size>3 && data[1]==c && data[2]!=c) // __bla or **bla
1170 {
1171 if (data[2]==' ' || data[2]=='\n' ||
1172 (ret = processEmphasis2(data.substr(2), c)) == 0)
1173 {
1174 return 0;
1175 }
1176 AUTO_TRACE_EXIT("result={}",ret+2);
1177 return ret+2;
1178 }
1179 if (size>4 && c!='~' && data[1]==c && data[2]==c && data[3]!=c) // ___bla or ***bla
1180 {
1181 if (data[3]==' ' || data[3]=='\n' ||
1182 (ret = processEmphasis3(data.substr(3), c)) == 0)
1183 {
1184 return 0;
1185 }
1186 AUTO_TRACE_EXIT("result={}",ret+3);
1187 return ret+3;
1188 }
1189 return 0;
1190}
1191
1193 std::string_view fmt, bool inline_img, bool explicitTitle,
1194 const QCString &title, const QCString &content,
1195 const QCString &link, const QCString &attrs,
1196 const FileDef *fd)
1197{
1198 AUTO_TRACE("fmt={} inline_img={} explicitTitle={} title={} content={} link={} attrs={}",
1199 fmt,inline_img,explicitTitle,Trace::trunc(title),Trace::trunc(content),link,attrs);
1200 QCString attributes = getFilteredImageAttributes(fmt, attrs);
1201 out+="@image";
1202 if (inline_img)
1203 {
1204 out+="{inline}";
1205 }
1206 out+=" ";
1207 out+=fmt;
1208 out+=" ";
1209 out+=link.mid(fd ? 0 : 5);
1210 if (!explicitTitle && !content.isEmpty())
1211 {
1212 out+=" \"";
1213 out+=escapeDoubleQuotes(content);
1214 out+="\"";
1215 }
1216 else if ((content.isEmpty() || explicitTitle) && !title.isEmpty())
1217 {
1218 out+=" \"";
1219 out+=escapeDoubleQuotes(title);
1220 out+="\"";
1221 }
1222 else
1223 {
1224 out+=" ";// so the line break will not be part of the image name
1225 }
1226 if (!attributes.isEmpty())
1227 {
1228 out+=" ";
1229 out+=attributes;
1230 out+=" ";
1231 }
1232 out+="\\ilinebr ";
1233}
1234
1235int Markdown::Private::processLink(const std::string_view data,size_t offset)
1236{
1237 AUTO_TRACE("data='{}' offset={}",Trace::trunc(data),offset);
1238 const size_t size = data.size();
1239 QCString content;
1240 QCString link;
1241 QCString title;
1242 bool isImageLink = FALSE;
1243 bool isImageInline = FALSE;
1244 bool isToc = FALSE;
1245 size_t i=1;
1246 if (data[0]=='!')
1247 {
1248 isImageLink = TRUE;
1249 if (size<2 || data[1]!='[')
1250 {
1251 return 0;
1252 }
1253
1254 // if there is non-whitespace before the ![ within the scope of two new lines, the image
1255 // is considered inlined, i.e. the image is not preceded by an empty line
1256 int numNLsNeeded=2;
1257 int pos = -1;
1258 while (pos>=-static_cast<int>(offset) && numNLsNeeded>0)
1259 {
1260 if (data.data()[pos]=='\n') numNLsNeeded--;
1261 else if (data.data()[pos]!=' ') // found non-whitespace, stop searching
1262 {
1263 isImageInline=true;
1264 break;
1265 }
1266 pos--;
1267 }
1268 // skip '!['
1269 i++;
1270 }
1271 size_t contentStart=i;
1272 int level=1;
1273 int nlTotal=0;
1274 int nl=0;
1275 // find the matching ]
1276 while (i<size)
1277 {
1278 if (data[i-1]=='\\') // skip escaped characters
1279 {
1280 }
1281 else if (data[i]=='[')
1282 {
1283 level++;
1284 }
1285 else if (data[i]==']')
1286 {
1287 level--;
1288 if (level<=0) break;
1289 }
1290 else if (data[i]=='\n')
1291 {
1292 nl++;
1293 if (nl>1) { return 0; } // only allow one newline in the content
1294 }
1295 i++;
1296 }
1297 nlTotal += nl;
1298 nl = 0;
1299 if (i>=size) return 0; // premature end of comment -> no link
1300 size_t contentEnd=i;
1301 content = data.substr(contentStart,contentEnd-contentStart);
1302 //printf("processLink: content={%s}\n",qPrint(content));
1303 if (!isImageLink && content.isEmpty()) { return 0; } // no link text
1304 i++; // skip over ]
1305
1306 bool whiteSpace = false;
1307 // skip whitespace
1308 while (i<size && data[i]==' ') { whiteSpace = true; i++; }
1309 if (i<size && data[i]=='\n') // one newline allowed here
1310 {
1311 whiteSpace = true;
1312 i++;
1313 // skip more whitespace
1314 while (i<size && data[i]==' ') i++;
1315 }
1316 if (whiteSpace && i<size && (data[i]=='(' || data[i]=='[')) return 0;
1317
1318 bool explicitTitle=FALSE;
1319 if (i<size && data[i]=='(') // inline link
1320 {
1321 i++;
1322 while (i<size && data[i]==' ') i++;
1323 bool uriFormat=false;
1324 if (i<size && data[i]=='<') { i++; uriFormat=true; }
1325 size_t linkStart=i;
1326 int braceCount=1;
1327 while (i<size && data[i]!='\'' && data[i]!='"' && braceCount>0)
1328 {
1329 if (data[i]=='\n') // unexpected EOL
1330 {
1331 nl++;
1332 if (nl>1) { return 0; }
1333 }
1334 else if (data[i]=='(')
1335 {
1336 braceCount++;
1337 }
1338 else if (data[i]==')')
1339 {
1340 braceCount--;
1341 }
1342 if (braceCount>0)
1343 {
1344 i++;
1345 }
1346 }
1347 nlTotal += nl;
1348 nl = 0;
1349 if (i>=size || data[i]=='\n') { return 0; }
1350 link = data.substr(linkStart,i-linkStart);
1351 link = link.stripWhiteSpace();
1352 //printf("processLink: link={%s}\n",qPrint(link));
1353 if (link.isEmpty()) { return 0; }
1354 if (uriFormat && link.at(link.length()-1)=='>') link=link.left(link.length()-1);
1355
1356 // optional title
1357 if (data[i]=='\'' || data[i]=='"')
1358 {
1359 char c = data[i];
1360 i++;
1361 size_t titleStart=i;
1362 nl=0;
1363 while (i<size)
1364 {
1365 if (data[i]=='\n')
1366 {
1367 if (nl>1) { return 0; }
1368 nl++;
1369 }
1370 else if (data[i]=='\\') // escaped char in string
1371 {
1372 i++;
1373 }
1374 else if (data[i]==c)
1375 {
1376 i++;
1377 break;
1378 }
1379 i++;
1380 }
1381 if (i>=size)
1382 {
1383 return 0;
1384 }
1385 size_t titleEnd = i-1;
1386 // search back for closing marker
1387 while (titleEnd>titleStart && data[titleEnd]==' ') titleEnd--;
1388 if (data[titleEnd]==c) // found it
1389 {
1390 title = data.substr(titleStart,titleEnd-titleStart);
1391 explicitTitle=TRUE;
1392 while (i<size)
1393 {
1394 if (data[i]==' ')i++; // remove space after the closing quote and the closing bracket
1395 else if (data[i] == ')') break; // the end bracket
1396 else // illegal
1397 {
1398 return 0;
1399 }
1400 }
1401 }
1402 else
1403 {
1404 return 0;
1405 }
1406 }
1407 i++;
1408 }
1409 else if (i<size && data[i]=='[') // reference link
1410 {
1411 i++;
1412 size_t linkStart=i;
1413 nl=0;
1414 // find matching ]
1415 while (i<size && data[i]!=']')
1416 {
1417 if (data[i]=='\n')
1418 {
1419 nl++;
1420 if (nl>1) { return 0; }
1421 }
1422 i++;
1423 }
1424 if (i>=size) { return 0; }
1425 // extract link
1426 link = data.substr(linkStart,i-linkStart);
1427 //printf("processLink: link={%s}\n",qPrint(link));
1428 link = link.stripWhiteSpace();
1429 if (link.isEmpty()) // shortcut link
1430 {
1431 link=content;
1432 }
1433 // lookup reference
1434 QCString link_lower = link.lower();
1435 auto lr_it=linkRefs.find(link_lower.str());
1436 if (lr_it!=linkRefs.end()) // found it
1437 {
1438 link = lr_it->second.link;
1439 title = lr_it->second.title;
1440 //printf("processLink: ref: link={%s} title={%s}\n",qPrint(link),qPrint(title));
1441 }
1442 else // reference not found!
1443 {
1444 //printf("processLink: ref {%s} do not exist\n",link.qPrint(lower()));
1445 return 0;
1446 }
1447 i++;
1448 }
1449 else if (i<size && data[i]!=':' && !content.isEmpty()) // minimal link ref notation [some id]
1450 {
1451 QCString content_lower = content.lower();
1452 auto lr_it = linkRefs.find(content_lower.str());
1453 //printf("processLink: minimal link {%s} lr=%p",qPrint(content),lr);
1454 if (lr_it!=linkRefs.end()) // found it
1455 {
1456 link = lr_it->second.link;
1457 title = lr_it->second.title;
1458 explicitTitle=TRUE;
1459 i=contentEnd;
1460 }
1461 else if (content=="TOC")
1462 {
1463 isToc=TRUE;
1464 i=contentEnd;
1465 }
1466 else
1467 {
1468 return 0;
1469 }
1470 i++;
1471 }
1472 else
1473 {
1474 return 0;
1475 }
1476 nlTotal += nl;
1477
1478 // search for optional image attributes
1479 QCString attributes;
1480 if (isImageLink)
1481 {
1482 size_t j = i;
1483 // skip over whitespace
1484 while (j<size && data[j]==' ') { j++; }
1485 if (j<size && data[j]=='{') // we have attributes
1486 {
1487 i = j;
1488 // skip over '{'
1489 i++;
1490 size_t attributesStart=i;
1491 nl=0;
1492 // find the matching '}'
1493 while (i<size)
1494 {
1495 if (data[i-1]=='\\') // skip escaped characters
1496 {
1497 }
1498 else if (data[i]=='{')
1499 {
1500 level++;
1501 }
1502 else if (data[i]=='}')
1503 {
1504 level--;
1505 if (level<=0) break;
1506 }
1507 else if (data[i]=='\n')
1508 {
1509 nl++;
1510 if (nl>1) { return 0; } // only allow one newline in the content
1511 }
1512 i++;
1513 }
1514 nlTotal += nl;
1515 if (i>=size) return 0; // premature end of comment -> no attributes
1516 size_t attributesEnd=i;
1517 attributes = data.substr(attributesStart,attributesEnd-attributesStart);
1518 i++; // skip over '}'
1519 }
1520 if (!isImageInline)
1521 {
1522 // if there is non-whitespace after the image within the scope of two new lines, the image
1523 // is considered inlined, i.e. the image is not followed by an empty line
1524 int numNLsNeeded=2;
1525 size_t pos = i;
1526 while (pos<size && numNLsNeeded>0)
1527 {
1528 if (data[pos]=='\n') numNLsNeeded--;
1529 else if (data[pos]!=' ') // found non-whitespace, stop searching
1530 {
1531 isImageInline=true;
1532 break;
1533 }
1534 pos++;
1535 }
1536 }
1537 }
1538
1539 if (isToc) // special case for [TOC]
1540 {
1541 int toc_level = Config_getInt(TOC_INCLUDE_HEADINGS);
1542 if (toc_level>=SectionType::MinLevel && toc_level<=SectionType::MaxLevel)
1543 {
1544 out+="@tableofcontents{html:";
1545 out+=QCString().setNum(toc_level);
1546 out+="}";
1547 }
1548 }
1549 else if (isImageLink)
1550 {
1551 bool ambig = false;
1552 FileDef *fd=nullptr;
1553 if (link.find("@ref ")!=-1 || link.find("\\ref ")!=-1 ||
1555 // assume doxygen symbol link or local image link
1556 {
1557 // check if different handling is needed per format
1558 writeMarkdownImage("html", isImageInline, explicitTitle, title, content, link, attributes, fd);
1559 writeMarkdownImage("latex", isImageInline, explicitTitle, title, content, link, attributes, fd);
1560 writeMarkdownImage("rtf", isImageInline, explicitTitle, title, content, link, attributes, fd);
1561 writeMarkdownImage("docbook", isImageInline, explicitTitle, title, content, link, attributes, fd);
1562 writeMarkdownImage("xml", isImageInline, explicitTitle, title, content, link, attributes, fd);
1563 }
1564 else
1565 {
1566 out+="<img src=\"";
1567 out+=link;
1568 out+="\" alt=\"";
1569 out+=content;
1570 out+="\"";
1571 if (!title.isEmpty())
1572 {
1573 out+=" title=\"";
1574 out+=substitute(title.simplifyWhiteSpace(),"\"","&quot;");
1575 out+="\"";
1576 }
1577 out+="/>";
1578 }
1579 }
1580 else
1581 {
1583 int lp=-1;
1584 if ((lp=link.find("@ref "))!=-1 || (lp=link.find("\\ref "))!=-1 || (lang==SrcLangExt::Markdown && !isURL(link)))
1585 // assume doxygen symbol link
1586 {
1587 if (lp==-1) // link to markdown page
1588 {
1589 out+="@ref \"";
1590 if (!(Portable::isAbsolutePath(link) || isURL(link)))
1591 {
1592 FileInfo forg(link.str());
1593 if (forg.exists() && forg.isReadable())
1594 {
1595 link = forg.absFilePath();
1596 }
1597 else if (!(forg.exists() && forg.isReadable()))
1598 {
1599 FileInfo fi(fileName.str());
1600 QCString mdFile = fileName.left(fileName.length()-fi.fileName().length()) + link;
1601 FileInfo fmd(mdFile.str());
1602 if (fmd.exists() && fmd.isReadable())
1603 {
1604 link = fmd.absFilePath().data();
1605 }
1606 }
1607 }
1608 out+=link;
1609 out+="\"";
1610 }
1611 else
1612 {
1613 out+=link;
1614 }
1615 out+=" \"";
1616 if (explicitTitle && !title.isEmpty())
1617 {
1618 out+=substitute(title,"\"","&quot;");
1619 }
1620 else
1621 {
1622 processInline(std::string_view(substitute(content,"\"","&quot;").str()));
1623 }
1624 out+="\"";
1625 }
1626 else if ((lp=link.find('#'))!=-1 || link.find('/')!=-1 || link.find('.')!=-1)
1627 { // file/url link
1628 if (lp==0 || (lp>0 && !isURL(link) && Config_getEnum(MARKDOWN_ID_STYLE)==MARKDOWN_ID_STYLE_t::GITHUB))
1629 {
1630 out+="@ref \"";
1632 out+="\" \"";
1633 out+=substitute(content.simplifyWhiteSpace(),"\"","&quot;");
1634 out+="\"";
1635 }
1636 else
1637 {
1638 out+="<a href=\"";
1639 out+=link;
1640 out+="\"";
1641 for (int ii = 0; ii < nlTotal; ii++) out+="\n";
1642 if (!title.isEmpty())
1643 {
1644 out+=" title=\"";
1645 out+=substitute(title.simplifyWhiteSpace(),"\"","&quot;");
1646 out+="\"";
1647 }
1648 out+=" ";
1650 out+=">";
1651 content = content.simplifyWhiteSpace();
1652 processInline(std::string_view(content.str()));
1653 out+="</a>";
1654 }
1655 }
1656 else // avoid link to e.g. F[x](y)
1657 {
1658 //printf("no link for '%s'\n",qPrint(link));
1659 return 0;
1660 }
1661 }
1662 AUTO_TRACE_EXIT("result={}",i);
1663 return static_cast<int>(i);
1664}
1665
1666/** `` ` `` parsing a code span (assuming codespan != 0) */
1667int Markdown::Private::processCodeSpan(std::string_view data,size_t offset)
1668{
1669 AUTO_TRACE("data='{}' offset={}",Trace::trunc(data),offset);
1670 const size_t size = data.size();
1671
1672 /* counting the number of backticks in the delimiter */
1673 size_t nb=0, end=0;
1674 while (nb<size && data[nb]=='`')
1675 {
1676 nb++;
1677 }
1678
1679 /* finding the next delimiter with the same amount of backticks */
1680 size_t i = 0;
1681 char pc = '`';
1682 bool markdownStrict = Config_getBool(MARKDOWN_STRICT);
1683 for (end=nb; end<size; end++)
1684 {
1685 //AUTO_TRACE_ADD("c={} nb={} i={} size={}",data[end],nb,i,size);
1686 if (data[end]=='`')
1687 {
1688 i++;
1689 if (nb==1) // `...`
1690 {
1691 if (end+1<size && data[end+1]=='`') // skip over `` inside `...`
1692 {
1693 AUTO_TRACE_ADD("case1.1");
1694 // skip
1695 end++;
1696 i=0;
1697 }
1698 else // normal end of `...`
1699 {
1700 AUTO_TRACE_ADD("case1.2");
1701 break;
1702 }
1703 }
1704 else if (i==nb) // ``...``
1705 {
1706 if (end+1<size && data[end+1]=='`') // do greedy match
1707 {
1708 // skip this quote and use the next one to terminate the sequence, e.g. ``X`Y```
1709 i--;
1710 AUTO_TRACE_ADD("case2.1");
1711 }
1712 else // normal end of ``...``
1713 {
1714 AUTO_TRACE_ADD("case2.2");
1715 break;
1716 }
1717 }
1718 }
1719 else if (data[end]=='\n')
1720 {
1721 // consecutive newlines
1722 if (pc == '\n')
1723 {
1724 AUTO_TRACE_EXIT("new paragraph");
1725 return 0;
1726 }
1727 pc = '\n';
1728 i = 0;
1729 }
1730 else if (!markdownStrict && data[end]=='\'' && nb==1 && (end+1==size || (end+1<size && data[end+1]!='\'' && !isIdChar(data[end+1]))))
1731 { // look for quoted strings like 'some word', but skip strings like `it's cool`
1732 out+="&lsquo;";
1733 out+=data.substr(nb,end-nb);
1734 out+="&rsquo;";
1735 AUTO_TRACE_EXIT("quoted end={}",end+1);
1736 return static_cast<int>(end+1);
1737 }
1738 else if (!markdownStrict && data[end]=='\'' && nb==2 && end+1<size && data[end+1]=='\'')
1739 { // look for '' to match a ``
1740 out+="&ldquo;";
1741 out+=data.substr(nb,end-nb);
1742 out+="&rdquo;";
1743 AUTO_TRACE_EXIT("double quoted end={}",end+1);
1744 return static_cast<int>(end+2);
1745 }
1746 else
1747 {
1748 if (data[end]!=' ') pc = data[end];
1749 i=0;
1750 }
1751 }
1752 if (i < nb && end >= size)
1753 {
1754 AUTO_TRACE_EXIT("no matching delimiter nb={} i={}",nb,i);
1755 if (nb>=3) // found ``` that is not at the start of the line, keep it as-is.
1756 {
1757 out+=data.substr(0,nb);
1758 return static_cast<int>(nb);
1759 }
1760 return 0; // no matching delimiter
1761 }
1762 while (end<size && data[end]=='`') // do greedy match in case we have more end backticks.
1763 {
1764 end++;
1765 }
1766
1767 //printf("found code span '%s'\n",qPrint(QCString(data+f_begin).left(f_end-f_begin)));
1768
1769 /* real code span */
1770 if (nb+nb < end)
1771 {
1772 QCString codeFragment = data.substr(nb, end-nb-nb);
1773 out+="<tt>";
1774 out+=escapeSpecialChars(codeFragment);
1775 out+="</tt>";
1776 }
1777 AUTO_TRACE_EXIT("result={} nb={}",end,nb);
1778 return static_cast<int>(end);
1779}
1780
1782{
1783 AUTO_TRACE("{}",Trace::trunc(data));
1784 if (Portable::strnstr(data.data(),g_doxy_nbsp,data.size())==nullptr) // no escape needed -> fast
1785 {
1786 out+=data;
1787 }
1788 else // escape needed -> slow
1789 {
1791 }
1792}
1793
1794int Markdown::Private::processSpecialCommand(std::string_view data, size_t offset)
1795{
1796 AUTO_TRACE("{}",Trace::trunc(data));
1797 const size_t size = data.size();
1798 size_t i=1;
1799 QCString endBlockName = isBlockCommand(data,offset);
1800 if (!endBlockName.isEmpty())
1801 {
1802 AUTO_TRACE_ADD("endBlockName={}",endBlockName);
1803 size_t l = endBlockName.length();
1804 while (i+l<size)
1805 {
1806 if ((data[i]=='\\' || data[i]=='@') && // command
1807 data[i-1]!='\\' && data[i-1]!='@') // not escaped
1808 {
1809 if (qstrncmp(&data[i+1],endBlockName.data(),l)==0)
1810 {
1811 //printf("found end at %d\n",i);
1812 addStrEscapeUtf8Nbsp(data.substr(0,i+1+l));
1813 AUTO_TRACE_EXIT("result={}",i+1+l);
1814 return static_cast<int>(i+1+l);
1815 }
1816 }
1817 i++;
1818 }
1819 }
1820 size_t endPos = isSpecialCommand(data,offset);
1821 if (endPos>0)
1822 {
1823 out+=data.substr(0,endPos);
1824 return static_cast<int>(endPos);
1825 }
1826 if (size>1 && (data[0]=='\\' || data[0]=='@')) // escaped characters
1827 {
1828 char c=data[1];
1829 if (c=='[' || c==']' || c=='*' || c=='(' || c==')' || c=='`' || c=='_')
1830 {
1831 out+=data[1];
1832 AUTO_TRACE_EXIT("2");
1833 return 2;
1834 }
1835 else if (c=='\\' || c=='@')
1836 {
1837 out+=data.substr(0,2);
1838 AUTO_TRACE_EXIT("2");
1839 return 2;
1840 }
1841 else if (c=='-' && size>3 && data[2]=='-' && data[3]=='-') // \---
1842 {
1843 out+=data.substr(1,3);
1844 AUTO_TRACE_EXIT("2");
1845 return 4;
1846 }
1847 else if (c=='-' && size>2 && data[2]=='-') // \--
1848 {
1849 out+=data.substr(1,2);
1850 AUTO_TRACE_EXIT("3");
1851 return 3;
1852 }
1853 }
1854 return 0;
1855}
1856
1857void Markdown::Private::processInline(std::string_view data)
1858{
1859 AUTO_TRACE("data='{}'",Trace::trunc(data));
1860 size_t i=0;
1861 size_t end=0;
1862 Action_t action;
1863 const size_t size = data.size();
1864 while (i<size)
1865 {
1866 // skip over characters that do not trigger a specific action
1867 while (end<size && ((action=Markdown::actions[static_cast<uint8_t>(data[end])])==nullptr)) end++;
1868 // and add them to the output
1869 out+=data.substr(i,end-i);
1870 if (end>=size) break;
1871 i=end;
1872 // do the action matching a special character at i
1873 int iend = action(*this,data.substr(i),i);
1874 if (iend<=0) // update end
1875 {
1876 end=i+1-iend;
1877 }
1878 else // skip until end
1879 {
1880 i+=iend;
1881 end=i;
1882 }
1883 }
1884}
1885
1886/** returns whether the line is a setext-style hdr underline */
1887int Markdown::Private::isHeaderline(std::string_view data, bool allowAdjustLevel)
1888{
1889 AUTO_TRACE("data='{}' allowAdjustLevel",Trace::trunc(data),allowAdjustLevel);
1890 size_t i=0, c=0;
1891 const size_t size = data.size();
1892 while (i<size && data[i]==' ') i++;
1893 if (i==size) return 0;
1894
1895 // test of level 1 header
1896 if (data[i]=='=')
1897 {
1898 while (i < size && data[i] == '=')
1899 {
1900 i++;
1901 c++;
1902 }
1903 while (i<size && data[i]==' ') i++;
1904 int level = (c>1 && (i>=size || data[i]=='\n')) ? 1 : 0;
1905 if (allowAdjustLevel && level==1 && indentLevel==-1)
1906 {
1907 // In case a page starts with a header line we use it as title, promoting it to @page.
1908 // We set g_indentLevel to -1 to promoting the other sections if they have a deeper
1909 // nesting level than the page header, i.e. @section..@subsection becomes @page..@section.
1910 // In case a section at the same level is found (@section..@section) however we need
1911 // to undo this (and the result will be @page..@section).
1912 indentLevel=0;
1913 }
1914 AUTO_TRACE_EXIT("result={}",indentLevel+level);
1915 return indentLevel+level;
1916 }
1917 // test of level 2 header
1918 if (data[i]=='-')
1919 {
1920 while (i < size && data[i] == '-')
1921 {
1922 i++;
1923 c++;
1924 }
1925 while (i<size && data[i]==' ') i++;
1926 return (c>1 && (i>=size || data[i]=='\n')) ? indentLevel+2 : 0;
1927 }
1928 return 0;
1929}
1930
1931/** returns true if this line starts a block quote */
1932static bool isBlockQuote(std::string_view data,size_t indent)
1933{
1934 AUTO_TRACE("data='{}' indent={}",Trace::trunc(data),indent);
1935 size_t i = 0;
1936 const size_t size = data.size();
1937 while (i<size && data[i]==' ') i++;
1938 if (i<indent+codeBlockIndent) // could be a quotation
1939 {
1940 // count >'s and skip spaces
1941 int level=0;
1942 while (i<size && (data[i]=='>' || data[i]==' '))
1943 {
1944 if (data[i]=='>') level++;
1945 i++;
1946 }
1947 // last characters should be a space or newline,
1948 // so a line starting with >= does not match, but only when level equals 1
1949 bool res = (level>0 && i<size && ((data[i-1]==' ') || data[i]=='\n')) || (level > 1);
1950 AUTO_TRACE_EXIT("result={}",res);
1951 return res;
1952 }
1953 else // too much indentation -> code block
1954 {
1955 AUTO_TRACE_EXIT("result=false: too much indentation");
1956 return false;
1957 }
1958}
1959
1960/** returns end of the link ref if this is indeed a link reference. */
1961static size_t isLinkRef(std::string_view data, QCString &refid, QCString &link, QCString &title)
1962{
1963 AUTO_TRACE("data='{}'",Trace::trunc(data));
1964 const size_t size = data.size();
1965 // format: start with [some text]:
1966 size_t i = 0;
1967 while (i<size && data[i]==' ') i++;
1968 if (i>=size || data[i]!='[') { return 0; }
1969 i++;
1970 size_t refIdStart=i;
1971 while (i<size && data[i]!='\n' && data[i]!=']') i++;
1972 if (i>=size || data[i]!=']') { return 0; }
1973 refid = data.substr(refIdStart,i-refIdStart);
1974 if (refid.isEmpty()) { return 0; }
1975 AUTO_TRACE_ADD("refid found {}",refid);
1976 //printf(" isLinkRef: found refid='%s'\n",qPrint(refid));
1977 i++;
1978 if (i>=size || data[i]!=':') { return 0; }
1979 i++;
1980
1981 // format: whitespace* \n? whitespace* (<url> | url)
1982 while (i<size && data[i]==' ') i++;
1983 if (i<size && data[i]=='\n')
1984 {
1985 i++;
1986 while (i<size && data[i]==' ') i++;
1987 }
1988 if (i>=size) { return 0; }
1989
1990 if (i<size && data[i]=='<') i++;
1991 size_t linkStart=i;
1992 while (i<size && data[i]!=' ' && data[i]!='\n') i++;
1993 size_t linkEnd=i;
1994 if (i<size && data[i]=='>') i++;
1995 if (linkStart==linkEnd) { return 0; } // empty link
1996 link = data.substr(linkStart,linkEnd-linkStart);
1997 AUTO_TRACE_ADD("link found {}",Trace::trunc(link));
1998 if (link=="@ref" || link=="\\ref")
1999 {
2000 size_t argStart=i;
2001 while (i<size && data[i]!='\n' && data[i]!='"') i++;
2002 link+=data.substr(argStart,i-argStart);
2003 }
2004
2005 title.clear();
2006
2007 // format: (whitespace* \n? whitespace* ( 'title' | "title" | (title) ))?
2008 size_t eol=0;
2009 while (i<size && data[i]==' ') i++;
2010 if (i<size && data[i]=='\n')
2011 {
2012 eol=i;
2013 i++;
2014 while (i<size && data[i]==' ') i++;
2015 }
2016 if (i>=size)
2017 {
2018 AUTO_TRACE_EXIT("result={}: end of isLinkRef while looking for title",i);
2019 return i; // end of buffer while looking for the optional title
2020 }
2021
2022 char c = data[i];
2023 if (c=='\'' || c=='"' || c=='(') // optional title present?
2024 {
2025 //printf(" start of title found! char='%c'\n",c);
2026 i++;
2027 if (c=='(') c=')'; // replace c by end character
2028 size_t titleStart=i;
2029 // search for end of the line
2030 while (i<size && data[i]!='\n') i++;
2031 eol = i;
2032
2033 // search back to matching character
2034 size_t end=i-1;
2035 while (end>titleStart && data[end]!=c) end--;
2036 if (end>titleStart)
2037 {
2038 title = data.substr(titleStart,end-titleStart);
2039 }
2040 AUTO_TRACE_ADD("title found {}",Trace::trunc(title));
2041 }
2042 while (i<size && data[i]==' ') i++;
2043 //printf("end of isLinkRef: i=%d size=%d data[i]='%c' eol=%d\n",
2044 // i,size,data[i],eol);
2045 if (i>=size) { AUTO_TRACE_EXIT("result={}",i); return i; } // end of buffer while ref id was found
2046 else if (eol>0) { AUTO_TRACE_EXIT("result={}",eol); return eol; } // end of line while ref id was found
2047 return 0; // invalid link ref
2048}
2049
2050static bool isHRuler(std::string_view data)
2051{
2052 AUTO_TRACE("data='{}'",Trace::trunc(data));
2053 size_t i=0;
2054 size_t size = data.size();
2055 if (size>0 && data[size-1]=='\n') size--; // ignore newline character
2056 while (i<size && data[i]==' ') i++;
2057 if (i>=size) { AUTO_TRACE_EXIT("result=false: empty line"); return false; } // empty line
2058 char c=data[i];
2059 if (c!='*' && c!='-' && c!='_')
2060 {
2061 AUTO_TRACE_EXIT("result=false: {} is not a hrule character",c);
2062 return false; // not a hrule character
2063 }
2064 int n=0;
2065 while (i<size)
2066 {
2067 if (data[i]==c)
2068 {
2069 n++; // count rule character
2070 }
2071 else if (data[i]!=' ')
2072 {
2073 AUTO_TRACE_EXIT("result=false: line contains non hruler characters");
2074 return false; // line contains non hruler characters
2075 }
2076 i++;
2077 }
2078 AUTO_TRACE_EXIT("result={}",n>=3);
2079 return n>=3; // at least 3 characters needed for a hruler
2080}
2081
2082QCString Markdown::Private::extractTitleId(QCString &title, int level, bool *pIsIdGenerated)
2083{
2084 AUTO_TRACE("title={} level={}",Trace::trunc(title),level);
2085 // match e.g. '{#id-b11} ' and capture 'id-b11'
2086 static const reg::Ex r2(R"({#(\a[\w-]*)}\s*$)");
2087 reg::Match match;
2088 std::string ti = title.str();
2089 if (reg::search(ti,match,r2))
2090 {
2091 std::string id = match[1].str();
2092 title = title.left(match.position());
2093 if (AnchorGenerator::instance().reserve(id)>0)
2094 {
2095 warn(fileName, lineNr, "An automatically generated id already has the name '{}'!", id);
2096 }
2097 //printf("found match id='%s' title=%s\n",qPrint(id),qPrint(title));
2098 AUTO_TRACE_EXIT("id={}",id);
2099 return id;
2100 }
2101 if (((level>0) && (level<=Config_getInt(TOC_INCLUDE_HEADINGS))) || (Config_getEnum(MARKDOWN_ID_STYLE)==MARKDOWN_ID_STYLE_t::GITHUB))
2102 {
2104 if (pIsIdGenerated) *pIsIdGenerated=true;
2105 //printf("auto-generated id='%s' title='%s'\n",qPrint(id),qPrint(title));
2106 AUTO_TRACE_EXIT("id={}",id);
2107 return id;
2108 }
2109 //printf("no id found in title '%s'\n",qPrint(title));
2110 return "";
2111}
2112
2113
2114int Markdown::Private::isAtxHeader(std::string_view data,
2115 QCString &header,QCString &id,bool allowAdjustLevel,bool *pIsIdGenerated)
2116{
2117 AUTO_TRACE("data='{}' header={} id={} allowAdjustLevel={}",Trace::trunc(data),Trace::trunc(header),id,allowAdjustLevel);
2118 size_t i = 0;
2119 int level = 0, blanks=0;
2120 const size_t size = data.size();
2121
2122 // find start of header text and determine heading level
2123 while (i<size && data[i]==' ') i++;
2124 if (i>=size || data[i]!='#')
2125 {
2126 return 0;
2127 }
2128 while (i < size && data[i] == '#')
2129 {
2130 i++;
2131 level++;
2132 }
2133 if (level>SectionType::MaxLevel) // too many #'s -> no section
2134 {
2135 return 0;
2136 }
2137 while (i < size && data[i] == ' ')
2138 {
2139 i++;
2140 blanks++;
2141 }
2142 if (level==1 && blanks==0)
2143 {
2144 return 0; // special case to prevent #someid seen as a header (see bug 671395)
2145 }
2146
2147 // find end of header text
2148 size_t end=i;
2149 while (end<size && data[end]!='\n') end++;
2150 while (end>i && (data[end-1]=='#' || data[end-1]==' ')) end--;
2151
2152 // store result
2153 header = data.substr(i,end-i);
2154 id = extractTitleId(header, level, pIsIdGenerated);
2155 if (!id.isEmpty()) // strip #'s between title and id
2156 {
2157 int idx=static_cast<int>(header.length())-1;
2158 while (idx>=0 && (header.at(idx)=='#' || header.at(idx)==' ')) idx--;
2159 header=header.left(idx+1);
2160 }
2161
2162 if (allowAdjustLevel && level==1 && indentLevel==-1)
2163 {
2164 // in case we find a `# Section` on a markdown page that started with the same level
2165 // header, we no longer need to artificially decrease the paragraph level.
2166 // So both
2167 // -------------------
2168 // # heading 1 <-- here we set g_indentLevel to -1
2169 // # heading 2 <-- here we set g_indentLevel back to 0 such that this will be a @section
2170 // -------------------
2171 // and
2172 // -------------------
2173 // # heading 1 <-- here we set g_indentLevel to -1
2174 // ## heading 2 <-- here we keep g_indentLevel at -1 such that @subsection will be @section
2175 // -------------------
2176 // will convert to
2177 // -------------------
2178 // @page md_page Heading 1
2179 // @section autotoc_md1 Heading 2
2180 // -------------------
2181
2182 indentLevel=0;
2183 }
2184 int res = level+indentLevel;
2185 AUTO_TRACE_EXIT("result={}",res);
2186 return res;
2187}
2188
2189static bool isEmptyLine(std::string_view data)
2190{
2191 AUTO_TRACE("data='{}'",Trace::trunc(data));
2192 size_t i=0;
2193 while (i<data.size())
2194 {
2195 if (data[i]=='\n') { AUTO_TRACE_EXIT("true"); return true; }
2196 if (data[i]!=' ') { AUTO_TRACE_EXIT("false"); return false; }
2197 i++;
2198 }
2199 AUTO_TRACE_EXIT("true");
2200 return true;
2201}
2202
2203#define isLiTag(i) \
2204 (data[(i)]=='<' && \
2205 (data[(i)+1]=='l' || data[(i)+1]=='L') && \
2206 (data[(i)+2]=='i' || data[(i)+2]=='I') && \
2207 (data[(i)+3]=='>'))
2208
2209// compute the indent from the start of the input, excluding list markers
2210// such as -, -#, *, +, 1., and <li>
2211static size_t computeIndentExcludingListMarkers(std::string_view data)
2212{
2213 AUTO_TRACE("data='{}'",Trace::trunc(data));
2214 size_t i=0;
2215 const size_t size=data.size();
2216 size_t indent=0;
2217 bool isDigit=FALSE;
2218 bool isLi=FALSE;
2219 bool listMarkerSkipped=FALSE;
2220 while (i<size &&
2221 (data[i]==' ' || // space
2222 (!listMarkerSkipped && // first list marker
2223 (data[i]=='+' || data[i]=='-' || data[i]=='*' || // unordered list char
2224 (data[i]=='#' && i>0 && data[i-1]=='-') || // -# item
2225 (isDigit=(data[i]>='1' && data[i]<='9')) || // ordered list marker?
2226 (isLi=(size>=3 && i+3<size && isLiTag(i))) // <li> tag
2227 )
2228 )
2229 )
2230 )
2231 {
2232 if (isDigit) // skip over ordered list marker '10. '
2233 {
2234 size_t j=i+1;
2235 while (j<size && ((data[j]>='0' && data[j]<='9') || data[j]=='.'))
2236 {
2237 if (data[j]=='.') // should be end of the list marker
2238 {
2239 if (j+1<size && data[j+1]==' ') // valid list marker
2240 {
2241 listMarkerSkipped=TRUE;
2242 indent+=j+1-i;
2243 i=j+1;
2244 break;
2245 }
2246 else // not a list marker
2247 {
2248 break;
2249 }
2250 }
2251 j++;
2252 }
2253 }
2254 else if (isLi)
2255 {
2256 i+=3; // skip over <li>
2257 indent+=3;
2258 listMarkerSkipped=TRUE;
2259 }
2260 else if (data[i]=='-' && size>=2 && i+2<size && data[i+1]=='#' && data[i+2]==' ')
2261 { // case "-# "
2262 listMarkerSkipped=TRUE; // only a single list marker is accepted
2263 i++; // skip over #
2264 indent++;
2265 }
2266 else if (data[i]!=' ' && i+1<size && data[i+1]==' ')
2267 { // case "- " or "+ " or "* "
2268 listMarkerSkipped=TRUE; // only a single list marker is accepted
2269 }
2270 if (data[i]!=' ' && !listMarkerSkipped)
2271 { // end of indent
2272 break;
2273 }
2274 indent++;
2275 i++;
2276 }
2277 AUTO_TRACE_EXIT("result={}",indent);
2278 return indent;
2279}
2280
2281static size_t isListMarker(std::string_view data)
2282{
2283 AUTO_TRACE("data='{}'",Trace::trunc(data));
2284 size_t normalIndent = 0;
2285 while (normalIndent<data.size() && data[normalIndent]==' ') normalIndent++;
2286 size_t listIndent = computeIndentExcludingListMarkers(data);
2287 size_t result = listIndent>normalIndent ? listIndent : 0;
2288 AUTO_TRACE_EXIT("result={}",result);
2289 return result;
2290}
2291
2292static bool isEndOfList(std::string_view data)
2293{
2294 AUTO_TRACE("data='{}'",Trace::trunc(data));
2295 int dots=0;
2296 size_t i=0;
2297 // end of list marker is an otherwise empty line with a dot.
2298 while (i<data.size())
2299 {
2300 if (data[i]=='.')
2301 {
2302 dots++;
2303 }
2304 else if (data[i]=='\n')
2305 {
2306 break;
2307 }
2308 else if (data[i]!=' ' && data[i]!='\t') // bail out if the line is not empty
2309 {
2310 AUTO_TRACE_EXIT("result=false");
2311 return false;
2312 }
2313 i++;
2314 }
2315 AUTO_TRACE_EXIT("result={}",dots==1);
2316 return dots==1;
2317}
2318
2319static bool isFencedCodeBlock(std::string_view data,size_t refIndent,
2320 QCString &lang,size_t &start,size_t &end,size_t &offset,
2321 QCString &fileName,int lineNr)
2322{
2323 AUTO_TRACE("data='{}' refIndent={}",Trace::trunc(data),refIndent);
2324 const char dot = '.';
2325 auto isAlphaChar = [ ](char c) { return (c>='A' && c<='Z') || (c>='a' && c<='z'); };
2326 auto isAlphaNChar = [ ](char c) { return (c>='A' && c<='Z') || (c>='a' && c<='z') || (c>='0' && c<='9') || (c=='+'); };
2327 auto isLangChar = [&](char c) { return c==dot || isAlphaChar(c); };
2328 // rules: at least 3 ~~~, end of the block same amount of ~~~'s, otherwise
2329 // return FALSE
2330 size_t i=0;
2331 size_t indent=0;
2332 int startTildes=0;
2333 const size_t size = data.size();
2334 while (i < size && data[i] == ' ')
2335 {
2336 indent++;
2337 i++;
2338 }
2339 if (indent>=refIndent+4)
2340 {
2341 AUTO_TRACE_EXIT("result=false: content is part of code block indent={} refIndent={}",indent,refIndent);
2342 return FALSE;
2343 } // part of code block
2344 char tildaChar='~';
2345 if (i<size && data[i]=='`') tildaChar='`';
2346 while (i < size && data[i] == tildaChar)
2347 {
2348 startTildes++;
2349 i++;
2350 }
2351 if (startTildes<3)
2352 {
2353 AUTO_TRACE_EXIT("result=false: no fence marker found #tildes={}",startTildes);
2354 return FALSE;
2355 } // not enough tildes
2356 if (i<size && data[i]=='{') // extract .py from ```{.py} ... ```
2357 {
2358 i++; // skip over {
2359 if (data[i] == dot) i++; // skip over initial dot
2360 size_t startLang=i;
2361 while (i<size && (data[i]!='\n' && data[i]!='}')) i++; // find matching }
2362 if (i<size && data[i]=='}')
2363 {
2364 lang = data.substr(startLang,i-startLang);
2365 i++;
2366 }
2367 else // missing closing bracket, treat `{` as part of the content
2368 {
2369 i=startLang-1;
2370 lang="";
2371 }
2372 }
2373 else if (i<size && isLangChar(data[i])) /// extract python or .py from ```python...``` or ```.py...```
2374 {
2375 if (data[i] == dot) i++; // skip over initial dot
2376 size_t startLang=i;
2377 if (i<size && isAlphaChar(data[i])) //check first character of language specifier
2378 {
2379 i++;
2380 while (i<size && isAlphaNChar(data[i])) i++; // find end of language specifier
2381 }
2382 lang = data.substr(startLang,i-startLang);
2383 }
2384 else // no language specified
2385 {
2386 lang="";
2387 }
2388
2389 start=i;
2390 while (i<size)
2391 {
2392 if (data[i]==tildaChar)
2393 {
2394 end=i;
2395 int endTildes=0;
2396 while (i < size && data[i] == tildaChar)
2397 {
2398 endTildes++;
2399 i++;
2400 }
2401 while (i<size && data[i]==' ') i++;
2402 {
2403 if (endTildes==startTildes)
2404 {
2405 offset=i;
2406 AUTO_TRACE_EXIT("result=true: found end marker at offset {} lang='{}'",offset,lang);
2407 return true;
2408 }
2409 }
2410 }
2411 i++;
2412 }
2413 warn(fileName, lineNr, "Ending Inside a fenced code block. Maybe the end marker for the block is missing?");
2414 AUTO_TRACE_EXIT("result=false: no end marker found lang={}'",lang);
2415 return false;
2416}
2417
2418static bool isCodeBlock(std::string_view data, size_t offset,size_t &indent)
2419{
2420 AUTO_TRACE("data='{}' offset={}",Trace::trunc(data),offset);
2421 //printf("<isCodeBlock(offset=%d,size=%d,indent=%d)\n",offset,size,indent);
2422 // determine the indent of this line
2423 size_t i=0;
2424 size_t indent0=0;
2425 const size_t size = data.size();
2426 while (i < size && data[i] == ' ')
2427 {
2428 indent0++;
2429 i++;
2430 }
2431
2432 if (indent0<codeBlockIndent)
2433 {
2434 AUTO_TRACE_EXIT("result={}: line is not indented enough {}<4",false,indent0);
2435 return false;
2436 }
2437 if (indent0>=size || data[indent0]=='\n') // empty line does not start a code block
2438 {
2439 AUTO_TRACE_EXIT("result={}: only spaces at the end of a comment block",false);
2440 return false;
2441 }
2442
2443 i=offset;
2444 int nl=0;
2445 int nl_pos[3];
2446 int offset_i = static_cast<int>(offset);
2447 // search back 3 lines and remember the start of lines -1 and -2
2448 while (i>0 && nl<3) // i counts down from offset to 1
2449 {
2450 int j = static_cast<int>(i)-offset_i-1; // j counts from -1 to -offset
2451 // since j can be negative we need to rewrap data in a std::string_view
2452 size_t nl_size = isNewline(std::string_view(data.data()+j,data.size()-j));
2453 if (nl_size>0)
2454 {
2455 nl_pos[nl++]=j+static_cast<int>(nl_size);
2456 }
2457 i--;
2458 }
2459
2460 // if there are only 2 preceding lines, then line -2 starts at -offset
2461 if (i==0 && nl==2) nl_pos[nl++]=-offset_i;
2462
2463 if (nl==3) // we have at least 2 preceding lines
2464 {
2465 //printf(" positions: nl_pos=[%d,%d,%d] line[-2]='%s' line[-1]='%s'\n",
2466 // nl_pos[0],nl_pos[1],nl_pos[2],
2467 // qPrint(QCString(data+nl_pos[1]).left(nl_pos[0]-nl_pos[1]-1)),
2468 // qPrint(QCString(data+nl_pos[2]).left(nl_pos[1]-nl_pos[2]-1)));
2469
2470 // check that line -1 is empty
2471 // Note that the offset is negative so we need to rewrap the string view
2472 if (!isEmptyLine(std::string_view(data.data()+nl_pos[1],nl_pos[0]-nl_pos[1]-1)))
2473 {
2474 AUTO_TRACE_EXIT("result={}",FALSE);
2475 return FALSE;
2476 }
2477
2478 // determine the indent of line -2
2479 // Note that the offset is negative so we need to rewrap the string view
2480 indent=std::max(indent,computeIndentExcludingListMarkers(
2481 std::string_view(data.data()+nl_pos[2],nl_pos[1]-nl_pos[2])));
2482
2483 //printf(">isCodeBlock local_indent %d>=%d+%d=%d\n",
2484 // indent0,indent,codeBlockIndent,indent0>=indent+codeBlockIndent);
2485 // if the difference is >4 spaces -> code block
2486 bool res = indent0>=indent+codeBlockIndent;
2487 AUTO_TRACE_EXIT("result={}: code block if indent difference >4 spaces",res);
2488 return res;
2489 }
2490 else // not enough lines to determine the relative indent, use global indent
2491 {
2492 // check that line -1 is empty
2493 // Note that the offset is negative so we need to rewrap the string view
2494 if (nl==1 && !isEmptyLine(std::string_view(data.data()-offset,offset-1)))
2495 {
2496 AUTO_TRACE_EXIT("result=false");
2497 return FALSE;
2498 }
2499 //printf(">isCodeBlock global indent %d>=%d+4=%d nl=%d\n",
2500 // indent0,indent,indent0>=indent+4,nl);
2501 bool res = indent0>=indent+codeBlockIndent;
2502 AUTO_TRACE_EXIT("result={}: code block if indent difference >4 spaces",res);
2503 return res;
2504 }
2505}
2506
2507/** Finds the location of the table's contains in the string \a data.
2508 * Only one line will be inspected.
2509 * @param[in] data pointer to the string buffer.
2510 * @param[out] start offset of the first character of the table content
2511 * @param[out] end offset of the last character of the table content
2512 * @param[out] columns number of table columns found
2513 * @returns The offset until the next line in the buffer.
2514 */
2515static size_t findTableColumns(std::string_view data,size_t &start,size_t &end,size_t &columns)
2516{
2517 AUTO_TRACE("data='{}'",Trace::trunc(data));
2518 const size_t size = data.size();
2519 size_t i=0,n=0;
2520 // find start character of the table line
2521 while (i<size && data[i]==' ') i++;
2522 if (i < size && data[i] == '|' && data[i] != '\n')
2523 {
2524 i++;
2525 n++; // leading | does not count
2526 }
2527 start = i;
2528
2529 // find end character of the table line
2530 size_t j = 0;
2531 while (i<size && (j = isNewline(data.substr(i)))==0) i++;
2532 size_t eol=i+j;
2533
2534 if (j>0 && i>0) i--; // move i to point before newline
2535 while (i>0 && data[i]==' ') i--;
2536 if (i > 0 && data[i - 1] != '\\' && data[i] == '|')
2537 {
2538 i--;
2539 n++; // trailing or escaped | does not count
2540 }
2541 end = i;
2542
2543 // count columns between start and end
2544 columns=0;
2545 if (end>start)
2546 {
2547 i=start;
2548 while (i<=end) // look for more column markers
2549 {
2550 if (data[i]=='|' && (i==0 || data[i-1]!='\\')) columns++;
2551 if (columns==1) columns++; // first | make a non-table into a two column table
2552 i++;
2553 }
2554 }
2555 if (n==2 && columns==0) // table row has | ... |
2556 {
2557 columns++;
2558 }
2559 AUTO_TRACE_EXIT("eol={} start={} end={} columns={}",eol,start,end,columns);
2560 return eol;
2561}
2562
2563/** Returns TRUE iff data points to the start of a table block */
2564static bool isTableBlock(std::string_view data)
2565{
2566 AUTO_TRACE("data='{}'",Trace::trunc(data));
2567 size_t cc0=0, start=0, end=0;
2568
2569 // the first line should have at least two columns separated by '|'
2570 size_t i = findTableColumns(data,start,end,cc0);
2571 if (i>=data.size() || cc0<1)
2572 {
2573 AUTO_TRACE_EXIT("result=false: no |'s in the header");
2574 return FALSE;
2575 }
2576
2577 size_t cc1 = 0;
2578 size_t ret = findTableColumns(data.substr(i),start,end,cc1);
2579 size_t j=i+start;
2580 // separator line should consist of |, - and : and spaces only
2581 while (j<=end+i)
2582 {
2583 if (data[j]!=':' && data[j]!='-' && data[j]!='|' && data[j]!=' ')
2584 {
2585 AUTO_TRACE_EXIT("result=false: invalid character '{}'",data[j]);
2586 return FALSE; // invalid characters in table separator
2587 }
2588 j++;
2589 }
2590 if (cc1!=cc0) // number of columns should be same as previous line
2591 {
2592 AUTO_TRACE_EXIT("result=false: different number of columns as previous line {}!={}",cc1,cc0);
2593 return FALSE;
2594 }
2595
2596 i+=ret; // goto next line
2597 size_t cc2 = 0;
2598 findTableColumns(data.substr(i),start,end,cc2);
2599
2600 AUTO_TRACE_EXIT("result={}",cc1==cc2);
2601 return cc1==cc2;
2602}
2603
2604size_t Markdown::Private::writeTableBlock(std::string_view data)
2605{
2606 AUTO_TRACE("data='{}'",Trace::trunc(data));
2607 const size_t size = data.size();
2608
2609 size_t columns=0, start=0, end=0;
2610 size_t i = findTableColumns(data,start,end,columns);
2611 size_t headerStart = start;
2612 size_t headerEnd = end;
2613
2614 // read cell alignments
2615 size_t cc = 0;
2616 size_t ret = findTableColumns(data.substr(i),start,end,cc);
2617 size_t k=0;
2618 std::vector<Alignment> columnAlignment(columns);
2619
2620 bool leftMarker=false, rightMarker=false, startFound=false;
2621 size_t j=start+i;
2622 while (j<=end+i)
2623 {
2624 if (!startFound)
2625 {
2626 if (data[j]==':') { leftMarker=TRUE; startFound=TRUE; }
2627 if (data[j]=='-') startFound=TRUE;
2628 //printf(" data[%d]=%c startFound=%d\n",j,data[j],startFound);
2629 }
2630 if (data[j]=='-') rightMarker=FALSE;
2631 else if (data[j]==':') rightMarker=TRUE;
2632 if (j<=end+i && (data[j]=='|' && (j==0 || data[j-1]!='\\')))
2633 {
2634 if (k<columns)
2635 {
2636 columnAlignment[k] = markersToAlignment(leftMarker,rightMarker);
2637 //printf("column[%d] alignment=%d\n",k,columnAlignment[k]);
2638 leftMarker=FALSE;
2639 rightMarker=FALSE;
2640 startFound=FALSE;
2641 }
2642 k++;
2643 }
2644 j++;
2645 }
2646 if (k<columns)
2647 {
2648 columnAlignment[k] = markersToAlignment(leftMarker,rightMarker);
2649 //printf("column[%d] alignment=%d\n",k,columnAlignment[k]);
2650 }
2651 // proceed to next line
2652 i+=ret;
2653
2654 // Store the table cell information by row then column. This
2655 // allows us to handle row spanning.
2656 std::vector<std::vector<TableCell> > tableContents;
2657
2658 size_t m = headerStart;
2659 std::vector<TableCell> headerContents(columns);
2660 for (k=0;k<columns;k++)
2661 {
2662 while (m<=headerEnd && (data[m]!='|' || (m>0 && data[m-1]=='\\')))
2663 {
2664 headerContents[k].cellText += data[m++];
2665 }
2666 m++;
2667 // do the column span test before stripping white space
2668 // || is spanning columns, | | is not
2669 headerContents[k].colSpan = headerContents[k].cellText.isEmpty();
2670 headerContents[k].cellText = headerContents[k].cellText.stripWhiteSpace();
2671 }
2672 tableContents.push_back(headerContents);
2673
2674 // write table cells
2675 while (i<size)
2676 {
2677 ret = findTableColumns(data.substr(i),start,end,cc);
2678 if (cc!=columns) break; // end of table
2679
2680 j=start+i;
2681 k=0;
2682 std::vector<TableCell> rowContents(columns);
2683 while (j<=end+i)
2684 {
2685 if (j<=end+i && (data[j]=='|' && (j==0 || data[j-1]!='\\')))
2686 {
2687 // do the column span test before stripping white space
2688 // || is spanning columns, | | is not
2689 rowContents[k].colSpan = rowContents[k].cellText.isEmpty();
2690 rowContents[k].cellText = rowContents[k].cellText.stripWhiteSpace();
2691 k++;
2692 } // if (j<=end+i && (data[j]=='|' && (j==0 || data[j-1]!='\\')))
2693 else
2694 {
2695 rowContents[k].cellText += data[j];
2696 } // else { if (j<=end+i && (data[j]=='|' && (j==0 || data[j-1]!='\\'))) }
2697 j++;
2698 } // while (j<=end+i)
2699 // do the column span test before stripping white space
2700 // || is spanning columns, | | is not
2701 rowContents[k].colSpan = rowContents[k].cellText.isEmpty();
2702 rowContents[k].cellText = rowContents[k].cellText.stripWhiteSpace();
2703 tableContents.push_back(rowContents);
2704
2705 // proceed to next line
2706 i+=ret;
2707 }
2708
2709 out+="<table class=\"markdownTable\">";
2710 QCString cellTag("th"), cellClass("class=\"markdownTableHead");
2711 for (size_t row = 0; row < tableContents.size(); row++)
2712 {
2713 if (row)
2714 {
2715 if (row % 2)
2716 {
2717 out+="\n<tr class=\"markdownTableRowOdd\">";
2718 }
2719 else
2720 {
2721 out+="\n<tr class=\"markdownTableRowEven\">";
2722 }
2723 }
2724 else
2725 {
2726 out+="\n <tr class=\"markdownTableHead\">";
2727 }
2728 for (size_t c = 0; c < columns; c++)
2729 {
2730 // save the cell text for use after column span computation
2731 QCString cellText(tableContents[row][c].cellText);
2732
2733 // Row span handling. Spanning rows will contain a caret ('^').
2734 // If the current cell contains just a caret, this is part of an
2735 // earlier row's span and the cell should not be added to the
2736 // output.
2737 if (tableContents[row][c].cellText == "^")
2738 {
2739 continue;
2740 }
2741 if (tableContents[row][c].colSpan)
2742 {
2743 int cr = static_cast<int>(c);
2744 while ( cr >= 0 && tableContents[row][cr].colSpan)
2745 {
2746 cr--;
2747 };
2748 if (cr >= 0 && tableContents[row][cr].cellText == "^") continue;
2749 }
2750 size_t rowSpan = 1, spanRow = row+1;
2751 while ((spanRow < tableContents.size()) &&
2752 (tableContents[spanRow][c].cellText == "^"))
2753 {
2754 spanRow++;
2755 rowSpan++;
2756 }
2757
2758 out+=" <" + cellTag + " " + cellClass;
2759 // use appropriate alignment style
2760 switch (columnAlignment[c])
2761 {
2762 case Alignment::Left: out+="Left\""; break;
2763 case Alignment::Right: out+="Right\""; break;
2764 case Alignment::Center: out+="Center\""; break;
2765 case Alignment::None: out+="None\""; break;
2766 }
2767
2768 if (rowSpan > 1)
2769 {
2770 QCString spanStr;
2771 spanStr.setNum(rowSpan);
2772 out+=" rowspan=\"" + spanStr + "\"";
2773 }
2774 // Column span handling, assumes that column spans will have
2775 // empty strings, which would indicate the sequence "||", used
2776 // to signify spanning columns.
2777 size_t colSpan = 1;
2778 while ((c+1 < columns) && tableContents[row][c+1].colSpan)
2779 {
2780 c++;
2781 colSpan++;
2782 }
2783 if (colSpan > 1)
2784 {
2785 QCString spanStr;
2786 spanStr.setNum(colSpan);
2787 out+=" colspan=\"" + spanStr + "\"";
2788 }
2789 // need at least one space on either side of the cell text in
2790 // order for doxygen to do other formatting
2791 out+="> " + cellText + " \\ilinebr </" + cellTag + ">";
2792 }
2793 cellTag = "td";
2794 cellClass = "class=\"markdownTableBody";
2795 out+=" </tr>";
2796 }
2797 out+="</table>\n";
2798
2799 AUTO_TRACE_EXIT("i={}",i);
2800 return i;
2801}
2802
2803
2804static bool hasLineBreak(std::string_view data)
2805{
2806 AUTO_TRACE("data='{}'",Trace::trunc(data));
2807 size_t i=0;
2808 size_t j=0;
2809 // search for end of line and also check if it is not a completely blank
2810 while (i<data.size() && data[i]!='\n')
2811 {
2812 if (data[i]!=' ' && data[i]!='\t') j++; // some non whitespace
2813 i++;
2814 }
2815 if (i>=data.size()) { return 0; } // empty line
2816 if (i<2) { return 0; } // not long enough
2817 bool res = (j>0 && data[i-1]==' ' && data[i-2]==' '); // non blank line with at two spaces at the end
2818 AUTO_TRACE_EXIT("result={}",res);
2819 return res;
2820}
2821
2822
2824{
2825 AUTO_TRACE("data='{}'",Trace::trunc(data));
2826 int level=0;
2827 QCString header;
2828 QCString id;
2829 if (isHRuler(data))
2830 {
2831 out+="<hr>\n";
2832 }
2833 else if ((level=isAtxHeader(data,header,id,TRUE)))
2834 {
2835 QCString hTag;
2836 if (!id.isEmpty())
2837 {
2838 switch (level)
2839 {
2840 case SectionType::Section: out+="@section "; break;
2841 case SectionType::Subsection: out+="@subsection "; break;
2842 case SectionType::Subsubsection: out+="@subsubsection "; break;
2843 case SectionType::Paragraph: out+="@paragraph "; break;
2844 case SectionType::Subparagraph: out+="@subparagraph "; break;
2845 case SectionType::Subsubparagraph: out+="@subsubparagraph "; break;
2846 }
2847 out+=id;
2848 out+=" ";
2849 out+=header;
2850 out+="\n";
2851 }
2852 else
2853 {
2854 hTag.sprintf("h%d",level);
2855 out+="<"+hTag+">";
2856 out+=header;
2857 out+="</"+hTag+">\n";
2858 }
2859 }
2860 else if (data.size()>0) // nothing interesting -> just output the line
2861 {
2862 size_t tmpSize = data.size();
2863 if (data[data.size()-1] == '\n') tmpSize--;
2864 out+=data.substr(0,tmpSize);
2865
2866 if (hasLineBreak(data))
2867 {
2868 out+="\\ilinebr<br>";
2869 }
2870 if (tmpSize != data.size()) out+='\n';
2871 }
2872}
2873
2874static const std::unordered_map<std::string,std::string> g_quotationHeaderMap = {
2875 // GitHub style Doxygen command
2876 { "[!note]", "\\note" },
2877 { "[!warning]", "\\warning" },
2878 { "[!tip]", "\\remark" },
2879 { "[!caution]", "\\attention" },
2880 { "[!important]", "\\important" }
2881};
2882
2883size_t Markdown::Private::writeBlockQuote(std::string_view data)
2884{
2885 AUTO_TRACE("data='{}'",Trace::trunc(data));
2886 size_t i=0;
2887 int curLevel=0;
2888 size_t end=0;
2889 const size_t size = data.size();
2890 std::string startCmd;
2891 int isGitHubAlert = false;
2892 int isGitHubFirst = false;
2893 while (i<size)
2894 {
2895 // find end of this line
2896 end=i+1;
2897 while (end<=size && data[end-1]!='\n') end++;
2898 size_t j=i;
2899 int level=0;
2900 size_t indent=i;
2901 // compute the quoting level
2902 while (j<end && (data[j]==' ' || data[j]=='>'))
2903 {
2904 if (data[j]=='>') { level++; indent=j+1; }
2905 else if (j>0 && data[j-1]=='>') indent=j+1;
2906 j++;
2907 }
2908 if (indent>0 && j>0 && data[j-1]=='>' &&
2909 !(j==size || data[j]=='\n')) // disqualify last > if not followed by space
2910 {
2911 indent--;
2912 level--;
2913 j--;
2914 }
2915 AUTO_TRACE_ADD("indent={} i={} j={} end={} level={} line={}",indent,i,j,end,level,Trace::trunc(&data[i]));
2916 if (level==0 && j<end-1 && !isListMarker(data.substr(j)) && !isHRuler(data.substr(j)))
2917 {
2918 level = curLevel; // lazy
2919 }
2920 if (level==1)
2921 {
2922 QCString txt = stripWhiteSpace(data.substr(indent,end-indent));
2923 auto it = g_quotationHeaderMap.find(txt.lower().str()); // TODO: in C++20 the std::string can be dropped
2924 if (it != g_quotationHeaderMap.end())
2925 {
2926 isGitHubAlert = true;
2927 isGitHubFirst = true;
2928 startCmd = it->second;
2929 }
2930 }
2931 if (level>curLevel) // quote level increased => add start markers
2932 {
2933 if (level!=1 || !isGitHubAlert) // normal block quote
2934 {
2935 for (int l=curLevel;l<level-1;l++)
2936 {
2937 out+="<blockquote>";
2938 }
2939 out += "<blockquote>&zwj;"; // empty blockquotes are also shown
2940 }
2941 else if (!startCmd.empty()) // GitHub style alert
2942 {
2943 out += startCmd + " ";
2944 }
2945 }
2946 else if (level<curLevel) // quote level decreased => add end markers
2947 {
2948 int decrLevel = curLevel;
2949 if (level==0 && isGitHubAlert)
2950 {
2951 decrLevel--;
2952 }
2953 for (int l=level;l<decrLevel;l++)
2954 {
2955 out += "</blockquote>\\ilinebr ";
2956 }
2957 }
2958 if (level==0)
2959 {
2960 curLevel=0;
2961 break; // end of quote block
2962 }
2963 // copy line without quotation marks
2964 if (curLevel!=0 || !isGitHubAlert)
2965 {
2966 std::string_view txt = data.substr(indent,end-indent);
2967 if (stripWhiteSpace(txt).empty() && !startCmd.empty())
2968 {
2969 if (!isGitHubFirst) out += "<br>";
2970 out += "<br>\n";
2971 }
2972 else
2973 {
2974 out += txt;
2975 }
2976 isGitHubFirst = false;
2977 }
2978 else // GitHub alert section
2979 {
2980 out+= "\n";
2981 }
2982 curLevel=level;
2983 // proceed with next line
2984 i=end;
2985 }
2986 // end of comment within blockquote => add end markers
2987 if (isGitHubAlert) // GitHub alert doesn't have a blockquote
2988 {
2989 curLevel--;
2990 }
2991 for (int l=0;l<curLevel;l++)
2992 {
2993 out+="</blockquote>";
2994 }
2995 AUTO_TRACE_EXIT("i={}",i);
2996 return i;
2997}
2998
2999// For code blocks that are outputted as part of an indented include or snippet command, we need to filter out
3000// the location string, i.e. '\ifile "..." \iline \ilinebr'.
3001bool skipOverFileAndLineCommands(std::string_view data,size_t indent,size_t &offset,std::string &location)
3002{
3003 size_t i = offset;
3004 size_t size = data.size();
3005 while (i<data.size() && data[i]==' ') i++;
3006 if (literal_at(data.substr(i),"\\ifile \""))
3007 {
3008 size_t locStart = i;
3009 if (i>offset) locStart--; // include the space before \ifile
3010 i+=8;
3011 bool found=false;
3012 while (i+9<size && data[i]!='\n')
3013 {
3014 if (literal_at(data.substr(i),"\\ilinebr "))
3015 {
3016 found=true;
3017 break;
3018 }
3019 i++;
3020 }
3021 if (found)
3022 {
3023 i+=9;
3024 location=data.substr(locStart,i-locStart);
3025 location+='\n';
3026 while (indent > 0 && i < size && data[i] == ' ')
3027 {
3028 i++;
3029 indent--;
3030 }
3031 if (i<size && data[i]=='\n') i++;
3032 offset = i;
3033 return true;
3034 }
3035 }
3036 return false;
3037}
3038
3039size_t Markdown::Private::writeCodeBlock(std::string_view data,size_t refIndent)
3040{
3041 AUTO_TRACE("data='{}' refIndent={}",Trace::trunc(data),refIndent);
3042 const size_t size = data.size();
3043 size_t i=0;
3044 // no need for \ilinebr here as the previous line was empty and was skipped
3045 out+="@iverbatim\n";
3046 int emptyLines=0;
3047 std::string location;
3048 while (i<size)
3049 {
3050 // find end of this line
3051 size_t end=i+1;
3052 while (end<=size && data[end-1]!='\n') end++;
3053 size_t j=i;
3054 size_t indent=0;
3055 while (j < end && data[j] == ' ')
3056 {
3057 j++;
3058 indent++;
3059 }
3060 //printf("j=%d end=%d indent=%d refIndent=%d tabSize=%d data={%s}\n",
3061 // j,end,indent,refIndent,Config_getInt(TAB_SIZE),qPrint(QCString(data+i).left(end-i-1)));
3062 if (j==end-1) // empty line
3063 {
3064 emptyLines++;
3065 i=end;
3066 }
3067 else if (indent>=refIndent+codeBlockIndent) // enough indent to continue the code block
3068 {
3069 while (emptyLines>0) // write skipped empty lines
3070 {
3071 // add empty line
3072 out+="\n";
3073 emptyLines--;
3074 }
3075 // add code line minus the indent
3076 size_t offset = i+refIndent+codeBlockIndent;
3077 std::string lineLoc;
3078 if (skipOverFileAndLineCommands(data,codeBlockIndent,offset,lineLoc))
3079 {
3080 location = lineLoc;
3081 }
3082 out+=data.substr(offset,end-offset);
3083 i=end;
3084 }
3085 else // end of code block
3086 {
3087 break;
3088 }
3089 }
3090 out+="@endiverbatim";
3091 if (!location.empty())
3092 {
3093 out+=location;
3094 }
3095 else
3096 {
3097 out+="\\ilinebr ";
3098 }
3099 while (emptyLines>0) // write skipped empty lines
3100 {
3101 // add empty line
3102 out+="\n";
3103 emptyLines--;
3104 }
3105 AUTO_TRACE_EXIT("i={}",i);
3106 return i;
3107}
3108
3109// start searching for the end of the line start at offset \a i
3110// keeping track of possible blocks that need to be skipped.
3111size_t Markdown::Private::findEndOfLine(std::string_view data,size_t offset)
3112{
3113 AUTO_TRACE("data='{}'",Trace::trunc(data));
3114 // find end of the line
3115 const size_t size = data.size();
3116 size_t nb=0, end=offset+1, j=0;
3117 while (end<=size && (j=isNewline(data.substr(end-1)))==0)
3118 {
3119 // while looking for the end of the line we might encounter a block
3120 // that needs to be passed unprocessed.
3121 if ((data[end-1]=='\\' || data[end-1]=='@') && // command
3122 (end<=1 || (data[end-2]!='\\' && data[end-2]!='@')) // not escaped
3123 )
3124 {
3125 QCString endBlockName = isBlockCommand(data.substr(end-1),end-1);
3126 end++;
3127 if (!endBlockName.isEmpty())
3128 {
3129 size_t l = endBlockName.length();
3130 for (;end+l+1<size;end++) // search for end of block marker
3131 {
3132 if ((data[end]=='\\' || data[end]=='@') &&
3133 data[end-1]!='\\' && data[end-1]!='@'
3134 )
3135 {
3136 if (qstrncmp(&data[end+1],endBlockName.data(),l)==0)
3137 {
3138 // found end marker, skip over this block
3139 //printf("feol.block out={%s}\n",qPrint(QCString(data+i).left(end+l+1-i)));
3140 end = end + l + 2;
3141 break;
3142 }
3143 }
3144 }
3145 }
3146 }
3147 else if (nb==0 && data[end-1]=='<' && size>=6 && end+6<size &&
3148 (end<=1 || (data[end-2]!='\\' && data[end-2]!='@'))
3149 )
3150 {
3151 if (tolower(data[end])=='p' && tolower(data[end+1])=='r' &&
3152 tolower(data[end+2])=='e' && (data[end+3]=='>' || data[end+3]==' ')) // <pre> tag
3153 {
3154 // skip part until including </pre>
3155 end = end + processHtmlTagWrite(data.substr(end-1),end-1,false);
3156 break;
3157 }
3158 else
3159 {
3160 end++;
3161 }
3162 }
3163 else if (nb==0 && data[end-1]=='`')
3164 {
3165 while (end <= size && data[end - 1] == '`')
3166 {
3167 end++;
3168 nb++;
3169 }
3170 }
3171 else if (nb>0 && data[end-1]=='`')
3172 {
3173 size_t enb=0;
3174 while (end <= size && data[end - 1] == '`')
3175 {
3176 end++;
3177 enb++;
3178 }
3179 if (enb==nb) nb=0;
3180 }
3181 else
3182 {
3183 end++;
3184 }
3185 }
3186 if (j>0) end+=j-1;
3187 AUTO_TRACE_EXIT("offset={} end={}",offset,end);
3188 return end;
3189}
3190
3191void Markdown::Private::writeFencedCodeBlock(std::string_view data,std::string_view lang,
3192 size_t blockStart,size_t blockEnd)
3193{
3194 AUTO_TRACE("data='{}' lang={} blockStart={} blockEnd={}",Trace::trunc(data),lang,blockStart,blockEnd);
3195 if (!lang.empty() && lang[0]=='.') lang=lang.substr(1);
3196 const size_t size=data.size();
3197 size_t i=0;
3198 while (i<size && (data[i]==' ' || data[i]=='\t'))
3199 {
3200 out+=data[i++];
3201 blockStart--;
3202 blockEnd--;
3203 }
3204 out+="@icode";
3205 if (!lang.empty())
3206 {
3207 out+="{"+lang+"}";
3208 }
3209 out+=" ";
3210 addStrEscapeUtf8Nbsp(data.substr(blockStart+i,blockEnd-blockStart));
3211 out+="@endicode ";
3212}
3213
3214QCString Markdown::Private::processQuotations(std::string_view data,size_t refIndent)
3215{
3216 AUTO_TRACE("data='{}' refIndex='{}'",Trace::trunc(data),refIndent);
3217 out.clear();
3218 size_t i=0,end=0;
3219 size_t pi=std::string::npos;
3220 bool newBlock = false;
3221 bool insideList = false;
3222 size_t currentIndent = refIndent;
3223 size_t listIndent = refIndent;
3224 const size_t size = data.size();
3225 QCString lang;
3226 while (i<size)
3227 {
3228 end = findEndOfLine(data,i);
3229 // line is now found at [i..end)
3230
3231 size_t lineIndent=0;
3232 while (lineIndent<end && data[i+lineIndent]==' ') lineIndent++;
3233 //printf("** lineIndent=%d line=(%s)\n",lineIndent,qPrint(QCString(data+i).left(end-i)));
3234
3235 if (newBlock)
3236 {
3237 //printf("** end of block\n");
3238 if (insideList && lineIndent<currentIndent) // end of list
3239 {
3240 //printf("** end of list\n");
3241 currentIndent = refIndent;
3242 insideList = false;
3243 }
3244 newBlock = false;
3245 }
3246
3247 if ((listIndent=isListMarker(data.substr(i,end-i)))) // see if we need to increase the indent level
3248 {
3249 if (listIndent<currentIndent+4)
3250 {
3251 //printf("** start of list\n");
3252 insideList = true;
3253 currentIndent = listIndent;
3254 }
3255 }
3256 else if (isEndOfList(data.substr(i,end-i)))
3257 {
3258 //printf("** end of list\n");
3259 insideList = false;
3260 currentIndent = listIndent;
3261 }
3262 else if (isEmptyLine(data.substr(i,end-i)))
3263 {
3264 //printf("** new block\n");
3265 newBlock = true;
3266 }
3267 //printf("currentIndent=%d listIndent=%d refIndent=%d\n",currentIndent,listIndent,refIndent);
3268
3269 if (pi!=std::string::npos)
3270 {
3271 size_t blockStart=0, blockEnd=0, blockOffset=0;
3272 if (isFencedCodeBlock(data.substr(pi),currentIndent,lang,blockStart,blockEnd,blockOffset,fileName,lineNr))
3273 {
3274 auto addSpecialCommand = [&](const QCString &startCmd,const QCString &endCmd)
3275 {
3276 size_t cmdPos = pi+blockStart+1;
3277 QCString pl = data.substr(cmdPos,blockEnd-blockStart-1);
3278 size_t ii = 0;
3279 int nl = 1;
3280 // check for absence of start command, either @start<cmd>, or \\start<cmd>
3281 while (ii<pl.length() && qisspace(pl[ii]))
3282 {
3283 if (pl[ii]=='\n') nl++;
3284 ii++; // skip leading whitespace
3285 }
3286 bool addNewLines = false;
3287 if (ii+startCmd.length()>=pl.length() || // no room for start command
3288 (pl[ii]!='\\' && pl[ii]!='@') || // no @ or \ after whitespace
3289 qstrncmp(pl.data()+ii+1,startCmd.data(),startCmd.length())!=0) // no start command
3290 {
3291 // input: output:
3292 // ----------------------------------------------------
3293 // ```{plantuml} => @startuml
3294 // A->B A->B
3295 // ``` @enduml
3296 // ----------------------------------------------------
3297 pl = "@"+startCmd+"\n" + pl + "@"+endCmd;
3298 addNewLines = false;
3299 }
3300 else // we have a @start... command inside the code block
3301 {
3302 // input: output:
3303 // ----------------------------------------------------
3304 // ```{plantuml} \n
3305 // \n
3306 // @startuml => @startuml
3307 // A->B A->B
3308 // @enduml @enduml
3309 // ``` \n
3310 // ----------------------------------------------------
3311 addNewLines = true;
3312 }
3313 if (addNewLines) for (int j=0;j<nl;j++) out+='\n';
3314 processSpecialCommand(pl.view().substr(ii),ii);
3315 if (addNewLines) out+='\n';
3316 };
3317
3318 if (!Config_getString(PLANTUML_JAR_PATH).isEmpty() && lang=="plantuml")
3319 {
3320 addSpecialCommand("startuml","enduml");
3321 }
3322 else if (Config_getBool(HAVE_DOT) && lang=="dot")
3323 {
3324 addSpecialCommand("dot","enddot");
3325 }
3326 else if (lang=="msc") // msc is built-in
3327 {
3328 addSpecialCommand("msc","endmsc");
3329 }
3330 else // normal code block
3331 {
3332 writeFencedCodeBlock(data.substr(pi),lang.view(),blockStart,blockEnd);
3333 }
3334 i=pi+blockOffset;
3335 pi=std::string::npos;
3336 end=i+1;
3337 continue;
3338 }
3339 else if (isBlockQuote(data.substr(pi,i-pi),currentIndent))
3340 {
3341 i = pi+writeBlockQuote(data.substr(pi));
3342 pi=std::string::npos;
3343 end=i+1;
3344 continue;
3345 }
3346 else
3347 {
3348 //printf("quote out={%s}\n",QCString(data+pi).left(i-pi).data());
3349 out+=data.substr(pi,i-pi);
3350 }
3351 }
3352 pi=i;
3353 i=end;
3354 }
3355 if (pi!=std::string::npos && pi<size) // deal with the last line
3356 {
3357 if (isBlockQuote(data.substr(pi),currentIndent))
3358 {
3359 writeBlockQuote(data.substr(pi));
3360 }
3361 else
3362 {
3363 if (QCString(data.substr(pi)).startsWith("```") || QCString(data.substr(pi)).startsWith("~~~"))
3364 {
3365 warn(fileName, lineNr, "Ending inside a fenced code block. Maybe the end marker for the block is missing?");
3366 }
3367 out+=data.substr(pi);
3368 }
3369 }
3370
3371 //printf("Process quotations\n---- input ----\n%s\n---- output ----\n%s\n------------\n",
3372 // qPrint(s),prv->out.get());
3373
3374 return out;
3375}
3376
3377QCString Markdown::Private::processBlocks(std::string_view data,const size_t indent)
3378{
3379 AUTO_TRACE("data='{}' indent={}",Trace::trunc(data),indent);
3380 out.clear();
3381 size_t pi = std::string::npos;
3382 QCString id,link,title;
3383
3384#if 0 // commented out, since starting with a comment block is probably a usage error
3385 // see also http://stackoverflow.com/q/20478611/784672
3386
3387 // special case when the documentation starts with a code block
3388 // since the first line is skipped when looking for a code block later on.
3389 if (end>codeBlockIndent && isCodeBlock(data,0,end,blockIndent))
3390 {
3391 i=writeCodeBlock(out,data,size,blockIndent);
3392 end=i+1;
3393 pi=-1;
3394 }
3395#endif
3396
3397 size_t currentIndent = indent;
3398 size_t listIndent = indent;
3399 bool insideList = false;
3400 bool newBlock = false;
3401 // process each line
3402 size_t i=0;
3403 while (i<data.size())
3404 {
3405 size_t end = findEndOfLine(data,i);
3406 // line is now found at [i..end)
3407
3408 size_t lineIndent=0;
3409 int level = 0;
3410 while (lineIndent<end && data[i+lineIndent]==' ') lineIndent++;
3411 //printf("** lineIndent=%d line=(%s)\n",lineIndent,qPrint(QCString(data+i).left(end-i)));
3412
3413 if (newBlock)
3414 {
3415 //printf("** end of block\n");
3416 if (insideList && lineIndent<currentIndent) // end of list
3417 {
3418 //printf("** end of list\n");
3419 currentIndent = indent;
3420 insideList = false;
3421 }
3422 newBlock = false;
3423 }
3424
3425 if ((listIndent=isListMarker(data.substr(i,end-i)))) // see if we need to increase the indent level
3426 {
3427 if (listIndent<currentIndent+4)
3428 {
3429 //printf("** start of list\n");
3430 insideList = true;
3431 currentIndent = listIndent;
3432 }
3433 }
3434 else if (isEndOfList(data.substr(i,end-i)))
3435 {
3436 //printf("** end of list\n");
3437 insideList = false;
3438 currentIndent = listIndent;
3439 }
3440 else if (isEmptyLine(data.substr(i,end-i)))
3441 {
3442 //printf("** new block\n");
3443 newBlock = true;
3444 }
3445
3446 //printf("indent=%d listIndent=%d blockIndent=%d\n",indent,listIndent,blockIndent);
3447
3448 //printf("findEndOfLine: pi=%d i=%d end=%d\n",pi,i,end);
3449
3450 if (pi!=std::string::npos)
3451 {
3452 size_t blockStart=0, blockEnd=0, blockOffset=0;
3453 QCString lang;
3454 size_t blockIndent = currentIndent;
3455 size_t ref = 0;
3456 //printf("isHeaderLine(%s)=%d\n",QCString(data+i).left(size-i).data(),level);
3457 QCString endBlockName;
3458 if (data[i]=='@' || data[i]=='\\') endBlockName = isBlockCommand(data.substr(i),i);
3459 if (!endBlockName.isEmpty())
3460 {
3461 // handle previous line
3462 if (isLinkRef(data.substr(pi,i-pi),id,link,title))
3463 {
3464 linkRefs.emplace(id.lower().str(),LinkRef(link,title));
3465 }
3466 else
3467 {
3468 writeOneLineHeaderOrRuler(data.substr(pi,i-pi));
3469 }
3470 out+=data[i];
3471 i++;
3472 size_t l = endBlockName.length();
3473 while (i+l<data.size())
3474 {
3475 if ((data[i]=='\\' || data[i]=='@') && // command
3476 data[i-1]!='\\' && data[i-1]!='@') // not escaped
3477 {
3478 if (qstrncmp(&data[i+1],endBlockName.data(),l)==0)
3479 {
3480 out+=data[i];
3481 out+=endBlockName;
3482 i+=l+1;
3483 break;
3484 }
3485 }
3486 out+=data[i];
3487 i++;
3488 }
3489 }
3490 else if ((level=isHeaderline(data.substr(i),TRUE))>0)
3491 {
3492 //printf("Found header at %d-%d\n",i,end);
3493 while (pi<data.size() && data[pi]==' ') pi++;
3494 QCString header = data.substr(pi,i-pi-1);
3495 id = extractTitleId(header, level);
3496 //printf("header='%s' is='%s'\n",qPrint(header),qPrint(id));
3497 if (!header.isEmpty())
3498 {
3499 if (!id.isEmpty())
3500 {
3501 out+=level==1?"@section ":"@subsection ";
3502 out+=id;
3503 out+=" ";
3504 out+=header;
3505 out+="\n\n";
3506 }
3507 else
3508 {
3509 out+=level==1?"<h1>":"<h2>";
3510 out+=header;
3511 out+=level==1?"\n</h1>\n":"\n</h2>\n";
3512 }
3513 }
3514 else
3515 {
3516 out+="\n<hr>\n";
3517 }
3518 pi=std::string::npos;
3519 i=end;
3520 end=i+1;
3521 continue;
3522 }
3523 else if ((ref=isLinkRef(data.substr(pi),id,link,title)))
3524 {
3525 //printf("found link ref: id='%s' link='%s' title='%s'\n",
3526 // qPrint(id),qPrint(link),qPrint(title));
3527 linkRefs.emplace(id.lower().str(),LinkRef(link,title));
3528 i=ref+pi;
3529 end=i+1;
3530 }
3531 else if (isFencedCodeBlock(data.substr(pi),currentIndent,lang,blockStart,blockEnd,blockOffset,fileName,lineNr))
3532 {
3533 //printf("Found FencedCodeBlock lang='%s' start=%d end=%d code={%s}\n",
3534 // qPrint(lang),blockStart,blockEnd,QCString(data+pi+blockStart).left(blockEnd-blockStart).data());
3535 writeFencedCodeBlock(data.substr(pi),lang.view(),blockStart,blockEnd);
3536 i=pi+blockOffset;
3537 pi=std::string::npos;
3538 end=i+1;
3539 continue;
3540 }
3541 else if (isCodeBlock(data.substr(i,end-i),i,blockIndent))
3542 {
3543 // skip previous line (it is empty anyway)
3544 i+=writeCodeBlock(data.substr(i),blockIndent);
3545 pi=std::string::npos;
3546 end=i+1;
3547 continue;
3548 }
3549 else if (isTableBlock(data.substr(pi)))
3550 {
3551 i=pi+writeTableBlock(data.substr(pi));
3552 pi=std::string::npos;
3553 end=i+1;
3554 continue;
3555 }
3556 else
3557 {
3558 writeOneLineHeaderOrRuler(data.substr(pi,i-pi));
3559 }
3560 }
3561 pi=i;
3562 i=end;
3563 }
3564 //printf("last line %d size=%d\n",i,size);
3565 if (pi!=std::string::npos && pi<data.size()) // deal with the last line
3566 {
3567 if (isLinkRef(data.substr(pi),id,link,title))
3568 {
3569 //printf("found link ref: id='%s' link='%s' title='%s'\n",
3570 // qPrint(id),qPrint(link),qPrint(title));
3571 linkRefs.emplace(id.lower().str(),LinkRef(link,title));
3572 }
3573 else
3574 {
3575 writeOneLineHeaderOrRuler(data.substr(pi));
3576 }
3577 }
3578
3579 return out;
3580}
3581
3582static bool isOtherPage(std::string_view data)
3583{
3584#define OPC(x) if (literal_at(data,#x " ") || literal_at(data,#x "\n")) return true
3585 OPC(dir); OPC(defgroup); OPC(addtogroup); OPC(weakgroup); OPC(ingroup);
3586 OPC(fn); OPC(property); OPC(typedef); OPC(var); OPC(def);
3587 OPC(enum); OPC(namespace); OPC(class); OPC(concept); OPC(module);
3588 OPC(protocol); OPC(category); OPC(union); OPC(struct); OPC(interface);
3589 OPC(idlexcept); OPC(file);
3590#undef OPC
3591
3592 return false;
3593}
3594
3596{
3597 AUTO_TRACE("docs={}",Trace::trunc(docs));
3598 size_t i=0;
3599 std::string_view data(docs.str());
3600 const size_t size = data.size();
3601 if (!data.empty())
3602 {
3603 while (i<size && (data[i]==' ' || data[i]=='\n'))
3604 {
3605 i++;
3606 }
3607 if (literal_at(data.substr(i),"<!--!")) // skip over <!--! marker
3608 {
3609 i+=5;
3610 while (i<size && (data[i]==' ' || data[i]=='\n')) // skip over spaces after the <!--! marker
3611 {
3612 i++;
3613 }
3614 }
3615 if (i+1<size &&
3616 (data[i]=='\\' || data[i]=='@') &&
3617 (literal_at(data.substr(i+1),"page ") || literal_at(data.substr(i+1),"mainpage"))
3618 )
3619 {
3620 if (literal_at(data.substr(i+1),"page "))
3621 {
3622 AUTO_TRACE_EXIT("result=ExplicitPageResult::explicitPage");
3624 }
3625 else
3626 {
3627 AUTO_TRACE_EXIT("result=ExplicitPageResult::explicitMainPage");
3629 }
3630 }
3631 else if (i+1<size && (data[i]=='\\' || data[i]=='@') && isOtherPage(data.substr(i+1)))
3632 {
3633 AUTO_TRACE_EXIT("result=ExplicitPageResult::explicitOtherPage");
3635 }
3636 }
3637 AUTO_TRACE_EXIT("result=ExplicitPageResult::notExplicit");
3639}
3640
3641QCString Markdown::extractPageTitle(QCString &docs, QCString &id, int &prepend, bool &isIdGenerated)
3642{
3643 AUTO_TRACE("docs={} prepend={}",Trace::trunc(docs),id,prepend);
3644 // first first non-empty line
3645 prepend = 0;
3646 QCString title;
3647 size_t i=0;
3648 QCString docs_org(docs);
3649 std::string_view data(docs_org.str());
3650 const size_t size = data.size();
3651 docs.clear();
3652 while (i<size && (data[i]==' ' || data[i]=='\n'))
3653 {
3654 if (data[i]=='\n') prepend++;
3655 i++;
3656 }
3657 if (i>=size) { return QCString(); }
3658 size_t end1=i+1;
3659 while (end1<size && data[end1-1]!='\n') end1++;
3660 //printf("i=%d end1=%d size=%d line='%s'\n",i,end1,size,docs.mid(i,end1-i).data());
3661 // first line from i..end1
3662 if (end1<size)
3663 {
3664 // second line form end1..end2
3665 size_t end2=end1+1;
3666 while (end2<size && data[end2-1]!='\n') end2++;
3667 if (prv->isHeaderline(data.substr(end1),FALSE))
3668 {
3669 title = data.substr(i,end1-i-1);
3670 docs+="\n\n"+docs_org.mid(end2);
3671 id = prv->extractTitleId(title, 0, &isIdGenerated);
3672 //printf("extractPageTitle(title='%s' docs='%s' id='%s')\n",title.data(),docs.data(),id.data());
3673 AUTO_TRACE_EXIT("result={} id={} isIdGenerated={}",Trace::trunc(title),id,isIdGenerated);
3674 return title;
3675 }
3676 }
3677 if (i<end1 && prv->isAtxHeader(data.substr(i,end1-i),title,id,FALSE,&isIdGenerated)>0)
3678 {
3679 docs+="\n";
3680 docs+=docs_org.mid(end1);
3681 }
3682 else
3683 {
3684 docs=docs_org;
3685 id = prv->extractTitleId(title, 0, &isIdGenerated);
3686 }
3687 AUTO_TRACE_EXIT("result={} id={} isIdGenerated={}",Trace::trunc(title),id,isIdGenerated);
3688 return title;
3689}
3690
3691
3692//---------------------------------------------------------------------------
3693
3694QCString Markdown::process(const QCString &input, int &startNewlines, bool fromParseInput)
3695{
3696 if (input.isEmpty()) return input;
3697 size_t refIndent=0;
3698
3699 // for replace tabs by spaces
3700 QCString s = input;
3701 if (s.at(s.length()-1)!='\n') s += "\n"; // see PR #6766
3702 s = detab(s,refIndent);
3703 //printf("======== DeTab =========\n---- output -----\n%s\n---------\n",qPrint(s));
3704
3705 // then process quotation blocks (as these may contain other blocks)
3706 s = prv->processQuotations(s.view(),refIndent);
3707 //printf("======== Quotations =========\n---- output -----\n%s\n---------\n",qPrint(s));
3708
3709 // then process block items (headers, rules, and code blocks, references)
3710 s = prv->processBlocks(s.view(),refIndent);
3711 //printf("======== Blocks =========\n---- output -----\n%s\n---------\n",qPrint(s));
3712
3713 // finally process the inline markup (links, emphasis and code spans)
3714 prv->out.clear();
3715 prv->out.reserve(s.length());
3716 prv->processInline(s.view());
3717 if (fromParseInput)
3718 {
3719 Debug::print(Debug::Markdown,0,"---- output -----\n{}\n=========\n",qPrint(prv->out));
3720 }
3721 else
3722 {
3723 Debug::print(Debug::Markdown,0,"======== Markdown =========\n---- input ------- \n{}\n---- output -----\n{}\n=========\n",input,prv->out);
3724 }
3725
3726 // post processing
3727 QCString result = substitute(prv->out,g_doxy_nbsp,"&nbsp;");
3728 const char *p = result.data();
3729 if (p)
3730 {
3731 while (*p==' ') p++; // skip over spaces
3732 while (*p=='\n') {startNewlines++;p++;}; // skip over newlines
3733 if (literal_at(p,"<br>")) p+=4; // skip over <br>
3734 }
3735 if (p>result.data())
3736 {
3737 // strip part of the input
3738 result = result.mid(static_cast<int>(p-result.data()));
3739 }
3740 return result;
3741}
3742
3743//---------------------------------------------------------------------------
3744
3746{
3747 AUTO_TRACE("fileName={}",fileName);
3748 QCString absFileName = FileInfo(fileName.str()).absFilePath();
3749 QCString baseFn = stripFromPath(absFileName);
3750 int i = baseFn.findRev('.');
3751 if (i!=-1) baseFn = baseFn.left(i);
3752 QCString baseName = escapeCharsInString(baseFn,false,false);
3753 //printf("markdownFileNameToId(%s)=md_%s\n",qPrint(fileName),qPrint(baseName));
3754 QCString res = "md_"+baseName;
3755 AUTO_TRACE_EXIT("result={}",res);
3756 return res;
3757}
3758
3759//---------------------------------------------------------------------------
3760
3765
3767{
3768}
3769
3773
3775 const char *fileBuf,
3776 const std::shared_ptr<Entry> &root,
3777 ClangTUParser* /*clangParser*/)
3778{
3779 std::shared_ptr<Entry> current = std::make_shared<Entry>();
3780 int prepend = 0; // number of empty lines in front
3781 current->lang = SrcLangExt::Markdown;
3782 current->fileName = fileName;
3783 current->docFile = fileName;
3784 current->docLine = 1;
3785 QCString docs = stripIndentation(fileBuf);
3786 if (!docs.stripWhiteSpace().size()) return;
3787 Debug::print(Debug::Markdown,0,"======== Markdown =========\n---- input ------- \n{}\n",fileBuf);
3788 QCString id;
3789 Markdown markdown(fileName,1,0);
3790 bool isIdGenerated = false;
3791 QCString title = markdown.extractPageTitle(docs, id, prepend, isIdGenerated).stripWhiteSpace();
3792 QCString generatedId;
3793 if (isIdGenerated)
3794 {
3795 generatedId = id;
3796 id = "";
3797 }
3798 int indentLevel=title.isEmpty() ? 0 : -1;
3799 markdown.setIndentLevel(indentLevel);
3800 FileInfo fi(fileName.str());
3801 QCString fn = fi.fileName();
3803 QCString mdfileAsMainPage = Config_getString(USE_MDFILE_AS_MAINPAGE);
3804 QCString mdFileNameId = markdownFileNameToId(fileName);
3805 bool wasEmpty = id.isEmpty();
3806 if (wasEmpty) id = mdFileNameId;
3807 QCString relFileName = stripFromPath(fileName);
3808 bool isSubdirDocs = Config_getBool(IMPLICIT_DIR_DOCS) && relFileName.lower().endsWith("/readme.md");
3809 switch (isExplicitPage(docs))
3810 {
3812 if (!mdfileAsMainPage.isEmpty() &&
3813 (fi.absFilePath()==FileInfo(mdfileAsMainPage.str()).absFilePath()) // file reference with path
3814 )
3815 {
3816 docs.prepend("@ianchor{" + title + "} " + id + "\\ilinebr ");
3817 docs.prepend("@mainpage "+title+"\\ilinebr ");
3818 }
3819 else if (id=="mainpage" || id=="index")
3820 {
3821 if (title.isEmpty()) title = titleFn;
3822 docs.prepend("@ianchor{" + title + "} " + id + "\\ilinebr ");
3823 docs.prepend("@mainpage "+title+"\\ilinebr ");
3824 }
3825 else if (isSubdirDocs)
3826 {
3827 if (!generatedId.isEmpty() && !title.isEmpty())
3828 {
3829 docs.prepend("@section " + generatedId + " " + title + "\\ilinebr ");
3830 }
3831 docs.prepend("@dir\\ilinebr ");
3832 }
3833 else
3834 {
3835 if (title.isEmpty())
3836 {
3837 title = titleFn;
3838 prepend = 0;
3839 }
3840 if (!wasEmpty)
3841 {
3842 docs.prepend("@ianchor{" + title + "} " + id + "\\ilinebr @ianchor{" + relFileName + "} " + mdFileNameId + "\\ilinebr ");
3843 }
3844 else if (!generatedId.isEmpty())
3845 {
3846 docs.prepend("@ianchor " + generatedId + "\\ilinebr ");
3847 }
3848 else if (Config_getEnum(MARKDOWN_ID_STYLE)==MARKDOWN_ID_STYLE_t::GITHUB)
3849 {
3850 QCString autoId = AnchorGenerator::instance().generate(title.str());
3851 docs.prepend("@ianchor{" + title + "} " + autoId + "\\ilinebr ");
3852 }
3853 docs.prepend("@page "+id+" "+title+"\\ilinebr ");
3854 }
3855 for (int i = 0; i < prepend; i++) docs.prepend("\n");
3856 break;
3858 {
3859 // look for `@page label My Title\n` and capture `label` (match[1]) and ` My Title` (match[2])
3860 static const reg::Ex re(R"([ ]*[\\@]page\s+(\a[\w-]*)(\s*[^\n]*)\n)");
3861 reg::Match match;
3862 std::string s = docs.str();
3863 if (reg::search(s,match,re))
3864 {
3865 QCString orgLabel = match[1].str();
3866 QCString orgTitle = match[2].str();
3867 orgTitle = orgTitle.stripWhiteSpace();
3868 QCString newLabel = markdownFileNameToId(fileName);
3869 docs = docs.left(match[1].position())+ // part before label
3870 newLabel+ // new label
3871 match[2].str()+ // part between orgLabel and \n
3872 "\\ilinebr @ianchor{" + orgTitle + "} "+orgLabel+"\n"+ // add original anchor plus \n of above
3873 docs.right(docs.length()-match.length()); // add remainder of docs
3874 }
3875 }
3876 break;
3878 break;
3880 break;
3881 }
3882 int lineNr=1;
3883
3884 p->commentScanner.enterFile(fileName,lineNr);
3885 Protection prot = Protection::Public;
3886 bool needsEntry = false;
3887 int position=0;
3888 GuardedSectionStack guards;
3889 QCString processedDocs = markdown.process(docs,lineNr,true);
3890 while (p->commentScanner.parseCommentBlock(
3891 this,
3892 current.get(),
3893 processedDocs,
3894 fileName,
3895 lineNr,
3896 FALSE, // isBrief
3897 FALSE, // javadoc autobrief
3898 FALSE, // inBodyDocs
3899 prot, // protection
3900 position,
3901 needsEntry,
3902 true,
3903 &guards
3904 ))
3905 {
3906 if (needsEntry)
3907 {
3908 QCString docFile = current->docFile;
3909 root->moveToSubEntryAndRefresh(current);
3910 current->lang = SrcLangExt::Markdown;
3911 current->docFile = docFile;
3912 current->docLine = lineNr;
3913 }
3914 }
3915 if (needsEntry)
3916 {
3917 root->moveToSubEntryAndKeep(current);
3918 }
3919 p->commentScanner.leaveFile(fileName,lineNr);
3920}
3921
3923{
3924 Doxygen::parserManager->getOutlineParser("*.cpp")->parsePrototype(text);
3925}
3926
3927//------------------------------------------------------------------------
#define eol
The end of line string for this machine.
static AnchorGenerator & instance()
Returns the singleton instance.
Definition anchor.cpp:38
static std::string addPrefixIfNeeded(const std::string &anchor)
Definition anchor.cpp:46
std::string generate(const std::string &title)
generates an anchor for a section with title.
Definition anchor.cpp:53
Clang parser object for a single translation unit, which consists of a source file and the directly o...
Definition clangparser.h:25
@ Markdown
Definition debug.h:37
static void print(DebugMask mask, int prio, fmt::format_string< Args... > fmt, Args &&... args)
Definition debug.h:76
static ParserManager * parserManager
Definition doxygen.h:130
static FileNameLinkedMap * imageNameLinkedMap
Definition doxygen.h:105
A model of a file symbol.
Definition filedef.h:99
Minimal replacement for QFileInfo.
Definition fileinfo.h:23
bool exists() const
Definition fileinfo.cpp:30
std::string fileName() const
Definition fileinfo.cpp:118
bool isReadable() const
Definition fileinfo.cpp:44
std::string absFilePath() const
Definition fileinfo.cpp:101
Helper class to process markdown formatted text.
Definition markdown.h:32
static ActionTable_t fill_table()
Definition markdown.cpp:182
std::array< Action_t, 256 > ActionTable_t
Definition markdown.h:45
std::unique_ptr< Private > prv
Definition markdown.h:43
static ActionTable_t actions
Definition markdown.h:47
void setIndentLevel(int level)
Definition markdown.cpp:210
QCString extractPageTitle(QCString &docs, QCString &id, int &prepend, bool &isIdGenerated)
Markdown(const QCString &fileName, int lineNr, int indentLevel=0)
Definition markdown.cpp:201
QCString process(const QCString &input, int &startNewlines, bool fromParseInput=false)
std::function< int(Private &, std::string_view, size_t)> Action_t
Definition markdown.h:44
void parseInput(const QCString &fileName, const char *fileBuf, const std::shared_ptr< Entry > &root, ClangTUParser *clangParser) override
Parses a single input file with the goal to build an Entry tree.
~MarkdownOutlineParser() override
void parsePrototype(const QCString &text) override
Callback function called by the comment block scanner.
std::unique_ptr< Private > p
Definition markdown.h:64
This is an alternative implementation of QCString.
Definition qcstring.h:101
int find(char c, int index=0, bool cs=TRUE) const
Definition qcstring.cpp:43
QCString & prepend(const char *s)
Definition qcstring.h:422
size_t length() const
Returns the length of the string, not counting the 0-terminator.
Definition qcstring.h:166
bool startsWith(const char *s) const
Definition qcstring.h:507
QCString mid(size_t index, size_t len=static_cast< size_t >(-1)) const
Definition qcstring.h:241
QCString lower() const
Definition qcstring.h:249
bool endsWith(const char *s) const
Definition qcstring.h:524
char & at(size_t i)
Returns a reference to the character at index i.
Definition qcstring.h:593
bool isEmpty() const
Returns TRUE iff the string is empty.
Definition qcstring.h:163
QCString stripWhiteSpace() const
returns a copy of this string with leading and trailing whitespace removed
Definition qcstring.h:260
const std::string & str() const
Definition qcstring.h:552
QCString & setNum(short n)
Definition qcstring.h:459
QCString simplifyWhiteSpace() const
return a copy of this string with leading and trailing whitespace removed and multiple whitespace cha...
Definition qcstring.cpp:190
QCString right(size_t len) const
Definition qcstring.h:234
size_t size() const
Returns the length of the string, not counting the 0-terminator.
Definition qcstring.h:169
QCString & sprintf(const char *format,...)
Definition qcstring.cpp:29
int findRev(char c, int index=-1, bool cs=TRUE) const
Definition qcstring.cpp:96
const char * data() const
Returns a pointer to the contents of the string in the form of a 0-terminated C string.
Definition qcstring.h:172
std::string_view view() const
Definition qcstring.h:174
QCString left(size_t len) const
Definition qcstring.h:229
void clear()
Definition qcstring.h:182
static constexpr int Section
Definition section.h:33
static constexpr int MaxLevel
Definition section.h:39
static constexpr int Subsection
Definition section.h:34
static constexpr int Subsubsection
Definition section.h:35
static constexpr int MinLevel
Definition section.h:32
static constexpr int Paragraph
Definition section.h:36
static constexpr int Subsubparagraph
Definition section.h:38
static constexpr int Subparagraph
Definition section.h:37
Class representing a regular expression.
Definition regex.h:39
Object representing the matching results.
Definition regex.h:151
Interface for the comment block scanner.
std::stack< GuardedSection > GuardedSectionStack
Definition commentscan.h:48
#define Config_getInt(name)
Definition config.h:34
#define Config_getBool(name)
Definition config.h:33
#define Config_getString(name)
Definition config.h:32
#define Config_getEnum(name)
Definition config.h:35
std::vector< std::string > StringVector
Definition containers.h:33
DirIterator end(const DirIterator &) noexcept
Definition dir.cpp:175
#define AUTO_TRACE_ADD(...)
Definition docnode.cpp:47
#define AUTO_TRACE(...)
Definition docnode.cpp:46
#define AUTO_TRACE_EXIT(...)
Definition docnode.cpp:48
static bool isOtherPage(std::string_view data)
#define AUTO_TRACE(...)
Definition markdown.cpp:61
static constexpr bool isAllowedEmphStr(const std::string_view &data, size_t offset)
Definition markdown.cpp:108
static bool hasLineBreak(std::string_view data)
ExplicitPageResult
Definition markdown.cpp:67
@ explicitMainPage
docs start with a mainpage command
Definition markdown.cpp:69
@ explicitPage
docs start with a page command
Definition markdown.cpp:68
@ notExplicit
docs doesn't start with either page or mainpage
Definition markdown.cpp:71
@ explicitOtherPage
docs start with a dir / defgroup / addtogroup command
Definition markdown.cpp:70
static bool isBlockQuote(std::string_view data, size_t indent)
returns true if this line starts a block quote
static size_t isLinkRef(std::string_view data, QCString &refid, QCString &link, QCString &title)
returns end of the link ref if this is indeed a link reference.
static QCString escapeDoubleQuotes(const QCString &s)
Definition markdown.cpp:238
static constexpr Alignment markersToAlignment(bool leftMarker, bool rightMarker)
helper function to convert presence of left and/or right alignment markers to an alignment value
Definition markdown.cpp:320
static bool isEndOfList(std::string_view data)
static size_t computeIndentExcludingListMarkers(std::string_view data)
static const char * g_doxy_nbsp
Definition markdown.cpp:218
static QCString escapeSpecialChars(const QCString &s)
Definition markdown.cpp:256
static constexpr bool ignoreCloseEmphChar(char c, char cn)
Definition markdown.cpp:116
#define OPC(x)
static constexpr bool isOpenEmphChar(char c)
Definition markdown.cpp:95
static bool isCodeBlock(std::string_view data, size_t offset, size_t &indent)
static bool isEmptyLine(std::string_view data)
#define AUTO_TRACE_EXIT(...)
Definition markdown.cpp:63
#define isLiTag(i)
static size_t findTableColumns(std::string_view data, size_t &start, size_t &end, size_t &columns)
Finds the location of the table's contains in the string data.
static const size_t codeBlockIndent
Definition markdown.cpp:219
static constexpr bool isIdChar(char c)
Definition markdown.cpp:77
static ExplicitPageResult isExplicitPage(const QCString &docs)
static bool isFencedCodeBlock(std::string_view data, size_t refIndent, QCString &lang, size_t &start, size_t &end, size_t &offset, QCString &fileName, int lineNr)
static const char * g_utf8_nbsp
Definition markdown.cpp:217
static const std::unordered_map< std::string, std::string > g_quotationHeaderMap
Alignment
Definition markdown.cpp:212
static size_t isListMarker(std::string_view data)
static bool isHRuler(std::string_view data)
static QCString getFilteredImageAttributes(std::string_view fmt, const QCString &attrs)
parse the image attributes and return attributes for given format
Definition markdown.cpp:341
bool skipOverFileAndLineCommands(std::string_view data, size_t indent, size_t &offset, std::string &location)
static constexpr bool extraChar(char c)
Definition markdown.cpp:86
static bool isTableBlock(std::string_view data)
Returns TRUE iff data points to the start of a table block.
static constexpr bool isUtf8Nbsp(char c1, char c2)
Definition markdown.cpp:103
size_t isNewline(std::string_view data)
Definition markdown.cpp:225
QCString markdownFileNameToId(const QCString &fileName)
processes string s and converts markdown into doxygen/html commands.
#define warn(file, line, fmt,...)
Definition message.h:97
bool isAbsolutePath(const QCString &fileName)
Definition portable.cpp:498
const char * strnstr(const char *haystack, const char *needle, size_t haystack_len)
Definition portable.cpp:601
QCString trunc(const QCString &s, size_t numChars=15)
Definition trace.h:56
Definition message.h:144
bool search(std::string_view str, Match &match, const Ex &re, size_t pos)
Search in a given string str starting at position pos for a match against regular expression re.
Definition regex.cpp:844
Portable versions of functions that are platform dependent.
static void decrLevel(yyscan_t yyscanner)
Definition pre.l:2251
QCString substitute(const QCString &s, const QCString &src, const QCString &dst)
substitute all occurrences of src in s by dst
Definition qcstring.cpp:571
int qstrncmp(const char *str1, const char *str2, size_t len)
Definition qcstring.h:75
bool qisspace(char c)
Definition qcstring.h:81
const char * qPrint(const char *s)
Definition qcstring.h:687
#define TRUE
Definition qcstring.h:37
#define FALSE
Definition qcstring.h:34
Some helper functions for std::string.
bool literal_at(const char *data, const char(&str)[N])
returns TRUE iff data points to a substring that matches string literal str
Definition stringutil.h:98
std::string_view stripWhiteSpace(std::string_view s)
Given a string view s, returns a new, narrower view on that string, skipping over any leading or trai...
Definition stringutil.h:72
int processEmphasis1(std::string_view data, char c)
process single emphasis
Definition markdown.cpp:847
int processQuoted(std::string_view data, size_t offset)
Process quoted section "...", can contain one embedded newline.
void writeMarkdownImage(std::string_view fmt, bool inline_img, bool explicitTitle, const QCString &title, const QCString &content, const QCString &link, const QCString &attributes, const FileDef *fd)
size_t writeTableBlock(std::string_view data)
size_t writeBlockQuote(std::string_view data)
size_t isSpecialCommand(std::string_view data, size_t offset)
Definition markdown.cpp:457
int processEmphasis3(std::string_view data, char c)
Parsing triple emphasis.
Definition markdown.cpp:913
int processCodeSpan(std::string_view data, size_t offset)
` parsing a code span (assuming codespan != 0)
int processSpecialCommand(std::string_view data, size_t offset)
QCString extractTitleId(QCString &title, int level, bool *pIsIdGenerated=nullptr)
void writeFencedCodeBlock(std::string_view data, std::string_view lang, size_t blockStart, size_t blockEnd)
int isHeaderline(std::string_view data, bool allowAdjustLevel)
returns whether the line is a setext-style hdr underline
size_t findEmphasisChar(std::string_view, char c, size_t c_size)
looks for the next emph char, skipping other constructs, and stopping when either it is found,...
Definition markdown.cpp:730
std::unordered_map< std::string, LinkRef > linkRefs
Definition markdown.cpp:175
void addStrEscapeUtf8Nbsp(std::string_view data)
QCString isBlockCommand(std::string_view data, size_t offset)
Definition markdown.cpp:388
size_t writeCodeBlock(std::string_view, size_t refIndent)
int processHtmlTag(std::string_view data, size_t offset)
QCString processQuotations(std::string_view data, size_t refIndent)
QCString processBlocks(std::string_view data, size_t indent)
int processEmphasis(std::string_view data, size_t offset)
int processLink(std::string_view data, size_t offset)
int processHtmlTagWrite(std::string_view data, size_t offset, bool doWrite)
Process a HTML tag.
int isAtxHeader(std::string_view data, QCString &header, QCString &id, bool allowAdjustLevel, bool *pIsIdGenerated=nullptr)
size_t findEndOfLine(std::string_view data, size_t offset)
int processEmphasis2(std::string_view data, char c)
process double emphasis
Definition markdown.cpp:881
void processInline(std::string_view data)
Private(const QCString &fn, int line, int indent)
Definition markdown.cpp:132
int processNmdash(std::string_view data, size_t offset)
Process ndash and mdashes.
Definition markdown.cpp:975
void writeOneLineHeaderOrRuler(std::string_view data)
QCString cellText
Definition markdown.cpp:126
bool colSpan
Definition markdown.cpp:127
Protection
Definition types.h:32
SrcLangExt
Definition types.h:207
SrcLangExt getLanguageFromFileName(const QCString &fileName, SrcLangExt defLang)
Definition util.cpp:5147
QCString stripIndentation(const QCString &s, bool skipFirstLine)
Definition util.cpp:5901
QCString escapeCharsInString(const QCString &name, bool allowDots, bool allowUnderscore)
Definition util.cpp:3271
QCString stripExtensionGeneral(const QCString &fName, const QCString &ext)
Definition util.cpp:4875
bool isURL(const QCString &url)
Checks whether the given url starts with a supported protocol.
Definition util.cpp:5865
static QCString stripFromPath(const QCString &p, const StringVector &l)
Definition util.cpp:299
QCString detab(const QCString &s, size_t &refIndent)
Definition util.cpp:6670
StringVector split(const std::string &s, const std::string &delimiter)
split input string s by string delimiter delimiter.
Definition util.cpp:6568
QCString externalLinkTarget(const bool parent)
Definition util.cpp:5661
QCString getFileNameExtension(const QCString &fn)
Definition util.cpp:5189
FileDef * findFileDef(const FileNameLinkedMap *fnMap, const QCString &n, bool &ambig)
Definition util.cpp:2838
A bunch of utility functions.