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 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{
2332 AUTO_TRACE("data='{}' refIndent={}",Trace::trunc(data),refIndent);
2333 const char dot = '.';
2334 auto isAlphaChar = [ ](char c) { return (c>='A' && c<='Z') || (c>='a' && c<='z'); };
2335 auto isAlphaNChar = [ ](char c) { return (c>='A' && c<='Z') || (c>='a' && c<='z') || (c>='0' && c<='9') || (c=='+'); };
2336 auto isLangChar = [&](char c) { return c==dot || isAlphaChar(c); };
2337 // rules: at least 3 ~~~, end of the block same amount of ~~~'s, otherwise
2338 // return FALSE
2339 size_t i=0;
2340 size_t indent=0;
2341 int startTildes=0;
2342 const size_t size = data.size();
2343 while (i < size && data[i] == ' ')
2344 {
2345 indent++;
2346 i++;
2347 }
2348 if (indent>=refIndent+4)
2349 {
2350 AUTO_TRACE_EXIT("result=false: content is part of code block indent={} refIndent={}",indent,refIndent);
2351 return FALSE;
2352 } // part of code block
2353 char tildaChar='~';
2354 if (i<size && data[i]=='`') tildaChar='`';
2355 while (i < size && data[i] == tildaChar)
2356 {
2357 startTildes++;
2358 i++;
2359 }
2360 if (startTildes<3)
2361 {
2362 AUTO_TRACE_EXIT("result=false: no fence marker found #tildes={}",startTildes);
2363 return FALSE;
2364 } // not enough tildes
2365 if (i<size && data[i]=='{') // extract .py from ```{.py} ... ```
2366 {
2367 i++; // skip over {
2368 if (data[i] == dot) i++; // skip over initial dot
2369 size_t startLang=i;
2370 while (i<size && (data[i]!='\n' && data[i]!='}')) i++; // find matching }
2371 if (i<size && data[i]=='}')
2372 {
2373 lang = data.substr(startLang,i-startLang);
2374 i++;
2375 }
2376 else // missing closing bracket, treat `{` as part of the content
2377 {
2378 i=startLang-1;
2379 lang="";
2380 }
2381 }
2382 else if (i<size && isLangChar(data[i])) /// extract python or .py from ```python...``` or ```.py...```
2383 {
2384 if (data[i] == dot) i++; // skip over initial dot
2385 size_t startLang=i;
2386 if (i<size && isAlphaChar(data[i])) //check first character of language specifier
2387 {
2388 i++;
2389 while (i<size && isAlphaNChar(data[i])) i++; // find end of language specifier
2390 }
2391 lang = data.substr(startLang,i-startLang);
2392 }
2393 else // no language specified
2394 {
2395 lang="";
2396 }
2397
2398 start=i;
2399 while (i<size)
2400 {
2401 if (data[i]==tildaChar)
2402 {
2403 end=i;
2404 int endTildes=0;
2405 while (i < size && data[i] == tildaChar)
2406 {
2407 endTildes++;
2408 i++;
2409 }
2410 while (i<size && data[i]==' ') i++;
2411 {
2412 if (endTildes==startTildes)
2413 {
2414 offset=i;
2415 AUTO_TRACE_EXIT("result=true: found end marker at offset {} lang='{}'",offset,lang);
2416 return true;
2417 }
2418 }
2419 }
2420 i++;
2421 }
2422 AUTO_TRACE_EXIT("result=false: no end marker found lang={}'",lang);
2423 return false;
2424}
2425
2426static bool isCodeBlock(std::string_view data, size_t offset,size_t &indent)
2427{
2428 AUTO_TRACE("data='{}' offset={}",Trace::trunc(data),offset);
2429 //printf("<isCodeBlock(offset=%d,size=%d,indent=%d)\n",offset,size,indent);
2430 // determine the indent of this line
2431 size_t i=0;
2432 size_t indent0=0;
2433 const size_t size = data.size();
2434 while (i < size && data[i] == ' ')
2435 {
2436 indent0++;
2437 i++;
2438 }
2439
2440 if (indent0<codeBlockIndent)
2441 {
2442 AUTO_TRACE_EXIT("result={}: line is not indented enough {}<4",false,indent0);
2443 return false;
2444 }
2445 if (indent0>=size || data[indent0]=='\n') // empty line does not start a code block
2446 {
2447 AUTO_TRACE_EXIT("result={}: only spaces at the end of a comment block",false);
2448 return false;
2449 }
2450
2451 i=offset;
2452 int nl=0;
2453 int nl_pos[3];
2454 int offset_i = static_cast<int>(offset);
2455 // search back 3 lines and remember the start of lines -1 and -2
2456 while (i>0 && nl<3) // i counts down from offset to 1
2457 {
2458 int j = static_cast<int>(i)-offset_i-1; // j counts from -1 to -offset
2459 // since j can be negative we need to rewrap data in a std::string_view
2460 size_t nl_size = isNewline(std::string_view(data.data()+j,data.size()-j));
2461 if (nl_size>0)
2462 {
2463 nl_pos[nl++]=j+static_cast<int>(nl_size);
2464 }
2465 i--;
2466 }
2467
2468 // if there are only 2 preceding lines, then line -2 starts at -offset
2469 if (i==0 && nl==2) nl_pos[nl++]=-offset_i;
2470
2471 if (nl==3) // we have at least 2 preceding lines
2472 {
2473 //printf(" positions: nl_pos=[%d,%d,%d] line[-2]='%s' line[-1]='%s'\n",
2474 // nl_pos[0],nl_pos[1],nl_pos[2],
2475 // qPrint(QCString(data+nl_pos[1]).left(nl_pos[0]-nl_pos[1]-1)),
2476 // qPrint(QCString(data+nl_pos[2]).left(nl_pos[1]-nl_pos[2]-1)));
2477
2478 // check that line -1 is empty
2479 // Note that the offset is negative so we need to rewrap the string view
2480 if (!isEmptyLine(std::string_view(data.data()+nl_pos[1],nl_pos[0]-nl_pos[1]-1)))
2481 {
2482 AUTO_TRACE_EXIT("result={}",FALSE);
2483 return FALSE;
2484 }
2485
2486 // determine the indent of line -2
2487 // Note that the offset is negative so we need to rewrap the string view
2488 indent=std::max(indent,computeIndentExcludingListMarkers(
2489 std::string_view(data.data()+nl_pos[2],nl_pos[1]-nl_pos[2])));
2490
2491 //printf(">isCodeBlock local_indent %d>=%d+%d=%d\n",
2492 // indent0,indent,codeBlockIndent,indent0>=indent+codeBlockIndent);
2493 // if the difference is >4 spaces -> code block
2494 bool res = indent0>=indent+codeBlockIndent;
2495 AUTO_TRACE_EXIT("result={}: code block if indent difference >4 spaces",res);
2496 return res;
2497 }
2498 else // not enough lines to determine the relative indent, use global indent
2499 {
2500 // check that line -1 is empty
2501 // Note that the offset is negative so we need to rewrap the string view
2502 if (nl==1 && !isEmptyLine(std::string_view(data.data()-offset,offset-1)))
2503 {
2504 AUTO_TRACE_EXIT("result=false");
2505 return FALSE;
2506 }
2507 //printf(">isCodeBlock global indent %d>=%d+4=%d nl=%d\n",
2508 // indent0,indent,indent0>=indent+4,nl);
2509 bool res = indent0>=indent+codeBlockIndent;
2510 AUTO_TRACE_EXIT("result={}: code block if indent difference >4 spaces",res);
2511 return res;
2512 }
2513}
2514
2515/** Finds the location of the table's contains in the string \a data.
2516 * Only one line will be inspected.
2517 * @param[in] data pointer to the string buffer.
2518 * @param[out] start offset of the first character of the table content
2519 * @param[out] end offset of the last character of the table content
2520 * @param[out] columns number of table columns found
2521 * @returns The offset until the next line in the buffer.
2522 */
2523static size_t findTableColumns(std::string_view data,size_t &start,size_t &end,size_t &columns)
2524{
2525 AUTO_TRACE("data='{}'",Trace::trunc(data));
2526 const size_t size = data.size();
2527 size_t i=0,n=0;
2528 // find start character of the table line
2529 while (i<size && data[i]==' ') i++;
2530 if (i < size && data[i] == '|' && data[i] != '\n')
2531 {
2532 i++;
2533 n++; // leading | does not count
2534 }
2535 start = i;
2536
2537 // find end character of the table line
2538 size_t j = 0;
2539 while (i<size && (j = isNewline(data.substr(i)))==0) i++;
2540 size_t eol=i+j;
2541
2542 if (j>0 && i>0) i--; // move i to point before newline
2543 while (i>0 && data[i]==' ') i--;
2544 if (i > 0 && data[i - 1] != '\\' && data[i] == '|')
2545 {
2546 i--;
2547 n++; // trailing or escaped | does not count
2548 }
2549 end = i;
2550
2551 // count columns between start and end
2552 columns=0;
2553 if (end>start)
2554 {
2555 i=start;
2556 while (i<=end) // look for more column markers
2557 {
2558 if (data[i]=='|' && (i==0 || data[i-1]!='\\')) columns++;
2559 if (columns==1) columns++; // first | make a non-table into a two column table
2560 i++;
2561 }
2562 }
2563 if (n==2 && columns==0) // table row has | ... |
2564 {
2565 columns++;
2566 }
2567 AUTO_TRACE_EXIT("eol={} start={} end={} columns={}",eol,start,end,columns);
2568 return eol;
2569}
2570
2571/** Returns TRUE iff data points to the start of a table block */
2572static bool isTableBlock(std::string_view data)
2573{
2574 AUTO_TRACE("data='{}'",Trace::trunc(data));
2575 size_t cc0=0, start=0, end=0;
2576
2577 // the first line should have at least two columns separated by '|'
2578 size_t i = findTableColumns(data,start,end,cc0);
2579 if (i>=data.size() || cc0<1)
2580 {
2581 AUTO_TRACE_EXIT("result=false: no |'s in the header");
2582 return FALSE;
2583 }
2584
2585 size_t cc1 = 0;
2586 size_t ret = findTableColumns(data.substr(i),start,end,cc1);
2587 size_t j=i+start;
2588 // separator line should consist of |, - and : and spaces only
2589 while (j<=end+i)
2590 {
2591 if (data[j]!=':' && data[j]!='-' && data[j]!='|' && data[j]!=' ')
2592 {
2593 AUTO_TRACE_EXIT("result=false: invalid character '{}'",data[j]);
2594 return FALSE; // invalid characters in table separator
2595 }
2596 j++;
2597 }
2598 if (cc1!=cc0) // number of columns should be same as previous line
2599 {
2600 AUTO_TRACE_EXIT("result=false: different number of columns as previous line {}!={}",cc1,cc0);
2601 return FALSE;
2602 }
2603
2604 i+=ret; // goto next line
2605 size_t cc2 = 0;
2606 findTableColumns(data.substr(i),start,end,cc2);
2607
2608 AUTO_TRACE_EXIT("result={}",cc1==cc2);
2609 return cc1==cc2;
2610}
2611
2612size_t Markdown::Private::writeTableBlock(std::string_view data)
2613{
2614 AUTO_TRACE("data='{}'",Trace::trunc(data));
2615 const size_t size = data.size();
2616
2617 size_t columns=0, start=0, end=0;
2618 size_t i = findTableColumns(data,start,end,columns);
2619 size_t headerStart = start;
2620 size_t headerEnd = end;
2621
2622 // read cell alignments
2623 size_t cc = 0;
2624 size_t ret = findTableColumns(data.substr(i),start,end,cc);
2625 size_t k=0;
2626 std::vector<Alignment> columnAlignment(columns);
2627
2628 bool leftMarker=false, rightMarker=false, startFound=false;
2629 size_t j=start+i;
2630 while (j<=end+i)
2631 {
2632 if (!startFound)
2633 {
2634 if (data[j]==':') { leftMarker=TRUE; startFound=TRUE; }
2635 if (data[j]=='-') startFound=TRUE;
2636 //printf(" data[%d]=%c startFound=%d\n",j,data[j],startFound);
2637 }
2638 if (data[j]=='-') rightMarker=FALSE;
2639 else if (data[j]==':') rightMarker=TRUE;
2640 if (j<=end+i && (data[j]=='|' && (j==0 || data[j-1]!='\\')))
2641 {
2642 if (k<columns)
2643 {
2644 columnAlignment[k] = markersToAlignment(leftMarker,rightMarker);
2645 //printf("column[%d] alignment=%d\n",k,columnAlignment[k]);
2646 leftMarker=FALSE;
2647 rightMarker=FALSE;
2648 startFound=FALSE;
2649 }
2650 k++;
2651 }
2652 j++;
2653 }
2654 if (k<columns)
2655 {
2656 columnAlignment[k] = markersToAlignment(leftMarker,rightMarker);
2657 //printf("column[%d] alignment=%d\n",k,columnAlignment[k]);
2658 }
2659 // proceed to next line
2660 i+=ret;
2661
2662 // Store the table cell information by row then column. This
2663 // allows us to handle row spanning.
2664 std::vector<std::vector<TableCell> > tableContents;
2665
2666 size_t m = headerStart;
2667 std::vector<TableCell> headerContents(columns);
2668 for (k=0;k<columns;k++)
2669 {
2670 while (m<=headerEnd && (data[m]!='|' || (m>0 && data[m-1]=='\\')))
2671 {
2672 headerContents[k].cellText += data[m++];
2673 }
2674 m++;
2675 // do the column span test before stripping white space
2676 // || is spanning columns, | | is not
2677 headerContents[k].colSpan = headerContents[k].cellText.isEmpty();
2678 headerContents[k].cellText = headerContents[k].cellText.stripWhiteSpace();
2679 }
2680 tableContents.push_back(headerContents);
2681
2682 // write table cells
2683 while (i<size)
2684 {
2685 ret = findTableColumns(data.substr(i),start,end,cc);
2686 if (cc!=columns) break; // end of table
2687
2688 j=start+i;
2689 k=0;
2690 std::vector<TableCell> rowContents(columns);
2691 while (j<=end+i)
2692 {
2693 if (j<=end+i && (data[j]=='|' && (j==0 || data[j-1]!='\\')))
2694 {
2695 // do the column span test before stripping white space
2696 // || is spanning columns, | | is not
2697 rowContents[k].colSpan = rowContents[k].cellText.isEmpty();
2698 rowContents[k].cellText = rowContents[k].cellText.stripWhiteSpace();
2699 k++;
2700 } // if (j<=end+i && (data[j]=='|' && (j==0 || data[j-1]!='\\')))
2701 else
2702 {
2703 rowContents[k].cellText += data[j];
2704 } // else { if (j<=end+i && (data[j]=='|' && (j==0 || data[j-1]!='\\'))) }
2705 j++;
2706 } // while (j<=end+i)
2707 // do the column span test before stripping white space
2708 // || is spanning columns, | | is not
2709 rowContents[k].colSpan = rowContents[k].cellText.isEmpty();
2710 rowContents[k].cellText = rowContents[k].cellText.stripWhiteSpace();
2711 tableContents.push_back(rowContents);
2712
2713 // proceed to next line
2714 i+=ret;
2715 }
2716
2717 out+="<table class=\"markdownTable\">";
2718 QCString cellTag("th"), cellClass("class=\"markdownTableHead");
2719 for (size_t row = 0; row < tableContents.size(); row++)
2720 {
2721 if (row)
2722 {
2723 if (row % 2)
2724 {
2725 out+="\n<tr class=\"markdownTableRowOdd\">";
2726 }
2727 else
2728 {
2729 out+="\n<tr class=\"markdownTableRowEven\">";
2730 }
2731 }
2732 else
2733 {
2734 out+="\n <tr class=\"markdownTableHead\">";
2735 }
2736 for (size_t c = 0; c < columns; c++)
2737 {
2738 // save the cell text for use after column span computation
2739 QCString cellText(tableContents[row][c].cellText);
2740
2741 // Row span handling. Spanning rows will contain a caret ('^').
2742 // If the current cell contains just a caret, this is part of an
2743 // earlier row's span and the cell should not be added to the
2744 // output.
2745 if (tableContents[row][c].cellText == "^")
2746 {
2747 continue;
2748 }
2749 if (tableContents[row][c].colSpan)
2750 {
2751 int cr = static_cast<int>(c);
2752 while ( cr >= 0 && tableContents[row][cr].colSpan)
2753 {
2754 cr--;
2755 };
2756 if (cr >= 0 && tableContents[row][cr].cellText == "^") continue;
2757 }
2758 size_t rowSpan = 1, spanRow = row+1;
2759 while ((spanRow < tableContents.size()) &&
2760 (tableContents[spanRow][c].cellText == "^"))
2761 {
2762 spanRow++;
2763 rowSpan++;
2764 }
2765
2766 out+=" <" + cellTag + " " + cellClass;
2767 // use appropriate alignment style
2768 switch (columnAlignment[c])
2769 {
2770 case Alignment::Left: out+="Left\""; break;
2771 case Alignment::Right: out+="Right\""; break;
2772 case Alignment::Center: out+="Center\""; break;
2773 case Alignment::None: out+="None\""; break;
2774 }
2775
2776 if (rowSpan > 1)
2777 {
2778 QCString spanStr;
2779 spanStr.setNum(rowSpan);
2780 out+=" rowspan=\"" + spanStr + "\"";
2781 }
2782 // Column span handling, assumes that column spans will have
2783 // empty strings, which would indicate the sequence "||", used
2784 // to signify spanning columns.
2785 size_t colSpan = 1;
2786 while ((c+1 < columns) && tableContents[row][c+1].colSpan)
2787 {
2788 c++;
2789 colSpan++;
2790 }
2791 if (colSpan > 1)
2792 {
2793 QCString spanStr;
2794 spanStr.setNum(colSpan);
2795 out+=" colspan=\"" + spanStr + "\"";
2796 }
2797 // need at least one space on either side of the cell text in
2798 // order for doxygen to do other formatting
2799 out+="> " + cellText + " \\ilinebr </" + cellTag + ">";
2800 }
2801 cellTag = "td";
2802 cellClass = "class=\"markdownTableBody";
2803 out+=" </tr>";
2804 }
2805 out+="</table>\n";
2806
2807 AUTO_TRACE_EXIT("i={}",i);
2808 return i;
2809}
2810
2811
2812static bool hasLineBreak(std::string_view data)
2813{
2814 AUTO_TRACE("data='{}'",Trace::trunc(data));
2815 size_t i=0;
2816 size_t j=0;
2817 // search for end of line and also check if it is not a completely blank
2818 while (i<data.size() && data[i]!='\n')
2819 {
2820 if (data[i]!=' ' && data[i]!='\t') j++; // some non whitespace
2821 i++;
2822 }
2823 if (i>=data.size()) { return 0; } // empty line
2824 if (i<2) { return 0; } // not long enough
2825 bool res = (j>0 && data[i-1]==' ' && data[i-2]==' '); // non blank line with at two spaces at the end
2826 AUTO_TRACE_EXIT("result={}",res);
2827 return res;
2828}
2829
2830
2832{
2833 AUTO_TRACE("data='{}'",Trace::trunc(data));
2834 int level=0;
2835 QCString header;
2836 QCString id;
2837 if (isHRuler(data))
2838 {
2839 out+="<hr>\n";
2840 }
2841 else if ((level=isAtxHeader(data,header,id,TRUE)))
2842 {
2843 QCString hTag;
2844 if (!id.isEmpty())
2845 {
2846 switch (level)
2847 {
2848 case SectionType::Section: out+="@section "; break;
2849 case SectionType::Subsection: out+="@subsection "; break;
2850 case SectionType::Subsubsection: out+="@subsubsection "; break;
2851 case SectionType::Paragraph: out+="@paragraph "; break;
2852 case SectionType::Subparagraph: out+="@subparagraph "; break;
2853 case SectionType::Subsubparagraph: out+="@subsubparagraph "; break;
2854 }
2855 out+=id;
2856 out+=" ";
2857 out+=header;
2858 out+="\n";
2859 }
2860 else
2861 {
2862 hTag.sprintf("h%d",level);
2863 out+="<"+hTag+">";
2864 out+=header;
2865 out+="</"+hTag+">\n";
2866 }
2867 }
2868 else if (data.size()>0) // nothing interesting -> just output the line
2869 {
2870 size_t tmpSize = data.size();
2871 if (data[data.size()-1] == '\n') tmpSize--;
2872 out+=data.substr(0,tmpSize);
2873
2874 if (hasLineBreak(data))
2875 {
2876 out+="\\ilinebr<br>";
2877 }
2878 if (tmpSize != data.size()) out+='\n';
2879 }
2880}
2881
2882static const std::unordered_map<std::string,std::string> g_quotationHeaderMap = {
2883 // GitHub style Doxygen command
2884 { "[!note]", "\\note" },
2885 { "[!warning]", "\\warning" },
2886 { "[!tip]", "\\remark" },
2887 { "[!caution]", "\\attention" },
2888 { "[!important]", "\\important" }
2889};
2890
2891size_t Markdown::Private::writeBlockQuote(std::string_view data)
2892{
2893 AUTO_TRACE("data='{}'",Trace::trunc(data));
2894 size_t i=0;
2895 int curLevel=0;
2896 size_t end=0;
2897 const size_t size = data.size();
2898 std::string startCmd;
2899 int isGitHubAlert = false;
2900 int isGitHubFirst = false;
2901 while (i<size)
2902 {
2903 // find end of this line
2904 end=i+1;
2905 while (end<=size && data[end-1]!='\n') end++;
2906 size_t j=i;
2907 int level=0;
2908 size_t indent=i;
2909 // compute the quoting level
2910 while (j<end && (data[j]==' ' || data[j]=='>'))
2911 {
2912 if (data[j]=='>') { level++; indent=j+1; }
2913 else if (j>0 && data[j-1]=='>') indent=j+1;
2914 j++;
2915 }
2916 if (indent>0 && j>0 && data[j-1]=='>' &&
2917 !(j==size || data[j]=='\n')) // disqualify last > if not followed by space
2918 {
2919 indent--;
2920 level--;
2921 j--;
2922 }
2923 AUTO_TRACE_ADD("indent={} i={} j={} end={} level={} line={}",indent,i,j,end,level,Trace::trunc(&data[i]));
2924 if (level==0 && j<end-1 && !isListMarker(data.substr(j)) && !isHRuler(data.substr(j)))
2925 {
2926 level = curLevel; // lazy
2927 }
2928 if (level==1)
2929 {
2930 QCString txt = stripWhiteSpace(data.substr(indent,end-indent));
2931 auto it = g_quotationHeaderMap.find(txt.lower().str()); // TODO: in C++20 the std::string can be dropped
2932 if (it != g_quotationHeaderMap.end())
2933 {
2934 isGitHubAlert = true;
2935 isGitHubFirst = true;
2936 startCmd = it->second;
2937 }
2938 }
2939 if (level>curLevel) // quote level increased => add start markers
2940 {
2941 if (level!=1 || !isGitHubAlert) // normal block quote
2942 {
2943 for (int l=curLevel;l<level-1;l++)
2944 {
2945 out+="<blockquote>";
2946 }
2947 out += "<blockquote>&zwj;"; // empty blockquotes are also shown
2948 }
2949 else if (!startCmd.empty()) // GitHub style alert
2950 {
2951 out += startCmd + " ";
2952 }
2953 }
2954 else if (level<curLevel) // quote level decreased => add end markers
2955 {
2956 int decrLevel = curLevel;
2957 if (level==0 && isGitHubAlert)
2958 {
2959 decrLevel--;
2960 }
2961 for (int l=level;l<decrLevel;l++)
2962 {
2963 out += "</blockquote>\\ilinebr ";
2964 }
2965 }
2966 if (level==0)
2967 {
2968 curLevel=0;
2969 break; // end of quote block
2970 }
2971 // copy line without quotation marks
2972 if (curLevel!=0 || !isGitHubAlert)
2973 {
2974 std::string_view txt = data.substr(indent,end-indent);
2975 if (stripWhiteSpace(txt).empty() && !startCmd.empty())
2976 {
2977 if (!isGitHubFirst) out += "<br>";
2978 out += "<br>\n";
2979 }
2980 else
2981 {
2982 out += txt;
2983 }
2984 isGitHubFirst = false;
2985 }
2986 else // GitHub alert section
2987 {
2988 out+= "\n";
2989 }
2990 curLevel=level;
2991 // proceed with next line
2992 i=end;
2993 }
2994 // end of comment within blockquote => add end markers
2995 if (isGitHubAlert) // GitHub alert doesn't have a blockquote
2996 {
2997 curLevel--;
2998 }
2999 for (int l=0;l<curLevel;l++)
3000 {
3001 out+="</blockquote>";
3002 }
3003 AUTO_TRACE_EXIT("i={}",i);
3004 return i;
3005}
3006
3007// For code blocks that are outputted as part of an indented include or snippet command, we need to filter out
3008// the location string, i.e. '\ifile "..." \iline \ilinebr'.
3009bool skipOverFileAndLineCommands(std::string_view data,size_t indent,size_t &offset,std::string &location)
3010{
3011 size_t i = offset;
3012 size_t size = data.size();
3013 while (i<data.size() && data[i]==' ') i++;
3014 if (literal_at(data.substr(i),"\\ifile \""))
3015 {
3016 size_t locStart = i;
3017 if (i>offset) locStart--; // include the space before \ifile
3018 i+=8;
3019 bool found=false;
3020 while (i+9<size && data[i]!='\n')
3021 {
3022 if (literal_at(data.substr(i),"\\ilinebr "))
3023 {
3024 found=true;
3025 break;
3026 }
3027 i++;
3028 }
3029 if (found)
3030 {
3031 i+=9;
3032 location=data.substr(locStart,i-locStart);
3033 location+='\n';
3034 while (indent > 0 && i < size && data[i] == ' ')
3035 {
3036 i++;
3037 indent--;
3038 }
3039 if (i<size && data[i]=='\n') i++;
3040 offset = i;
3041 return true;
3042 }
3043 }
3044 return false;
3045}
3046
3047size_t Markdown::Private::writeCodeBlock(std::string_view data,size_t refIndent)
3048{
3049 AUTO_TRACE("data='{}' refIndent={}",Trace::trunc(data),refIndent);
3050 const size_t size = data.size();
3051 size_t i=0;
3052 // no need for \ilinebr here as the previous line was empty and was skipped
3053 out+="@iverbatim\n";
3054 int emptyLines=0;
3055 std::string location;
3056 while (i<size)
3057 {
3058 // find end of this line
3059 size_t end=i+1;
3060 while (end<=size && data[end-1]!='\n') end++;
3061 size_t j=i;
3062 size_t indent=0;
3063 while (j < end && data[j] == ' ')
3064 {
3065 j++;
3066 indent++;
3067 }
3068 //printf("j=%d end=%d indent=%d refIndent=%d tabSize=%d data={%s}\n",
3069 // j,end,indent,refIndent,Config_getInt(TAB_SIZE),qPrint(QCString(data+i).left(end-i-1)));
3070 if (j==end-1) // empty line
3071 {
3072 emptyLines++;
3073 i=end;
3074 }
3075 else if (indent>=refIndent+codeBlockIndent) // enough indent to continue the code block
3076 {
3077 while (emptyLines>0) // write skipped empty lines
3078 {
3079 // add empty line
3080 out+="\n";
3081 emptyLines--;
3082 }
3083 // add code line minus the indent
3084 size_t offset = i+refIndent+codeBlockIndent;
3085 std::string lineLoc;
3086 if (skipOverFileAndLineCommands(data,codeBlockIndent,offset,lineLoc))
3087 {
3088 location = lineLoc;
3089 }
3090 out+=data.substr(offset,end-offset);
3091 i=end;
3092 }
3093 else // end of code block
3094 {
3095 break;
3096 }
3097 }
3098 out+="@endiverbatim";
3099 if (!location.empty())
3100 {
3101 out+=location;
3102 }
3103 else
3104 {
3105 out+="\\ilinebr ";
3106 }
3107 while (emptyLines>0) // write skipped empty lines
3108 {
3109 // add empty line
3110 out+="\n";
3111 emptyLines--;
3112 }
3113 AUTO_TRACE_EXIT("i={}",i);
3114 return i;
3115}
3116
3117// start searching for the end of the line start at offset \a i
3118// keeping track of possible blocks that need to be skipped.
3119size_t Markdown::Private::findEndOfLine(std::string_view data,size_t offset)
3120{
3121 AUTO_TRACE("data='{}'",Trace::trunc(data));
3122 // find end of the line
3123 const size_t size = data.size();
3124 size_t nb=0, end=offset+1, j=0;
3125 while (end<=size && (j=isNewline(data.substr(end-1)))==0)
3126 {
3127 // while looking for the end of the line we might encounter a block
3128 // that needs to be passed unprocessed.
3129 if ((data[end-1]=='\\' || data[end-1]=='@') && // command
3130 (end<=1 || (data[end-2]!='\\' && data[end-2]!='@')) // not escaped
3131 )
3132 {
3133 QCString endBlockName = isBlockCommand(data.substr(end-1),end-1);
3134 end++;
3135 if (!endBlockName.isEmpty())
3136 {
3137 size_t l = endBlockName.length();
3138 for (;end+l+1<size;end++) // search for end of block marker
3139 {
3140 if ((data[end]=='\\' || data[end]=='@') &&
3141 data[end-1]!='\\' && data[end-1]!='@'
3142 )
3143 {
3144 if (qstrncmp(&data[end+1],endBlockName.data(),l)==0)
3145 {
3146 // found end marker, skip over this block
3147 //printf("feol.block out={%s}\n",qPrint(QCString(data+i).left(end+l+1-i)));
3148 end = end + l + 2;
3149 break;
3150 }
3151 }
3152 }
3153 }
3154 }
3155 else if (nb==0 && data[end-1]=='<' && size>=6 && end+6<size &&
3156 (end<=1 || (data[end-2]!='\\' && data[end-2]!='@'))
3157 )
3158 {
3159 if (tolower(data[end])=='p' && tolower(data[end+1])=='r' &&
3160 tolower(data[end+2])=='e' && (data[end+3]=='>' || data[end+3]==' ')) // <pre> tag
3161 {
3162 // skip part until including </pre>
3163 end = end + processHtmlTagWrite(data.substr(end-1),end-1,false);
3164 break;
3165 }
3166 else
3167 {
3168 end++;
3169 }
3170 }
3171 else if (nb==0 && data[end-1]=='`')
3172 {
3173 while (end <= size && data[end - 1] == '`')
3174 {
3175 end++;
3176 nb++;
3177 }
3178 }
3179 else if (nb>0 && data[end-1]=='`')
3180 {
3181 size_t enb=0;
3182 while (end <= size && data[end - 1] == '`')
3183 {
3184 end++;
3185 enb++;
3186 }
3187 if (enb==nb) nb=0;
3188 }
3189 else
3190 {
3191 end++;
3192 }
3193 }
3194 if (j>0) end+=j-1;
3195 AUTO_TRACE_EXIT("offset={} end={}",offset,end);
3196 return end;
3197}
3198
3199void Markdown::Private::writeFencedCodeBlock(std::string_view data,std::string_view lang,
3200 size_t blockStart,size_t blockEnd)
3201{
3202 AUTO_TRACE("data='{}' lang={} blockStart={} blockEnd={}",Trace::trunc(data),lang,blockStart,blockEnd);
3203 if (!lang.empty() && lang[0]=='.') lang=lang.substr(1);
3204 const size_t size=data.size();
3205 size_t i=0;
3206 while (i<size && (data[i]==' ' || data[i]=='\t'))
3207 {
3208 out+=data[i++];
3209 blockStart--;
3210 blockEnd--;
3211 }
3212 out+="@icode";
3213 if (!lang.empty())
3214 {
3215 out+="{"+lang+"}";
3216 }
3217 out+=" ";
3218 addStrEscapeUtf8Nbsp(data.substr(blockStart+i,blockEnd-blockStart));
3219 out+="@endicode ";
3220}
3221
3222QCString Markdown::Private::processQuotations(std::string_view data,size_t refIndent)
3223{
3224 AUTO_TRACE("data='{}' refIndex='{}'",Trace::trunc(data),refIndent);
3225 out.clear();
3226 size_t i=0,end=0;
3227 size_t pi=std::string::npos;
3228 bool newBlock = false;
3229 bool insideList = false;
3230 size_t currentIndent = refIndent;
3231 size_t listIndent = refIndent;
3232 const size_t size = data.size();
3233 QCString lang;
3234 while (i<size)
3235 {
3236 end = findEndOfLine(data,i);
3237 // line is now found at [i..end)
3238
3239 size_t lineIndent=0;
3240 while (lineIndent<end && data[i+lineIndent]==' ') lineIndent++;
3241 //printf("** lineIndent=%d line=(%s)\n",lineIndent,qPrint(QCString(data+i).left(end-i)));
3242
3243 if (newBlock)
3244 {
3245 //printf("** end of block\n");
3246 if (insideList && lineIndent<currentIndent) // end of list
3247 {
3248 //printf("** end of list\n");
3249 currentIndent = refIndent;
3250 insideList = false;
3251 }
3252 newBlock = false;
3253 }
3254
3255 if ((listIndent=isListMarker(data.substr(i,end-i)))) // see if we need to increase the indent level
3256 {
3257 if (listIndent<currentIndent+4)
3258 {
3259 //printf("** start of list\n");
3260 insideList = true;
3261 currentIndent = listIndent;
3262 }
3263 }
3264 else if (isEndOfList(data.substr(i,end-i)))
3265 {
3266 //printf("** end of list\n");
3267 insideList = false;
3268 currentIndent = listIndent;
3269 }
3270 else if (isEmptyLine(data.substr(i,end-i)))
3271 {
3272 //printf("** new block\n");
3273 newBlock = true;
3274 }
3275 //printf("currentIndent=%d listIndent=%d refIndent=%d\n",currentIndent,listIndent,refIndent);
3276
3277 if (pi!=std::string::npos)
3278 {
3279 size_t blockStart=0, blockEnd=0, blockOffset=0;
3280 if (isFencedCodeBlock(data.substr(pi),currentIndent,lang,blockStart,blockEnd,blockOffset))
3281 {
3282 auto addSpecialCommand = [&](const QCString &startCmd,const QCString &endCmd)
3283 {
3284 size_t cmdPos = pi+blockStart+1;
3285 QCString pl = data.substr(cmdPos,blockEnd-blockStart-1);
3286 size_t ii = 0;
3287 int nl = 1;
3288 // check for absence of start command, either @start<cmd>, or \\start<cmd>
3289 while (ii<pl.length() && qisspace(pl[ii]))
3290 {
3291 if (pl[ii]=='\n') nl++;
3292 ii++; // skip leading whitespace
3293 }
3294 bool addNewLines = false;
3295 if (ii+startCmd.length()>=pl.length() || // no room for start command
3296 (pl[ii]!='\\' && pl[ii]!='@') || // no @ or \ after whitespace
3297 qstrncmp(pl.data()+ii+1,startCmd.data(),startCmd.length())!=0) // no start command
3298 {
3299 // input: output:
3300 // ----------------------------------------------------
3301 // ```{plantuml} => @startuml
3302 // A->B A->B
3303 // ``` @enduml
3304 // ----------------------------------------------------
3305 pl = "@"+startCmd+"\n" + pl + "@"+endCmd;
3306 addNewLines = false;
3307 }
3308 else // we have a @start... command inside the code block
3309 {
3310 // input: output:
3311 // ----------------------------------------------------
3312 // ```{plantuml} \n
3313 // \n
3314 // @startuml => @startuml
3315 // A->B A->B
3316 // @enduml @enduml
3317 // ``` \n
3318 // ----------------------------------------------------
3319 addNewLines = true;
3320 }
3321 if (addNewLines) for (int j=0;j<nl;j++) out+='\n';
3322 processSpecialCommand(pl.view().substr(ii),ii);
3323 if (addNewLines) out+='\n';
3324 };
3325
3326 if (!Config_getString(PLANTUML_JAR_PATH).isEmpty() && lang=="plantuml")
3327 {
3328 addSpecialCommand("startuml","enduml");
3329 }
3330 else if (Config_getBool(HAVE_DOT) && lang=="dot")
3331 {
3332 addSpecialCommand("dot","enddot");
3333 }
3334 else if (lang=="msc") // msc is built-in
3335 {
3336 addSpecialCommand("msc","endmsc");
3337 }
3338 else // normal code block
3339 {
3340 writeFencedCodeBlock(data.substr(pi),lang.view(),blockStart,blockEnd);
3341 }
3342 i=pi+blockOffset;
3343 pi=std::string::npos;
3344 end=i+1;
3345 continue;
3346 }
3347 else if (isBlockQuote(data.substr(pi,i-pi),currentIndent))
3348 {
3349 i = pi+writeBlockQuote(data.substr(pi));
3350 pi=std::string::npos;
3351 end=i+1;
3352 continue;
3353 }
3354 else
3355 {
3356 //printf("quote out={%s}\n",QCString(data+pi).left(i-pi).data());
3357 out+=data.substr(pi,i-pi);
3358 }
3359 }
3360 pi=i;
3361 i=end;
3362 }
3363 if (pi!=std::string::npos && pi<size) // deal with the last line
3364 {
3365 if (isBlockQuote(data.substr(pi),currentIndent))
3366 {
3367 writeBlockQuote(data.substr(pi));
3368 }
3369 else
3370 {
3371 out+=data.substr(pi);
3372 }
3373 }
3374
3375 //printf("Process quotations\n---- input ----\n%s\n---- output ----\n%s\n------------\n",
3376 // qPrint(s),prv->out.get());
3377
3378 return out;
3379}
3380
3381QCString Markdown::Private::processBlocks(std::string_view data,const size_t indent)
3382{
3383 AUTO_TRACE("data='{}' indent={}",Trace::trunc(data),indent);
3384 out.clear();
3385 size_t pi = std::string::npos;
3386 QCString id,link,title;
3387
3388#if 0 // commented out, since starting with a comment block is probably a usage error
3389 // see also http://stackoverflow.com/q/20478611/784672
3390
3391 // special case when the documentation starts with a code block
3392 // since the first line is skipped when looking for a code block later on.
3393 if (end>codeBlockIndent && isCodeBlock(data,0,end,blockIndent))
3394 {
3395 i=writeCodeBlock(out,data,size,blockIndent);
3396 end=i+1;
3397 pi=-1;
3398 }
3399#endif
3400
3401 size_t currentIndent = indent;
3402 size_t listIndent = indent;
3403 bool insideList = false;
3404 bool newBlock = false;
3405 // process each line
3406 size_t i=0;
3407 while (i<data.size())
3408 {
3409 size_t end = findEndOfLine(data,i);
3410 // line is now found at [i..end)
3411
3412 size_t lineIndent=0;
3413 int level = 0;
3414 while (lineIndent<end && data[i+lineIndent]==' ') lineIndent++;
3415 //printf("** lineIndent=%d line=(%s)\n",lineIndent,qPrint(QCString(data+i).left(end-i)));
3416
3417 if (newBlock)
3418 {
3419 //printf("** end of block\n");
3420 if (insideList && lineIndent<currentIndent) // end of list
3421 {
3422 //printf("** end of list\n");
3423 currentIndent = indent;
3424 insideList = false;
3425 }
3426 newBlock = false;
3427 }
3428
3429 if ((listIndent=isListMarker(data.substr(i,end-i)))) // see if we need to increase the indent level
3430 {
3431 if (listIndent<currentIndent+4)
3432 {
3433 //printf("** start of list\n");
3434 insideList = true;
3435 currentIndent = listIndent;
3436 }
3437 }
3438 else if (isEndOfList(data.substr(i,end-i)))
3439 {
3440 //printf("** end of list\n");
3441 insideList = false;
3442 currentIndent = listIndent;
3443 }
3444 else if (isEmptyLine(data.substr(i,end-i)))
3445 {
3446 //printf("** new block\n");
3447 newBlock = true;
3448 }
3449
3450 //printf("indent=%d listIndent=%d blockIndent=%d\n",indent,listIndent,blockIndent);
3451
3452 //printf("findEndOfLine: pi=%d i=%d end=%d\n",pi,i,end);
3453
3454 if (pi!=std::string::npos)
3455 {
3456 size_t blockStart=0, blockEnd=0, blockOffset=0;
3457 QCString lang;
3458 size_t blockIndent = currentIndent;
3459 size_t ref = 0;
3460 //printf("isHeaderLine(%s)=%d\n",QCString(data+i).left(size-i).data(),level);
3461 QCString endBlockName;
3462 if (data[i]=='@' || data[i]=='\\') endBlockName = isBlockCommand(data.substr(i),i);
3463 if (!endBlockName.isEmpty())
3464 {
3465 // handle previous line
3466 if (isLinkRef(data.substr(pi,i-pi),id,link,title))
3467 {
3468 linkRefs.emplace(id.lower().str(),LinkRef(link,title));
3469 }
3470 else
3471 {
3472 writeOneLineHeaderOrRuler(data.substr(pi,i-pi));
3473 }
3474 out+=data[i];
3475 i++;
3476 size_t l = endBlockName.length();
3477 while (i+l<data.size())
3478 {
3479 if ((data[i]=='\\' || data[i]=='@') && // command
3480 data[i-1]!='\\' && data[i-1]!='@') // not escaped
3481 {
3482 if (qstrncmp(&data[i+1],endBlockName.data(),l)==0)
3483 {
3484 out+=data[i];
3485 out+=endBlockName;
3486 i+=l+1;
3487 break;
3488 }
3489 }
3490 out+=data[i];
3491 i++;
3492 }
3493 }
3494 else if ((level=isHeaderline(data.substr(i),TRUE))>0)
3495 {
3496 //printf("Found header at %d-%d\n",i,end);
3497 while (pi<data.size() && data[pi]==' ') pi++;
3498 QCString header = data.substr(pi,i-pi-1);
3499 id = extractTitleId(header, level);
3500 //printf("header='%s' is='%s'\n",qPrint(header),qPrint(id));
3501 if (!header.isEmpty())
3502 {
3503 if (!id.isEmpty())
3504 {
3505 out+=level==1?"@section ":"@subsection ";
3506 out+=id;
3507 out+=" ";
3508 out+=header;
3509 out+="\n\n";
3510 }
3511 else
3512 {
3513 out+=level==1?"<h1>":"<h2>";
3514 out+=header;
3515 out+=level==1?"\n</h1>\n":"\n</h2>\n";
3516 }
3517 }
3518 else
3519 {
3520 out+="\n<hr>\n";
3521 }
3522 pi=std::string::npos;
3523 i=end;
3524 end=i+1;
3525 continue;
3526 }
3527 else if ((ref=isLinkRef(data.substr(pi),id,link,title)))
3528 {
3529 //printf("found link ref: id='%s' link='%s' title='%s'\n",
3530 // qPrint(id),qPrint(link),qPrint(title));
3531 linkRefs.emplace(id.lower().str(),LinkRef(link,title));
3532 i=ref+pi;
3533 end=i+1;
3534 }
3535 else if (isFencedCodeBlock(data.substr(pi),currentIndent,lang,blockStart,blockEnd,blockOffset))
3536 {
3537 //printf("Found FencedCodeBlock lang='%s' start=%d end=%d code={%s}\n",
3538 // qPrint(lang),blockStart,blockEnd,QCString(data+pi+blockStart).left(blockEnd-blockStart).data());
3539 writeFencedCodeBlock(data.substr(pi),lang.view(),blockStart,blockEnd);
3540 i=pi+blockOffset;
3541 pi=std::string::npos;
3542 end=i+1;
3543 continue;
3544 }
3545 else if (isCodeBlock(data.substr(i,end-i),i,blockIndent))
3546 {
3547 // skip previous line (it is empty anyway)
3548 i+=writeCodeBlock(data.substr(i),blockIndent);
3549 pi=std::string::npos;
3550 end=i+1;
3551 continue;
3552 }
3553 else if (isTableBlock(data.substr(pi)))
3554 {
3555 i=pi+writeTableBlock(data.substr(pi));
3556 pi=std::string::npos;
3557 end=i+1;
3558 continue;
3559 }
3560 else
3561 {
3562 writeOneLineHeaderOrRuler(data.substr(pi,i-pi));
3563 }
3564 }
3565 pi=i;
3566 i=end;
3567 }
3568 //printf("last line %d size=%d\n",i,size);
3569 if (pi!=std::string::npos && pi<data.size()) // deal with the last line
3570 {
3571 if (isLinkRef(data.substr(pi),id,link,title))
3572 {
3573 //printf("found link ref: id='%s' link='%s' title='%s'\n",
3574 // qPrint(id),qPrint(link),qPrint(title));
3575 linkRefs.emplace(id.lower().str(),LinkRef(link,title));
3576 }
3577 else
3578 {
3579 writeOneLineHeaderOrRuler(data.substr(pi));
3580 }
3581 }
3582
3583 return out;
3584}
3585
3586static bool isOtherPage(std::string_view data)
3587{
3588#define OPC(x) if (literal_at(data,#x " ") || literal_at(data,#x "\n")) return true
3589 OPC(dir); OPC(defgroup); OPC(addtogroup); OPC(weakgroup); OPC(ingroup);
3590 OPC(fn); OPC(property); OPC(typedef); OPC(var); OPC(def);
3591 OPC(enum); OPC(namespace); OPC(class); OPC(concept); OPC(module);
3592 OPC(protocol); OPC(category); OPC(union); OPC(struct); OPC(interface);
3593 OPC(idlexcept); OPC(file);
3594#undef OPC
3595
3596 return false;
3597}
3598
3600{
3601 AUTO_TRACE("docs={}",Trace::trunc(docs));
3602 size_t i=0;
3603 std::string_view data(docs.str());
3604 const size_t size = data.size();
3605 if (!data.empty())
3606 {
3607 while (i<size && (data[i]==' ' || data[i]=='\n'))
3608 {
3609 i++;
3610 }
3611 if (literal_at(data.substr(i),"<!--!")) // skip over <!--! marker
3612 {
3613 i+=5;
3614 while (i<size && (data[i]==' ' || data[i]=='\n')) // skip over spaces after the <!--! marker
3615 {
3616 i++;
3617 }
3618 }
3619 if (i+1<size &&
3620 (data[i]=='\\' || data[i]=='@') &&
3621 (literal_at(data.substr(i+1),"page ") || literal_at(data.substr(i+1),"mainpage"))
3622 )
3623 {
3624 if (literal_at(data.substr(i+1),"page "))
3625 {
3626 AUTO_TRACE_EXIT("result=ExplicitPageResult::explicitPage");
3628 }
3629 else
3630 {
3631 AUTO_TRACE_EXIT("result=ExplicitPageResult::explicitMainPage");
3633 }
3634 }
3635 else if (i+1<size && (data[i]=='\\' || data[i]=='@') && isOtherPage(data.substr(i+1)))
3636 {
3637 AUTO_TRACE_EXIT("result=ExplicitPageResult::explicitOtherPage");
3639 }
3640 }
3641 AUTO_TRACE_EXIT("result=ExplicitPageResult::notExplicit");
3643}
3644
3645QCString Markdown::extractPageTitle(QCString &docs, QCString &id, int &prepend, bool &isIdGenerated)
3646{
3647 AUTO_TRACE("docs={} prepend={}",Trace::trunc(docs),id,prepend);
3648 // first first non-empty line
3649 prepend = 0;
3650 QCString title;
3651 size_t i=0;
3652 QCString docs_org(docs);
3653 std::string_view data(docs_org.str());
3654 const size_t size = data.size();
3655 docs.clear();
3656 while (i<size && (data[i]==' ' || data[i]=='\n'))
3657 {
3658 if (data[i]=='\n') prepend++;
3659 i++;
3660 }
3661 if (i>=size) { return QCString(); }
3662 size_t end1=i+1;
3663 while (end1<size && data[end1-1]!='\n') end1++;
3664 //printf("i=%d end1=%d size=%d line='%s'\n",i,end1,size,docs.mid(i,end1-i).data());
3665 // first line from i..end1
3666 if (end1<size)
3667 {
3668 // second line form end1..end2
3669 size_t end2=end1+1;
3670 while (end2<size && data[end2-1]!='\n') end2++;
3671 if (prv->isHeaderline(data.substr(end1),FALSE))
3672 {
3673 title = data.substr(i,end1-i-1);
3674 docs+="\n\n"+docs_org.mid(end2);
3675 id = prv->extractTitleId(title, 0, &isIdGenerated);
3676 //printf("extractPageTitle(title='%s' docs='%s' id='%s')\n",title.data(),docs.data(),id.data());
3677 AUTO_TRACE_EXIT("result={} id={} isIdGenerated={}",Trace::trunc(title),id,isIdGenerated);
3678 return title;
3679 }
3680 }
3681 if (i<end1 && prv->isAtxHeader(data.substr(i,end1-i),title,id,FALSE,&isIdGenerated)>0)
3682 {
3683 docs+="\n";
3684 docs+=docs_org.mid(end1);
3685 }
3686 else
3687 {
3688 docs=docs_org;
3689 id = prv->extractTitleId(title, 0, &isIdGenerated);
3690 }
3691 AUTO_TRACE_EXIT("result={} id={} isIdGenerated={}",Trace::trunc(title),id,isIdGenerated);
3692 return title;
3693}
3694
3695
3696//---------------------------------------------------------------------------
3697
3698QCString Markdown::process(const QCString &input, int &startNewlines, bool fromParseInput)
3699{
3700 if (input.isEmpty()) return input;
3701 size_t refIndent=0;
3702
3703 // for replace tabs by spaces
3704 QCString s = input;
3705 if (s.at(s.length()-1)!='\n') s += "\n"; // see PR #6766
3706 s = detab(s,refIndent);
3707 //printf("======== DeTab =========\n---- output -----\n%s\n---------\n",qPrint(s));
3708
3709 // then process quotation blocks (as these may contain other blocks)
3710 s = prv->processQuotations(s.view(),refIndent);
3711 //printf("======== Quotations =========\n---- output -----\n%s\n---------\n",qPrint(s));
3712
3713 // then process block items (headers, rules, and code blocks, references)
3714 s = prv->processBlocks(s.view(),refIndent);
3715 //printf("======== Blocks =========\n---- output -----\n%s\n---------\n",qPrint(s));
3716
3717 // finally process the inline markup (links, emphasis and code spans)
3718 prv->out.clear();
3719 prv->out.reserve(s.length());
3720 prv->processInline(s.view());
3721 if (fromParseInput)
3722 {
3723 Debug::print(Debug::Markdown,0,"---- output -----\n{}\n=========\n",qPrint(prv->out));
3724 }
3725 else
3726 {
3727 Debug::print(Debug::Markdown,0,"======== Markdown =========\n---- input ------- \n{}\n---- output -----\n{}\n=========\n",input,prv->out);
3728 }
3729
3730 // post processing
3731 QCString result = substitute(prv->out,g_doxy_nbsp,"&nbsp;");
3732 const char *p = result.data();
3733 if (p)
3734 {
3735 while (*p==' ') p++; // skip over spaces
3736 while (*p=='\n') {startNewlines++;p++;}; // skip over newlines
3737 if (literal_at(p,"<br>")) p+=4; // skip over <br>
3738 }
3739 if (p>result.data())
3740 {
3741 // strip part of the input
3742 result = result.mid(static_cast<int>(p-result.data()));
3743 }
3744 return result;
3745}
3746
3747//---------------------------------------------------------------------------
3748
3750{
3751 AUTO_TRACE("fileName={}",fileName);
3752 QCString absFileName = FileInfo(fileName.str()).absFilePath();
3753 QCString baseFn = stripFromPath(absFileName);
3754 int i = baseFn.findRev('.');
3755 if (i!=-1) baseFn = baseFn.left(i);
3756 QCString baseName = escapeCharsInString(baseFn,false,false);
3757 //printf("markdownFileNameToId(%s)=md_%s\n",qPrint(fileName),qPrint(baseName));
3758 QCString res = "md_"+baseName;
3759 AUTO_TRACE_EXIT("result={}",res);
3760 return res;
3761}
3762
3763//---------------------------------------------------------------------------
3764
3769
3771{
3772}
3773
3777
3779 const char *fileBuf,
3780 const std::shared_ptr<Entry> &root,
3781 ClangTUParser* /*clangParser*/)
3782{
3783 std::shared_ptr<Entry> current = std::make_shared<Entry>();
3784 int prepend = 0; // number of empty lines in front
3785 current->lang = SrcLangExt::Markdown;
3786 current->fileName = fileName;
3787 current->docFile = fileName;
3788 current->docLine = 1;
3789 QCString docs = stripIndentation(fileBuf);
3790 if (!docs.stripWhiteSpace().size()) return;
3791 Debug::print(Debug::Markdown,0,"======== Markdown =========\n---- input ------- \n{}\n",fileBuf);
3792 QCString id;
3793 Markdown markdown(fileName,1,0);
3794 bool isIdGenerated = false;
3795 QCString title = markdown.extractPageTitle(docs, id, prepend, isIdGenerated).stripWhiteSpace();
3796 QCString generatedId;
3797 if (isIdGenerated)
3798 {
3799 generatedId = id;
3800 id = "";
3801 }
3802 int indentLevel=title.isEmpty() ? 0 : -1;
3803 markdown.setIndentLevel(indentLevel);
3804 FileInfo fi(fileName.str());
3805 QCString fn = fi.fileName();
3807 QCString mdfileAsMainPage = Config_getString(USE_MDFILE_AS_MAINPAGE);
3808 QCString mdFileNameId = markdownFileNameToId(fileName);
3809 bool wasEmpty = id.isEmpty();
3810 if (wasEmpty) id = mdFileNameId;
3811 QCString relFileName = stripFromPath(fileName);
3812 bool isSubdirDocs = Config_getBool(IMPLICIT_DIR_DOCS) && relFileName.lower().endsWith("/readme.md");
3813 switch (isExplicitPage(docs))
3814 {
3816 if (!mdfileAsMainPage.isEmpty() &&
3817 (fi.absFilePath()==FileInfo(mdfileAsMainPage.str()).absFilePath()) // file reference with path
3818 )
3819 {
3820 docs.prepend("@ianchor{" + title + "} " + id + "\\ilinebr ");
3821 docs.prepend("@mainpage "+title+"\\ilinebr ");
3822 }
3823 else if (id=="mainpage" || id=="index")
3824 {
3825 if (title.isEmpty()) title = titleFn;
3826 docs.prepend("@ianchor{" + title + "} " + id + "\\ilinebr ");
3827 docs.prepend("@mainpage "+title+"\\ilinebr ");
3828 }
3829 else if (isSubdirDocs)
3830 {
3831 if (!generatedId.isEmpty() && !title.isEmpty())
3832 {
3833 docs.prepend("@section " + generatedId + " " + title + "\\ilinebr ");
3834 }
3835 docs.prepend("@dir\\ilinebr ");
3836 }
3837 else
3838 {
3839 if (title.isEmpty())
3840 {
3841 title = titleFn;
3842 prepend = 0;
3843 }
3844 if (!wasEmpty)
3845 {
3846 docs.prepend("@ianchor{" + title + "} " + id + "\\ilinebr @ianchor{" + relFileName + "} " + mdFileNameId + "\\ilinebr ");
3847 }
3848 else if (!generatedId.isEmpty())
3849 {
3850 docs.prepend("@ianchor " + generatedId + "\\ilinebr ");
3851 }
3852 else if (Config_getEnum(MARKDOWN_ID_STYLE)==MARKDOWN_ID_STYLE_t::GITHUB)
3853 {
3854 QCString autoId = AnchorGenerator::instance().generate(title.str());
3855 docs.prepend("@ianchor{" + title + "} " + autoId + "\\ilinebr ");
3856 }
3857 docs.prepend("@page "+id+" "+title+"\\ilinebr ");
3858 }
3859 for (int i = 0; i < prepend; i++) docs.prepend("\n");
3860 break;
3862 {
3863 // look for `@page label My Title\n` and capture `label` (match[1]) and ` My Title` (match[2])
3864 static const reg::Ex re(R"([ ]*[\\@]page\s+(\a[\w-]*)(\s*[^\n]*)\n)");
3865 reg::Match match;
3866 std::string s = docs.str();
3867 if (reg::search(s,match,re))
3868 {
3869 QCString orgLabel = match[1].str();
3870 QCString orgTitle = match[2].str();
3871 orgTitle = orgTitle.stripWhiteSpace();
3872 QCString newLabel = markdownFileNameToId(fileName);
3873 docs = docs.left(match[1].position())+ // part before label
3874 newLabel+ // new label
3875 match[2].str()+ // part between orgLabel and \n
3876 "\\ilinebr @ianchor{" + orgTitle + "} "+orgLabel+"\n"+ // add original anchor plus \n of above
3877 docs.right(docs.length()-match.length()); // add remainder of docs
3878 }
3879 }
3880 break;
3882 break;
3884 break;
3885 }
3886 int lineNr=1;
3887
3888 p->commentScanner.enterFile(fileName,lineNr);
3889 Protection prot = Protection::Public;
3890 bool needsEntry = false;
3891 int position=0;
3892 GuardedSectionStack guards;
3893 QCString processedDocs = markdown.process(docs,lineNr,true);
3894 while (p->commentScanner.parseCommentBlock(
3895 this,
3896 current.get(),
3897 processedDocs,
3898 fileName,
3899 lineNr,
3900 FALSE, // isBrief
3901 FALSE, // javadoc autobrief
3902 FALSE, // inBodyDocs
3903 prot, // protection
3904 position,
3905 needsEntry,
3906 true,
3907 &guards
3908 ))
3909 {
3910 if (needsEntry)
3911 {
3912 QCString docFile = current->docFile;
3913 root->moveToSubEntryAndRefresh(current);
3914 current->lang = SrcLangExt::Markdown;
3915 current->docFile = docFile;
3916 current->docLine = lineNr;
3917 }
3918 }
3919 if (needsEntry)
3920 {
3921 root->moveToSubEntryAndKeep(current);
3922 }
3923 p->commentScanner.leaveFile(fileName,lineNr);
3924}
3925
3927{
3928 Doxygen::parserManager->getOutlineParser("*.cpp")->parsePrototype(text);
3929}
3930
3931//------------------------------------------------------------------------
#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:131
static FileNameLinkedMap * imageNameLinkedMap
Definition doxygen.h:106
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
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 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 bool isFencedCodeBlock(std::string_view data, size_t refIndent, QCString &lang, size_t &start, size_t &end, size_t &offset)
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:2240
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:3272
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:300
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:2839
A bunch of utility functions.