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=='`';
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]=='\\') // 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 else if (size>1 && data[0]=='@') // escaped characters
1855 {
1856 char c=data[1];
1857 if (c=='\\' || c=='@')
1858 {
1859 out+=data.substr(0,2);
1860 AUTO_TRACE_EXIT("2");
1861 return 2;
1862 }
1863 }
1864 return 0;
1865}
1866
1867void Markdown::Private::processInline(std::string_view data)
1868{
1869 AUTO_TRACE("data='{}'",Trace::trunc(data));
1870 size_t i=0;
1871 size_t end=0;
1872 Action_t action;
1873 const size_t size = data.size();
1874 while (i<size)
1875 {
1876 // skip over characters that do not trigger a specific action
1877 while (end<size && ((action=Markdown::actions[static_cast<uint8_t>(data[end])])==nullptr)) end++;
1878 // and add them to the output
1879 out+=data.substr(i,end-i);
1880 if (end>=size) break;
1881 i=end;
1882 // do the action matching a special character at i
1883 int iend = action(*this,data.substr(i),i);
1884 if (iend<=0) // update end
1885 {
1886 end=i+1-iend;
1887 }
1888 else // skip until end
1889 {
1890 i+=iend;
1891 end=i;
1892 }
1893 }
1894}
1895
1896/** returns whether the line is a setext-style hdr underline */
1897int Markdown::Private::isHeaderline(std::string_view data, bool allowAdjustLevel)
1898{
1899 AUTO_TRACE("data='{}' allowAdjustLevel",Trace::trunc(data),allowAdjustLevel);
1900 size_t i=0, c=0;
1901 const size_t size = data.size();
1902 while (i<size && data[i]==' ') i++;
1903 if (i==size) return 0;
1904
1905 // test of level 1 header
1906 if (data[i]=='=')
1907 {
1908 while (i < size && data[i] == '=')
1909 {
1910 i++;
1911 c++;
1912 }
1913 while (i<size && data[i]==' ') i++;
1914 int level = (c>1 && (i>=size || data[i]=='\n')) ? 1 : 0;
1915 if (allowAdjustLevel && level==1 && indentLevel==-1)
1916 {
1917 // In case a page starts with a header line we use it as title, promoting it to @page.
1918 // We set g_indentLevel to -1 to promoting the other sections if they have a deeper
1919 // nesting level than the page header, i.e. @section..@subsection becomes @page..@section.
1920 // In case a section at the same level is found (@section..@section) however we need
1921 // to undo this (and the result will be @page..@section).
1922 indentLevel=0;
1923 }
1924 AUTO_TRACE_EXIT("result={}",indentLevel+level);
1925 return indentLevel+level;
1926 }
1927 // test of level 2 header
1928 if (data[i]=='-')
1929 {
1930 while (i < size && data[i] == '-')
1931 {
1932 i++;
1933 c++;
1934 }
1935 while (i<size && data[i]==' ') i++;
1936 return (c>1 && (i>=size || data[i]=='\n')) ? indentLevel+2 : 0;
1937 }
1938 return 0;
1939}
1940
1941/** returns true if this line starts a block quote */
1942static bool isBlockQuote(std::string_view data,size_t indent)
1943{
1944 AUTO_TRACE("data='{}' indent={}",Trace::trunc(data),indent);
1945 size_t i = 0;
1946 const size_t size = data.size();
1947 while (i<size && data[i]==' ') i++;
1948 if (i<indent+codeBlockIndent) // could be a quotation
1949 {
1950 // count >'s and skip spaces
1951 int level=0;
1952 while (i<size && (data[i]=='>' || data[i]==' '))
1953 {
1954 if (data[i]=='>') level++;
1955 i++;
1956 }
1957 // last characters should be a space or newline,
1958 // so a line starting with >= does not match, but only when level equals 1
1959 bool res = (level>0 && i<size && ((data[i-1]==' ') || data[i]=='\n')) || (level > 1);
1960 AUTO_TRACE_EXIT("result={}",res);
1961 return res;
1962 }
1963 else // too much indentation -> code block
1964 {
1965 AUTO_TRACE_EXIT("result=false: too much indentation");
1966 return false;
1967 }
1968}
1969
1970/** returns end of the link ref if this is indeed a link reference. */
1971static size_t isLinkRef(std::string_view data, QCString &refid, QCString &link, QCString &title)
1972{
1973 AUTO_TRACE("data='{}'",Trace::trunc(data));
1974 const size_t size = data.size();
1975 // format: start with [some text]:
1976 size_t i = 0;
1977 while (i<size && data[i]==' ') i++;
1978 if (i>=size || data[i]!='[') { return 0; }
1979 i++;
1980 size_t refIdStart=i;
1981 while (i<size && data[i]!='\n' && data[i]!=']') i++;
1982 if (i>=size || data[i]!=']') { return 0; }
1983 refid = data.substr(refIdStart,i-refIdStart);
1984 if (refid.isEmpty()) { return 0; }
1985 AUTO_TRACE_ADD("refid found {}",refid);
1986 //printf(" isLinkRef: found refid='%s'\n",qPrint(refid));
1987 i++;
1988 if (i>=size || data[i]!=':') { return 0; }
1989 i++;
1990
1991 // format: whitespace* \n? whitespace* (<url> | url)
1992 while (i<size && data[i]==' ') i++;
1993 if (i<size && data[i]=='\n')
1994 {
1995 i++;
1996 while (i<size && data[i]==' ') i++;
1997 }
1998 if (i>=size) { return 0; }
1999
2000 if (i<size && data[i]=='<') i++;
2001 size_t linkStart=i;
2002 while (i<size && data[i]!=' ' && data[i]!='\n') i++;
2003 size_t linkEnd=i;
2004 if (i<size && data[i]=='>') i++;
2005 if (linkStart==linkEnd) { return 0; } // empty link
2006 link = data.substr(linkStart,linkEnd-linkStart);
2007 AUTO_TRACE_ADD("link found {}",Trace::trunc(link));
2008 if (link=="@ref" || link=="\\ref")
2009 {
2010 size_t argStart=i;
2011 while (i<size && data[i]!='\n' && data[i]!='"') i++;
2012 link+=data.substr(argStart,i-argStart);
2013 }
2014
2015 title.clear();
2016
2017 // format: (whitespace* \n? whitespace* ( 'title' | "title" | (title) ))?
2018 size_t eol=0;
2019 while (i<size && data[i]==' ') i++;
2020 if (i<size && data[i]=='\n')
2021 {
2022 eol=i;
2023 i++;
2024 while (i<size && data[i]==' ') i++;
2025 }
2026 if (i>=size)
2027 {
2028 AUTO_TRACE_EXIT("result={}: end of isLinkRef while looking for title",i);
2029 return i; // end of buffer while looking for the optional title
2030 }
2031
2032 char c = data[i];
2033 if (c=='\'' || c=='"' || c=='(') // optional title present?
2034 {
2035 //printf(" start of title found! char='%c'\n",c);
2036 i++;
2037 if (c=='(') c=')'; // replace c by end character
2038 size_t titleStart=i;
2039 // search for end of the line
2040 while (i<size && data[i]!='\n') i++;
2041 eol = i;
2042
2043 // search back to matching character
2044 size_t end=i-1;
2045 while (end>titleStart && data[end]!=c) end--;
2046 if (end>titleStart)
2047 {
2048 title = data.substr(titleStart,end-titleStart);
2049 }
2050 AUTO_TRACE_ADD("title found {}",Trace::trunc(title));
2051 }
2052 while (i<size && data[i]==' ') i++;
2053 //printf("end of isLinkRef: i=%d size=%d data[i]='%c' eol=%d\n",
2054 // i,size,data[i],eol);
2055 if (i>=size) { AUTO_TRACE_EXIT("result={}",i); return i; } // end of buffer while ref id was found
2056 else if (eol>0) { AUTO_TRACE_EXIT("result={}",eol); return eol; } // end of line while ref id was found
2057 return 0; // invalid link ref
2058}
2059
2060static bool isHRuler(std::string_view data)
2061{
2062 AUTO_TRACE("data='{}'",Trace::trunc(data));
2063 size_t i=0;
2064 size_t size = data.size();
2065 if (size>0 && data[size-1]=='\n') size--; // ignore newline character
2066 while (i<size && data[i]==' ') i++;
2067 if (i>=size) { AUTO_TRACE_EXIT("result=false: empty line"); return false; } // empty line
2068 char c=data[i];
2069 if (c!='*' && c!='-' && c!='_')
2070 {
2071 AUTO_TRACE_EXIT("result=false: {} is not a hrule character",c);
2072 return false; // not a hrule character
2073 }
2074 int n=0;
2075 while (i<size)
2076 {
2077 if (data[i]==c)
2078 {
2079 n++; // count rule character
2080 }
2081 else if (data[i]!=' ')
2082 {
2083 AUTO_TRACE_EXIT("result=false: line contains non hruler characters");
2084 return false; // line contains non hruler characters
2085 }
2086 i++;
2087 }
2088 AUTO_TRACE_EXIT("result={}",n>=3);
2089 return n>=3; // at least 3 characters needed for a hruler
2090}
2091
2092QCString Markdown::Private::extractTitleId(QCString &title, int level, bool *pIsIdGenerated)
2093{
2094 AUTO_TRACE("title={} level={}",Trace::trunc(title),level);
2095 // match e.g. '{#id-b11} ' and capture 'id-b11'
2096 static const reg::Ex r2(R"({#(\a[\w-]*)}\s*$)");
2097 reg::Match match;
2098 std::string ti = title.str();
2099 if (reg::search(ti,match,r2))
2100 {
2101 std::string id = match[1].str();
2102 title = title.left(match.position());
2103 if (AnchorGenerator::instance().reserve(id)>0)
2104 {
2105 warn(fileName, lineNr, "An automatically generated id already has the name '{}'!", id);
2106 }
2107 //printf("found match id='%s' title=%s\n",qPrint(id),qPrint(title));
2108 AUTO_TRACE_EXIT("id={}",id);
2109 return id;
2110 }
2111 if (((level>0) && (level<=Config_getInt(TOC_INCLUDE_HEADINGS))) || (Config_getEnum(MARKDOWN_ID_STYLE)==MARKDOWN_ID_STYLE_t::GITHUB))
2112 {
2114 if (pIsIdGenerated) *pIsIdGenerated=true;
2115 //printf("auto-generated id='%s' title='%s'\n",qPrint(id),qPrint(title));
2116 AUTO_TRACE_EXIT("id={}",id);
2117 return id;
2118 }
2119 //printf("no id found in title '%s'\n",qPrint(title));
2120 return "";
2121}
2122
2123
2124int Markdown::Private::isAtxHeader(std::string_view data,
2125 QCString &header,QCString &id,bool allowAdjustLevel,bool *pIsIdGenerated)
2126{
2127 AUTO_TRACE("data='{}' header={} id={} allowAdjustLevel={}",Trace::trunc(data),Trace::trunc(header),id,allowAdjustLevel);
2128 size_t i = 0;
2129 int level = 0, blanks=0;
2130 const size_t size = data.size();
2131
2132 // find start of header text and determine heading level
2133 while (i<size && data[i]==' ') i++;
2134 if (i>=size || data[i]!='#')
2135 {
2136 return 0;
2137 }
2138 while (i < size && data[i] == '#')
2139 {
2140 i++;
2141 level++;
2142 }
2143 if (level>SectionType::MaxLevel) // too many #'s -> no section
2144 {
2145 return 0;
2146 }
2147 while (i < size && data[i] == ' ')
2148 {
2149 i++;
2150 blanks++;
2151 }
2152 if (level==1 && blanks==0)
2153 {
2154 return 0; // special case to prevent #someid seen as a header (see bug 671395)
2155 }
2156
2157 // find end of header text
2158 size_t end=i;
2159 while (end<size && data[end]!='\n') end++;
2160 while (end>i && (data[end-1]=='#' || data[end-1]==' ')) end--;
2161
2162 // store result
2163 header = data.substr(i,end-i);
2164 id = extractTitleId(header, level, pIsIdGenerated);
2165 if (!id.isEmpty()) // strip #'s between title and id
2166 {
2167 int idx=static_cast<int>(header.length())-1;
2168 while (idx>=0 && (header.at(idx)=='#' || header.at(idx)==' ')) idx--;
2169 header=header.left(idx+1);
2170 }
2171
2172 if (allowAdjustLevel && level==1 && indentLevel==-1)
2173 {
2174 // in case we find a `# Section` on a markdown page that started with the same level
2175 // header, we no longer need to artificially decrease the paragraph level.
2176 // So both
2177 // -------------------
2178 // # heading 1 <-- here we set g_indentLevel to -1
2179 // # heading 2 <-- here we set g_indentLevel back to 0 such that this will be a @section
2180 // -------------------
2181 // and
2182 // -------------------
2183 // # heading 1 <-- here we set g_indentLevel to -1
2184 // ## heading 2 <-- here we keep g_indentLevel at -1 such that @subsection will be @section
2185 // -------------------
2186 // will convert to
2187 // -------------------
2188 // @page md_page Heading 1
2189 // @section autotoc_md1 Heading 2
2190 // -------------------
2191
2192 indentLevel=0;
2193 }
2194 int res = level+indentLevel;
2195 AUTO_TRACE_EXIT("result={}",res);
2196 return res;
2197}
2198
2199static bool isEmptyLine(std::string_view data)
2200{
2201 AUTO_TRACE("data='{}'",Trace::trunc(data));
2202 size_t i=0;
2203 while (i<data.size())
2204 {
2205 if (data[i]=='\n') { AUTO_TRACE_EXIT("true"); return true; }
2206 if (data[i]!=' ') { AUTO_TRACE_EXIT("false"); return false; }
2207 i++;
2208 }
2209 AUTO_TRACE_EXIT("true");
2210 return true;
2211}
2212
2213#define isLiTag(i) \
2214 (data[(i)]=='<' && \
2215 (data[(i)+1]=='l' || data[(i)+1]=='L') && \
2216 (data[(i)+2]=='i' || data[(i)+2]=='I') && \
2217 (data[(i)+3]=='>'))
2218
2219// compute the indent from the start of the input, excluding list markers
2220// such as -, -#, *, +, 1., and <li>
2221static size_t computeIndentExcludingListMarkers(std::string_view data)
2222{
2223 AUTO_TRACE("data='{}'",Trace::trunc(data));
2224 size_t i=0;
2225 const size_t size=data.size();
2226 size_t indent=0;
2227 bool isDigit=FALSE;
2228 bool isLi=FALSE;
2229 bool listMarkerSkipped=FALSE;
2230 while (i<size &&
2231 (data[i]==' ' || // space
2232 (!listMarkerSkipped && // first list marker
2233 (data[i]=='+' || data[i]=='-' || data[i]=='*' || // unordered list char
2234 (data[i]=='#' && i>0 && data[i-1]=='-') || // -# item
2235 (isDigit=(data[i]>='1' && data[i]<='9')) || // ordered list marker?
2236 (isLi=(size>=3 && i+3<size && isLiTag(i))) // <li> tag
2237 )
2238 )
2239 )
2240 )
2241 {
2242 if (isDigit) // skip over ordered list marker '10. '
2243 {
2244 size_t j=i+1;
2245 while (j<size && ((data[j]>='0' && data[j]<='9') || data[j]=='.'))
2246 {
2247 if (data[j]=='.') // should be end of the list marker
2248 {
2249 if (j+1<size && data[j+1]==' ') // valid list marker
2250 {
2251 listMarkerSkipped=TRUE;
2252 indent+=j+1-i;
2253 i=j+1;
2254 break;
2255 }
2256 else // not a list marker
2257 {
2258 break;
2259 }
2260 }
2261 j++;
2262 }
2263 }
2264 else if (isLi)
2265 {
2266 i+=3; // skip over <li>
2267 indent+=3;
2268 listMarkerSkipped=TRUE;
2269 }
2270 else if (data[i]=='-' && size>=2 && i+2<size && data[i+1]=='#' && data[i+2]==' ')
2271 { // case "-# "
2272 listMarkerSkipped=TRUE; // only a single list marker is accepted
2273 i++; // skip over #
2274 indent++;
2275 }
2276 else if (data[i]!=' ' && i+1<size && data[i+1]==' ')
2277 { // case "- " or "+ " or "* "
2278 listMarkerSkipped=TRUE; // only a single list marker is accepted
2279 }
2280 if (data[i]!=' ' && !listMarkerSkipped)
2281 { // end of indent
2282 break;
2283 }
2284 indent++;
2285 i++;
2286 }
2287 AUTO_TRACE_EXIT("result={}",indent);
2288 return indent;
2289}
2290
2291static size_t isListMarker(std::string_view data)
2292{
2293 AUTO_TRACE("data='{}'",Trace::trunc(data));
2294 size_t normalIndent = 0;
2295 while (normalIndent<data.size() && data[normalIndent]==' ') normalIndent++;
2296 size_t listIndent = computeIndentExcludingListMarkers(data);
2297 size_t result = listIndent>normalIndent ? listIndent : 0;
2298 AUTO_TRACE_EXIT("result={}",result);
2299 return result;
2300}
2301
2302static bool isEndOfList(std::string_view data)
2303{
2304 AUTO_TRACE("data='{}'",Trace::trunc(data));
2305 int dots=0;
2306 size_t i=0;
2307 // end of list marker is an otherwise empty line with a dot.
2308 while (i<data.size())
2309 {
2310 if (data[i]=='.')
2311 {
2312 dots++;
2313 }
2314 else if (data[i]=='\n')
2315 {
2316 break;
2317 }
2318 else if (data[i]!=' ' && data[i]!='\t') // bail out if the line is not empty
2319 {
2320 AUTO_TRACE_EXIT("result=false");
2321 return false;
2322 }
2323 i++;
2324 }
2325 AUTO_TRACE_EXIT("result={}",dots==1);
2326 return dots==1;
2327}
2328
2329static bool isFencedCodeBlock(std::string_view data,size_t refIndent,
2330 QCString &lang,size_t &start,size_t &end,size_t &offset,
2331 QCString &fileName,int lineNr)
2332{
2333 AUTO_TRACE("data='{}' refIndent={}",Trace::trunc(data),refIndent);
2334 const char dot = '.';
2335 auto isAlphaChar = [ ](char c) { return (c>='A' && c<='Z') || (c>='a' && c<='z'); };
2336 auto isAlphaNChar = [ ](char c) { return (c>='A' && c<='Z') || (c>='a' && c<='z') || (c>='0' && c<='9') || (c=='+'); };
2337 auto isLangChar = [&](char c) { return c==dot || isAlphaChar(c); };
2338 // rules: at least 3 ~~~, end of the block same amount of ~~~'s, otherwise
2339 // return FALSE
2340 size_t i=0;
2341 size_t indent=0;
2342 int startTildes=0;
2343 const size_t size = data.size();
2344 while (i < size && data[i] == ' ')
2345 {
2346 indent++;
2347 i++;
2348 }
2349 if (indent>=refIndent+4)
2350 {
2351 AUTO_TRACE_EXIT("result=false: content is part of code block indent={} refIndent={}",indent,refIndent);
2352 return FALSE;
2353 } // part of code block
2354 char tildaChar='~';
2355 if (i<size && data[i]=='`') tildaChar='`';
2356 while (i < size && data[i] == tildaChar)
2357 {
2358 startTildes++;
2359 i++;
2360 }
2361 if (startTildes<3)
2362 {
2363 AUTO_TRACE_EXIT("result=false: no fence marker found #tildes={}",startTildes);
2364 return FALSE;
2365 } // not enough tildes
2366 if (i<size && data[i]=='{') // extract .py from ```{.py} ... ```
2367 {
2368 i++; // skip over {
2369 if (data[i] == dot) i++; // skip over initial dot
2370 size_t startLang=i;
2371 while (i<size && (data[i]!='\n' && data[i]!='}')) i++; // find matching }
2372 if (i<size && data[i]=='}')
2373 {
2374 lang = data.substr(startLang,i-startLang);
2375 i++;
2376 }
2377 else // missing closing bracket, treat `{` as part of the content
2378 {
2379 i=startLang-1;
2380 lang="";
2381 }
2382 }
2383 else if (i<size && isLangChar(data[i])) /// extract python or .py from ```python...``` or ```.py...```
2384 {
2385 if (data[i] == dot) i++; // skip over initial dot
2386 size_t startLang=i;
2387 if (i<size && isAlphaChar(data[i])) //check first character of language specifier
2388 {
2389 i++;
2390 while (i<size && isAlphaNChar(data[i])) i++; // find end of language specifier
2391 }
2392 lang = data.substr(startLang,i-startLang);
2393 }
2394 else // no language specified
2395 {
2396 lang="";
2397 }
2398
2399 start=i;
2400 while (i<size)
2401 {
2402 if (data[i]==tildaChar)
2403 {
2404 end=i;
2405 int endTildes=0;
2406 while (i < size && data[i] == tildaChar)
2407 {
2408 endTildes++;
2409 i++;
2410 }
2411 while (i<size && data[i]==' ') i++;
2412 {
2413 if (endTildes==startTildes)
2414 {
2415 offset=i;
2416 AUTO_TRACE_EXIT("result=true: found end marker at offset {} lang='{}'",offset,lang);
2417 return true;
2418 }
2419 }
2420 }
2421 i++;
2422 }
2423 warn(fileName, lineNr, "Ending Inside a fenced code block. Maybe the end marker for the block is missing?");
2424 AUTO_TRACE_EXIT("result=false: no end marker found lang={}'",lang);
2425 return false;
2426}
2427
2428static bool isCodeBlock(std::string_view data, size_t offset,size_t &indent)
2429{
2430 AUTO_TRACE("data='{}' offset={}",Trace::trunc(data),offset);
2431 //printf("<isCodeBlock(offset=%d,size=%d,indent=%d)\n",offset,size,indent);
2432 // determine the indent of this line
2433 size_t i=0;
2434 size_t indent0=0;
2435 const size_t size = data.size();
2436 while (i < size && data[i] == ' ')
2437 {
2438 indent0++;
2439 i++;
2440 }
2441
2442 if (indent0<codeBlockIndent)
2443 {
2444 AUTO_TRACE_EXIT("result={}: line is not indented enough {}<4",false,indent0);
2445 return false;
2446 }
2447 if (indent0>=size || data[indent0]=='\n') // empty line does not start a code block
2448 {
2449 AUTO_TRACE_EXIT("result={}: only spaces at the end of a comment block",false);
2450 return false;
2451 }
2452
2453 i=offset;
2454 int nl=0;
2455 int nl_pos[3];
2456 int offset_i = static_cast<int>(offset);
2457 // search back 3 lines and remember the start of lines -1 and -2
2458 while (i>0 && nl<3) // i counts down from offset to 1
2459 {
2460 int j = static_cast<int>(i)-offset_i-1; // j counts from -1 to -offset
2461 // since j can be negative we need to rewrap data in a std::string_view
2462 size_t nl_size = isNewline(std::string_view(data.data()+j,data.size()-j));
2463 if (nl_size>0)
2464 {
2465 nl_pos[nl++]=j+static_cast<int>(nl_size);
2466 }
2467 i--;
2468 }
2469
2470 // if there are only 2 preceding lines, then line -2 starts at -offset
2471 if (i==0 && nl==2) nl_pos[nl++]=-offset_i;
2472
2473 if (nl==3) // we have at least 2 preceding lines
2474 {
2475 //printf(" positions: nl_pos=[%d,%d,%d] line[-2]='%s' line[-1]='%s'\n",
2476 // nl_pos[0],nl_pos[1],nl_pos[2],
2477 // qPrint(QCString(data+nl_pos[1]).left(nl_pos[0]-nl_pos[1]-1)),
2478 // qPrint(QCString(data+nl_pos[2]).left(nl_pos[1]-nl_pos[2]-1)));
2479
2480 // check that line -1 is empty
2481 // Note that the offset is negative so we need to rewrap the string view
2482 if (!isEmptyLine(std::string_view(data.data()+nl_pos[1],nl_pos[0]-nl_pos[1]-1)))
2483 {
2484 AUTO_TRACE_EXIT("result={}",FALSE);
2485 return FALSE;
2486 }
2487
2488 // determine the indent of line -2
2489 // Note that the offset is negative so we need to rewrap the string view
2490 indent=std::max(indent,computeIndentExcludingListMarkers(
2491 std::string_view(data.data()+nl_pos[2],nl_pos[1]-nl_pos[2])));
2492
2493 //printf(">isCodeBlock local_indent %d>=%d+%d=%d\n",
2494 // indent0,indent,codeBlockIndent,indent0>=indent+codeBlockIndent);
2495 // if the difference is >4 spaces -> code block
2496 bool res = indent0>=indent+codeBlockIndent;
2497 AUTO_TRACE_EXIT("result={}: code block if indent difference >4 spaces",res);
2498 return res;
2499 }
2500 else // not enough lines to determine the relative indent, use global indent
2501 {
2502 // check that line -1 is empty
2503 // Note that the offset is negative so we need to rewrap the string view
2504 if (nl==1 && !isEmptyLine(std::string_view(data.data()-offset,offset-1)))
2505 {
2506 AUTO_TRACE_EXIT("result=false");
2507 return FALSE;
2508 }
2509 //printf(">isCodeBlock global indent %d>=%d+4=%d nl=%d\n",
2510 // indent0,indent,indent0>=indent+4,nl);
2511 bool res = indent0>=indent+codeBlockIndent;
2512 AUTO_TRACE_EXIT("result={}: code block if indent difference >4 spaces",res);
2513 return res;
2514 }
2515}
2516
2517/** Finds the location of the table's contains in the string \a data.
2518 * Only one line will be inspected.
2519 * @param[in] data pointer to the string buffer.
2520 * @param[out] start offset of the first character of the table content
2521 * @param[out] end offset of the last character of the table content
2522 * @param[out] columns number of table columns found
2523 * @returns The offset until the next line in the buffer.
2524 */
2525static size_t findTableColumns(std::string_view data,size_t &start,size_t &end,size_t &columns)
2526{
2527 AUTO_TRACE("data='{}'",Trace::trunc(data));
2528 const size_t size = data.size();
2529 size_t i=0,n=0;
2530 // find start character of the table line
2531 while (i<size && data[i]==' ') i++;
2532 if (i < size && data[i] == '|' && data[i] != '\n')
2533 {
2534 i++;
2535 n++; // leading | does not count
2536 }
2537 start = i;
2538
2539 // find end character of the table line
2540 size_t j = 0;
2541 while (i<size && (j = isNewline(data.substr(i)))==0) i++;
2542 size_t eol=i+j;
2543
2544 if (j>0 && i>0) i--; // move i to point before newline
2545 while (i>0 && data[i]==' ') i--;
2546 if (i > 0 && data[i - 1] != '\\' && data[i] == '|')
2547 {
2548 i--;
2549 n++; // trailing or escaped | does not count
2550 }
2551 end = i;
2552
2553 // count columns between start and end
2554 columns=0;
2555 if (end>start)
2556 {
2557 i=start;
2558 while (i<=end) // look for more column markers
2559 {
2560 if (data[i]=='|' && (i==0 || data[i-1]!='\\')) columns++;
2561 if (columns==1) columns++; // first | make a non-table into a two column table
2562 i++;
2563 }
2564 }
2565 if (n==2 && columns==0) // table row has | ... |
2566 {
2567 columns++;
2568 }
2569 AUTO_TRACE_EXIT("eol={} start={} end={} columns={}",eol,start,end,columns);
2570 return eol;
2571}
2572
2573/** Returns TRUE iff data points to the start of a table block */
2574static bool isTableBlock(std::string_view data)
2575{
2576 AUTO_TRACE("data='{}'",Trace::trunc(data));
2577 size_t cc0=0, start=0, end=0;
2578
2579 // the first line should have at least two columns separated by '|'
2580 size_t i = findTableColumns(data,start,end,cc0);
2581 if (i>=data.size() || cc0<1)
2582 {
2583 AUTO_TRACE_EXIT("result=false: no |'s in the header");
2584 return FALSE;
2585 }
2586
2587 size_t cc1 = 0;
2588 size_t ret = findTableColumns(data.substr(i),start,end,cc1);
2589 size_t j=i+start;
2590 // separator line should consist of |, - and : and spaces only
2591 while (j<=end+i)
2592 {
2593 if (data[j]!=':' && data[j]!='-' && data[j]!='|' && data[j]!=' ')
2594 {
2595 AUTO_TRACE_EXIT("result=false: invalid character '{}'",data[j]);
2596 return FALSE; // invalid characters in table separator
2597 }
2598 j++;
2599 }
2600 if (cc1!=cc0) // number of columns should be same as previous line
2601 {
2602 AUTO_TRACE_EXIT("result=false: different number of columns as previous line {}!={}",cc1,cc0);
2603 return FALSE;
2604 }
2605
2606 i+=ret; // goto next line
2607 size_t cc2 = 0;
2608 findTableColumns(data.substr(i),start,end,cc2);
2609
2610 AUTO_TRACE_EXIT("result={}",cc1==cc2);
2611 return cc1==cc2;
2612}
2613
2614size_t Markdown::Private::writeTableBlock(std::string_view data)
2615{
2616 AUTO_TRACE("data='{}'",Trace::trunc(data));
2617 const size_t size = data.size();
2618
2619 size_t columns=0, start=0, end=0;
2620 size_t i = findTableColumns(data,start,end,columns);
2621 size_t headerStart = start;
2622 size_t headerEnd = end;
2623
2624 // read cell alignments
2625 size_t cc = 0;
2626 size_t ret = findTableColumns(data.substr(i),start,end,cc);
2627 size_t k=0;
2628 std::vector<Alignment> columnAlignment(columns);
2629
2630 bool leftMarker=false, rightMarker=false, startFound=false;
2631 size_t j=start+i;
2632 while (j<=end+i)
2633 {
2634 if (!startFound)
2635 {
2636 if (data[j]==':') { leftMarker=TRUE; startFound=TRUE; }
2637 if (data[j]=='-') startFound=TRUE;
2638 //printf(" data[%d]=%c startFound=%d\n",j,data[j],startFound);
2639 }
2640 if (data[j]=='-') rightMarker=FALSE;
2641 else if (data[j]==':') rightMarker=TRUE;
2642 if (j<=end+i && (data[j]=='|' && (j==0 || data[j-1]!='\\')))
2643 {
2644 if (k<columns)
2645 {
2646 columnAlignment[k] = markersToAlignment(leftMarker,rightMarker);
2647 //printf("column[%d] alignment=%d\n",k,columnAlignment[k]);
2648 leftMarker=FALSE;
2649 rightMarker=FALSE;
2650 startFound=FALSE;
2651 }
2652 k++;
2653 }
2654 j++;
2655 }
2656 if (k<columns)
2657 {
2658 columnAlignment[k] = markersToAlignment(leftMarker,rightMarker);
2659 //printf("column[%d] alignment=%d\n",k,columnAlignment[k]);
2660 }
2661 // proceed to next line
2662 i+=ret;
2663
2664 // Store the table cell information by row then column. This
2665 // allows us to handle row spanning.
2666 std::vector<std::vector<TableCell> > tableContents;
2667
2668 size_t m = headerStart;
2669 std::vector<TableCell> headerContents(columns);
2670 for (k=0;k<columns;k++)
2671 {
2672 while (m<=headerEnd && (data[m]!='|' || (m>0 && data[m-1]=='\\')))
2673 {
2674 headerContents[k].cellText += data[m++];
2675 }
2676 m++;
2677 // do the column span test before stripping white space
2678 // || is spanning columns, | | is not
2679 headerContents[k].colSpan = headerContents[k].cellText.isEmpty();
2680 headerContents[k].cellText = headerContents[k].cellText.stripWhiteSpace();
2681 }
2682 tableContents.push_back(headerContents);
2683
2684 // write table cells
2685 while (i<size)
2686 {
2687 ret = findTableColumns(data.substr(i),start,end,cc);
2688 if (cc!=columns) break; // end of table
2689
2690 j=start+i;
2691 k=0;
2692 std::vector<TableCell> rowContents(columns);
2693 while (j<=end+i)
2694 {
2695 if (j<=end+i && (data[j]=='|' && (j==0 || data[j-1]!='\\')))
2696 {
2697 // do the column span test before stripping white space
2698 // || is spanning columns, | | is not
2699 rowContents[k].colSpan = rowContents[k].cellText.isEmpty();
2700 rowContents[k].cellText = rowContents[k].cellText.stripWhiteSpace();
2701 k++;
2702 } // if (j<=end+i && (data[j]=='|' && (j==0 || data[j-1]!='\\')))
2703 else
2704 {
2705 rowContents[k].cellText += data[j];
2706 } // else { if (j<=end+i && (data[j]=='|' && (j==0 || data[j-1]!='\\'))) }
2707 j++;
2708 } // while (j<=end+i)
2709 // do the column span test before stripping white space
2710 // || is spanning columns, | | is not
2711 rowContents[k].colSpan = rowContents[k].cellText.isEmpty();
2712 rowContents[k].cellText = rowContents[k].cellText.stripWhiteSpace();
2713 tableContents.push_back(rowContents);
2714
2715 // proceed to next line
2716 i+=ret;
2717 }
2718
2719 out+="<table class=\"markdownTable\">";
2720 QCString cellTag("th"), cellClass("class=\"markdownTableHead");
2721 for (size_t row = 0; row < tableContents.size(); row++)
2722 {
2723 if (row)
2724 {
2725 if (row % 2)
2726 {
2727 out+="\n<tr class=\"markdownTableRowOdd\">";
2728 }
2729 else
2730 {
2731 out+="\n<tr class=\"markdownTableRowEven\">";
2732 }
2733 }
2734 else
2735 {
2736 out+="\n <tr class=\"markdownTableHead\">";
2737 }
2738 for (size_t c = 0; c < columns; c++)
2739 {
2740 // save the cell text for use after column span computation
2741 QCString cellText(tableContents[row][c].cellText);
2742
2743 // Row span handling. Spanning rows will contain a caret ('^').
2744 // If the current cell contains just a caret, this is part of an
2745 // earlier row's span and the cell should not be added to the
2746 // output.
2747 if (tableContents[row][c].cellText == "^")
2748 {
2749 continue;
2750 }
2751 if (tableContents[row][c].colSpan)
2752 {
2753 int cr = static_cast<int>(c);
2754 while ( cr >= 0 && tableContents[row][cr].colSpan)
2755 {
2756 cr--;
2757 };
2758 if (cr >= 0 && tableContents[row][cr].cellText == "^") continue;
2759 }
2760 size_t rowSpan = 1, spanRow = row+1;
2761 while ((spanRow < tableContents.size()) &&
2762 (tableContents[spanRow][c].cellText == "^"))
2763 {
2764 spanRow++;
2765 rowSpan++;
2766 }
2767
2768 out+=" <" + cellTag + " " + cellClass;
2769 // use appropriate alignment style
2770 switch (columnAlignment[c])
2771 {
2772 case Alignment::Left: out+="Left\""; break;
2773 case Alignment::Right: out+="Right\""; break;
2774 case Alignment::Center: out+="Center\""; break;
2775 case Alignment::None: out+="None\""; break;
2776 }
2777
2778 if (rowSpan > 1)
2779 {
2780 QCString spanStr;
2781 spanStr.setNum(rowSpan);
2782 out+=" rowspan=\"" + spanStr + "\"";
2783 }
2784 // Column span handling, assumes that column spans will have
2785 // empty strings, which would indicate the sequence "||", used
2786 // to signify spanning columns.
2787 size_t colSpan = 1;
2788 while ((c+1 < columns) && tableContents[row][c+1].colSpan)
2789 {
2790 c++;
2791 colSpan++;
2792 }
2793 if (colSpan > 1)
2794 {
2795 QCString spanStr;
2796 spanStr.setNum(colSpan);
2797 out+=" colspan=\"" + spanStr + "\"";
2798 }
2799 // need at least one space on either side of the cell text in
2800 // order for doxygen to do other formatting
2801 out+="> " + cellText + " \\ilinebr </" + cellTag + ">";
2802 }
2803 cellTag = "td";
2804 cellClass = "class=\"markdownTableBody";
2805 out+=" </tr>";
2806 }
2807 out+="</table>\n";
2808
2809 AUTO_TRACE_EXIT("i={}",i);
2810 return i;
2811}
2812
2813
2814static bool hasLineBreak(std::string_view data)
2815{
2816 AUTO_TRACE("data='{}'",Trace::trunc(data));
2817 size_t i=0;
2818 size_t j=0;
2819 // search for end of line and also check if it is not a completely blank
2820 while (i<data.size() && data[i]!='\n')
2821 {
2822 if (data[i]!=' ' && data[i]!='\t') j++; // some non whitespace
2823 i++;
2824 }
2825 if (i>=data.size()) { return 0; } // empty line
2826 if (i<2) { return 0; } // not long enough
2827 bool res = (j>0 && data[i-1]==' ' && data[i-2]==' '); // non blank line with at two spaces at the end
2828 AUTO_TRACE_EXIT("result={}",res);
2829 return res;
2830}
2831
2832
2834{
2835 AUTO_TRACE("data='{}'",Trace::trunc(data));
2836 int level=0;
2837 QCString header;
2838 QCString id;
2839 if (isHRuler(data))
2840 {
2841 out+="<hr>\n";
2842 }
2843 else if ((level=isAtxHeader(data,header,id,TRUE)))
2844 {
2845 QCString hTag;
2846 if (!id.isEmpty())
2847 {
2848 switch (level)
2849 {
2850 case SectionType::Section: out+="@section "; break;
2851 case SectionType::Subsection: out+="@subsection "; break;
2852 case SectionType::Subsubsection: out+="@subsubsection "; break;
2853 case SectionType::Paragraph: out+="@paragraph "; break;
2854 case SectionType::Subparagraph: out+="@subparagraph "; break;
2855 case SectionType::Subsubparagraph: out+="@subsubparagraph "; break;
2856 }
2857 out+=id;
2858 out+=" ";
2859 out+=header;
2860 out+="\n";
2861 }
2862 else
2863 {
2864 hTag.sprintf("h%d",level);
2865 out+="<"+hTag+">";
2866 out+=header;
2867 out+="</"+hTag+">\n";
2868 }
2869 }
2870 else if (data.size()>0) // nothing interesting -> just output the line
2871 {
2872 size_t tmpSize = data.size();
2873 if (data[data.size()-1] == '\n') tmpSize--;
2874 out+=data.substr(0,tmpSize);
2875
2876 if (hasLineBreak(data))
2877 {
2878 out+="\\ilinebr<br>";
2879 }
2880 if (tmpSize != data.size()) out+='\n';
2881 }
2882}
2883
2884static const std::unordered_map<std::string,std::string> g_quotationHeaderMap = {
2885 // GitHub style Doxygen command
2886 { "[!note]", "\\note" },
2887 { "[!warning]", "\\warning" },
2888 { "[!tip]", "\\remark" },
2889 { "[!caution]", "\\attention" },
2890 { "[!important]", "\\important" }
2891};
2892
2893size_t Markdown::Private::writeBlockQuote(std::string_view data)
2894{
2895 AUTO_TRACE("data='{}'",Trace::trunc(data));
2896 size_t i=0;
2897 int curLevel=0;
2898 size_t end=0;
2899 const size_t size = data.size();
2900 std::string startCmd;
2901 int isGitHubAlert = false;
2902 int isGitHubFirst = false;
2903 while (i<size)
2904 {
2905 // find end of this line
2906 end=i+1;
2907 while (end<=size && data[end-1]!='\n') end++;
2908 size_t j=i;
2909 int level=0;
2910 size_t indent=i;
2911 // compute the quoting level
2912 while (j<end && (data[j]==' ' || data[j]=='>'))
2913 {
2914 if (data[j]=='>') { level++; indent=j+1; }
2915 else if (j>0 && data[j-1]=='>') indent=j+1;
2916 j++;
2917 }
2918 if (indent>0 && j>0 && data[j-1]=='>' &&
2919 !(j==size || data[j]=='\n')) // disqualify last > if not followed by space
2920 {
2921 indent--;
2922 level--;
2923 j--;
2924 }
2925 AUTO_TRACE_ADD("indent={} i={} j={} end={} level={} line={}",indent,i,j,end,level,Trace::trunc(&data[i]));
2926 if (level==0 && j<end-1 && !isListMarker(data.substr(j)) && !isHRuler(data.substr(j)))
2927 {
2928 level = curLevel; // lazy
2929 }
2930 if (level==1)
2931 {
2932 QCString txt = stripWhiteSpace(data.substr(indent,end-indent));
2933 auto it = g_quotationHeaderMap.find(txt.lower().str()); // TODO: in C++20 the std::string can be dropped
2934 if (it != g_quotationHeaderMap.end())
2935 {
2936 isGitHubAlert = true;
2937 isGitHubFirst = true;
2938 startCmd = it->second;
2939 }
2940 }
2941 if (level>curLevel) // quote level increased => add start markers
2942 {
2943 if (level!=1 || !isGitHubAlert) // normal block quote
2944 {
2945 for (int l=curLevel;l<level-1;l++)
2946 {
2947 out+="<blockquote>";
2948 }
2949 out += "<blockquote>&zwj;"; // empty blockquotes are also shown
2950 }
2951 else if (!startCmd.empty()) // GitHub style alert
2952 {
2953 out += startCmd + " ";
2954 }
2955 }
2956 else if (level<curLevel) // quote level decreased => add end markers
2957 {
2958 int decrLevel = curLevel;
2959 if (level==0 && isGitHubAlert)
2960 {
2961 decrLevel--;
2962 }
2963 for (int l=level;l<decrLevel;l++)
2964 {
2965 out += "</blockquote>\\ilinebr ";
2966 }
2967 }
2968 if (level==0)
2969 {
2970 curLevel=0;
2971 break; // end of quote block
2972 }
2973 // copy line without quotation marks
2974 if (curLevel!=0 || !isGitHubAlert)
2975 {
2976 std::string_view txt = data.substr(indent,end-indent);
2977 if (stripWhiteSpace(txt).empty() && !startCmd.empty())
2978 {
2979 if (!isGitHubFirst) out += "<br>";
2980 out += "<br>\n";
2981 }
2982 else
2983 {
2984 out += txt;
2985 }
2986 isGitHubFirst = false;
2987 }
2988 else // GitHub alert section
2989 {
2990 out+= "\n";
2991 }
2992 curLevel=level;
2993 // proceed with next line
2994 i=end;
2995 }
2996 // end of comment within blockquote => add end markers
2997 if (isGitHubAlert) // GitHub alert doesn't have a blockquote
2998 {
2999 curLevel--;
3000 }
3001 for (int l=0;l<curLevel;l++)
3002 {
3003 out+="</blockquote>";
3004 }
3005 AUTO_TRACE_EXIT("i={}",i);
3006 return i;
3007}
3008
3009// For code blocks that are outputted as part of an indented include or snippet command, we need to filter out
3010// the location string, i.e. '\ifile "..." \iline \ilinebr'.
3011bool skipOverFileAndLineCommands(std::string_view data,size_t indent,size_t &offset,std::string &location)
3012{
3013 size_t i = offset;
3014 size_t size = data.size();
3015 while (i<data.size() && data[i]==' ') i++;
3016 if (literal_at(data.substr(i),"\\ifile \""))
3017 {
3018 size_t locStart = i;
3019 if (i>offset) locStart--; // include the space before \ifile
3020 i+=8;
3021 bool found=false;
3022 while (i+9<size && data[i]!='\n')
3023 {
3024 if (literal_at(data.substr(i),"\\ilinebr "))
3025 {
3026 found=true;
3027 break;
3028 }
3029 i++;
3030 }
3031 if (found)
3032 {
3033 i+=9;
3034 location=data.substr(locStart,i-locStart);
3035 location+='\n';
3036 while (indent > 0 && i < size && data[i] == ' ')
3037 {
3038 i++;
3039 indent--;
3040 }
3041 if (i<size && data[i]=='\n') i++;
3042 offset = i;
3043 return true;
3044 }
3045 }
3046 return false;
3047}
3048
3049size_t Markdown::Private::writeCodeBlock(std::string_view data,size_t refIndent)
3050{
3051 AUTO_TRACE("data='{}' refIndent={}",Trace::trunc(data),refIndent);
3052 const size_t size = data.size();
3053 size_t i=0;
3054 // no need for \ilinebr here as the previous line was empty and was skipped
3055 out+="@iverbatim\n";
3056 int emptyLines=0;
3057 std::string location;
3058 while (i<size)
3059 {
3060 // find end of this line
3061 size_t end=i+1;
3062 while (end<=size && data[end-1]!='\n') end++;
3063 size_t j=i;
3064 size_t indent=0;
3065 while (j < end && data[j] == ' ')
3066 {
3067 j++;
3068 indent++;
3069 }
3070 //printf("j=%d end=%d indent=%d refIndent=%d tabSize=%d data={%s}\n",
3071 // j,end,indent,refIndent,Config_getInt(TAB_SIZE),qPrint(QCString(data+i).left(end-i-1)));
3072 if (j==end-1) // empty line
3073 {
3074 emptyLines++;
3075 i=end;
3076 }
3077 else if (indent>=refIndent+codeBlockIndent) // enough indent to continue the code block
3078 {
3079 while (emptyLines>0) // write skipped empty lines
3080 {
3081 // add empty line
3082 out+="\n";
3083 emptyLines--;
3084 }
3085 // add code line minus the indent
3086 size_t offset = i+refIndent+codeBlockIndent;
3087 std::string lineLoc;
3088 if (skipOverFileAndLineCommands(data,codeBlockIndent,offset,lineLoc))
3089 {
3090 location = lineLoc;
3091 }
3092 out+=data.substr(offset,end-offset);
3093 i=end;
3094 }
3095 else // end of code block
3096 {
3097 break;
3098 }
3099 }
3100 out+="@endiverbatim";
3101 if (!location.empty())
3102 {
3103 out+=location;
3104 }
3105 else
3106 {
3107 out+="\\ilinebr ";
3108 }
3109 while (emptyLines>0) // write skipped empty lines
3110 {
3111 // add empty line
3112 out+="\n";
3113 emptyLines--;
3114 }
3115 AUTO_TRACE_EXIT("i={}",i);
3116 return i;
3117}
3118
3119// start searching for the end of the line start at offset \a i
3120// keeping track of possible blocks that need to be skipped.
3121size_t Markdown::Private::findEndOfLine(std::string_view data,size_t offset)
3122{
3123 AUTO_TRACE("data='{}'",Trace::trunc(data));
3124 // find end of the line
3125 const size_t size = data.size();
3126 size_t nb=0, end=offset+1, j=0;
3127 while (end<=size && (j=isNewline(data.substr(end-1)))==0)
3128 {
3129 // while looking for the end of the line we might encounter a block
3130 // that needs to be passed unprocessed.
3131 if ((data[end-1]=='\\' || data[end-1]=='@') && // command
3132 (end<=1 || (data[end-2]!='\\' && data[end-2]!='@')) // not escaped
3133 )
3134 {
3135 QCString endBlockName = isBlockCommand(data.substr(end-1),end-1);
3136 end++;
3137 if (!endBlockName.isEmpty())
3138 {
3139 size_t l = endBlockName.length();
3140 for (;end+l+1<size;end++) // search for end of block marker
3141 {
3142 if ((data[end]=='\\' || data[end]=='@') &&
3143 data[end-1]!='\\' && data[end-1]!='@'
3144 )
3145 {
3146 if (qstrncmp(&data[end+1],endBlockName.data(),l)==0)
3147 {
3148 // found end marker, skip over this block
3149 //printf("feol.block out={%s}\n",qPrint(QCString(data+i).left(end+l+1-i)));
3150 end = end + l + 2;
3151 break;
3152 }
3153 }
3154 }
3155 }
3156 }
3157 else if (nb==0 && data[end-1]=='<' && size>=6 && end+6<size &&
3158 (end<=1 || (data[end-2]!='\\' && data[end-2]!='@'))
3159 )
3160 {
3161 if (tolower(data[end])=='p' && tolower(data[end+1])=='r' &&
3162 tolower(data[end+2])=='e' && (data[end+3]=='>' || data[end+3]==' ')) // <pre> tag
3163 {
3164 // skip part until including </pre>
3165 end = end + processHtmlTagWrite(data.substr(end-1),end-1,false);
3166 break;
3167 }
3168 else
3169 {
3170 end++;
3171 }
3172 }
3173 else if (nb==0 && data[end-1]=='`')
3174 {
3175 while (end <= size && data[end - 1] == '`')
3176 {
3177 end++;
3178 nb++;
3179 }
3180 }
3181 else if (nb>0 && data[end-1]=='`')
3182 {
3183 size_t enb=0;
3184 while (end <= size && data[end - 1] == '`')
3185 {
3186 end++;
3187 enb++;
3188 }
3189 if (enb==nb) nb=0;
3190 }
3191 else
3192 {
3193 end++;
3194 }
3195 }
3196 if (j>0) end+=j-1;
3197 AUTO_TRACE_EXIT("offset={} end={}",offset,end);
3198 return end;
3199}
3200
3201void Markdown::Private::writeFencedCodeBlock(std::string_view data,std::string_view lang,
3202 size_t blockStart,size_t blockEnd)
3203{
3204 AUTO_TRACE("data='{}' lang={} blockStart={} blockEnd={}",Trace::trunc(data),lang,blockStart,blockEnd);
3205 if (!lang.empty() && lang[0]=='.') lang=lang.substr(1);
3206 const size_t size=data.size();
3207 size_t i=0;
3208 while (i<size && (data[i]==' ' || data[i]=='\t'))
3209 {
3210 out+=data[i++];
3211 blockStart--;
3212 blockEnd--;
3213 }
3214 out+="@icode";
3215 if (!lang.empty())
3216 {
3217 out+="{"+lang+"}";
3218 }
3219 out+=" ";
3220 addStrEscapeUtf8Nbsp(data.substr(blockStart+i,blockEnd-blockStart));
3221 out+="@endicode ";
3222}
3223
3224QCString Markdown::Private::processQuotations(std::string_view data,size_t refIndent)
3225{
3226 AUTO_TRACE("data='{}' refIndex='{}'",Trace::trunc(data),refIndent);
3227 out.clear();
3228 size_t i=0,end=0;
3229 size_t pi=std::string::npos;
3230 bool newBlock = false;
3231 bool insideList = false;
3232 size_t currentIndent = refIndent;
3233 size_t listIndent = refIndent;
3234 const size_t size = data.size();
3235 QCString lang;
3236 while (i<size)
3237 {
3238 end = findEndOfLine(data,i);
3239 // line is now found at [i..end)
3240
3241 size_t lineIndent=0;
3242 while (lineIndent<end && data[i+lineIndent]==' ') lineIndent++;
3243 //printf("** lineIndent=%d line=(%s)\n",lineIndent,qPrint(QCString(data+i).left(end-i)));
3244
3245 if (newBlock)
3246 {
3247 //printf("** end of block\n");
3248 if (insideList && lineIndent<currentIndent) // end of list
3249 {
3250 //printf("** end of list\n");
3251 currentIndent = refIndent;
3252 insideList = false;
3253 }
3254 newBlock = false;
3255 }
3256
3257 if ((listIndent=isListMarker(data.substr(i,end-i)))) // see if we need to increase the indent level
3258 {
3259 if (listIndent<currentIndent+4)
3260 {
3261 //printf("** start of list\n");
3262 insideList = true;
3263 currentIndent = listIndent;
3264 }
3265 }
3266 else if (isEndOfList(data.substr(i,end-i)))
3267 {
3268 //printf("** end of list\n");
3269 insideList = false;
3270 currentIndent = listIndent;
3271 }
3272 else if (isEmptyLine(data.substr(i,end-i)))
3273 {
3274 //printf("** new block\n");
3275 newBlock = true;
3276 }
3277 //printf("currentIndent=%d listIndent=%d refIndent=%d\n",currentIndent,listIndent,refIndent);
3278
3279 if (pi!=std::string::npos)
3280 {
3281 size_t blockStart=0, blockEnd=0, blockOffset=0;
3282 if (isFencedCodeBlock(data.substr(pi),currentIndent,lang,blockStart,blockEnd,blockOffset,fileName,lineNr))
3283 {
3284 auto addSpecialCommand = [&](const QCString &startCmd,const QCString &endCmd)
3285 {
3286 size_t cmdPos = pi+blockStart+1;
3287 QCString pl = data.substr(cmdPos,blockEnd-blockStart-1);
3288 size_t ii = 0;
3289 int nl = 1;
3290 // check for absence of start command, either @start<cmd>, or \\start<cmd>
3291 while (ii<pl.length() && qisspace(pl[ii]))
3292 {
3293 if (pl[ii]=='\n') nl++;
3294 ii++; // skip leading whitespace
3295 }
3296 bool addNewLines = false;
3297 if (ii+startCmd.length()>=pl.length() || // no room for start command
3298 (pl[ii]!='\\' && pl[ii]!='@') || // no @ or \ after whitespace
3299 qstrncmp(pl.data()+ii+1,startCmd.data(),startCmd.length())!=0) // no start command
3300 {
3301 // input: output:
3302 // ----------------------------------------------------
3303 // ```{plantuml} => @startuml
3304 // A->B A->B
3305 // ``` @enduml
3306 // ----------------------------------------------------
3307 pl = "@"+startCmd+"\n" + pl + "@"+endCmd;
3308 addNewLines = false;
3309 }
3310 else // we have a @start... command inside the code block
3311 {
3312 // input: output:
3313 // ----------------------------------------------------
3314 // ```{plantuml} \n
3315 // \n
3316 // @startuml => @startuml
3317 // A->B A->B
3318 // @enduml @enduml
3319 // ``` \n
3320 // ----------------------------------------------------
3321 addNewLines = true;
3322 }
3323 if (addNewLines) for (int j=0;j<nl;j++) out+='\n';
3324 processSpecialCommand(pl.view().substr(ii),ii);
3325 if (addNewLines) out+='\n';
3326 };
3327
3328 if (!Config_getString(PLANTUML_JAR_PATH).isEmpty() && lang=="plantuml")
3329 {
3330 addSpecialCommand("startuml","enduml");
3331 }
3332 else if (Config_getBool(HAVE_DOT) && lang=="dot")
3333 {
3334 addSpecialCommand("dot","enddot");
3335 }
3336 else if (lang=="msc") // msc is built-in
3337 {
3338 addSpecialCommand("msc","endmsc");
3339 }
3340 else // normal code block
3341 {
3342 writeFencedCodeBlock(data.substr(pi),lang.view(),blockStart,blockEnd);
3343 }
3344 i=pi+blockOffset;
3345 pi=std::string::npos;
3346 end=i+1;
3347 continue;
3348 }
3349 else if (isBlockQuote(data.substr(pi,i-pi),currentIndent))
3350 {
3351 i = pi+writeBlockQuote(data.substr(pi));
3352 pi=std::string::npos;
3353 end=i+1;
3354 continue;
3355 }
3356 else
3357 {
3358 //printf("quote out={%s}\n",QCString(data+pi).left(i-pi).data());
3359 out+=data.substr(pi,i-pi);
3360 }
3361 }
3362 pi=i;
3363 i=end;
3364 }
3365 if (pi!=std::string::npos && pi<size) // deal with the last line
3366 {
3367 if (isBlockQuote(data.substr(pi),currentIndent))
3368 {
3369 writeBlockQuote(data.substr(pi));
3370 }
3371 else
3372 {
3373 if (QCString(data.substr(pi)).startsWith("```") || QCString(data.substr(pi)).startsWith("~~~"))
3374 {
3375 warn(fileName, lineNr, "Ending inside a fenced code block. Maybe the end marker for the block is missing?");
3376 }
3377 out+=data.substr(pi);
3378 }
3379 }
3380
3381 //printf("Process quotations\n---- input ----\n%s\n---- output ----\n%s\n------------\n",
3382 // qPrint(s),prv->out.get());
3383
3384 return out;
3385}
3386
3387QCString Markdown::Private::processBlocks(std::string_view data,const size_t indent)
3388{
3389 AUTO_TRACE("data='{}' indent={}",Trace::trunc(data),indent);
3390 out.clear();
3391 size_t pi = std::string::npos;
3392 QCString id,link,title;
3393
3394#if 0 // commented out, since starting with a comment block is probably a usage error
3395 // see also http://stackoverflow.com/q/20478611/784672
3396
3397 // special case when the documentation starts with a code block
3398 // since the first line is skipped when looking for a code block later on.
3399 if (end>codeBlockIndent && isCodeBlock(data,0,end,blockIndent))
3400 {
3401 i=writeCodeBlock(out,data,size,blockIndent);
3402 end=i+1;
3403 pi=-1;
3404 }
3405#endif
3406
3407 size_t currentIndent = indent;
3408 size_t listIndent = indent;
3409 bool insideList = false;
3410 bool newBlock = false;
3411 // process each line
3412 size_t i=0;
3413 while (i<data.size())
3414 {
3415 size_t end = findEndOfLine(data,i);
3416 // line is now found at [i..end)
3417
3418 size_t lineIndent=0;
3419 int level = 0;
3420 while (lineIndent<end && data[i+lineIndent]==' ') lineIndent++;
3421 //printf("** lineIndent=%d line=(%s)\n",lineIndent,qPrint(QCString(data+i).left(end-i)));
3422
3423 if (newBlock)
3424 {
3425 //printf("** end of block\n");
3426 if (insideList && lineIndent<currentIndent) // end of list
3427 {
3428 //printf("** end of list\n");
3429 currentIndent = indent;
3430 insideList = false;
3431 }
3432 newBlock = false;
3433 }
3434
3435 if ((listIndent=isListMarker(data.substr(i,end-i)))) // see if we need to increase the indent level
3436 {
3437 if (listIndent<currentIndent+4)
3438 {
3439 //printf("** start of list\n");
3440 insideList = true;
3441 currentIndent = listIndent;
3442 }
3443 }
3444 else if (isEndOfList(data.substr(i,end-i)))
3445 {
3446 //printf("** end of list\n");
3447 insideList = false;
3448 currentIndent = listIndent;
3449 }
3450 else if (isEmptyLine(data.substr(i,end-i)))
3451 {
3452 //printf("** new block\n");
3453 newBlock = true;
3454 }
3455
3456 //printf("indent=%d listIndent=%d blockIndent=%d\n",indent,listIndent,blockIndent);
3457
3458 //printf("findEndOfLine: pi=%d i=%d end=%d\n",pi,i,end);
3459
3460 if (pi!=std::string::npos)
3461 {
3462 size_t blockStart=0, blockEnd=0, blockOffset=0;
3463 QCString lang;
3464 size_t blockIndent = currentIndent;
3465 size_t ref = 0;
3466 //printf("isHeaderLine(%s)=%d\n",QCString(data+i).left(size-i).data(),level);
3467 QCString endBlockName;
3468 if (data[i]=='@' || data[i]=='\\') endBlockName = isBlockCommand(data.substr(i),i);
3469 if (!endBlockName.isEmpty())
3470 {
3471 // handle previous line
3472 if (isLinkRef(data.substr(pi,i-pi),id,link,title))
3473 {
3474 linkRefs.emplace(id.lower().str(),LinkRef(link,title));
3475 }
3476 else
3477 {
3478 writeOneLineHeaderOrRuler(data.substr(pi,i-pi));
3479 }
3480 out+=data[i];
3481 i++;
3482 size_t l = endBlockName.length();
3483 while (i+l<data.size())
3484 {
3485 if ((data[i]=='\\' || data[i]=='@') && // command
3486 data[i-1]!='\\' && data[i-1]!='@') // not escaped
3487 {
3488 if (qstrncmp(&data[i+1],endBlockName.data(),l)==0)
3489 {
3490 out+=data[i];
3491 out+=endBlockName;
3492 i+=l+1;
3493 break;
3494 }
3495 }
3496 out+=data[i];
3497 i++;
3498 }
3499 }
3500 else if ((level=isHeaderline(data.substr(i),TRUE))>0)
3501 {
3502 //printf("Found header at %d-%d\n",i,end);
3503 while (pi<data.size() && data[pi]==' ') pi++;
3504 QCString header = data.substr(pi,i-pi-1);
3505 id = extractTitleId(header, level);
3506 //printf("header='%s' is='%s'\n",qPrint(header),qPrint(id));
3507 if (!header.isEmpty())
3508 {
3509 if (!id.isEmpty())
3510 {
3511 out+=level==1?"@section ":"@subsection ";
3512 out+=id;
3513 out+=" ";
3514 out+=header;
3515 out+="\n\n";
3516 }
3517 else
3518 {
3519 out+=level==1?"<h1>":"<h2>";
3520 out+=header;
3521 out+=level==1?"\n</h1>\n":"\n</h2>\n";
3522 }
3523 }
3524 else
3525 {
3526 out+="\n<hr>\n";
3527 }
3528 pi=std::string::npos;
3529 i=end;
3530 end=i+1;
3531 continue;
3532 }
3533 else if ((ref=isLinkRef(data.substr(pi),id,link,title)))
3534 {
3535 //printf("found link ref: id='%s' link='%s' title='%s'\n",
3536 // qPrint(id),qPrint(link),qPrint(title));
3537 linkRefs.emplace(id.lower().str(),LinkRef(link,title));
3538 i=ref+pi;
3539 end=i+1;
3540 }
3541 else if (isFencedCodeBlock(data.substr(pi),currentIndent,lang,blockStart,blockEnd,blockOffset,fileName,lineNr))
3542 {
3543 //printf("Found FencedCodeBlock lang='%s' start=%d end=%d code={%s}\n",
3544 // qPrint(lang),blockStart,blockEnd,QCString(data+pi+blockStart).left(blockEnd-blockStart).data());
3545 writeFencedCodeBlock(data.substr(pi),lang.view(),blockStart,blockEnd);
3546 i=pi+blockOffset;
3547 pi=std::string::npos;
3548 end=i+1;
3549 continue;
3550 }
3551 else if (isCodeBlock(data.substr(i,end-i),i,blockIndent))
3552 {
3553 // skip previous line (it is empty anyway)
3554 i+=writeCodeBlock(data.substr(i),blockIndent);
3555 pi=std::string::npos;
3556 end=i+1;
3557 continue;
3558 }
3559 else if (isTableBlock(data.substr(pi)))
3560 {
3561 i=pi+writeTableBlock(data.substr(pi));
3562 pi=std::string::npos;
3563 end=i+1;
3564 continue;
3565 }
3566 else
3567 {
3568 writeOneLineHeaderOrRuler(data.substr(pi,i-pi));
3569 }
3570 }
3571 pi=i;
3572 i=end;
3573 }
3574 //printf("last line %d size=%d\n",i,size);
3575 if (pi!=std::string::npos && pi<data.size()) // deal with the last line
3576 {
3577 if (isLinkRef(data.substr(pi),id,link,title))
3578 {
3579 //printf("found link ref: id='%s' link='%s' title='%s'\n",
3580 // qPrint(id),qPrint(link),qPrint(title));
3581 linkRefs.emplace(id.lower().str(),LinkRef(link,title));
3582 }
3583 else
3584 {
3585 writeOneLineHeaderOrRuler(data.substr(pi));
3586 }
3587 }
3588
3589 return out;
3590}
3591
3592static bool isOtherPage(std::string_view data)
3593{
3594#define OPC(x) if (literal_at(data,#x " ") || literal_at(data,#x "\n")) return true
3595 OPC(dir); OPC(defgroup); OPC(addtogroup); OPC(weakgroup); OPC(ingroup);
3596 OPC(fn); OPC(property); OPC(typedef); OPC(var); OPC(def);
3597 OPC(enum); OPC(namespace); OPC(class); OPC(concept); OPC(module);
3598 OPC(protocol); OPC(category); OPC(union); OPC(struct); OPC(interface);
3599 OPC(idlexcept); OPC(file);
3600#undef OPC
3601
3602 return false;
3603}
3604
3606{
3607 AUTO_TRACE("docs={}",Trace::trunc(docs));
3608 size_t i=0;
3609 std::string_view data(docs.str());
3610 const size_t size = data.size();
3611 if (!data.empty())
3612 {
3613 while (i<size && (data[i]==' ' || data[i]=='\n'))
3614 {
3615 i++;
3616 }
3617 if (literal_at(data.substr(i),"<!--!")) // skip over <!--! marker
3618 {
3619 i+=5;
3620 while (i<size && (data[i]==' ' || data[i]=='\n')) // skip over spaces after the <!--! marker
3621 {
3622 i++;
3623 }
3624 }
3625 if (i+1<size &&
3626 (data[i]=='\\' || data[i]=='@') &&
3627 (literal_at(data.substr(i+1),"page ") || literal_at(data.substr(i+1),"mainpage"))
3628 )
3629 {
3630 if (literal_at(data.substr(i+1),"page "))
3631 {
3632 AUTO_TRACE_EXIT("result=ExplicitPageResult::explicitPage");
3634 }
3635 else
3636 {
3637 AUTO_TRACE_EXIT("result=ExplicitPageResult::explicitMainPage");
3639 }
3640 }
3641 else if (i+1<size && (data[i]=='\\' || data[i]=='@') && isOtherPage(data.substr(i+1)))
3642 {
3643 AUTO_TRACE_EXIT("result=ExplicitPageResult::explicitOtherPage");
3645 }
3646 }
3647 AUTO_TRACE_EXIT("result=ExplicitPageResult::notExplicit");
3649}
3650
3651QCString Markdown::extractPageTitle(QCString &docs, QCString &id, int &prepend, bool &isIdGenerated)
3652{
3653 AUTO_TRACE("docs={} prepend={}",Trace::trunc(docs),id,prepend);
3654 // first first non-empty line
3655 prepend = 0;
3656 QCString title;
3657 size_t i=0;
3658 QCString docs_org(docs);
3659 std::string_view data(docs_org.str());
3660 const size_t size = data.size();
3661 docs.clear();
3662 while (i<size && (data[i]==' ' || data[i]=='\n'))
3663 {
3664 if (data[i]=='\n') prepend++;
3665 i++;
3666 }
3667 if (i>=size) { return QCString(); }
3668 size_t end1=i+1;
3669 while (end1<size && data[end1-1]!='\n') end1++;
3670 //printf("i=%d end1=%d size=%d line='%s'\n",i,end1,size,docs.mid(i,end1-i).data());
3671 // first line from i..end1
3672 if (end1<size)
3673 {
3674 // second line form end1..end2
3675 size_t end2=end1+1;
3676 while (end2<size && data[end2-1]!='\n') end2++;
3677 if (prv->isHeaderline(data.substr(end1),FALSE))
3678 {
3679 title = data.substr(i,end1-i-1);
3680 docs+="\n\n"+docs_org.mid(end2);
3681 id = prv->extractTitleId(title, 0, &isIdGenerated);
3682 //printf("extractPageTitle(title='%s' docs='%s' id='%s')\n",title.data(),docs.data(),id.data());
3683 AUTO_TRACE_EXIT("result={} id={} isIdGenerated={}",Trace::trunc(title),id,isIdGenerated);
3684 return title;
3685 }
3686 }
3687 if (i<end1 && prv->isAtxHeader(data.substr(i,end1-i),title,id,FALSE,&isIdGenerated)>0)
3688 {
3689 docs+="\n";
3690 docs+=docs_org.mid(end1);
3691 }
3692 else
3693 {
3694 docs=docs_org;
3695 id = prv->extractTitleId(title, 0, &isIdGenerated);
3696 }
3697 AUTO_TRACE_EXIT("result={} id={} isIdGenerated={}",Trace::trunc(title),id,isIdGenerated);
3698 return title;
3699}
3700
3701
3702//---------------------------------------------------------------------------
3703
3704QCString Markdown::process(const QCString &input, int &startNewlines, bool fromParseInput)
3705{
3706 if (input.isEmpty()) return input;
3707 size_t refIndent=0;
3708
3709 // for replace tabs by spaces
3710 QCString s = input;
3711 if (s.at(s.length()-1)!='\n') s += "\n"; // see PR #6766
3712 s = detab(s,refIndent);
3713 //printf("======== DeTab =========\n---- output -----\n%s\n---------\n",qPrint(s));
3714
3715 // then process quotation blocks (as these may contain other blocks)
3716 s = prv->processQuotations(s.view(),refIndent);
3717 //printf("======== Quotations =========\n---- output -----\n%s\n---------\n",qPrint(s));
3718
3719 // then process block items (headers, rules, and code blocks, references)
3720 s = prv->processBlocks(s.view(),refIndent);
3721 //printf("======== Blocks =========\n---- output -----\n%s\n---------\n",qPrint(s));
3722
3723 // finally process the inline markup (links, emphasis and code spans)
3724 prv->out.clear();
3725 prv->out.reserve(s.length());
3726 prv->processInline(s.view());
3727 if (fromParseInput)
3728 {
3729 Debug::print(Debug::Markdown,0,"---- output -----\n{}\n=========\n",qPrint(prv->out));
3730 }
3731 else
3732 {
3733 Debug::print(Debug::Markdown,0,"======== Markdown =========\n---- input ------- \n{}\n---- output -----\n{}\n=========\n",input,prv->out);
3734 }
3735
3736 // post processing
3737 QCString result = substitute(prv->out,g_doxy_nbsp,"&nbsp;");
3738 const char *p = result.data();
3739 if (p)
3740 {
3741 while (*p==' ') p++; // skip over spaces
3742 while (*p=='\n') {startNewlines++;p++;}; // skip over newlines
3743 if (literal_at(p,"<br>")) p+=4; // skip over <br>
3744 }
3745 if (p>result.data())
3746 {
3747 // strip part of the input
3748 result = result.mid(static_cast<int>(p-result.data()));
3749 }
3750 return result;
3751}
3752
3753//---------------------------------------------------------------------------
3754
3756{
3757 AUTO_TRACE("fileName={}",fileName);
3758 QCString absFileName = FileInfo(fileName.str()).absFilePath();
3759 QCString baseFn = stripFromPath(absFileName);
3760 int i = baseFn.findRev('.');
3761 if (i!=-1) baseFn = baseFn.left(i);
3762 QCString baseName = escapeCharsInString(baseFn,false,false);
3763 //printf("markdownFileNameToId(%s)=md_%s\n",qPrint(fileName),qPrint(baseName));
3764 QCString res = "md_"+baseName;
3765 AUTO_TRACE_EXIT("result={}",res);
3766 return res;
3767}
3768
3769//---------------------------------------------------------------------------
3770
3775
3777{
3778}
3779
3783
3785 const char *fileBuf,
3786 const std::shared_ptr<Entry> &root,
3787 ClangTUParser* /*clangParser*/)
3788{
3789 std::shared_ptr<Entry> current = std::make_shared<Entry>();
3790 int prepend = 0; // number of empty lines in front
3791 current->lang = SrcLangExt::Markdown;
3792 current->fileName = fileName;
3793 current->docFile = fileName;
3794 current->docLine = 1;
3795 QCString docs = stripIndentation(fileBuf);
3796 if (!docs.stripWhiteSpace().size()) return;
3797 Debug::print(Debug::Markdown,0,"======== Markdown =========\n---- input ------- \n{}\n",fileBuf);
3798 QCString id;
3799 Markdown markdown(fileName,1,0);
3800 bool isIdGenerated = false;
3801 QCString title = markdown.extractPageTitle(docs, id, prepend, isIdGenerated).stripWhiteSpace();
3802 QCString generatedId;
3803 if (isIdGenerated)
3804 {
3805 generatedId = id;
3806 id = "";
3807 }
3808 int indentLevel=title.isEmpty() ? 0 : -1;
3809 markdown.setIndentLevel(indentLevel);
3810 FileInfo fi(fileName.str());
3811 QCString fn = fi.fileName();
3813 QCString mdfileAsMainPage = Config_getString(USE_MDFILE_AS_MAINPAGE);
3814 QCString mdFileNameId = markdownFileNameToId(fileName);
3815 bool wasEmpty = id.isEmpty();
3816 if (wasEmpty) id = mdFileNameId;
3817 QCString relFileName = stripFromPath(fileName);
3818 bool isSubdirDocs = Config_getBool(IMPLICIT_DIR_DOCS) && relFileName.lower().endsWith("/readme.md");
3819 switch (isExplicitPage(docs))
3820 {
3822 if (!mdfileAsMainPage.isEmpty() &&
3823 (fi.absFilePath()==FileInfo(mdfileAsMainPage.str()).absFilePath()) // file reference with path
3824 )
3825 {
3826 docs.prepend("@ianchor{" + title + "} " + id + "\\ilinebr ");
3827 docs.prepend("@mainpage "+title+"\\ilinebr ");
3828 }
3829 else if (id=="mainpage" || id=="index")
3830 {
3831 if (title.isEmpty()) title = titleFn;
3832 docs.prepend("@ianchor{" + title + "} " + id + "\\ilinebr ");
3833 docs.prepend("@mainpage "+title+"\\ilinebr ");
3834 }
3835 else if (isSubdirDocs)
3836 {
3837 if (!generatedId.isEmpty() && !title.isEmpty())
3838 {
3839 docs.prepend("@section " + generatedId + " " + title + "\\ilinebr ");
3840 }
3841 docs.prepend("@dir\\ilinebr ");
3842 }
3843 else
3844 {
3845 if (title.isEmpty())
3846 {
3847 title = titleFn;
3848 prepend = 0;
3849 }
3850 if (!wasEmpty)
3851 {
3852 docs.prepend("@ianchor{" + title + "} " + id + "\\ilinebr @ianchor{" + relFileName + "} " + mdFileNameId + "\\ilinebr ");
3853 }
3854 else if (!generatedId.isEmpty())
3855 {
3856 docs.prepend("@ianchor " + generatedId + "\\ilinebr ");
3857 }
3858 else if (Config_getEnum(MARKDOWN_ID_STYLE)==MARKDOWN_ID_STYLE_t::GITHUB)
3859 {
3860 QCString autoId = AnchorGenerator::instance().generate(title.str());
3861 docs.prepend("@ianchor{" + title + "} " + autoId + "\\ilinebr ");
3862 }
3863 docs.prepend("@page "+id+" "+title+"\\ilinebr ");
3864 }
3865 for (int i = 0; i < prepend; i++) docs.prepend("\n");
3866 break;
3868 {
3869 // look for `@page label My Title\n` and capture `label` (match[1]) and ` My Title` (match[2])
3870 static const reg::Ex re(R"([ ]*[\\@]page\s+(\a[\w-]*)(\s*[^\n]*)\n)");
3871 reg::Match match;
3872 std::string s = docs.str();
3873 if (reg::search(s,match,re))
3874 {
3875 QCString orgLabel = match[1].str();
3876 QCString orgTitle = match[2].str();
3877 orgTitle = orgTitle.stripWhiteSpace();
3878 QCString newLabel = markdownFileNameToId(fileName);
3879 docs = docs.left(match[1].position())+ // part before label
3880 newLabel+ // new label
3881 match[2].str()+ // part between orgLabel and \n
3882 "\\ilinebr @ianchor{" + orgTitle + "} "+orgLabel+"\n"+ // add original anchor plus \n of above
3883 docs.right(docs.length()-match.length()); // add remainder of docs
3884 }
3885 }
3886 break;
3888 break;
3890 break;
3891 }
3892 int lineNr=1;
3893
3894 p->commentScanner.enterFile(fileName,lineNr);
3895 Protection prot = Protection::Public;
3896 bool needsEntry = false;
3897 int position=0;
3898 GuardedSectionStack guards;
3899 QCString processedDocs = markdown.process(docs,lineNr,true);
3900 while (p->commentScanner.parseCommentBlock(
3901 this,
3902 current.get(),
3903 processedDocs,
3904 fileName,
3905 lineNr,
3906 FALSE, // isBrief
3907 FALSE, // javadoc autobrief
3908 FALSE, // inBodyDocs
3909 prot, // protection
3910 position,
3911 needsEntry,
3912 true,
3913 &guards
3914 ))
3915 {
3916 if (needsEntry)
3917 {
3918 QCString docFile = current->docFile;
3919 root->moveToSubEntryAndRefresh(current);
3920 current->lang = SrcLangExt::Markdown;
3921 current->docFile = docFile;
3922 current->docLine = lineNr;
3923 }
3924 }
3925 if (needsEntry)
3926 {
3927 root->moveToSubEntryAndKeep(current);
3928 }
3929 p->commentScanner.leaveFile(fileName,lineNr);
3930}
3931
3933{
3934 Doxygen::parserManager->getOutlineParser("*.cpp")->parsePrototype(text);
3935}
3936
3937//------------------------------------------------------------------------
#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.