Doxygen
Loading...
Searching...
No Matches
markdown.cpp File Reference
#include "markdown.h"
#include <memory>
#include <string>
#include <string_view>
#include <unordered_map>
#include <vector>
#include "anchor.h"
#include "cmdmapper.h"
#include "commentscan.h"
#include "config.h"
#include "debug.h"
#include "doxygen.h"
#include "entry.h"
#include "filedef.h"
#include "fileinfo.h"
#include "filename.h"
#include "message.h"
#include "plantuml.h"
#include "portable.h"
#include "regex.h"
#include "section.h"
#include "stringutil.h"
#include "trace.h"
#include "util.h"
Include dependency graph for markdown.cpp:

Go to the source code of this file.

Classes

struct  TableCell
struct  Markdown::Private
struct  Markdown::Private::LinkRef
struct  MarkdownOutlineParser::Private

Macros

#define AUTO_TRACE(...)
#define AUTO_TRACE_ADD(...)
#define AUTO_TRACE_EXIT(...)
#define isLiTag(i)
#define OPC(x)

Enumerations

enum class  ExplicitPageResult { explicitPage , explicitMainPage , explicitOtherPage , notExplicit }
enum class  Alignment { None , Left , Center , Right }

Functions

static constexpr bool isIdChar (char c)
static constexpr bool extraChar (char c)
static constexpr bool isOpenEmphChar (char c)
static constexpr bool isUtf8Nbsp (char c1, char c2)
static constexpr bool isAllowedEmphStr (const std::string_view &data, size_t offset)
static constexpr bool ignoreCloseEmphChar (char c, char cn)
size_t isNewline (std::string_view data)
static DString escapeDoubleQuotes (const DString &s)
static DString escapeSpecialChars (const DString &s)
static constexpr Alignment markersToAlignment (bool leftMarker, bool rightMarker)
 helper function to convert presence of left and/or right alignment markers to an alignment value
static DString getFilteredImageAttributes (std::string_view fmt, const DString &attrs)
 parse the image attributes and return attributes for given format
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, DString &refid, DString &link, DString &title)
 returns end of the link ref if this is indeed a link reference.
static bool isHRuler (std::string_view data)
static bool isEmptyLine (std::string_view data)
static size_t computeIndentExcludingListMarkers (std::string_view data)
static size_t isListMarker (std::string_view data)
static bool isEndOfList (std::string_view data)
static bool isFencedCodeBlock (std::string_view data, size_t refIndent, DString &lang, size_t &start, size_t &end, size_t &offset, DString &fileName, int lineNr)
static bool isCodeBlock (std::string_view data, size_t offset, size_t &indent)
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 bool isTableBlock (std::string_view data)
 Returns true iff data points to the start of a table block.
static bool hasLineBreak (std::string_view data)
bool skipOverFileAndLineCommands (std::string_view data, size_t indent, size_t &offset, std::string &location)
static bool isOtherPage (std::string_view data)
static ExplicitPageResult isExplicitPage (const DString &docs)
DString markdownFileNameToId (const DString &fileName)
 processes string s and converts markdown into doxygen/html commands.

Variables

static const char * g_utf8_nbsp = "\xc2\xa0"
static const char * g_doxy_nbsp = "&_doxy_nbsp;"
static const size_t codeBlockIndent = 4
static const std::unordered_map< std::string, std::string > g_quotationHeaderMap

Macro Definition Documentation

◆ AUTO_TRACE

#define AUTO_TRACE ( ...)
Value:
(void)0

Definition at line 68 of file markdown.cpp.

Referenced by escapeDoubleQuotes(), escapeSpecialChars(), and getFilteredImageAttributes().

◆ AUTO_TRACE_ADD

#define AUTO_TRACE_ADD ( ...)
Value:
(void)0

Definition at line 69 of file markdown.cpp.

◆ AUTO_TRACE_EXIT

#define AUTO_TRACE_EXIT ( ...)
Value:
(void)0

Definition at line 70 of file markdown.cpp.

Referenced by escapeDoubleQuotes(), escapeSpecialChars(), and getFilteredImageAttributes().

◆ isLiTag

#define isLiTag ( i)
Value:
(data[(i)]=='<' && \
(data[(i)+1]=='l' || data[(i)+1]=='L') && \
(data[(i)+2]=='i' || data[(i)+2]=='I') && \
(data[(i)+3]=='>'))

Definition at line 2252 of file markdown.cpp.

Referenced by computeIndentExcludingListMarkers().

◆ OPC

#define OPC ( x)
Value:
if (literal_at(data,#x " ") || literal_at(data,#x "\n")) return true
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:101

Referenced by isOtherPage().

Enumeration Type Documentation

◆ Alignment

enum class Alignment
strong
Enumerator
None 
Left 
Center 
Right 

Definition at line 219 of file markdown.cpp.

◆ ExplicitPageResult

enum class ExplicitPageResult
strong
Enumerator
explicitPage 

docs start with a page command

explicitMainPage 

docs start with a mainpage command

explicitOtherPage 

docs start with a dir / defgroup / addtogroup command

notExplicit 

docs doesn't start with either page or mainpage

Definition at line 73 of file markdown.cpp.

74{
75 explicitPage, /**< docs start with a page command */
76 explicitMainPage, /**< docs start with a mainpage command */
77 explicitOtherPage, /**< docs start with a dir / defgroup / addtogroup command */
78 notExplicit /**< docs doesn't start with either page or mainpage */
79};
@ explicitMainPage
docs start with a mainpage command
Definition markdown.cpp:76
@ explicitPage
docs start with a page command
Definition markdown.cpp:75
@ notExplicit
docs doesn't start with either page or mainpage
Definition markdown.cpp:78
@ explicitOtherPage
docs start with a dir / defgroup / addtogroup command
Definition markdown.cpp:77

Function Documentation

◆ computeIndentExcludingListMarkers()

size_t computeIndentExcludingListMarkers ( std::string_view data)
static

Definition at line 2260 of file markdown.cpp.

2261{
2262 AUTO_TRACE("data='{}'",Trace::trunc(data));
2263 size_t i=0;
2264 const size_t size=data.size();
2265 size_t indent=0;
2266 bool isDigit=false;
2267 bool isLi=false;
2268 bool listMarkerSkipped=false;
2269 while (i<size &&
2270 (data[i]==' ' || // space
2271 (!listMarkerSkipped && // first list marker
2272 (data[i]=='+' || data[i]=='-' || data[i]=='*' || // unordered list char
2273 (data[i]=='#' && i>0 && data[i-1]=='-') || // -# item
2274 (isDigit=(data[i]>='1' && data[i]<='9')) || // ordered list marker?
2275 (isLi=(size>=3 && i+3<size && isLiTag(i))) // <li> tag
2276 )
2277 )
2278 )
2279 )
2280 {
2281 if (isDigit) // skip over ordered list marker '10. '
2282 {
2283 size_t j=i+1;
2284 while (j<size && ((data[j]>='0' && data[j]<='9') || data[j]=='.'))
2285 {
2286 if (data[j]=='.') // should be end of the list marker
2287 {
2288 if (j+1<size && data[j+1]==' ') // valid list marker
2289 {
2290 listMarkerSkipped=true;
2291 indent+=j+1-i;
2292 i=j+1;
2293 break;
2294 }
2295 else // not a list marker
2296 {
2297 break;
2298 }
2299 }
2300 j++;
2301 }
2302 }
2303 else if (isLi)
2304 {
2305 i+=3; // skip over <li>
2306 indent+=3;
2307 listMarkerSkipped=true;
2308 }
2309 else if (data[i]=='-' && size>=2 && i+2<size && data[i+1]=='#' && data[i+2]==' ')
2310 { // case "-# "
2311 listMarkerSkipped=true; // only a single list marker is accepted
2312 i++; // skip over #
2313 indent++;
2314 }
2315 else if (data[i]!=' ' && i+1<size && data[i+1]==' ')
2316 { // case "- " or "+ " or "* "
2317 listMarkerSkipped=true; // only a single list marker is accepted
2318 }
2319 if (data[i]!=' ' && !listMarkerSkipped)
2320 { // end of indent
2321 break;
2322 }
2323 indent++;
2324 i++;
2325 }
2326 AUTO_TRACE_EXIT("result={}",indent);
2327 return indent;
2328}
#define AUTO_TRACE(...)
Definition markdown.cpp:68
#define AUTO_TRACE_EXIT(...)
Definition markdown.cpp:70
#define isLiTag(i)
DString trunc(const DString &s, size_t numChars=15)
Definition trace.h:56

References AUTO_TRACE, AUTO_TRACE_EXIT, isLiTag, and Trace::trunc().

Referenced by isCodeBlock(), and isListMarker().

◆ escapeDoubleQuotes()

DString escapeDoubleQuotes ( const DString & s)
static

Definition at line 245 of file markdown.cpp.

246{
247 AUTO_TRACE("s={}",Trace::trunc(s));
248 if (s.empty()) return s;
249 DString result;
250 const char *p=s.data();
251 char c=0, pc='\0';
252 while ((c=*p++))
253 {
254 if (c=='"' && pc!='\\') result+='\\';
255 result+=c;
256 pc=c;
257 }
258 AUTO_TRACE_EXIT("result={}",result);
259 return result;
260}
A String class for use with Doxygen wrapping std::string and adding some additional functionality off...
Definition dstring.h:84
bool empty() const
Returns true iff the string is empty (std::string compatible alias for isEmpty()).
Definition dstring.h:148
const char * data() const
Returns a pointer to the contents of the string in the form of a 0-terminated C string.
Definition dstring.h:157

References AUTO_TRACE, AUTO_TRACE_EXIT, DString::data(), DString::empty(), and Trace::trunc().

Referenced by Markdown::Private::writeMarkdownImage().

◆ escapeSpecialChars()

DString escapeSpecialChars ( const DString & s)
static

Definition at line 263 of file markdown.cpp.

264{
265 AUTO_TRACE("s={}",Trace::trunc(s));
266 if (s.empty()) return s;
267 bool insideQuote=false;
268 DString result;
269 const char *p=s.data();
270 char c=0, pc='\0';
271 while ((c=*p++))
272 {
273 switch (c)
274 {
275 case '"':
276 if (pc!='\\')
277 {
278 if (Config_getBool(MARKDOWN_STRICT))
279 {
280 result+='\\';
281 }
282 else // For Doxygen's markup style a quoted text is left untouched
283 {
284 insideQuote=!insideQuote;
285 }
286 }
287 result+=c;
288 break;
289 case '<':
290 // fall through
291 case '>':
292 if (!insideQuote)
293 {
294 result+='\\';
295 result+=c;
296 if ((p[0]==':') && (p[1]==':'))
297 {
298 result+='\\';
299 result+=':';
300 p++;
301 }
302 }
303 else
304 {
305 result+=c;
306 }
307 break;
308 case '\\': if (!insideQuote) { result+='\\'; } result+='\\'; break;
309 case '@': if (!insideQuote) { result+='\\'; } result+='@'; break;
310 // commented out next line due to regression when using % to suppress a link
311 //case '%': if (!insideQuote) { result+='\\'; } result+='%'; break;
312 case '#': if (!insideQuote) { result+='\\'; } result+='#'; break;
313 case '$': if (!insideQuote) { result+='\\'; } result+='$'; break;
314 case '&': if (!insideQuote) { result+='\\'; } result+='&'; break;
315 default:
316 result+=c; break;
317 }
318 pc=c;
319 }
320 AUTO_TRACE_EXIT("result={}",result);
321 return result;
322}
#define Config_getBool(name)
Definition config.h:33

References AUTO_TRACE, AUTO_TRACE_EXIT, Config_getBool, DString::data(), DString::empty(), and Trace::trunc().

Referenced by Markdown::Private::processCodeSpan().

◆ extraChar()

constexpr bool extraChar ( char c)
staticconstexpr

Definition at line 93 of file markdown.cpp.

94{
95 return c=='-' || c=='+' || c=='!' || c=='?' || c=='$' || c=='@' ||
96 c=='&' || c=='*' || c=='_' || c=='%' || c=='[' || c=='(' ||
97 c=='.' || c=='>' || c==':' || c==',' || c==';' || c=='\'' ||
98 c=='"' || c=='`' || c=='\\';
99}

Referenced by Markdown::Private::processEmphasis().

◆ findTableColumns()

size_t findTableColumns ( std::string_view data,
size_t & start,
size_t & end,
size_t & columns )
static

Finds the location of the table's contains in the string data.

Only one line will be inspected.

Parameters
[in]datapointer to the string buffer.
[out]startoffset of the first character of the table content
[out]endoffset of the last character of the table content
[out]columnsnumber of table columns found
Returns
The offset until the next line in the buffer.

Definition at line 2566 of file markdown.cpp.

2567{
2568 AUTO_TRACE("data='{}'",Trace::trunc(data));
2569 const size_t size = data.size();
2570 size_t i=0,n=0;
2571 // find start character of the table line
2572 while (i<size && data[i]==' ') i++;
2573 if (i < size && data[i] == '|' && data[i] != '\n')
2574 {
2575 i++;
2576 n++; // leading | does not count
2577 }
2578 start = i;
2579
2580 // find end character of the table line
2581 size_t j = 0;
2582 while (i<size && (j = isNewline(data.substr(i)))==0) i++;
2583 size_t eol=i+j;
2584
2585 if (j>0 && i>0) i--; // move i to point before newline
2586 while (i>0 && data[i]==' ') i--;
2587 if (i > 0 && data[i - 1] != '\\' && data[i] == '|')
2588 {
2589 i--;
2590 n++; // trailing or escaped | does not count
2591 }
2592 end = i;
2593
2594 // count columns between start and end
2595 columns=0;
2596 if (end>start)
2597 {
2598 i=start;
2599 while (i<=end) // look for more column markers
2600 {
2601 if (data[i]=='|' && (i==0 || data[i-1]!='\\')) columns++;
2602 if (columns==1) columns++; // first | make a non-table into a two column table
2603 i++;
2604 }
2605 }
2606 if (n==2 && columns==0) // table row has | ... |
2607 {
2608 columns++;
2609 }
2610 AUTO_TRACE_EXIT("eol={} start={} end={} columns={}",eol,start,end,columns);
2611 return eol;
2612}
#define eol
The end of line string for this machine.
DirIterator end(const DirIterator &) noexcept
Definition dir.cpp:180
size_t isNewline(std::string_view data)
Definition markdown.cpp:232

References AUTO_TRACE, AUTO_TRACE_EXIT, end(), eol, isNewline(), and Trace::trunc().

Referenced by isTableBlock(), and Markdown::Private::writeTableBlock().

◆ getFilteredImageAttributes()

DString getFilteredImageAttributes ( std::string_view fmt,
const DString & attrs )
static

parse the image attributes and return attributes for given format

Definition at line 348 of file markdown.cpp.

349{
350 AUTO_TRACE("fmt={} attrs={}",fmt,attrs);
351 StringVector attrList = split(attrs.str(),",");
352 for (const auto &attr_ : attrList)
353 {
354 DString attr = DString(attr_).stripWhiteSpace();
355 if (size_t i = attr.find(':'); i!=DString::npos && i>0) // has format
356 {
357 DString format = attr.left(i).stripWhiteSpace().lower();
358 if (format == fmt) // matching format
359 {
360 AUTO_TRACE_EXIT("result={}",attr.mid(i+1));
361 return attr.mid(i+1); // keep part after :
362 }
363 }
364 else // option that applies to all formats
365 {
366 AUTO_TRACE_EXIT("result={}",attr);
367 return attr;
368 }
369 }
370 return DString();
371}
DString mid(size_t index, size_t len=npos) const
Definition dstring.h:318
DString lower() const
Definition dstring.h:326
static constexpr size_t npos
value used to indicate 'not found' or 'to the end of the string', matching std::string::npos
Definition dstring.h:178
size_t find(char c, size_t pos=0) const
Definition dstring.h:239
DString stripWhiteSpace() const
returns a copy of this string with leading and trailing whitespace removed
Definition dstring.h:337
DString left(size_t len) const
Definition dstring.h:306
const std::string & str() const
Definition dstring.h:645
std::vector< std::string > StringVector
Definition containers.h:33
Definition message.h:146
StringVector split(const std::string &s, const std::string &delimiter, bool retainEmpty=false)
split input string s by string delimiter delimiter.
Definition stringutil.h:119

References AUTO_TRACE, AUTO_TRACE_EXIT, DString::find(), DString::left(), DString::lower(), DString::mid(), DString::npos, split(), DString::str(), and DString::stripWhiteSpace().

Referenced by Markdown::Private::writeMarkdownImage().

◆ hasLineBreak()

bool hasLineBreak ( std::string_view data)
static

Definition at line 2855 of file markdown.cpp.

2856{
2857 AUTO_TRACE("data='{}'",Trace::trunc(data));
2858 size_t i=0;
2859 size_t j=0;
2860 // search for end of line and also check if it is not a completely blank
2861 while (i<data.size() && data[i]!='\n')
2862 {
2863 if (data[i]!=' ' && data[i]!='\t') j++; // some non whitespace
2864 i++;
2865 }
2866 if (i>=data.size()) { return 0; } // empty line
2867 if (i<2) { return 0; } // not long enough
2868 bool res = (j>0 && data[i-1]==' ' && data[i-2]==' '); // non blank line with at two spaces at the end
2869 AUTO_TRACE_EXIT("result={}",res);
2870 return res;
2871}

References AUTO_TRACE, AUTO_TRACE_EXIT, and Trace::trunc().

Referenced by Markdown::Private::writeOneLineHeaderOrRuler().

◆ ignoreCloseEmphChar()

constexpr bool ignoreCloseEmphChar ( char c,
char cn )
staticconstexpr

Definition at line 123 of file markdown.cpp.

124{
125 return c=='(' || c=='{' || c=='[' || (c=='<' && cn!='/') || c=='\\' || c=='@';
126}

Referenced by Markdown::Private::findEmphasisChar().

◆ isAllowedEmphStr()

constexpr bool isAllowedEmphStr ( const std::string_view & data,
size_t offset )
staticconstexpr

Definition at line 115 of file markdown.cpp.

116{
117 return ((offset>0 && !isOpenEmphChar(data.data()[-1])) &&
118 (offset>1 && !isUtf8Nbsp(data.data()[-2],data.data()[-1])));
119}
static constexpr bool isOpenEmphChar(char c)
Definition markdown.cpp:102
static constexpr bool isUtf8Nbsp(char c1, char c2)
Definition markdown.cpp:110

References isOpenEmphChar(), and isUtf8Nbsp().

Referenced by Markdown::Private::processEmphasis().

◆ isBlockQuote()

bool isBlockQuote ( std::string_view data,
size_t indent )
static

returns true if this line starts a block quote

Definition at line 1981 of file markdown.cpp.

1982{
1983 AUTO_TRACE("data='{}' indent={}",Trace::trunc(data),indent);
1984 size_t i = 0;
1985 const size_t size = data.size();
1986 while (i<size && data[i]==' ') i++;
1987 if (i<indent+codeBlockIndent) // could be a quotation
1988 {
1989 // count >'s and skip spaces
1990 int level=0;
1991 while (i<size && (data[i]=='>' || data[i]==' '))
1992 {
1993 if (data[i]=='>') level++;
1994 i++;
1995 }
1996 // last characters should be a space or newline,
1997 // so a line starting with >= does not match, but only when level equals 1
1998 bool res = (level>0 && i<size && ((data[i-1]==' ') || data[i]=='\n')) || (level > 1);
1999 AUTO_TRACE_EXIT("result={}",res);
2000 return res;
2001 }
2002 else // too much indentation -> code block
2003 {
2004 AUTO_TRACE_EXIT("result=false: too much indentation");
2005 return false;
2006 }
2007}
static const size_t codeBlockIndent
Definition markdown.cpp:226

References AUTO_TRACE, AUTO_TRACE_EXIT, codeBlockIndent, and Trace::trunc().

Referenced by Markdown::Private::processQuotations().

◆ isCodeBlock()

bool isCodeBlock ( std::string_view data,
size_t offset,
size_t & indent )
static

Definition at line 2469 of file markdown.cpp.

2470{
2471 AUTO_TRACE("data='{}' offset={}",Trace::trunc(data),offset);
2472 //printf("<isCodeBlock(offset=%d,size=%d,indent=%d)\n",offset,size,indent);
2473 // determine the indent of this line
2474 size_t i=0;
2475 size_t indent0=0;
2476 const size_t size = data.size();
2477 while (i < size && data[i] == ' ')
2478 {
2479 indent0++;
2480 i++;
2481 }
2482
2483 if (indent0<codeBlockIndent)
2484 {
2485 AUTO_TRACE_EXIT("result={}: line is not indented enough {}<4",false,indent0);
2486 return false;
2487 }
2488 if (indent0>=size || data[indent0]=='\n') // empty line does not start a code block
2489 {
2490 AUTO_TRACE_EXIT("result={}: only spaces at the end of a comment block",false);
2491 return false;
2492 }
2493
2494 i=offset;
2495 int nl=0;
2496 int nl_pos[3];
2497 int offset_i = static_cast<int>(offset);
2498 // search back 3 lines and remember the start of lines -1 and -2
2499 while (i>0 && nl<3) // i counts down from offset to 1
2500 {
2501 int j = static_cast<int>(i)-offset_i-1; // j counts from -1 to -offset
2502 // since j can be negative we need to rewrap data in a std::string_view
2503 size_t nl_size = isNewline(std::string_view(data.data()+j,data.size()-j));
2504 if (nl_size>0)
2505 {
2506 nl_pos[nl++]=j+static_cast<int>(nl_size);
2507 }
2508 i--;
2509 }
2510
2511 // if there are only 2 preceding lines, then line -2 starts at -offset
2512 if (i==0 && nl==2) nl_pos[nl++]=-offset_i;
2513
2514 if (nl==3) // we have at least 2 preceding lines
2515 {
2516 //printf(" positions: nl_pos=[%d,%d,%d] line[-2]='%s' line[-1]='%s'\n",
2517 // nl_pos[0],nl_pos[1],nl_pos[2],
2518 // qPrint(DString(data+nl_pos[1]).left(nl_pos[0]-nl_pos[1]-1)),
2519 // qPrint(DString(data+nl_pos[2]).left(nl_pos[1]-nl_pos[2]-1)));
2520
2521 // check that line -1 is empty
2522 // Note that the offset is negative so we need to rewrap the string view
2523 if (!isEmptyLine(std::string_view(data.data()+nl_pos[1],nl_pos[0]-nl_pos[1]-1)))
2524 {
2525 AUTO_TRACE_EXIT("result={}",false);
2526 return false;
2527 }
2528
2529 // determine the indent of line -2
2530 // Note that the offset is negative so we need to rewrap the string view
2531 indent=std::max(indent,computeIndentExcludingListMarkers(
2532 std::string_view(data.data()+nl_pos[2],nl_pos[1]-nl_pos[2])));
2533
2534 //printf(">isCodeBlock local_indent %d>=%d+%d=%d\n",
2535 // indent0,indent,codeBlockIndent,indent0>=indent+codeBlockIndent);
2536 // if the difference is >4 spaces -> code block
2537 bool res = indent0>=indent+codeBlockIndent;
2538 AUTO_TRACE_EXIT("result={}: code block if indent difference >4 spaces",res);
2539 return res;
2540 }
2541 else // not enough lines to determine the relative indent, use global indent
2542 {
2543 // check that line -1 is empty
2544 // Note that the offset is negative so we need to rewrap the string view
2545 if (nl==1 && !isEmptyLine(std::string_view(data.data()-offset,offset-1)))
2546 {
2547 AUTO_TRACE_EXIT("result=false");
2548 return false;
2549 }
2550 //printf(">isCodeBlock global indent %d>=%d+4=%d nl=%d\n",
2551 // indent0,indent,indent0>=indent+4,nl);
2552 bool res = indent0>=indent+codeBlockIndent;
2553 AUTO_TRACE_EXIT("result={}: code block if indent difference >4 spaces",res);
2554 return res;
2555 }
2556}
static size_t computeIndentExcludingListMarkers(std::string_view data)
static bool isEmptyLine(std::string_view data)

References AUTO_TRACE, AUTO_TRACE_EXIT, codeBlockIndent, computeIndentExcludingListMarkers(), isEmptyLine(), isNewline(), and Trace::trunc().

Referenced by Markdown::Private::processBlocks().

◆ isEmptyLine()

bool isEmptyLine ( std::string_view data)
static

Definition at line 2238 of file markdown.cpp.

2239{
2240 AUTO_TRACE("data='{}'",Trace::trunc(data));
2241 size_t i=0;
2242 while (i<data.size())
2243 {
2244 if (data[i]=='\n') { AUTO_TRACE_EXIT("true"); return true; }
2245 if (data[i]!=' ') { AUTO_TRACE_EXIT("false"); return false; }
2246 i++;
2247 }
2248 AUTO_TRACE_EXIT("true");
2249 return true;
2250}

References AUTO_TRACE, AUTO_TRACE_EXIT, and Trace::trunc().

Referenced by isCodeBlock(), Markdown::Private::processBlocks(), and Markdown::Private::processQuotations().

◆ isEndOfList()

bool isEndOfList ( std::string_view data)
static

Definition at line 2341 of file markdown.cpp.

2342{
2343 AUTO_TRACE("data='{}'",Trace::trunc(data));
2344 int dots=0;
2345 size_t i=0;
2346 // end of list marker is an otherwise empty line with a dot.
2347 while (i<data.size())
2348 {
2349 if (data[i]=='.')
2350 {
2351 dots++;
2352 }
2353 else if (data[i]=='\n')
2354 {
2355 break;
2356 }
2357 else if (data[i]!=' ' && data[i]!='\t') // bail out if the line is not empty
2358 {
2359 AUTO_TRACE_EXIT("result=false");
2360 return false;
2361 }
2362 i++;
2363 }
2364 AUTO_TRACE_EXIT("result={}",dots==1);
2365 return dots==1;
2366}

References AUTO_TRACE, AUTO_TRACE_EXIT, and Trace::trunc().

Referenced by Markdown::Private::processBlocks(), and Markdown::Private::processQuotations().

◆ isExplicitPage()

ExplicitPageResult isExplicitPage ( const DString & docs)
static

Definition at line 3651 of file markdown.cpp.

3652{
3653 AUTO_TRACE("docs={}",Trace::trunc(docs));
3654 size_t i=0;
3655 std::string_view data(docs.str());
3656 const size_t size = data.size();
3657 if (!data.empty())
3658 {
3659 while (i<size && (data[i]==' ' || data[i]=='\n'))
3660 {
3661 i++;
3662 }
3663 if (literal_at(data.substr(i),"<!--!")) // skip over <!--! marker
3664 {
3665 i+=5;
3666 while (i<size && (data[i]==' ' || data[i]=='\n')) // skip over spaces after the <!--! marker
3667 {
3668 i++;
3669 }
3670 }
3671 if (i+1<size &&
3672 (data[i]=='\\' || data[i]=='@') &&
3673 (literal_at(data.substr(i+1),"page ") || literal_at(data.substr(i+1),"mainpage"))
3674 )
3675 {
3676 if (literal_at(data.substr(i+1),"page "))
3677 {
3678 AUTO_TRACE_EXIT("result=ExplicitPageResult::explicitPage");
3680 }
3681 else
3682 {
3683 AUTO_TRACE_EXIT("result=ExplicitPageResult::explicitMainPage");
3685 }
3686 }
3687 else if (i+1<size && (data[i]=='\\' || data[i]=='@') && isOtherPage(data.substr(i+1)))
3688 {
3689 AUTO_TRACE_EXIT("result=ExplicitPageResult::explicitOtherPage");
3691 }
3692 }
3693 AUTO_TRACE_EXIT("result=ExplicitPageResult::notExplicit");
3695}
static bool isOtherPage(std::string_view data)

References AUTO_TRACE, AUTO_TRACE_EXIT, explicitMainPage, explicitOtherPage, explicitPage, isOtherPage(), literal_at(), notExplicit, DString::str(), and Trace::trunc().

Referenced by MarkdownOutlineParser::parseInput().

◆ isFencedCodeBlock()

bool isFencedCodeBlock ( std::string_view data,
size_t refIndent,
DString & lang,
size_t & start,
size_t & end,
size_t & offset,
DString & fileName,
int lineNr )
static

extract python or .py from python... or .py...

Definition at line 2368 of file markdown.cpp.

2371{
2372 AUTO_TRACE("data='{}' refIndent={}",Trace::trunc(data),refIndent);
2373 const char dot = '.';
2374 auto isAlphaChar = [ ](char c) { return (c>='A' && c<='Z') || (c>='a' && c<='z'); };
2375 auto isAlphaNChar = [ ](char c) { return (c>='A' && c<='Z') || (c>='a' && c<='z') || (c>='0' && c<='9') || (c=='+'); };
2376 auto isLangChar = [&](char c) { return c==dot || isAlphaChar(c); };
2377 // rules: at least 3 ~~~, end of the block same amount of ~~~'s, otherwise
2378 // return false
2379 size_t i=0;
2380 size_t indent=0;
2381 int startTildes=0;
2382 const size_t size = data.size();
2383 while (i < size && data[i] == ' ')
2384 {
2385 indent++;
2386 i++;
2387 }
2388 if (indent>=refIndent+4)
2389 {
2390 AUTO_TRACE_EXIT("result=false: content is part of code block indent={} refIndent={}",indent,refIndent);
2391 return false;
2392 } // part of code block
2393 char tildaChar='~';
2394 if (i<size && data[i]=='`') tildaChar='`';
2395 while (i < size && data[i] == tildaChar)
2396 {
2397 startTildes++;
2398 i++;
2399 }
2400 if (startTildes<3)
2401 {
2402 AUTO_TRACE_EXIT("result=false: no fence marker found #tildes={}",startTildes);
2403 return false;
2404 } // not enough tildes
2405 // skip whitespace
2406 while (i<size && data[i]==' ') { i++; }
2407 if (i<size && data[i]=='{') // extract .py from ```{.py} ... ```
2408 {
2409 i++; // skip over {
2410 if (data[i] == dot) i++; // skip over initial dot
2411 size_t startLang=i;
2412 while (i<size && (data[i]!='\n' && data[i]!='}')) i++; // find matching }
2413 if (i<size && data[i]=='}')
2414 {
2415 lang = data.substr(startLang,i-startLang);
2416 i++;
2417 }
2418 else // missing closing bracket, treat `{` as part of the content
2419 {
2420 i=startLang-1;
2421 lang="";
2422 }
2423 }
2424 else if (i<size && isLangChar(data[i])) /// extract python or .py from ```python...``` or ```.py...```
2425 {
2426 if (data[i] == dot) i++; // skip over initial dot
2427 size_t startLang=i;
2428 if (i<size && isAlphaChar(data[i])) //check first character of language specifier
2429 {
2430 i++;
2431 while (i<size && isAlphaNChar(data[i])) i++; // find end of language specifier
2432 }
2433 lang = data.substr(startLang,i-startLang);
2434 }
2435 else // no language specified
2436 {
2437 lang="";
2438 }
2439
2440 start=i;
2441 while (i<size)
2442 {
2443 if (data[i]==tildaChar)
2444 {
2445 end=i;
2446 int endTildes=0;
2447 while (i < size && data[i] == tildaChar)
2448 {
2449 endTildes++;
2450 i++;
2451 }
2452 while (i<size && data[i]==' ') i++;
2453 {
2454 if (endTildes==startTildes)
2455 {
2456 offset=i;
2457 AUTO_TRACE_EXIT("result=true: found end marker at offset {} lang='{}'",offset,lang);
2458 return true;
2459 }
2460 }
2461 }
2462 i++;
2463 }
2464 warn(fileName, lineNr, "Ending Inside a fenced code block. Maybe the end marker for the block is missing?");
2465 AUTO_TRACE_EXIT("result=false: no end marker found lang={}'",lang);
2466 return false;
2467}
DString substr(size_t pos=0, size_t count=npos) const
Returns a substring of length count starting at pos.
Definition dstring.h:223
#define warn(file, line, fmt,...)
Definition message.h:97

References AUTO_TRACE, AUTO_TRACE_EXIT, end(), DString::substr(), Trace::trunc(), and warn.

Referenced by Markdown::Private::processBlocks(), and Markdown::Private::processQuotations().

◆ isHRuler()

bool isHRuler ( std::string_view data)
static

Definition at line 2099 of file markdown.cpp.

2100{
2101 AUTO_TRACE("data='{}'",Trace::trunc(data));
2102 size_t i=0;
2103 size_t size = data.size();
2104 if (size>0 && data[size-1]=='\n') size--; // ignore newline character
2105 while (i<size && data[i]==' ') i++;
2106 if (i>=size) { AUTO_TRACE_EXIT("result=false: empty line"); return false; } // empty line
2107 char c=data[i];
2108 if (c!='*' && c!='-' && c!='_')
2109 {
2110 AUTO_TRACE_EXIT("result=false: {} is not a hrule character",c);
2111 return false; // not a hrule character
2112 }
2113 int n=0;
2114 while (i<size)
2115 {
2116 if (data[i]==c)
2117 {
2118 n++; // count rule character
2119 }
2120 else if (data[i]!=' ')
2121 {
2122 AUTO_TRACE_EXIT("result=false: line contains non hruler characters");
2123 return false; // line contains non hruler characters
2124 }
2125 i++;
2126 }
2127 AUTO_TRACE_EXIT("result={}",n>=3);
2128 return n>=3; // at least 3 characters needed for a hruler
2129}

References AUTO_TRACE, AUTO_TRACE_EXIT, and Trace::trunc().

Referenced by Markdown::Private::writeBlockQuote(), and Markdown::Private::writeOneLineHeaderOrRuler().

◆ isIdChar()

constexpr bool isIdChar ( char c)
staticconstexpr

Definition at line 84 of file markdown.cpp.

85{
86 return (c>='a' && c<='z') ||
87 (c>='A' && c<='Z') ||
88 (c>='0' && c<='9') ||
89 (static_cast<unsigned char>(c)>=0x80); // unicode characters
90}

Referenced by Markdown::Private::findEmphasisChar(), Markdown::Private::processCodeSpan(), Markdown::Private::processEmphasis(), and Markdown::Private::processHtmlTagWrite().

◆ isLinkRef()

size_t isLinkRef ( std::string_view data,
DString & refid,
DString & link,
DString & title )
static

returns end of the link ref if this is indeed a link reference.

Definition at line 2010 of file markdown.cpp.

2011{
2012 AUTO_TRACE("data='{}'",Trace::trunc(data));
2013 const size_t size = data.size();
2014 // format: start with [some text]:
2015 size_t i = 0;
2016 while (i<size && data[i]==' ') i++;
2017 if (i>=size || data[i]!='[') { return 0; }
2018 i++;
2019 size_t refIdStart=i;
2020 while (i<size && data[i]!='\n' && data[i]!=']') i++;
2021 if (i>=size || data[i]!=']') { return 0; }
2022 refid = data.substr(refIdStart,i-refIdStart);
2023 if (refid.empty()) { return 0; }
2024 AUTO_TRACE_ADD("refid found {}",refid);
2025 //printf(" isLinkRef: found refid='%s'\n",qPrint(refid));
2026 i++;
2027 if (i>=size || data[i]!=':') { return 0; }
2028 i++;
2029
2030 // format: whitespace* \n? whitespace* (<url> | url)
2031 while (i<size && data[i]==' ') i++;
2032 if (i<size && data[i]=='\n')
2033 {
2034 i++;
2035 while (i<size && data[i]==' ') i++;
2036 }
2037 if (i>=size) { return 0; }
2038
2039 if (i<size && data[i]=='<') i++;
2040 size_t linkStart=i;
2041 while (i<size && data[i]!=' ' && data[i]!='\n') i++;
2042 size_t linkEnd=i;
2043 if (i<size && data[i]=='>') i++;
2044 if (linkStart==linkEnd) { return 0; } // empty link
2045 link = data.substr(linkStart,linkEnd-linkStart);
2046 AUTO_TRACE_ADD("link found {}",Trace::trunc(link));
2047 if (link=="@ref" || link=="\\ref")
2048 {
2049 size_t argStart=i;
2050 while (i<size && data[i]!='\n' && data[i]!='"') i++;
2051 link+=data.substr(argStart,i-argStart);
2052 }
2053
2054 title.clear();
2055
2056 // format: (whitespace* \n? whitespace* ( 'title' | "title" | (title) ))?
2057 size_t eol=0;
2058 while (i<size && data[i]==' ') i++;
2059 if (i<size && data[i]=='\n')
2060 {
2061 eol=i;
2062 i++;
2063 while (i<size && data[i]==' ') i++;
2064 }
2065 if (i>=size)
2066 {
2067 AUTO_TRACE_EXIT("result={}: end of isLinkRef while looking for title",i);
2068 return i; // end of buffer while looking for the optional title
2069 }
2070
2071 char c = data[i];
2072 if (c=='\'' || c=='"' || c=='(') // optional title present?
2073 {
2074 //printf(" start of title found! char='%c'\n",c);
2075 i++;
2076 if (c=='(') c=')'; // replace c by end character
2077 size_t titleStart=i;
2078 // search for end of the line
2079 while (i<size && data[i]!='\n') i++;
2080 eol = i;
2081
2082 // search back to matching character
2083 size_t end=i-1;
2084 while (end>titleStart && data[end]!=c) end--;
2085 if (end>titleStart)
2086 {
2087 title = data.substr(titleStart,end-titleStart);
2088 }
2089 AUTO_TRACE_ADD("title found {}",Trace::trunc(title));
2090 }
2091 while (i<size && data[i]==' ') i++;
2092 //printf("end of isLinkRef: i=%d size=%d data[i]='%c' eol=%d\n",
2093 // i,size,data[i],eol);
2094 if (i>=size) { AUTO_TRACE_EXIT("result={}",i); return i; } // end of buffer while ref id was found
2095 else if (eol>0) { AUTO_TRACE_EXIT("result={}",eol); return eol; } // end of line while ref id was found
2096 return 0; // invalid link ref
2097}
void clear()
Definition dstring.h:214
#define AUTO_TRACE_ADD(...)
Definition markdown.cpp:69

References AUTO_TRACE, AUTO_TRACE_ADD, AUTO_TRACE_EXIT, DString::clear(), DString::empty(), end(), eol, DString::substr(), and Trace::trunc().

Referenced by Markdown::Private::processBlocks().

◆ isListMarker()

size_t isListMarker ( std::string_view data)
static

Definition at line 2330 of file markdown.cpp.

2331{
2332 AUTO_TRACE("data='{}'",Trace::trunc(data));
2333 size_t normalIndent = 0;
2334 while (normalIndent<data.size() && data[normalIndent]==' ') normalIndent++;
2335 size_t listIndent = computeIndentExcludingListMarkers(data);
2336 size_t result = listIndent>normalIndent ? listIndent : 0;
2337 AUTO_TRACE_EXIT("result={}",result);
2338 return result;
2339}

References AUTO_TRACE, AUTO_TRACE_EXIT, computeIndentExcludingListMarkers(), and Trace::trunc().

Referenced by Markdown::Private::processBlocks(), Markdown::Private::processQuotations(), and Markdown::Private::writeBlockQuote().

◆ isNewline()

size_t isNewline ( std::string_view data)
inline

Definition at line 232 of file markdown.cpp.

233{
234 // normal newline
235 if (data[0] == '\n') return 1;
236 // artificial new line from ^^ in ALIASES
237 if (literal_at(data,"\\ilinebr"))
238 {
239 return (data.size()>8 && data[8]==' ') ? 9 : 8; // also count space after \ilinebr if present
240 }
241 return 0;
242}

References literal_at().

Referenced by Markdown::Private::findEndOfLine(), findTableColumns(), and isCodeBlock().

◆ isOpenEmphChar()

constexpr bool isOpenEmphChar ( char c)
staticconstexpr

Definition at line 102 of file markdown.cpp.

103{
104 return c=='\n' || c==' ' || c=='\'' || c=='<' ||
105 c=='>' || c=='{' || c=='(' || c=='[' ||
106 c==',' || c==':' || c==';';
107}

Referenced by isAllowedEmphStr().

◆ isOtherPage()

bool isOtherPage ( std::string_view data)
static

Definition at line 3638 of file markdown.cpp.

3639{
3640#define OPC(x) if (literal_at(data,#x " ") || literal_at(data,#x "\n")) return true
3641 OPC(dir); OPC(defgroup); OPC(addtogroup); OPC(weakgroup); OPC(ingroup);
3642 OPC(fn); OPC(property); OPC(typedef); OPC(var); OPC(def);
3643 OPC(enum); OPC(namespace); OPC(class); OPC(concept); OPC(module);
3644 OPC(protocol); OPC(category); OPC(union); OPC(struct); OPC(interface);
3645 OPC(idlexcept); OPC(file); OPC(record);
3646#undef OPC
3647
3648 return false;
3649}
#define OPC(x)

References OPC.

Referenced by isExplicitPage().

◆ isTableBlock()

bool isTableBlock ( std::string_view data)
static

Returns true iff data points to the start of a table block.

Definition at line 2615 of file markdown.cpp.

2616{
2617 AUTO_TRACE("data='{}'",Trace::trunc(data));
2618 size_t cc0=0, start=0, end=0;
2619
2620 // the first line should have at least two columns separated by '|'
2621 size_t i = findTableColumns(data,start,end,cc0);
2622 if (i>=data.size() || cc0<1)
2623 {
2624 AUTO_TRACE_EXIT("result=false: no |'s in the header");
2625 return false;
2626 }
2627
2628 size_t cc1 = 0;
2629 size_t ret = findTableColumns(data.substr(i),start,end,cc1);
2630 size_t j=i+start;
2631 // separator line should consist of |, - and : and spaces only
2632 while (j<=end+i)
2633 {
2634 if (data[j]!=':' && data[j]!='-' && data[j]!='|' && data[j]!=' ')
2635 {
2636 AUTO_TRACE_EXIT("result=false: invalid character '{}'",data[j]);
2637 return false; // invalid characters in table separator
2638 }
2639 j++;
2640 }
2641 if (cc1!=cc0) // number of columns should be same as previous line
2642 {
2643 AUTO_TRACE_EXIT("result=false: different number of columns as previous line {}!={}",cc1,cc0);
2644 return false;
2645 }
2646
2647 i+=ret; // goto next line
2648 size_t cc2 = 0;
2649 findTableColumns(data.substr(i),start,end,cc2);
2650
2651 AUTO_TRACE_EXIT("result={}",cc1==cc2);
2652 return cc1==cc2;
2653}
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.

References AUTO_TRACE, AUTO_TRACE_EXIT, end(), findTableColumns(), and Trace::trunc().

Referenced by Markdown::Private::processBlocks().

◆ isUtf8Nbsp()

constexpr bool isUtf8Nbsp ( char c1,
char c2 )
staticconstexpr

Definition at line 110 of file markdown.cpp.

111{
112 return c1==static_cast<char>(0xc2) && c2==static_cast<char>(0xa0);
113}

Referenced by isAllowedEmphStr().

◆ markdownFileNameToId()

DString markdownFileNameToId ( const DString & fileName)

processes string s and converts markdown into doxygen/html commands.

Definition at line 3801 of file markdown.cpp.

3802{
3803 AUTO_TRACE("fileName={}",fileName);
3804 DString absFileName = FileInfo(fileName.str()).absFilePath();
3805 DString baseFn = stripFromPath(absFileName);
3806 if (size_t i = baseFn.rfind('.'); i!=DString::npos) baseFn = baseFn.left(i);
3807 DString baseName = escapeCharsInString(baseFn,false,false);
3808 //printf("markdownFileNameToId(%s)=md_%s\n",qPrint(fileName),qPrint(baseName));
3809 DString res = "md_"+baseName;
3810 AUTO_TRACE_EXIT("result={}",res);
3811 return res;
3812}
size_t rfind(char c, size_t pos=npos) const
Definition dstring.h:244
Minimal replacement for QFileInfo.
Definition fileinfo.h:26
std::string absFilePath() const
Definition fileinfo.cpp:106
DString escapeCharsInString(const DString &name, bool allowDots, bool allowUnderscore)
Definition util.cpp:2820
DString stripFromPath(const DString &path)
Definition util.cpp:219

References FileInfo::absFilePath(), AUTO_TRACE, AUTO_TRACE_EXIT, escapeCharsInString(), DString::left(), DString::npos, DString::rfind(), DString::str(), and stripFromPath().

Referenced by DocRef::DocRef(), DocSecRefItem::parse(), and MarkdownOutlineParser::parseInput().

◆ markersToAlignment()

constexpr Alignment markersToAlignment ( bool leftMarker,
bool rightMarker )
staticconstexpr

helper function to convert presence of left and/or right alignment markers to an alignment value

Definition at line 327 of file markdown.cpp.

328{
329 if (leftMarker && rightMarker)
330 {
331 return Alignment::Center;
332 }
333 else if (leftMarker)
334 {
335 return Alignment::Left;
336 }
337 else if (rightMarker)
338 {
339 return Alignment::Right;
340 }
341 else
342 {
343 return Alignment::None;
344 }
345}

References Center, Left, None, and Right.

Referenced by Markdown::Private::writeTableBlock().

◆ skipOverFileAndLineCommands()

bool skipOverFileAndLineCommands ( std::string_view data,
size_t indent,
size_t & offset,
std::string & location )

Definition at line 3052 of file markdown.cpp.

3053{
3054 size_t i = offset;
3055 size_t size = data.size();
3056 while (i<data.size() && data[i]==' ') i++;
3057 if (literal_at(data.substr(i),"\\ifile \""))
3058 {
3059 size_t locStart = i;
3060 if (i>offset) locStart--; // include the space before \ifile
3061 i+=8;
3062 bool found=false;
3063 while (i+9<size && data[i]!='\n')
3064 {
3065 if (literal_at(data.substr(i),"\\ilinebr "))
3066 {
3067 found=true;
3068 break;
3069 }
3070 i++;
3071 }
3072 if (found)
3073 {
3074 i+=9;
3075 location=data.substr(locStart,i-locStart);
3076 location+='\n';
3077 while (indent > 0 && i < size && data[i] == ' ')
3078 {
3079 i++;
3080 indent--;
3081 }
3082 if (i<size && data[i]=='\n') i++;
3083 offset = i;
3084 return true;
3085 }
3086 }
3087 return false;
3088}

References literal_at().

Referenced by Markdown::Private::writeCodeBlock().

Variable Documentation

◆ codeBlockIndent

const size_t codeBlockIndent = 4
static

◆ g_doxy_nbsp

const char* g_doxy_nbsp = "&_doxy_nbsp;"
static

Definition at line 225 of file markdown.cpp.

Referenced by Markdown::Private::addStrEscapeUtf8Nbsp(), and Markdown::process().

◆ g_quotationHeaderMap

const std::unordered_map<std::string,std::string> g_quotationHeaderMap
static
Initial value:
= {
{ "[!note]", "\\note" },
{ "[!warning]", "\\warning" },
{ "[!tip]", "\\remark" },
{ "[!caution]", "\\attention" },
{ "[!important]", "\\important" }
}

Definition at line 2925 of file markdown.cpp.

Referenced by Markdown::Private::writeBlockQuote().

◆ g_utf8_nbsp

const char* g_utf8_nbsp = "\xc2\xa0"
static

Definition at line 224 of file markdown.cpp.

Referenced by Markdown::Private::addStrEscapeUtf8Nbsp().