Doxygen
Loading...
Searching...
No Matches
Markdown::Private Struct Reference
Collaboration diagram for Markdown::Private:

Classes

struct  LinkRef

Public Member Functions

 Private (const DString &fn, int line, int indent)
DString processQuotations (std::string_view data, size_t refIndent)
DString processBlocks (std::string_view data, size_t indent)
DString isBlockCommand (std::string_view data, size_t offset)
size_t isSpecialCommand (std::string_view data, size_t offset)
size_t findEndOfLine (std::string_view data, size_t offset)
int processHtmlTagWrite (std::string_view data, size_t offset, bool doWrite)
 Process a HTML tag.
int processHtmlTag (std::string_view data, size_t offset)
int processEmphasis (std::string_view data, size_t offset)
int processEmphasis1 (std::string_view data, char c)
 process single emphasis
int processEmphasis2 (std::string_view data, char c)
 process double emphasis
int processEmphasis3 (std::string_view data, char c)
 Parsing triple emphasis.
int processNmdash (std::string_view data, size_t offset)
 Process ndash and mdashes.
int processQuoted (std::string_view data, size_t offset)
 Process quoted section "...", can contain one embedded newline.
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)
int processLink (std::string_view data, size_t offset)
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, or we are at the end of a paragraph.
void addStrEscapeUtf8Nbsp (std::string_view data)
void processInline (std::string_view data)
void writeMarkdownImage (std::string_view fmt, bool inline_img, bool explicitTitle, const DString &title, const DString &content, const DString &link, const DString &attributes, const FileDef *fd)
int isHeaderline (std::string_view data, bool allowAdjustLevel)
 returns whether the line is a setext-style hdr underline
int isAtxHeader (std::string_view data, DString &header, DString &id, bool allowAdjustLevel, bool *pIsIdGenerated=nullptr)
void writeOneLineHeaderOrRuler (std::string_view data)
void writeFencedCodeBlock (std::string_view data, std::string_view lang, size_t blockStart, size_t blockEnd)
size_t writeBlockQuote (std::string_view data)
size_t writeCodeBlock (std::string_view, size_t refIndent)
size_t writeTableBlock (std::string_view data)
DString extractTitleId (DString &title, int level, bool *pIsIdGenerated=nullptr)

Public Attributes

std::unordered_map< std::string, LinkReflinkRefs
DString fileName
int lineNr = 0
int indentLevel =0
DString out

Detailed Description

Definition at line 137 of file markdown.cpp.

Constructor & Destructor Documentation

◆ Private()

Markdown::Private::Private ( const DString & fn,
int line,
int indent )
inline

Definition at line 139 of file markdown.cpp.

139: fileName(fn), lineNr(line), indentLevel(indent) { }

References fileName, indentLevel, and lineNr.

Member Function Documentation

◆ addStrEscapeUtf8Nbsp()

void Markdown::Private::addStrEscapeUtf8Nbsp ( std::string_view data)

Definition at line 1829 of file markdown.cpp.

1830{
1831 AUTO_TRACE("{}",Trace::trunc(data));
1832 if (Portable::strnstr(data.data(),g_doxy_nbsp,data.size())==nullptr) // no escape needed -> fast
1833 {
1834 out+=data;
1835 }
1836 else // escape needed -> slow
1837 {
1838 out+=substitute(DString(data),g_doxy_nbsp,g_utf8_nbsp);
1839 }
1840}
#define AUTO_TRACE(...)
Definition docnode.cpp:53
DString substitute(const DString &s, const DString &src, const DString &dst)
substitute all occurrences of src in s by dst
Definition dstring.cpp:485
static const char * g_doxy_nbsp
Definition markdown.cpp:225
static const char * g_utf8_nbsp
Definition markdown.cpp:224
const char * strnstr(const char *haystack, const char *needle, size_t haystack_len)
Definition portable.cpp:616
DString trunc(const DString &s, size_t numChars=15)
Definition trace.h:56

References AUTO_TRACE, g_doxy_nbsp, g_utf8_nbsp, out, Portable::strnstr(), substitute(), and Trace::trunc().

Referenced by processSpecialCommand(), and writeFencedCodeBlock().

◆ extractTitleId()

DString Markdown::Private::extractTitleId ( DString & title,
int level,
bool * pIsIdGenerated = nullptr )

Definition at line 2130 of file markdown.cpp.

2131{
2132 AUTO_TRACE("title={} level={}",Trace::trunc(title),level);
2133 // match e.g. '{#id-b11} ' and capture 'id-b11'
2134 static const reg::Ex r2(R"({#(\a[\w-]*)}\s*$)");
2135 reg::Match match;
2136 std::string ti = title.str();
2137 if (reg::search(ti,match,r2))
2138 {
2139 std::string id = match[1].str();
2140 title = title.left(match.position());
2141 if (AnchorGenerator::instance().reserve(id)>0)
2142 {
2143 warn(fileName, lineNr, "An automatically generated id already has the name '{}'!", id);
2144 }
2145 //printf("found match id='%s' title=%s\n",qPrint(id),qPrint(title));
2146 AUTO_TRACE_EXIT("id={}",id);
2147 return id;
2148 }
2149 if (((level>0) && (level<=Config_getInt(TOC_INCLUDE_HEADINGS))) || (Config_getEnum(MARKDOWN_ID_STYLE)==MARKDOWN_ID_STYLE_t::GITHUB))
2150 {
2151 DString id = AnchorGenerator::instance().generate(ti);
2152 if (pIsIdGenerated) *pIsIdGenerated=true;
2153 //printf("auto-generated id='%s' title='%s'\n",qPrint(id),qPrint(title));
2154 AUTO_TRACE_EXIT("id={}",id);
2155 return id;
2156 }
2157 //printf("no id found in title '%s'\n",qPrint(title));
2158 return "";
2159}
static AnchorGenerator & instance()
Returns the singleton instance.
Definition anchor.cpp:41
std::string generate(const std::string &title)
generates an anchor for a section with title.
Definition anchor.cpp:56
DString left(size_t len) const
Definition dstring.h:306
const std::string & str() const
Definition dstring.h:645
#define Config_getInt(name)
Definition config.h:34
#define Config_getEnum(name)
Definition config.h:35
#define AUTO_TRACE_EXIT(...)
Definition docnode.cpp:55
#define warn(file, line, fmt,...)
Definition message.h:97
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:850
bool match(std::string_view str, Match &match, const Ex &re)
Matches a given string str for a match against regular expression re.
Definition regex.cpp:861

References AUTO_TRACE, AUTO_TRACE_EXIT, Config_getEnum, Config_getInt, fileName, AnchorGenerator::generate(), AnchorGenerator::instance(), DString::left(), lineNr, reg::search(), DString::str(), Trace::trunc(), and warn.

Referenced by isAtxHeader(), and processBlocks().

◆ findEmphasisChar()

size_t Markdown::Private::findEmphasisChar ( std::string_view data,
char c,
size_t c_size )

looks for the next emph char, skipping other constructs, and stopping when either it is found, or we are at the end of a paragraph.

Definition at line 742 of file markdown.cpp.

743{
744 AUTO_TRACE("data='{}' c={} c_size={}",Trace::trunc(data),c,c_size);
745 size_t i = 1;
746 const size_t size = data.size();
747
748 while (i<size)
749 {
750 while (i<size && data[i]!=c &&
751 data[i]!='\\' && data[i]!='@' &&
752 !(data[i]=='/' && data[i-1]=='<') && // html end tag also ends emphasis
753 data[i]!='\n') i++;
754 // avoid overflow (unclosed emph token)
755 if (i==size)
756 {
757 return 0;
758 }
759 //printf("findEmphasisChar: data=[%s] i=%d c=%c\n",data,i,data[i]);
760
761 // not counting escaped chars or characters that are unlikely
762 // to appear as the end of the emphasis char
763 if (ignoreCloseEmphChar(data[i-1],data[i]))
764 {
765 i++;
766 continue;
767 }
768 else
769 {
770 // get length of emphasis token
771 size_t len = 0;
772 while (i+len<size && data[i+len]==c)
773 {
774 len++;
775 }
776
777 if (len>0)
778 {
779 if (len!=c_size || (i+len<size && isIdChar(data[i+len]))) // to prevent touching some_underscore_identifier
780 {
781 i+=len;
782 continue;
783 }
784 AUTO_TRACE_EXIT("result={}",i);
785 return static_cast<int>(i); // found it
786 }
787 }
788
789 // skipping a code span
790 if (data[i]=='`')
791 {
792 int snb=0;
793 while (i < size && data[i] == '`')
794 {
795 snb++;
796 i++;
797 }
798
799 // find same pattern to end the span
800 int enb=0;
801 while (i<size && enb<snb)
802 {
803 if (data[i]=='`') enb++;
804 if (snb==1 && data[i]=='\'') break; // ` ended by '
805 i++;
806 }
807 }
808 else if (data[i]=='@' || data[i]=='\\')
809 { // skip over blocks that should not be processed
810 DString endBlockName = isBlockCommand(data.substr(i),i);
811 if (!endBlockName.empty())
812 {
813 i++;
814 size_t l = endBlockName.length();
815 while (i+l<size)
816 {
817 if ((data[i]=='\\' || data[i]=='@') && // command
818 data[i-1]!='\\' && data[i-1]!='@') // not escaped
819 {
820 if (dstrncmp(&data[i+1],endBlockName.data(),l)==0)
821 {
822 break;
823 }
824 }
825 i++;
826 }
827 }
828 else if (i+1<size && isIdChar(data[i+1])) // @cmd, stop processing, see bug 690385
829 {
830 return 0;
831 }
832 else
833 {
834 i++;
835 }
836 }
837 else if (data[i-1]=='<' && data[i]=='/') // html end tag invalidates emphasis
838 {
839 return 0;
840 }
841 else if (data[i]=='\n') // end * or _ at paragraph boundary
842 {
843 i++;
844 while (i<size && data[i]==' ') i++;
845 if (i>=size || data[i]=='\n')
846 {
847 return 0;
848 } // empty line -> paragraph
849 }
850 else // should not get here!
851 {
852 i++;
853 }
854 }
855 return 0;
856}
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
size_t length() const
Returns the length of the string, not counting the 0-terminator.
Definition dstring.h:151
int dstrncmp(const char *str1, const char *str2, size_t len)
Definition dstring.h:56
static constexpr bool ignoreCloseEmphChar(char c, char cn)
Definition markdown.cpp:123
static constexpr bool isIdChar(char c)
Definition markdown.cpp:84
DString isBlockCommand(std::string_view data, size_t offset)
Definition markdown.cpp:395

References AUTO_TRACE, AUTO_TRACE_EXIT, DString::data(), dstrncmp(), DString::empty(), ignoreCloseEmphChar(), isBlockCommand(), isIdChar(), DString::length(), and Trace::trunc().

Referenced by processEmphasis1(), processEmphasis2(), and processEmphasis3().

◆ findEndOfLine()

size_t Markdown::Private::findEndOfLine ( std::string_view data,
size_t offset )

Definition at line 3161 of file markdown.cpp.

3162{
3163 AUTO_TRACE("data='{}'",Trace::trunc(data));
3164 // find end of the line
3165 const size_t size = data.size();
3166 size_t nb=0, end=offset+1, j=0;
3167 while (end<=size && (j=isNewline(data.substr(end-1)))==0)
3168 {
3169 // while looking for the end of the line we might encounter a block
3170 // that needs to be passed unprocessed.
3171 if ((data[end-1]=='\\' || data[end-1]=='@') && // command
3172 (end<=1 || (data[end-2]!='\\' && data[end-2]!='@')) // not escaped
3173 )
3174 {
3175 DString endBlockName = isBlockCommand(data.substr(end-1),end-1);
3176 end++;
3177 if (!endBlockName.empty())
3178 {
3179 size_t l = endBlockName.length();
3180 for (;end+l+1<size;end++) // search for end of block marker
3181 {
3182 if ((data[end]=='\\' || data[end]=='@') &&
3183 data[end-1]!='\\' && data[end-1]!='@'
3184 )
3185 {
3186 if (dstrncmp(&data[end+1],endBlockName.data(),l)==0)
3187 {
3188 // found end marker, skip over this block
3189 //printf("feol.block out={%s}\n",qPrint(DString(data+i).left(end+l+1-i)));
3190 end = end + l + 2;
3191 break;
3192 }
3193 }
3194 }
3195 }
3196 }
3197 else if (nb==0 && data[end-1]=='<' && size>=6 && end+6<size &&
3198 (end<=1 || (data[end-2]!='\\' && data[end-2]!='@'))
3199 )
3200 {
3201 if (tolower(data[end])=='p' && tolower(data[end+1])=='r' &&
3202 tolower(data[end+2])=='e' && (data[end+3]=='>' || data[end+3]==' ')) // <pre> tag
3203 {
3204 // skip part until including </pre>
3205 end = end + processHtmlTagWrite(data.substr(end-1),end-1,false);
3206 break;
3207 }
3208 else
3209 {
3210 end++;
3211 }
3212 }
3213 else if (nb==0 && data[end-1]=='`')
3214 {
3215 while (end <= size && data[end - 1] == '`')
3216 {
3217 end++;
3218 nb++;
3219 }
3220 }
3221 else if (nb>0 && data[end-1]=='`')
3222 {
3223 size_t enb=0;
3224 while (end <= size && data[end - 1] == '`')
3225 {
3226 end++;
3227 enb++;
3228 }
3229 if (enb==nb) nb=0;
3230 }
3231 else
3232 {
3233 end++;
3234 }
3235 }
3236 if (j>0) end+=j-1;
3237 AUTO_TRACE_EXIT("offset={} end={}",offset,end);
3238 return end;
3239}
DirIterator end(const DirIterator &) noexcept
Definition dir.cpp:181
size_t isNewline(std::string_view data)
Definition markdown.cpp:232
int processHtmlTagWrite(std::string_view data, size_t offset, bool doWrite)
Process a HTML tag.

References AUTO_TRACE, AUTO_TRACE_EXIT, DString::data(), dstrncmp(), DString::empty(), end(), isBlockCommand(), isNewline(), DString::length(), processHtmlTagWrite(), and Trace::trunc().

Referenced by processBlocks(), and processQuotations().

◆ isAtxHeader()

int Markdown::Private::isAtxHeader ( std::string_view data,
DString & header,
DString & id,
bool allowAdjustLevel,
bool * pIsIdGenerated = nullptr )

Definition at line 2162 of file markdown.cpp.

2164{
2165 AUTO_TRACE("data='{}' header={} id={} allowAdjustLevel={}",Trace::trunc(data),Trace::trunc(header),id,allowAdjustLevel);
2166 size_t i = 0;
2167 int level = 0, blanks=0;
2168 const size_t size = data.size();
2169
2170 // find start of header text and determine heading level
2171 while (i<size && data[i]==' ') i++;
2172 if (i>=size || data[i]!='#')
2173 {
2174 return 0;
2175 }
2176 while (i < size && data[i] == '#')
2177 {
2178 i++;
2179 level++;
2180 }
2181 if (level>SectionType::MaxLevel) // too many #'s -> no section
2182 {
2183 return 0;
2184 }
2185 while (i < size && data[i] == ' ')
2186 {
2187 i++;
2188 blanks++;
2189 }
2190 if (level==1 && blanks==0)
2191 {
2192 return 0; // special case to prevent #someid seen as a header (see bug 671395)
2193 }
2194
2195 // find end of header text
2196 size_t end=i;
2197 while (end<size && data[end]!='\n') end++;
2198 while (end>i && (data[end-1]=='#' || data[end-1]==' ')) end--;
2199
2200 // store result
2201 header = data.substr(i,end-i);
2202 id = extractTitleId(header, level, pIsIdGenerated);
2203 if (!id.empty()) // strip #'s between title and id
2204 {
2205 int idx=static_cast<int>(header.length())-1;
2206 while (idx>=0 && (header.at(idx)=='#' || header.at(idx)==' ')) idx--;
2207 header=header.left(idx+1);
2208 }
2209
2210 if (allowAdjustLevel && level==1 && indentLevel==-1)
2211 {
2212 // in case we find a `# Section` on a markdown page that started with the same level
2213 // header, we no longer need to artificially decrease the paragraph level.
2214 // So both
2215 // -------------------
2216 // # heading 1 <-- here we set g_indentLevel to -1
2217 // # heading 2 <-- here we set g_indentLevel back to 0 such that this will be a @section
2218 // -------------------
2219 // and
2220 // -------------------
2221 // # heading 1 <-- here we set g_indentLevel to -1
2222 // ## heading 2 <-- here we keep g_indentLevel at -1 such that @subsection will be @section
2223 // -------------------
2224 // will convert to
2225 // -------------------
2226 // @page md_page Heading 1
2227 // @section autotoc_md1 Heading 2
2228 // -------------------
2229
2230 indentLevel=0;
2231 }
2232 int res = level+indentLevel;
2233 AUTO_TRACE_EXIT("result={}",res);
2234 return res;
2235}
DString substr(size_t pos=0, size_t count=npos) const
Returns a substring of length count starting at pos.
Definition dstring.h:223
char & at(size_t i)
Returns a reference to the character at index i.
Definition dstring.h:686
static constexpr int MaxLevel
Definition section.h:39
DString extractTitleId(DString &title, int level, bool *pIsIdGenerated=nullptr)

References DString::at(), AUTO_TRACE, AUTO_TRACE_EXIT, end(), extractTitleId(), indentLevel, DString::left(), DString::length(), SectionType::MaxLevel, DString::substr(), and Trace::trunc().

Referenced by writeOneLineHeaderOrRuler().

◆ isBlockCommand()

DString Markdown::Private::isBlockCommand ( std::string_view data,
size_t offset )

Definition at line 395 of file markdown.cpp.

396{
397 DString result;
398 AUTO_TRACE("data='{}' offset={}",Trace::trunc(data),offset);
399
400 using EndBlockFunc = DString (*)(const std::string &,bool,char);
401
402 static constexpr auto getEndBlock = [](const std::string &blockName,bool,char) -> DString
403 {
404 return "end"+blockName;
405 };
406 static constexpr auto getEndCode = [](const std::string &blockName,bool openBracket,char) -> DString
407 {
408 return openBracket ? DString("}") : "end"+blockName;
409 };
410 static constexpr auto getEndUml = [](const std::string &/* blockName */,bool,char) -> DString
411 {
412 return "enduml";
413 };
414 static constexpr auto getEndFormula = [](const std::string &/* blockName */,bool,char nextChar) -> DString
415 {
416 switch (nextChar)
417 {
418 case '$': return "f$";
419 case '(': return "f)";
420 case '[': return "f]";
421 case '{': return "f}";
422 }
423 return "";
424 };
425
426 // table mapping a block start command to a function that can return the matching end block string
427 static const std::unordered_map<std::string,EndBlockFunc> blockNames =
428 {
429 { "dot", getEndBlock },
430 { "code", getEndCode },
431 { "icode", getEndBlock },
432 { "msc", getEndBlock },
433 { "verbatim", getEndBlock },
434 { "iverbatim", getEndBlock },
435 { "iliteral", getEndBlock },
436 { "latexonly", getEndBlock },
437 { "htmlonly", getEndBlock },
438 { "xmlonly", getEndBlock },
439 { "rtfonly", getEndBlock },
440 { "manonly", getEndBlock },
441 { "docbookonly", getEndBlock },
442 { "startuml", getEndUml },
443 { "mermaid", getEndBlock },
444 { "f", getEndFormula }
445 };
446
447 const size_t size = data.size();
448 bool openBracket = offset>0 && data.data()[-1]=='{';
449 bool isEscaped = offset>0 && (data.data()[-1]=='\\' || data.data()[-1]=='@');
450 if (isEscaped) return result;
451
452 size_t end=1;
453 while (end<size && (data[end]>='a' && data[end]<='z')) end++;
454 if (end==1) return result;
455 std::string blockName(data.substr(1,end-1));
456 auto it = blockNames.find(blockName);
457 if (it!=blockNames.end()) // there is a function assigned
458 {
459 result = it->second(blockName, openBracket, end<size ? data[end] : 0);
460 }
461 AUTO_TRACE_EXIT("result={}",result);
462 return result;
463}

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

Referenced by findEmphasisChar(), findEndOfLine(), processBlocks(), and processSpecialCommand().

◆ isHeaderline()

int Markdown::Private::isHeaderline ( std::string_view data,
bool allowAdjustLevel )

returns whether the line is a setext-style hdr underline

Definition at line 1935 of file markdown.cpp.

1936{
1937 AUTO_TRACE("data='{}' allowAdjustLevel",Trace::trunc(data),allowAdjustLevel);
1938 size_t i=0, c=0;
1939 const size_t size = data.size();
1940 while (i<size && data[i]==' ') i++;
1941 if (i==size) return 0;
1942
1943 // test of level 1 header
1944 if (data[i]=='=')
1945 {
1946 while (i < size && data[i] == '=')
1947 {
1948 i++;
1949 c++;
1950 }
1951 while (i<size && data[i]==' ') i++;
1952 int level = (c>1 && (i>=size || data[i]=='\n')) ? 1 : 0;
1953 if (allowAdjustLevel && level==1 && indentLevel==-1)
1954 {
1955 // In case a page starts with a header line we use it as title, promoting it to @page.
1956 // We set g_indentLevel to -1 to promoting the other sections if they have a deeper
1957 // nesting level than the page header, i.e. @section..@subsection becomes @page..@section.
1958 // In case a section at the same level is found (@section..@section) however we need
1959 // to undo this (and the result will be @page..@section).
1960 indentLevel=0;
1961 }
1962 AUTO_TRACE_EXIT("result={}",indentLevel+level);
1963 return indentLevel+level;
1964 }
1965 // test of level 2 header
1966 if (data[i]=='-')
1967 {
1968 while (i < size && data[i] == '-')
1969 {
1970 i++;
1971 c++;
1972 }
1973 while (i<size && data[i]==' ') i++;
1974 return (c>1 && (i>=size || data[i]=='\n')) ? indentLevel+2 : 0;
1975 }
1976 return 0;
1977}

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

Referenced by processBlocks().

◆ isSpecialCommand()

size_t Markdown::Private::isSpecialCommand ( std::string_view data,
size_t offset )

Definition at line 465 of file markdown.cpp.

466{
467 AUTO_TRACE("data='{}' offset={}",Trace::trunc(data),offset);
468
469 using EndCmdFunc = size_t (*)(std::string_view,size_t);
470
471 static constexpr auto endOfLine = [](std::string_view data_,size_t offset_) -> size_t
472 {
473 // skip until the end of line (allowing line continuation characters)
474 char lc = 0;
475 char c = 0;
476 while (offset_<data_.size() && ((c=data_[offset_])!='\n' || lc=='\\'))
477 {
478 if (c=='\\') lc='\\'; // last character was a line continuation
479 else if (c!=' ') lc=0; // rest line continuation
480 offset_++;
481 }
482 return offset_;
483 };
484
485 static constexpr auto endOfLabels = [](std::string_view data_,size_t offset_,bool multi_) -> size_t
486 {
487 if (offset_<data_.size() && data_[offset_]==' ') // we expect a space before the label
488 {
489 char c = 0;
490 offset_++;
491 bool done=false;
492 while (!done)
493 {
494 // skip over spaces
495 while (offset_<data_.size() && data_[offset_]==' ')
496 {
497 offset_++;
498 }
499 // skip over label
500 while (offset_<data_.size() && (c=data_[offset_])!=' ' && c!=',' && c!='\\' && c!='@' && c!='\n')
501 {
502 offset_++;
503 }
504 // optionally skip over a comma separated list of labels
505 if (multi_ && offset_<data_.size() && (data_[offset_]==',' || data_[offset_]==' '))
506 {
507 size_t off = offset_;
508 while (off<data_.size() && data_[off]==' ')
509 {
510 off++;
511 }
512 if (off<data_.size() && data_[off]==',')
513 {
514 offset_ = ++off;
515 }
516 else // no next label found
517 {
518 done=true;
519 }
520 }
521 else
522 {
523 done=true;
524 }
525 }
526 return offset_;
527 }
528 return 0;
529 };
530
531 static constexpr auto endOfLabel = [](std::string_view data_,size_t offset_) -> size_t
532 {
533 return endOfLabels(data_,offset_,false);
534 };
535
536 static constexpr auto endOfLabelOpt = [](std::string_view data_,size_t offset_) -> size_t
537 {
538 size_t index=offset_;
539 if (index<data_.size() && data_[index]==' ') // skip over optional spaces
540 {
541 index++;
542 while (index<data_.size() && data_[index]==' ') index++;
543 }
544 if (index<data_.size() && data_[index]=='{') // find matching '}'
545 {
546 index++;
547 char c = 0;
548 while (index<data_.size() && (c=data_[index])!='}' && c!='\\' && c!='@' && c!='\n') index++;
549 if (index==data_.size() || data_[index]!='}') return 0; // invalid option
550 offset_=index+1; // part after {...} is the option
551 }
552 return endOfLabel(data_,offset_);
553 };
554
555 static constexpr auto endOfParam = [](std::string_view data_,size_t offset_) -> size_t
556 {
557 size_t index=offset_;
558 if (index<data_.size() && data_[index]==' ') // skip over optional spaces
559 {
560 index++;
561 while (index<data_.size() && data_[index]==' ') index++;
562 }
563 if (index<data_.size() && data_[index]=='[') // find matching ']'
564 {
565 index++;
566 char c = 0;
567 while (index<data_.size() && (c=data_[index])!=']' && c!='\n') index++;
568 if (index==data_.size() || data_[index]!=']') return 0; // invalid parameter
569 offset_=index+1; // part after [...] is the parameter name
570 }
571 return endOfLabels(data_,offset_,true);
572 };
573
574 static constexpr auto endOfRetVal = [](std::string_view data_,size_t offset_) -> size_t
575 {
576 return endOfLabels(data_,offset_,true);
577 };
578
579 static constexpr auto endOfFuncLike = [](std::string_view data_,size_t offset_,bool allowSpaces) -> size_t
580 {
581 if (offset_<data_.size() && data_[offset_]==' ') // we expect a space before the name
582 {
583 char c=0;
584 offset_++;
585 // skip over spaces
586 while (offset_<data_.size() && data_[offset_]==' ')
587 {
588 offset_++;
589 }
590 // skip over name (and optionally type)
591 while (offset_<data_.size() && (c=data_[offset_])!='\n' && (allowSpaces || c!=' ') && c!='(')
592 {
593 if (literal_at(data_.substr(offset_),"\\ilinebr ")) break;
594 offset_++;
595 }
596 if (c=='(') // find the end of the function
597 {
598 int count=1;
599 offset_++;
600 while (offset_<data_.size() && (c=data_[offset_++]))
601 {
602 if (c=='(') count++;
603 else if (c==')') count--;
604 if (count==0) return offset_;
605 }
606 }
607 return offset_;
608 }
609 return 0;
610 };
611
612 static constexpr auto endOfFunc = [](std::string_view data_,size_t offset_) -> size_t
613 {
614 return endOfFuncLike(data_,offset_,true);
615 };
616
617 static constexpr auto endOfGuard = [](std::string_view data_,size_t offset_) -> size_t
618 {
619 return endOfFuncLike(data_,offset_,false);
620 };
621
622 static const std::unordered_map<std::string,EndCmdFunc> cmdNames =
623 {
624 { "a", endOfLabel },
625 { "addindex", endOfLine },
626 { "addtogroup", endOfLabel },
627 { "anchor", endOfLabel },
628 { "b", endOfLabel },
629 { "c", endOfLabel },
630 { "category", endOfLine },
631 { "cite", endOfLabelOpt },
632 { "class", endOfLine },
633 { "concept", endOfLine },
634 { "copybrief", endOfFunc },
635 { "copydetails", endOfFunc },
636 { "copydoc", endOfFunc },
637 { "def", endOfFunc },
638 { "defgroup", endOfLabel },
639 { "diafile", endOfLine },
640 { "dir", endOfLine },
641 { "dockbookinclude",endOfLine },
642 { "dontinclude", endOfLine },
643 { "dotfile", endOfLine },
644 { "e", endOfLabel },
645 { "elseif", endOfGuard },
646 { "em", endOfLabel },
647 { "emoji", endOfLabel },
648 { "enum", endOfLabel },
649 { "example", endOfLine },
650 { "exception", endOfLabel },
651 { "extends", endOfLabel },
652 { "file", endOfLine },
653 { "fn", endOfFunc },
654 { "headerfile", endOfLine },
655 { "htmlinclude", endOfLine },
656 { "ianchor", endOfLabelOpt },
657 { "idlexcept", endOfLine },
658 { "if", endOfGuard },
659 { "ifnot", endOfGuard },
660 { "image", endOfLine },
661 { "implements", endOfLine },
662 { "include", endOfLine },
663 { "includedoc", endOfLine },
664 { "includelineno", endOfLine },
665 { "ingroup", endOfLabel },
666 { "interface", endOfLine },
667 { "latexinclude", endOfLine },
668 { "maninclude", endOfLine },
669 { "memberof", endOfLabel },
670 { "mermaidfile", endOfLine },
671 { "mscfile", endOfLine },
672 { "namespace", endOfLabel },
673 { "noop", endOfLine },
674 { "overload", endOfLine },
675 { "p", endOfLabel },
676 { "package", endOfLabel },
677 { "page", endOfLabel },
678 { "paragraph", endOfLabel },
679 { "param", endOfParam },
680 { "property", endOfLine },
681 { "protocol", endOfLine },
682 { "qualifier", endOfLine },
683 { "ref", endOfLabel },
684 { "refitem", endOfLine },
685 { "related", endOfLabel },
686 { "relatedalso", endOfLabel },
687 { "relates", endOfLabel },
688 { "relatesalso", endOfLabel },
689 { "requirement", endOfLabel },
690 { "retval", endOfRetVal},
691 { "rtfinclude", endOfLine },
692 { "section", endOfLabel },
693 { "skip", endOfLine },
694 { "skipline", endOfLine },
695 { "snippet", endOfLine },
696 { "snippetdoc", endOfLine },
697 { "snippetlineno", endOfLine },
698 { "struct", endOfLine },
699 { "satisfies", endOfLabel },
700 { "subpage", endOfLabel },
701 { "subparagraph", endOfLabel },
702 { "subsubparagraph",endOfLabel },
703 { "subsection", endOfLabel },
704 { "subsubsection", endOfLabel },
705 { "throw", endOfLabel },
706 { "throws", endOfLabel },
707 { "tparam", endOfLabel },
708 { "typedef", endOfLine },
709 { "plantumlfile", endOfLine },
710 { "union", endOfLine },
711 { "until", endOfLine },
712 { "var", endOfLine },
713 { "verbinclude", endOfLine },
714 { "verifies", endOfLabel },
715 { "weakgroup", endOfLabel },
716 { "xmlinclude", endOfLine },
717 { "xrefitem", endOfLabel }
718 };
719
720 bool isEscaped = offset>0 && (data.data()[-1]=='\\' || data.data()[-1]=='@');
721 if (isEscaped) return 0;
722
723 const size_t size = data.size();
724 size_t end=1;
725 while (end<size && (data[end]>='a' && data[end]<='z')) end++;
726 if (end==1) return 0;
727 std::string cmdName(data.substr(1,end-1));
728 size_t result=0;
729 auto it = cmdNames.find(cmdName);
730 if (it!=cmdNames.end()) // command with parameters that should be ignored by markdown
731 {
732 // find the end of the parameters
733 result = it->second(data,end);
734 }
735 AUTO_TRACE_EXIT("result={}",result);
736 return result;
737}
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

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

Referenced by processSpecialCommand().

◆ processBlocks()

DString Markdown::Private::processBlocks ( std::string_view data,
size_t indent )

Definition at line 3432 of file markdown.cpp.

3433{
3434 AUTO_TRACE("data='{}' indent={}",Trace::trunc(data),indent);
3435 out.clear();
3436 size_t pi = std::string::npos;
3437 DString id,link,title;
3438
3439#if 0 // commented out, since starting with a comment block is probably a usage error
3440 // see also http://stackoverflow.com/q/20478611/784672
3441
3442 // special case when the documentation starts with a code block
3443 // since the first line is skipped when looking for a code block later on.
3444 if (end>codeBlockIndent && isCodeBlock(data,0,end,blockIndent))
3445 {
3446 i=writeCodeBlock(out,data,size,blockIndent);
3447 end=i+1;
3448 pi=-1;
3449 }
3450#endif
3451
3452 size_t currentIndent = indent;
3453 size_t listIndent = indent;
3454 bool insideList = false;
3455 bool newBlock = false;
3456 // process each line
3457 size_t i=0;
3458 while (i<data.size())
3459 {
3460 size_t end = findEndOfLine(data,i);
3461 // line is now found at [i..end)
3462
3463 size_t lineIndent=0;
3464 int level = 0;
3465 while (lineIndent<end && data[i+lineIndent]==' ') lineIndent++;
3466 //printf("** lineIndent=%d line=(%s)\n",lineIndent,qPrint(DString(data+i).left(end-i)));
3467
3468 if (newBlock)
3469 {
3470 //printf("** end of block\n");
3471 if (insideList && lineIndent<currentIndent) // end of list
3472 {
3473 //printf("** end of list\n");
3474 currentIndent = indent;
3475 insideList = false;
3476 }
3477 newBlock = false;
3478 }
3479
3480 if ((listIndent=isListMarker(data.substr(i,end-i)))) // see if we need to increase the indent level
3481 {
3482 if (listIndent<currentIndent+4)
3483 {
3484 //printf("** start of list\n");
3485 insideList = true;
3486 currentIndent = listIndent;
3487 }
3488 }
3489 else if (isEndOfList(data.substr(i,end-i)))
3490 {
3491 //printf("** end of list\n");
3492 insideList = false;
3493 currentIndent = listIndent;
3494 }
3495 else if (isEmptyLine(data.substr(i,end-i)))
3496 {
3497 //printf("** new block\n");
3498 newBlock = true;
3499 }
3500
3501 //printf("indent=%d listIndent=%d blockIndent=%d\n",indent,listIndent,blockIndent);
3502
3503 //printf("findEndOfLine: pi=%d i=%d end=%d\n",pi,i,end);
3504
3505 if (pi!=std::string::npos)
3506 {
3507 size_t blockStart=0, blockEnd=0, blockOffset=0;
3508 DString lang;
3509 size_t blockIndent = currentIndent;
3510 size_t ref = 0;
3511 //printf("isHeaderLine(%s)=%d\n",DString(data+i).left(size-i).data(),level);
3512 DString endBlockName;
3513 if (data[i]=='@' || data[i]=='\\') endBlockName = isBlockCommand(data.substr(i),i);
3514 if (!endBlockName.empty())
3515 {
3516 // handle previous line
3517 if (isLinkRef(data.substr(pi,i-pi),id,link,title))
3518 {
3519 linkRefs.emplace(id.lower().str(),LinkRef(link,title));
3520 }
3521 else
3522 {
3523 writeOneLineHeaderOrRuler(data.substr(pi,i-pi));
3524 }
3525 out+=data[i];
3526 i++;
3527 size_t l = endBlockName.length();
3528 while (i+l<data.size())
3529 {
3530 if ((data[i]=='\\' || data[i]=='@') && // command
3531 data[i-1]!='\\' && data[i-1]!='@') // not escaped
3532 {
3533 if (dstrncmp(&data[i+1],endBlockName.data(),l)==0)
3534 {
3535 out+=data[i];
3536 out+=endBlockName;
3537 i+=l+1;
3538 break;
3539 }
3540 }
3541 out+=data[i];
3542 i++;
3543 }
3544 }
3545 else if ((level=isHeaderline(data.substr(i),true))>0)
3546 {
3547 //printf("Found header at %d-%d\n",i,end);
3548 while (pi<data.size() && data[pi]==' ') pi++;
3549 DString header = data.substr(pi,i-pi-1);
3550 id = extractTitleId(header, level);
3551 //printf("header='%s' is='%s'\n",qPrint(header),qPrint(id));
3552 if (!header.empty())
3553 {
3554 if (!id.empty())
3555 {
3556 out+=level==1?"@section ":"@subsection ";
3557 out+=id;
3558 out+=" ";
3559 out+=header;
3560 out+="\n\n";
3561 }
3562 else
3563 {
3564 out+=level==1?"<h1>":"<h2>";
3565 out+=header;
3566 out+=level==1?"\n</h1>\n":"\n</h2>\n";
3567 }
3568 }
3569 else
3570 {
3571 out+="\n<hr>\n";
3572 }
3573 pi=std::string::npos;
3574 i=end;
3575 end=i+1;
3576 continue;
3577 }
3578 else if ((ref=isLinkRef(data.substr(pi),id,link,title)))
3579 {
3580 //printf("found link ref: id='%s' link='%s' title='%s'\n",
3581 // qPrint(id),qPrint(link),qPrint(title));
3582 linkRefs.emplace(id.lower().str(),LinkRef(link,title));
3583 i=ref+pi;
3584 end=i+1;
3585 }
3586 else if (isFencedCodeBlock(data.substr(pi),currentIndent,lang,blockStart,blockEnd,blockOffset,fileName,lineNr))
3587 {
3588 //printf("Found FencedCodeBlock lang='%s' start=%d end=%d code={%s}\n",
3589 // qPrint(lang),blockStart,blockEnd,DString(data+pi+blockStart).left(blockEnd-blockStart).data());
3590 writeFencedCodeBlock(data.substr(pi),lang.view(),blockStart,blockEnd);
3591 i=pi+blockOffset;
3592 pi=std::string::npos;
3593 end=i+1;
3594 continue;
3595 }
3596 else if (isCodeBlock(data.substr(i,end-i),i,blockIndent))
3597 {
3598 // skip previous line (it is empty anyway)
3599 i+=writeCodeBlock(data.substr(i),blockIndent);
3600 pi=std::string::npos;
3601 end=i+1;
3602 continue;
3603 }
3604 else if (isTableBlock(data.substr(pi)))
3605 {
3606 i=pi+writeTableBlock(data.substr(pi));
3607 pi=std::string::npos;
3608 end=i+1;
3609 continue;
3610 }
3611 else
3612 {
3613 writeOneLineHeaderOrRuler(data.substr(pi,i-pi));
3614 }
3615 }
3616 pi=i;
3617 i=end;
3618 }
3619 //printf("last line %d size=%d\n",i,size);
3620 if (pi!=std::string::npos && pi<data.size()) // deal with the last line
3621 {
3622 if (isLinkRef(data.substr(pi),id,link,title))
3623 {
3624 //printf("found link ref: id='%s' link='%s' title='%s'\n",
3625 // qPrint(id),qPrint(link),qPrint(title));
3626 linkRefs.emplace(id.lower().str(),LinkRef(link,title));
3627 }
3628 else
3629 {
3630 writeOneLineHeaderOrRuler(data.substr(pi));
3631 }
3632 }
3633
3634 return out;
3635}
void clear()
Definition dstring.h:214
std::string_view view() const
Definition dstring.h:162
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 bool isEmptyLine(std::string_view data)
static const size_t codeBlockIndent
Definition markdown.cpp:226
static size_t isListMarker(std::string_view data)
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 isTableBlock(std::string_view data)
Returns true iff data points to the start of a table block.
size_t writeTableBlock(std::string_view data)
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
std::unordered_map< std::string, LinkRef > linkRefs
Definition markdown.cpp:182
size_t writeCodeBlock(std::string_view, size_t refIndent)
size_t findEndOfLine(std::string_view data, size_t offset)
void writeOneLineHeaderOrRuler(std::string_view data)

References AUTO_TRACE, codeBlockIndent, DString::data(), dstrncmp(), DString::empty(), end(), extractTitleId(), fileName, findEndOfLine(), isBlockCommand(), isCodeBlock(), isEmptyLine(), isEndOfList(), isFencedCodeBlock(), isHeaderline(), isLinkRef(), isListMarker(), isTableBlock(), DString::length(), lineNr, linkRefs, out, DString::substr(), Trace::trunc(), DString::view(), writeCodeBlock(), writeFencedCodeBlock(), writeOneLineHeaderOrRuler(), and writeTableBlock().

◆ processCodeSpan()

int Markdown::Private::processCodeSpan ( std::string_view data,
size_t offset )

` parsing a code span (assuming codespan != 0)

Definition at line 1715 of file markdown.cpp.

1716{
1717 AUTO_TRACE("data='{}' offset={}",Trace::trunc(data),offset);
1718 const size_t size = data.size();
1719
1720 /* counting the number of backticks in the delimiter */
1721 size_t nb=0, end=0;
1722 while (nb<size && data[nb]=='`')
1723 {
1724 nb++;
1725 }
1726
1727 /* finding the next delimiter with the same amount of backticks */
1728 size_t i = 0;
1729 char pc = '`';
1730 bool markdownStrict = Config_getBool(MARKDOWN_STRICT);
1731 for (end=nb; end<size; end++)
1732 {
1733 //AUTO_TRACE_ADD("c={} nb={} i={} size={}",data[end],nb,i,size);
1734 if (data[end]=='`')
1735 {
1736 i++;
1737 if (nb==1) // `...`
1738 {
1739 if (end+1<size && data[end+1]=='`') // skip over `` inside `...`
1740 {
1741 AUTO_TRACE_ADD("case1.1");
1742 // skip
1743 end++;
1744 i=0;
1745 }
1746 else // normal end of `...`
1747 {
1748 AUTO_TRACE_ADD("case1.2");
1749 break;
1750 }
1751 }
1752 else if (i==nb) // ``...``
1753 {
1754 if (end+1<size && data[end+1]=='`') // do greedy match
1755 {
1756 // skip this quote and use the next one to terminate the sequence, e.g. ``X`Y```
1757 i--;
1758 AUTO_TRACE_ADD("case2.1");
1759 }
1760 else // normal end of ``...``
1761 {
1762 AUTO_TRACE_ADD("case2.2");
1763 break;
1764 }
1765 }
1766 }
1767 else if (data[end]=='\n')
1768 {
1769 // consecutive newlines
1770 if (pc == '\n')
1771 {
1772 AUTO_TRACE_EXIT("new paragraph");
1773 return 0;
1774 }
1775 pc = '\n';
1776 i = 0;
1777 }
1778 else if (!markdownStrict && data[end]=='\'' && nb==1 && (end+1==size || (end+1<size && data[end+1]!='\'' && !isIdChar(data[end+1]))))
1779 { // look for quoted strings like 'some word', but skip strings like `it's cool`
1780 out+="&lsquo;";
1781 out+=data.substr(nb,end-nb);
1782 out+="&rsquo;";
1783 AUTO_TRACE_EXIT("quoted end={}",end+1);
1784 return static_cast<int>(end+1);
1785 }
1786 else if (!markdownStrict && data[end]=='\'' && nb==2 && end+1<size && data[end+1]=='\'')
1787 { // look for '' to match a ``
1788 out+="&ldquo;";
1789 out+=data.substr(nb,end-nb);
1790 out+="&rdquo;";
1791 AUTO_TRACE_EXIT("double quoted end={}",end+1);
1792 return static_cast<int>(end+2);
1793 }
1794 else
1795 {
1796 if (data[end]!=' ') pc = data[end];
1797 i=0;
1798 }
1799 }
1800 if (i < nb && end >= size)
1801 {
1802 AUTO_TRACE_EXIT("no matching delimiter nb={} i={}",nb,i);
1803 if (nb>=3) // found ``` that is not at the start of the line, keep it as-is.
1804 {
1805 out+=data.substr(0,nb);
1806 return static_cast<int>(nb);
1807 }
1808 return 0; // no matching delimiter
1809 }
1810 while (end<size && data[end]=='`') // do greedy match in case we have more end backticks.
1811 {
1812 end++;
1813 }
1814
1815 //printf("found code span '%s'\n",qPrint(DString(data+f_begin).left(f_end-f_begin)));
1816
1817 /* real code span */
1818 if (nb+nb < end)
1819 {
1820 DString codeFragment = data.substr(nb, end-nb-nb);
1821 out+="<tt>";
1822 out+=escapeSpecialChars(codeFragment);
1823 out+="</tt>";
1824 }
1825 AUTO_TRACE_EXIT("result={} nb={}",end,nb);
1826 return static_cast<int>(end);
1827}
#define Config_getBool(name)
Definition config.h:33
#define AUTO_TRACE_ADD(...)
Definition docnode.cpp:54
static DString escapeSpecialChars(const DString &s)
Definition markdown.cpp:263

References AUTO_TRACE, AUTO_TRACE_ADD, AUTO_TRACE_EXIT, Config_getBool, end(), escapeSpecialChars(), isIdChar(), out, DString::substr(), and Trace::trunc().

Referenced by Markdown::fill_table().

◆ processEmphasis()

int Markdown::Private::processEmphasis ( std::string_view data,
size_t offset )

Definition at line 1155 of file markdown.cpp.

1156{
1157 AUTO_TRACE("data='{}' offset={}",Trace::trunc(data),offset);
1158 const size_t size = data.size();
1159
1160 if (isAllowedEmphStr(data,offset) || // invalid char before * or _
1161 (size>1 && data[0]!=data[1] && !(isIdChar(data[1]) || extraChar(data[1]))) || // invalid char after * or _
1162 (size>2 && data[0]==data[1] && !(isIdChar(data[2]) || extraChar(data[2])))) // invalid char after ** or __
1163 {
1164 AUTO_TRACE_EXIT("invalid surrounding characters");
1165 return 0;
1166 }
1167
1168 char c = data[0];
1169 int ret = 0;
1170 if (size>2 && c!='~' && data[1]!=c) // _bla or *bla
1171 {
1172 // whitespace cannot follow an opening emphasis
1173 if (data[1]==' ' || data[1]=='\n' ||
1174 (ret = processEmphasis1(data.substr(1), c)) == 0)
1175 {
1176 return 0;
1177 }
1178 AUTO_TRACE_EXIT("result={}",ret+1);
1179 return ret+1;
1180 }
1181 if (size>3 && data[1]==c && data[2]!=c) // __bla or **bla
1182 {
1183 if (data[2]==' ' || data[2]=='\n' ||
1184 (ret = processEmphasis2(data.substr(2), c)) == 0)
1185 {
1186 return 0;
1187 }
1188 AUTO_TRACE_EXIT("result={}",ret+2);
1189 return ret+2;
1190 }
1191 if (size>4 && c!='~' && data[1]==c && data[2]==c && data[3]!=c) // ___bla or ***bla
1192 {
1193 if (data[3]==' ' || data[3]=='\n' ||
1194 (ret = processEmphasis3(data.substr(3), c)) == 0)
1195 {
1196 return 0;
1197 }
1198 AUTO_TRACE_EXIT("result={}",ret+3);
1199 return ret+3;
1200 }
1201 return 0;
1202}
static constexpr bool isAllowedEmphStr(const std::string_view &data, size_t offset)
Definition markdown.cpp:115
static constexpr bool extraChar(char c)
Definition markdown.cpp:93
int processEmphasis1(std::string_view data, char c)
process single emphasis
Definition markdown.cpp:859
int processEmphasis3(std::string_view data, char c)
Parsing triple emphasis.
Definition markdown.cpp:925
int processEmphasis2(std::string_view data, char c)
process double emphasis
Definition markdown.cpp:893

References AUTO_TRACE, AUTO_TRACE_EXIT, extraChar(), isAllowedEmphStr(), isIdChar(), processEmphasis1(), processEmphasis2(), processEmphasis3(), and Trace::trunc().

Referenced by Markdown::fill_table().

◆ processEmphasis1()

int Markdown::Private::processEmphasis1 ( std::string_view data,
char c )

process single emphasis

Definition at line 859 of file markdown.cpp.

860{
861 AUTO_TRACE("data='{}' c={}",Trace::trunc(data),c);
862 size_t i = 0;
863 const size_t size = data.size();
864
865 /* skipping one symbol if coming from emph3 */
866 if (size>1 && data[0]==c && data[1]==c) { i=1; }
867
868 while (i<size)
869 {
870 size_t len = findEmphasisChar(data.substr(i), c, 1);
871 if (len==0) { return 0; }
872 i+=len;
873 if (i>=size) { return 0; }
874
875 if (i+1<size && data[i+1]==c)
876 {
877 i++;
878 continue;
879 }
880 if (data[i]==c && data[i-1]!=' ' && data[i-1]!='\n')
881 {
882 out+="<em>";
883 processInline(data.substr(0,i));
884 out+="</em>";
885 AUTO_TRACE_EXIT("result={}",i+1);
886 return static_cast<int>(i+1);
887 }
888 }
889 return 0;
890}
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:742
void processInline(std::string_view data)

References AUTO_TRACE, AUTO_TRACE_EXIT, findEmphasisChar(), out, processInline(), and Trace::trunc().

Referenced by processEmphasis(), and processEmphasis3().

◆ processEmphasis2()

int Markdown::Private::processEmphasis2 ( std::string_view data,
char c )

process double emphasis

Definition at line 893 of file markdown.cpp.

894{
895 AUTO_TRACE("data='{}' c={}",Trace::trunc(data),c);
896 size_t i = 0;
897 const size_t size = data.size();
898
899 while (i<size)
900 {
901 size_t len = findEmphasisChar(data.substr(i), c, 2);
902 if (len==0)
903 {
904 return 0;
905 }
906 i += len;
907 if (i+1<size && data[i]==c && data[i+1]==c && i && data[i-1]!=' ' && data[i-1]!='\n')
908 {
909 if (c == '~') out+="<strike>";
910 else out+="<strong>";
911 processInline(data.substr(0,i));
912 if (c == '~') out+="</strike>";
913 else out+="</strong>";
914 AUTO_TRACE_EXIT("result={}",i+2);
915 return static_cast<int>(i+2);
916 }
917 i++;
918 }
919 return 0;
920}

References AUTO_TRACE, AUTO_TRACE_EXIT, findEmphasisChar(), out, processInline(), and Trace::trunc().

Referenced by processEmphasis(), and processEmphasis3().

◆ processEmphasis3()

int Markdown::Private::processEmphasis3 ( std::string_view data,
char c )

Parsing triple emphasis.

Finds the first closing tag, and delegates to the other emph

Definition at line 925 of file markdown.cpp.

926{
927 AUTO_TRACE("data='{}' c={}",Trace::trunc(data),c);
928 size_t i = 0;
929 const size_t size = data.size();
930
931 while (i<size)
932 {
933 size_t len = findEmphasisChar(data.substr(i), c, 3);
934 if (len==0)
935 {
936 return 0;
937 }
938 i+=len;
939
940 /* skip whitespace preceded symbols */
941 if (data[i]!=c || data[i-1]==' ' || data[i-1]=='\n')
942 {
943 continue;
944 }
945
946 if (i+2<size && data[i+1]==c && data[i+2]==c)
947 {
948 out+="<em><strong>";
949 processInline(data.substr(0,i));
950 out+="</strong></em>";
951 AUTO_TRACE_EXIT("result={}",i+3);
952 return static_cast<int>(i+3);
953 }
954 else if (i+1<size && data[i+1]==c)
955 {
956 // double symbol found, handing over to emph1
957 len = processEmphasis1(std::string_view(data.data()-2, size+2), c);
958 if (len==0)
959 {
960 return 0;
961 }
962 else
963 {
964 AUTO_TRACE_EXIT("result={}",len-2);
965 return static_cast<int>(len - 2);
966 }
967 }
968 else
969 {
970 // single symbol found, handing over to emph2
971 len = processEmphasis2(std::string_view(data.data()-1, size+1), c);
972 if (len==0)
973 {
974 return 0;
975 }
976 else
977 {
978 AUTO_TRACE_EXIT("result={}",len-1);
979 return static_cast<int>(len - 1);
980 }
981 }
982 }
983 return 0;
984}

References AUTO_TRACE, AUTO_TRACE_EXIT, findEmphasisChar(), out, processEmphasis1(), processEmphasis2(), processInline(), and Trace::trunc().

Referenced by processEmphasis().

◆ processHtmlTag()

int Markdown::Private::processHtmlTag ( std::string_view data,
size_t offset )

Definition at line 1149 of file markdown.cpp.

1150{
1151 AUTO_TRACE("data='{}' offset={}",Trace::trunc(data),offset);
1152 return processHtmlTagWrite(data,offset,true);
1153}

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

Referenced by Markdown::fill_table().

◆ processHtmlTagWrite()

int Markdown::Private::processHtmlTagWrite ( std::string_view data,
size_t offset,
bool doWrite )

Process a HTML tag.

Note that

..

are treated specially, in the sense that all code inside is written unprocessed

Definition at line 1055 of file markdown.cpp.

1056{
1057 AUTO_TRACE("data='{}' offset={} doWrite={}",Trace::trunc(data),offset,doWrite);
1058 if (offset>0 && data.data()[-1]=='\\') { return 0; } // escaped <
1059
1060 const size_t size = data.size();
1061
1062 // find the end of the html tag
1063 size_t i=1;
1064 size_t l=0;
1065 // compute length of the tag name
1066 while (i < size && isIdChar(data[i]))
1067 {
1068 i++;
1069 l++;
1070 }
1071 DString tagName(data.substr(1,i-1));
1072 if (tagName.lower()=="pre") // found <pre> tag
1073 {
1074 bool insideStr=false;
1075 while (i+6<size)
1076 {
1077 char c=data[i];
1078 if (!insideStr && c=='<') // potential start of html tag
1079 {
1080 if (data[i+1]=='/' &&
1081 tolower(data[i+2])=='p' && tolower(data[i+3])=='r' &&
1082 tolower(data[i+4])=='e' && tolower(data[i+5])=='>')
1083 { // found </pre> tag, copy from start to end of tag
1084 if (doWrite) out+=data.substr(0,i+6);
1085 //printf("found <pre>..</pre> [%d..%d]\n",0,i+6);
1086 AUTO_TRACE_EXIT("result={}",i+6);
1087 return static_cast<int>(i+6);
1088 }
1089 }
1090 else if (insideStr && c=='"')
1091 {
1092 if (data[i-1]!='\\') insideStr=false;
1093 }
1094 else if (c=='"')
1095 {
1096 insideStr=true;
1097 }
1098 i++;
1099 }
1100 }
1101 else // some other html tag
1102 {
1103 if (l>0 && i<size)
1104 {
1105 if (data[i]=='/' && i+1<size && data[i+1]=='>') // <bla/>
1106 {
1107 //printf("Found htmlTag={%s}\n",qPrint(DString(data).left(i+2)));
1108 if (doWrite) out+=data.substr(0,i+2);
1109 AUTO_TRACE_EXIT("result={}",i+2);
1110 return static_cast<int>(i+2);
1111 }
1112 else if (data[i]=='>') // <bla>
1113 {
1114 //printf("Found htmlTag={%s}\n",qPrint(DString(data).left(i+1)));
1115 if (doWrite) out+=data.substr(0,i+1);
1116 AUTO_TRACE_EXIT("result={}",i+1);
1117 return static_cast<int>(i+1);
1118 }
1119 else if (data[i]==' ') // <bla attr=...
1120 {
1121 i++;
1122 bool insideAttr=false;
1123 while (i<size)
1124 {
1125 if (!insideAttr && data[i]=='"')
1126 {
1127 insideAttr=true;
1128 }
1129 else if (data[i]=='"' && data[i-1]!='\\')
1130 {
1131 insideAttr=false;
1132 }
1133 else if (!insideAttr && data[i]=='>') // found end of tag
1134 {
1135 //printf("Found htmlTag={%s}\n",qPrint(DString(data).left(i+1)));
1136 if (doWrite) out+=data.substr(0,i+1);
1137 AUTO_TRACE_EXIT("result={}",i+1);
1138 return static_cast<int>(i+1);
1139 }
1140 i++;
1141 }
1142 }
1143 }
1144 }
1145 AUTO_TRACE_EXIT("not a valid html tag");
1146 return 0;
1147}

References AUTO_TRACE, AUTO_TRACE_EXIT, isIdChar(), DString::lower(), out, and Trace::trunc().

Referenced by findEndOfLine(), and processHtmlTag().

◆ processInline()

void Markdown::Private::processInline ( std::string_view data)

Definition at line 1905 of file markdown.cpp.

1906{
1907 AUTO_TRACE("data='{}'",Trace::trunc(data));
1908 size_t i=0;
1909 size_t end=0;
1910 Action_t action;
1911 const size_t size = data.size();
1912 while (i<size)
1913 {
1914 // skip over characters that do not trigger a specific action
1915 while (end<size && ((action=Markdown::actions[static_cast<uint8_t>(data[end])])==nullptr)) end++;
1916 // and add them to the output
1917 out+=data.substr(i,end-i);
1918 if (end>=size) break;
1919 i=end;
1920 // do the action matching a special character at i
1921 int iend = action(*this,data.substr(i),i);
1922 if (iend<=0) // update end
1923 {
1924 end=i+1-iend;
1925 }
1926 else // skip until end
1927 {
1928 i+=iend;
1929 end=i;
1930 }
1931 }
1932}
static ActionTable_t actions
Definition markdown.h:48
std::function< int(Private &, std::string_view, size_t)> Action_t
Definition markdown.h:45

References Markdown::actions, AUTO_TRACE, end(), out, and Trace::trunc().

Referenced by processEmphasis1(), processEmphasis2(), processEmphasis3(), and processLink().

◆ processLink()

int Markdown::Private::processLink ( std::string_view data,
size_t offset )

Definition at line 1247 of file markdown.cpp.

1248{
1249 AUTO_TRACE("data='{}' offset={}",Trace::trunc(data),offset);
1250 const size_t size = data.size();
1251
1252 DString content;
1253 DString link;
1254 DString title;
1255 bool isImageLink = false;
1256 bool isImageInline = false;
1257 bool isToc = false;
1258 size_t i=1;
1259 if (data[0]=='!')
1260 {
1261 isImageLink = true;
1262 if (size<2 || data[1]!='[')
1263 {
1264 return 0;
1265 }
1266
1267 // if there is non-whitespace before the ![ within the scope of two new lines, the image
1268 // is considered inlined, i.e. the image is not preceded by an empty line
1269 int numNLsNeeded=2;
1270 int pos = -1;
1271 while (pos>=-static_cast<int>(offset) && numNLsNeeded>0)
1272 {
1273 if (data.data()[pos]=='\n') numNLsNeeded--;
1274 else if (data.data()[pos]!=' ') // found non-whitespace, stop searching
1275 {
1276 isImageInline=true;
1277 break;
1278 }
1279 pos--;
1280 }
1281 // skip '!['
1282 i++;
1283 }
1284 size_t contentStart=i;
1285 int level=1;
1286 int nlTotal=0;
1287 int nl=0;
1288 // find the matching ]
1289 while (i<size)
1290 {
1291 if (data[i-1]=='\\') // skip escaped characters
1292 {
1293 }
1294 else if (data[i]=='[')
1295 {
1296 level++;
1297 }
1298 else if (data[i]==']')
1299 {
1300 level--;
1301 if (level<=0) break;
1302 }
1303 else if (data[i]=='\n')
1304 {
1305 nl++;
1306 if (nl>1) { return 0; } // only allow one newline in the content
1307 }
1308 i++;
1309 }
1310 nlTotal += nl;
1311 nl = 0;
1312 if (i>=size) return 0; // premature end of comment -> no link
1313 size_t contentEnd=i;
1314 content = data.substr(contentStart,contentEnd-contentStart);
1315 //printf("processLink: content={%s}\n",qPrint(content));
1316 if (!isImageLink && content.empty()) { return 0; } // no link text
1317 i++; // skip over ]
1318
1319 bool whiteSpace = false;
1320 // skip whitespace
1321 while (i<size && data[i]==' ') { whiteSpace = true; i++; }
1322 if (i<size && data[i]=='\n') // one newline allowed here
1323 {
1324 whiteSpace = true;
1325 i++;
1326 // skip more whitespace
1327 while (i<size && data[i]==' ') i++;
1328 }
1329 if (whiteSpace && i<size && (data[i]=='(' || data[i]=='[')) return 0;
1330
1331 bool explicitTitle=false;
1332 if (i<size && data[i]=='(') // inline link
1333 {
1334 i++;
1335 while (i<size && data[i]==' ') i++;
1336 bool uriFormat=false;
1337 if (i<size && data[i]=='<') { i++; uriFormat=true; }
1338 size_t linkStart=i;
1339 int braceCount=1;
1340 int nlConsec = 0;
1341 while (i<size && data[i]!='\'' && data[i]!='"' && braceCount>0)
1342 {
1343 if (data[i]=='\n') // unexpected EOL
1344 {
1345 nl++;
1346 nlConsec++;
1347 if (nlConsec>1) { return 0; }
1348 }
1349 else if (data[i]=='(')
1350 {
1351 braceCount++;
1352 nlConsec = 0;
1353 }
1354 else if (data[i]==')')
1355 {
1356 braceCount--;
1357 nlConsec = 0;
1358 }
1359 else if (data[i]!=' ')
1360 {
1361 nlConsec = 0;
1362 }
1363 if (braceCount>0)
1364 {
1365 i++;
1366 }
1367 }
1368 nlTotal += nl;
1369 nl = 0;
1370 if (i>=size || data[i]=='\n') { return 0; }
1371 link = data.substr(linkStart,i-linkStart);
1372 link = link.stripWhiteSpace();
1373 //printf("processLink: link={%s}\n",qPrint(link));
1374 if (link.empty()) { return 0; }
1375 if (uriFormat && link.at(link.length()-1)=='>') link=link.left(link.length()-1);
1376
1377 // optional title
1378 if (data[i]=='\'' || data[i]=='"')
1379 {
1380 char c = data[i];
1381 i++;
1382 size_t titleStart=i;
1383 nl=0;
1384 while (i<size)
1385 {
1386 if (data[i]=='\n')
1387 {
1388 if (nl>1) { return 0; }
1389 nl++;
1390 }
1391 else if (data[i]=='\\') // escaped char in string
1392 {
1393 i++;
1394 }
1395 else if (data[i]==c)
1396 {
1397 i++;
1398 break;
1399 }
1400 i++;
1401 }
1402 if (i>=size)
1403 {
1404 return 0;
1405 }
1406 size_t titleEnd = i-1;
1407 // search back for closing marker
1408 while (titleEnd>titleStart && data[titleEnd]==' ') titleEnd--;
1409 if (data[titleEnd]==c) // found it
1410 {
1411 title = data.substr(titleStart,titleEnd-titleStart);
1412 explicitTitle=true;
1413 while (i<size)
1414 {
1415 if (data[i]==' ')i++; // remove space after the closing quote and the closing bracket
1416 else if (data[i] == ')') break; // the end bracket
1417 else // illegal
1418 {
1419 return 0;
1420 }
1421 }
1422 }
1423 else
1424 {
1425 return 0;
1426 }
1427 }
1428 i++;
1429 }
1430 else if (i<size && data[i]=='[') // reference link
1431 {
1432 i++;
1433 size_t linkStart=i;
1434 nl=0;
1435 // find matching ]
1436 while (i<size && data[i]!=']')
1437 {
1438 if (data[i]=='\n')
1439 {
1440 nl++;
1441 if (nl>1) { return 0; }
1442 }
1443 i++;
1444 }
1445 if (i>=size) { return 0; }
1446 // extract link
1447 link = data.substr(linkStart,i-linkStart);
1448 //printf("processLink: link={%s}\n",qPrint(link));
1449 link = link.stripWhiteSpace();
1450 if (link.empty()) // shortcut link
1451 {
1452 link=content;
1453 }
1454 // lookup reference
1455 DString link_lower = link.lower();
1456 auto lr_it=linkRefs.find(link_lower.str());
1457 if (lr_it!=linkRefs.end()) // found it
1458 {
1459 link = lr_it->second.link;
1460 title = lr_it->second.title;
1461 //printf("processLink: ref: link={%s} title={%s}\n",qPrint(link),qPrint(title));
1462 }
1463 else // reference not found!
1464 {
1465 //printf("processLink: ref {%s} do not exist\n",link.qPrint(lower()));
1466 return 0;
1467 }
1468 i++;
1469 }
1470 else if (i<size && data[i]!=':' && !content.empty()) // minimal link ref notation [some id]
1471 {
1472 DString content_lower = content.lower();
1473 auto lr_it = linkRefs.find(content_lower.str());
1474 //printf("processLink: minimal link {%s} lr=%p",qPrint(content),lr);
1475 if (lr_it!=linkRefs.end()) // found it
1476 {
1477 link = lr_it->second.link;
1478 title = lr_it->second.title;
1479 explicitTitle=true;
1480 i=contentEnd;
1481 }
1482 else if (content=="TOC")
1483 {
1484 isToc=true;
1485 i=contentEnd;
1486 }
1487 else
1488 {
1489 return 0;
1490 }
1491 i++;
1492 }
1493 else
1494 {
1495 return 0;
1496 }
1497 nlTotal += nl;
1498
1499 // search for optional image attributes
1500 DString attributes;
1501 if (isImageLink)
1502 {
1503 size_t j = i;
1504 // skip over whitespace
1505 while (j<size && data[j]==' ') { j++; }
1506 if (j<size && data[j]=='{') // we have attributes
1507 {
1508 i = j;
1509 // skip over '{'
1510 i++;
1511 size_t attributesStart=i;
1512 nl=0;
1513 // find the matching '}'
1514 while (i<size)
1515 {
1516 if (data[i-1]=='\\') // skip escaped characters
1517 {
1518 }
1519 else if (data[i]=='{')
1520 {
1521 level++;
1522 }
1523 else if (data[i]=='}')
1524 {
1525 level--;
1526 if (level<=0) break;
1527 }
1528 else if (data[i]=='\n')
1529 {
1530 nl++;
1531 if (nl>1) { return 0; } // only allow one newline in the content
1532 }
1533 i++;
1534 }
1535 nlTotal += nl;
1536 if (i>=size) return 0; // premature end of comment -> no attributes
1537 size_t attributesEnd=i;
1538 attributes = data.substr(attributesStart,attributesEnd-attributesStart);
1539 i++; // skip over '}'
1540 }
1541 if (!isImageInline)
1542 {
1543 // if there is non-whitespace after the image within the scope of two new lines, the image
1544 // is considered inlined, i.e. the image is not followed by an empty line
1545 int numNLsNeeded=2;
1546 size_t pos = i;
1547 while (pos<size && numNLsNeeded>0)
1548 {
1549 if (data[pos]=='\n') numNLsNeeded--;
1550 else if (data[pos]!=' ') // found non-whitespace, stop searching
1551 {
1552 isImageInline=true;
1553 break;
1554 }
1555 pos++;
1556 }
1557 }
1558 }
1559
1560 if (isToc) // special case for [TOC]
1561 {
1562 int toc_level = Config_getInt(TOC_INCLUDE_HEADINGS);
1563 if (toc_level>=SectionType::MinLevel && toc_level<=SectionType::MaxLevel)
1564 {
1565 out+="@tableofcontents{html:";
1566 out+=DString().setNum(toc_level);
1567 out+="}";
1568 }
1569 }
1570 else if (isImageLink)
1571 {
1572 bool ambig = false;
1573 FileDef *fd=nullptr;
1574 if (link.find("@ref ")!=DString::npos || link.find("\\ref ")!=DString::npos ||
1575 (fd=Doxygen::imageNameLinkedMap->findFileDef(link,ambig)))
1576 // assume doxygen symbol link or local image link
1577 {
1578 // check if different handling is needed per format
1579 writeMarkdownImage("html", isImageInline, explicitTitle, title, content, link, attributes, fd);
1580 writeMarkdownImage("latex", isImageInline, explicitTitle, title, content, link, attributes, fd);
1581 writeMarkdownImage("rtf", isImageInline, explicitTitle, title, content, link, attributes, fd);
1582 writeMarkdownImage("docbook", isImageInline, explicitTitle, title, content, link, attributes, fd);
1583 writeMarkdownImage("xml", isImageInline, explicitTitle, title, content, link, attributes, fd);
1584 }
1585 else
1586 {
1587 out+="<img src=\"";
1588 out+=link;
1589 out+="\" alt=\"";
1590 out+=content;
1591 out+="\"";
1592 if (!title.empty())
1593 {
1594 out+=" title=\"";
1595 out+=substitute(title.simplifyWhiteSpace(),"\"","&quot;");
1596 out+="\"";
1597 }
1598 out+="/>";
1599 }
1600 }
1601 else
1602 {
1604 size_t lp=DString::npos;
1605 if ((lp = link.find("@ref "))!=DString::npos ||
1606 (lp = link.find("\\ref "))!=DString::npos ||
1607 (lang==SrcLangExt::Markdown && !isURL(link)))
1608 // assume doxygen symbol link
1609 {
1610 if (lp==DString::npos) // link to markdown page
1611 {
1612 out+="@ref \"";
1613 if (!(Portable::isAbsolutePath(link) || isURL(link)))
1614 {
1615 FileInfo forg(link.str());
1616 if (forg.exists() && forg.isReadable())
1617 {
1618 link = forg.absFilePath();
1619 }
1620 else if (!(forg.exists() && forg.isReadable()))
1621 {
1622 FileInfo fi(fileName.str());
1623 DString mdFile = fileName.left(fileName.length()-fi.fileName().length()) + link;
1624 FileInfo fmd(mdFile.str());
1625 if (fmd.exists() && fmd.isReadable())
1626 {
1627 link = fmd.absFilePath().data();
1628 }
1629 }
1630 }
1631 out+=link;
1632 out+="\"";
1633 }
1634 else
1635 {
1636 out+=link;
1637 }
1638 out+=" \"";
1639 if (explicitTitle && !title.empty())
1640 {
1641 out+=substitute(title,"\"","&quot;");
1642 }
1643 else
1644 {
1645 processInline(std::string_view(substitute(content,"\"","&quot;").str()));
1646 }
1647 out+="\"";
1648 }
1649 else if ((lp = link.find('#'))!=DString::npos ||
1650 (lp = link.find('/'))!=DString::npos ||
1651 (lp = link.find('.'))!=DString::npos)
1652 { // file/url link
1653 bool isRef = false;
1654 if (lp==0 || (lp>0 && !isURL(link) && Config_getEnum(MARKDOWN_ID_STYLE)==MARKDOWN_ID_STYLE_t::GITHUB))
1655 {
1656 out+="@ref \"";
1658 out+="\" \"";
1659 out+=substitute(content.simplifyWhiteSpace(),"\"","&quot;");
1660 out+="\"";
1661 isRef = true;
1662 }
1663 else
1664 {
1665 out+="<a href=\"";
1666 out+=link;
1667 out+="\"";
1668 for (int ii = 0; ii < nlTotal; ii++) out+="\n";
1669 if (!title.empty())
1670 {
1671 out+=" title=\"";
1672 out+=substitute(title.simplifyWhiteSpace(),"\"","&quot;");
1673 out+="\"";
1674 }
1675 out+=" ";
1677 out+=">";
1678 }
1679
1680 content = content.simplifyWhiteSpace();
1681 bool foundNameRef = false;
1682 if (!content.empty() && (content.at(0)=='#' || content.at(0)=='@'))
1683 {
1684 size_t endOfId=1;
1685 while (endOfId<content.length() && isId(content.at(endOfId))) endOfId++;
1686 DString user = content.mid(1,endOfId-1);
1687 if (!user.empty() && (content.at(0)=='#' || (!CommentScanner::isCommand(user) && Mappers::cmdMapper->map(user)==CommandType::UNKNOWN)))
1688 {
1689 // assume @name or #name instead of command
1690 out+='@';
1691 out+=content;
1692 foundNameRef = true;
1693 }
1694 }
1695 if (!isRef)
1696 {
1697 if (!foundNameRef)
1698 {
1699 processInline(std::string_view(content.str()));
1700 }
1701 out+="</a>";
1702 }
1703 }
1704 else // avoid link to e.g. F[x](y)
1705 {
1706 //printf("no link for '%s'\n",qPrint(link));
1707 return 0;
1708 }
1709 }
1710 AUTO_TRACE_EXIT("result={}",i);
1711 return static_cast<int>(i);
1712}
static std::string addPrefixIfNeeded(const std::string &anchor)
Definition anchor.cpp:49
static bool isCommand(const DString &cmdName)
DString & setNum(short n)
Definition dstring.h:552
DString mid(size_t index, size_t len=npos) const
Definition dstring.h:318
DString lower() const
Definition dstring.h:326
DString simplifyWhiteSpace() const
return a copy of this string with leading and trailing whitespace removed and multiple internal white...
Definition dstring.cpp:127
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
static FileNameLinkedMap * imageNameLinkedMap
Definition doxygen.h:98
FileDef * findFileDef(const DString &n, bool &ambig) const
Returns the file definition in fnMap that matches the file name n.
Definition filename.cpp:38
static constexpr int MinLevel
Definition section.h:32
bool isId(int c)
Returns true if c is a valid character for an identifier.
Definition dstring.h:895
const Mapper< CommandType > * cmdMapper
bool isAbsolutePath(const DString &fileName)
Definition portable.cpp:513
void writeMarkdownImage(std::string_view fmt, bool inline_img, bool explicitTitle, const DString &title, const DString &content, const DString &link, const DString &attributes, const FileDef *fd)
SrcLangExt
Definition types.h:207
bool isURL(const DString &url)
Checks whether the given url starts with a supported protocol.
Definition util.cpp:4646
DString externalLinkTarget(const bool parent)
Definition util.cpp:4491
SrcLangExt getLanguageFromFileName(const DString &fileName, SrcLangExt defLang)
Definition util.cpp:4168

References FileInfo::absFilePath(), AnchorGenerator::addPrefixIfNeeded(), DString::at(), AUTO_TRACE, AUTO_TRACE_EXIT, Mappers::cmdMapper, Config_getEnum, Config_getInt, DString::empty(), FileInfo::exists(), externalLinkTarget(), FileInfo::fileName(), fileName, DString::find(), FileNameLinkedMap::findFileDef(), getLanguageFromFileName(), Doxygen::imageNameLinkedMap, Portable::isAbsolutePath(), CommentScanner::isCommand(), isId(), FileInfo::isReadable(), isURL(), DString::left(), DString::length(), linkRefs, DString::lower(), SectionType::MaxLevel, DString::mid(), SectionType::MinLevel, DString::npos, out, processInline(), DString::setNum(), DString::simplifyWhiteSpace(), DString::str(), DString::stripWhiteSpace(), substitute(), DString::substr(), Trace::trunc(), UNKNOWN, and writeMarkdownImage().

Referenced by Markdown::fill_table().

◆ processNmdash()

int Markdown::Private::processNmdash ( std::string_view data,
size_t offset )

Process ndash and mdashes.

Definition at line 987 of file markdown.cpp.

988{
989 AUTO_TRACE("data='{}' offset={}",Trace::trunc(data),offset);
990 const size_t size = data.size();
991 // precondition: data[0]=='-'
992 size_t i=1;
993 int count=1;
994 if (i<size && data[i]=='-') // found --
995 {
996 count++;
997 i++;
998 }
999 if (i<size && data[i]=='-') // found ---
1000 {
1001 count++;
1002 i++;
1003 }
1004 if (i<size && data[i]=='-') // found ----
1005 {
1006 count++;
1007 }
1008 if (count>=2 && offset>=2 && literal_at(data.data()-2,"<!"))
1009 { AUTO_TRACE_EXIT("result={}",1-count); return 1-count; } // start HTML comment
1010 if (count==2 && size > 2 && data[2]=='>')
1011 { return 0; } // end HTML comment
1012 if (count==3 && size > 3 && data[3]=='>')
1013 { return 0; } // end HTML comment
1014 if (count==2 && (offset<8 || !literal_at(data.data()-8,"operator"))) // -- => ndash
1015 {
1016 out+="&ndash;";
1017 AUTO_TRACE_EXIT("result=2");
1018 return 2;
1019 }
1020 else if (count==3) // --- => ndash
1021 {
1022 out+="&mdash;";
1023 AUTO_TRACE_EXIT("result=3");
1024 return 3;
1025 }
1026 // not an ndash or mdash
1027 return 0;
1028}

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

Referenced by Markdown::fill_table().

◆ processQuotations()

DString Markdown::Private::processQuotations ( std::string_view data,
size_t refIndent )

Definition at line 3264 of file markdown.cpp.

3265{
3266 AUTO_TRACE("data='{}' refIndex='{}'",Trace::trunc(data),refIndent);
3267 out.clear();
3268 size_t i=0,end=0;
3269 size_t pi=std::string::npos;
3270 bool newBlock = false;
3271 bool insideList = false;
3272 size_t currentIndent = refIndent;
3273 size_t listIndent = refIndent;
3274 const size_t size = data.size();
3275 DString lang;
3276 while (i<size)
3277 {
3278 end = findEndOfLine(data,i);
3279 // line is now found at [i..end)
3280
3281 size_t lineIndent=0;
3282 while (lineIndent<end && data[i+lineIndent]==' ') lineIndent++;
3283 //printf("** lineIndent=%d line=(%s)\n",lineIndent,qPrint(DString(data+i).left(end-i)));
3284
3285 if (newBlock)
3286 {
3287 //printf("** end of block\n");
3288 if (insideList && lineIndent<currentIndent) // end of list
3289 {
3290 //printf("** end of list\n");
3291 currentIndent = refIndent;
3292 insideList = false;
3293 }
3294 newBlock = false;
3295 }
3296
3297 if ((listIndent=isListMarker(data.substr(i,end-i)))) // see if we need to increase the indent level
3298 {
3299 if (listIndent<currentIndent+4)
3300 {
3301 //printf("** start of list\n");
3302 insideList = true;
3303 currentIndent = listIndent;
3304 }
3305 }
3306 else if (isEndOfList(data.substr(i,end-i)))
3307 {
3308 //printf("** end of list\n");
3309 insideList = false;
3310 currentIndent = listIndent;
3311 }
3312 else if (isEmptyLine(data.substr(i,end-i)))
3313 {
3314 //printf("** new block\n");
3315 newBlock = true;
3316 }
3317 //printf("currentIndent=%d listIndent=%d refIndent=%d\n",currentIndent,listIndent,refIndent);
3318
3319 if (pi!=std::string::npos)
3320 {
3321 size_t blockStart=0, blockEnd=0, blockOffset=0;
3322 if (isFencedCodeBlock(data.substr(pi),currentIndent,lang,blockStart,blockEnd,blockOffset,fileName,lineNr))
3323 {
3324 auto addSpecialCommand = [&](const DString &startCmd,const DString &endCmd)
3325 {
3326 size_t cmdPos = pi+blockStart+1;
3327 DString pl = data.substr(cmdPos,blockEnd-blockStart-1);
3328 size_t ii = 0;
3329 int nl = 1;
3330 // check for absence of start command, either @start<cmd>, or \\start<cmd>
3331 while (ii<pl.length() && disspace(pl[ii]))
3332 {
3333 if (pl[ii]=='\n') nl++;
3334 ii++; // skip leading whitespace
3335 }
3336 bool addNewLines = false;
3337 if (ii+startCmd.length()>=pl.length() || // no room for start command
3338 (pl[ii]!='\\' && pl[ii]!='@') || // no @ or \ after whitespace
3339 dstrncmp(pl.data()+ii+1,startCmd.data(),startCmd.length())!=0) // no start command
3340 {
3341 // input: output:
3342 // ----------------------------------------------------
3343 // ```{plantuml} => @startuml
3344 // A->B A->B
3345 // ``` @enduml
3346 // ----------------------------------------------------
3347 pl = "@"+startCmd+"\n" + pl + "@"+endCmd;
3348 ii=0;
3349 addNewLines = false;
3350 }
3351 else // we have a @start... command inside the code block
3352 {
3353 // input: output:
3354 // ----------------------------------------------------
3355 // ```{plantuml} \n
3356 // \n
3357 // @startuml => @startuml
3358 // A->B A->B
3359 // @enduml @enduml
3360 // ``` \n
3361 // ----------------------------------------------------
3362 addNewLines = true;
3363 }
3364 if (addNewLines) for (int j=0;j<nl;j++) out+='\n';
3365 processSpecialCommand(pl.view().substr(ii),ii);
3366 if (addNewLines) out+='\n';
3367 };
3368
3369 if (PlantumlManager::isEnabled() && lang=="plantuml")
3370 {
3371 addSpecialCommand("startuml","enduml");
3372 }
3373 else if (Config_getBool(HAVE_DOT) && lang=="dot")
3374 {
3375 addSpecialCommand("dot","enddot");
3376 }
3377 else if (lang=="msc") // msc is built-in
3378 {
3379 addSpecialCommand("msc","endmsc");
3380 }
3381 else if (lang=="mermaid")
3382 {
3383 addSpecialCommand("mermaid","endmermaid");
3384 }
3385 else // normal code block
3386 {
3387 writeFencedCodeBlock(data.substr(pi),lang.view(),blockStart,blockEnd);
3388 }
3389 i=pi+blockOffset;
3390 pi=std::string::npos;
3391 end=i+1;
3392 continue;
3393 }
3394 else if (isBlockQuote(data.substr(pi,i-pi),currentIndent))
3395 {
3396 i = pi+writeBlockQuote(data.substr(pi));
3397 pi=std::string::npos;
3398 end=i+1;
3399 continue;
3400 }
3401 else
3402 {
3403 //printf("quote out={%s}\n",DString(data+pi).left(i-pi).data());
3404 out+=data.substr(pi,i-pi);
3405 }
3406 }
3407 pi=i;
3408 i=end;
3409 }
3410 if (pi!=std::string::npos && pi<size) // deal with the last line
3411 {
3412 if (isBlockQuote(data.substr(pi),currentIndent))
3413 {
3414 writeBlockQuote(data.substr(pi));
3415 }
3416 else
3417 {
3418 if (DString(data.substr(pi)).startsWith("```") || DString(data.substr(pi)).startsWith("~~~"))
3419 {
3420 warn(fileName, lineNr, "Ending inside a fenced code block. Maybe the end marker for the block is missing?");
3421 }
3422 out+=data.substr(pi);
3423 }
3424 }
3425
3426 //printf("Process quotations\n---- input ----\n%s\n---- output ----\n%s\n------------\n",
3427 // qPrint(s),prv->out.get());
3428
3429 return out;
3430}
static bool isEnabled()
Returns true if doxygen has been configured to run PlantUML, i.e.
Definition plantuml.cpp:246
bool disspace(char c)
Definition dstring.h:62
static bool isBlockQuote(std::string_view data, size_t indent)
returns true if this line starts a block quote
size_t writeBlockQuote(std::string_view data)
int processSpecialCommand(std::string_view data, size_t offset)

References AUTO_TRACE, Config_getBool, DString::data(), disspace(), dstrncmp(), end(), fileName, findEndOfLine(), isBlockQuote(), isEmptyLine(), PlantumlManager::isEnabled(), isEndOfList(), isFencedCodeBlock(), isListMarker(), DString::length(), lineNr, out, processSpecialCommand(), DString::startsWith(), DString::substr(), Trace::trunc(), DString::view(), warn, writeBlockQuote(), and writeFencedCodeBlock().

◆ processQuoted()

int Markdown::Private::processQuoted ( std::string_view data,
size_t offset )

Process quoted section "...", can contain one embedded newline.

Definition at line 1031 of file markdown.cpp.

1032{
1033 AUTO_TRACE("data='{}'",Trace::trunc(data));
1034 const size_t size = data.size();
1035 size_t i=1;
1036 int nl=0;
1037 while (i<size && data[i]!='"' && nl<2)
1038 {
1039 if (data[i]=='\n') nl++;
1040 i++;
1041 }
1042 if (i<size && data[i]=='"' && nl<2)
1043 {
1044 out+=data.substr(0,i+1);
1045 AUTO_TRACE_EXIT("result={}",i+2);
1046 return static_cast<int>(i+1);
1047 }
1048 // not a quoted section
1049 return 0;
1050}

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

Referenced by Markdown::fill_table().

◆ processSpecialCommand()

int Markdown::Private::processSpecialCommand ( std::string_view data,
size_t offset )

Definition at line 1842 of file markdown.cpp.

1843{
1844 AUTO_TRACE("{}",Trace::trunc(data));
1845 const size_t size = data.size();
1846 size_t i=1;
1847 DString endBlockName = isBlockCommand(data,offset);
1848 if (!endBlockName.empty())
1849 {
1850 AUTO_TRACE_ADD("endBlockName={}",endBlockName);
1851 size_t l = endBlockName.length();
1852 while (i+l<size)
1853 {
1854 if ((data[i]=='\\' || data[i]=='@') && // command
1855 data[i-1]!='\\' && data[i-1]!='@') // not escaped
1856 {
1857 if (dstrncmp(&data[i+1],endBlockName.data(),l)==0)
1858 {
1859 //printf("found end at %d\n",i);
1860 addStrEscapeUtf8Nbsp(data.substr(0,i+1+l));
1861 AUTO_TRACE_EXIT("result={}",i+1+l);
1862 return static_cast<int>(i+1+l);
1863 }
1864 }
1865 i++;
1866 }
1867 }
1868 size_t endPos = isSpecialCommand(data,offset);
1869 if (endPos>0)
1870 {
1871 out+=data.substr(0,endPos);
1872 return static_cast<int>(endPos);
1873 }
1874 if (size>1 && (data[0]=='\\' || data[0]=='@')) // escaped characters
1875 {
1876 char c=data[1];
1877 if (c=='[' || c==']' || c=='*' || c=='(' || c==')' || c=='`' || c=='_')
1878 {
1879 out+=data[1];
1880 AUTO_TRACE_EXIT("2");
1881 return 2;
1882 }
1883 else if (c=='\\' || c=='@')
1884 {
1885 out+=data.substr(0,2);
1886 AUTO_TRACE_EXIT("2");
1887 return 2;
1888 }
1889 else if (c=='-' && size>3 && data[2]=='-' && data[3]=='-') // \---
1890 {
1891 out+=data.substr(1,3);
1892 AUTO_TRACE_EXIT("2");
1893 return 4;
1894 }
1895 else if (c=='-' && size>2 && data[2]=='-') // \--
1896 {
1897 out+=data.substr(1,2);
1898 AUTO_TRACE_EXIT("3");
1899 return 3;
1900 }
1901 }
1902 return 0;
1903}
size_t isSpecialCommand(std::string_view data, size_t offset)
Definition markdown.cpp:465
void addStrEscapeUtf8Nbsp(std::string_view data)

References addStrEscapeUtf8Nbsp(), AUTO_TRACE, AUTO_TRACE_ADD, AUTO_TRACE_EXIT, DString::data(), dstrncmp(), DString::empty(), isBlockCommand(), isSpecialCommand(), DString::length(), out, and Trace::trunc().

Referenced by Markdown::fill_table(), and processQuotations().

◆ writeBlockQuote()

size_t Markdown::Private::writeBlockQuote ( std::string_view data)

Definition at line 2933 of file markdown.cpp.

2934{
2935 AUTO_TRACE("data='{}'",Trace::trunc(data));
2936 size_t i=0;
2937 int curLevel=0;
2938 size_t end=0;
2939 const size_t size = data.size();
2940 std::string startCmd;
2941 int isGitHubAlert = false;
2942 int isGitHubFirst = false;
2943 while (i<size)
2944 {
2945 // find end of this line
2946 end=i+1;
2947 while (end<=size && data[end-1]!='\n') end++;
2948 size_t j=i;
2949 int level=0;
2950 size_t indent=i;
2951 // compute the quoting level
2952 while (j<end && (data[j]==' ' || data[j]=='>'))
2953 {
2954 if (data[j]=='>') { level++; indent=j+1; }
2955 else if (j>0 && data[j-1]=='>') indent=j+1;
2956 j++;
2957 }
2958 if (indent>0 && j>0 && data[j-1]=='>' &&
2959 !(j==size || data[j]=='\n')) // disqualify last > if not followed by space
2960 {
2961 indent--;
2962 level--;
2963 j--;
2964 }
2965 AUTO_TRACE_ADD("indent={} i={} j={} end={} level={} line={}",indent,i,j,end,level,Trace::trunc(&data[i]));
2966 if (level==0 && j<end-1 && !isListMarker(data.substr(j)) && !isHRuler(data.substr(j)))
2967 {
2968 level = curLevel; // lazy
2969 }
2970 if (level==1)
2971 {
2972 DString txt = stripWhiteSpace(data.substr(indent,end-indent));
2973 auto it = g_quotationHeaderMap.find(txt.lower().str()); // TODO: in C++20 the std::string can be dropped
2974 if (it != g_quotationHeaderMap.end())
2975 {
2976 isGitHubAlert = true;
2977 isGitHubFirst = true;
2978 startCmd = it->second;
2979 }
2980 }
2981 if (level>curLevel) // quote level increased => add start markers
2982 {
2983 if (level!=1 || !isGitHubAlert) // normal block quote
2984 {
2985 for (int l=curLevel;l<level-1;l++)
2986 {
2987 out+="<blockquote>";
2988 }
2989 out += "<blockquote>&zwj;"; // empty blockquotes are also shown
2990 }
2991 else if (!startCmd.empty()) // GitHub style alert
2992 {
2993 out += startCmd + " ";
2994 }
2995 }
2996 else if (level<curLevel) // quote level decreased => add end markers
2997 {
2998 int decrLevel = curLevel;
2999 if (level==0 && isGitHubAlert)
3000 {
3001 decrLevel--;
3002 }
3003 for (int l=level;l<decrLevel;l++)
3004 {
3005 out += "</blockquote>\\ilinebr ";
3006 }
3007 }
3008 if (level==0)
3009 {
3010 curLevel=0;
3011 break; // end of quote block
3012 }
3013 // copy line without quotation marks
3014 if (curLevel!=0 || !isGitHubAlert)
3015 {
3016 std::string_view txt = data.substr(indent,end-indent);
3017 if (stripWhiteSpace(txt).empty() && !startCmd.empty())
3018 {
3019 if (!isGitHubFirst) out += "<br>";
3020 out += "<br>\n";
3021 }
3022 else
3023 {
3024 out += txt;
3025 }
3026 isGitHubFirst = false;
3027 }
3028 else // GitHub alert section
3029 {
3030 out+= "\n";
3031 }
3032 curLevel=level;
3033 // proceed with next line
3034 i=end;
3035 }
3036 // end of comment within blockquote => add end markers
3037 if (isGitHubAlert) // GitHub alert doesn't have a blockquote
3038 {
3039 curLevel--;
3040 }
3041 for (int l=0;l<curLevel;l++)
3042 {
3043 out+="</blockquote>";
3044 }
3045 AUTO_TRACE_EXIT("i={}",i);
3046 return i;
3047}
static const std::unordered_map< std::string, std::string > g_quotationHeaderMap
static bool isHRuler(std::string_view data)
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:75

References AUTO_TRACE, AUTO_TRACE_ADD, AUTO_TRACE_EXIT, end(), g_quotationHeaderMap, isHRuler(), isListMarker(), DString::lower(), out, DString::str(), stripWhiteSpace(), and Trace::trunc().

Referenced by processQuotations().

◆ writeCodeBlock()

size_t Markdown::Private::writeCodeBlock ( std::string_view data,
size_t refIndent )

Definition at line 3089 of file markdown.cpp.

3090{
3091 AUTO_TRACE("data='{}' refIndent={}",Trace::trunc(data),refIndent);
3092 const size_t size = data.size();
3093 size_t i=0;
3094 // no need for \ilinebr here as the previous line was empty and was skipped
3095 out+="@iverbatim\n";
3096 int emptyLines=0;
3097 std::string location;
3098 while (i<size)
3099 {
3100 // find end of this line
3101 size_t end=i+1;
3102 while (end<=size && data[end-1]!='\n') end++;
3103 size_t j=i;
3104 size_t indent=0;
3105 while (j < end && data[j] == ' ')
3106 {
3107 j++;
3108 indent++;
3109 }
3110 //printf("j=%d end=%d indent=%d refIndent=%d tabSize=%d data={%s}\n",
3111 // j,end,indent,refIndent,Config_getInt(TAB_SIZE),qPrint(DString(data+i).left(end-i-1)));
3112 if (j==end-1) // empty line
3113 {
3114 emptyLines++;
3115 i=end;
3116 }
3117 else if (indent>=refIndent+codeBlockIndent) // enough indent to continue the code block
3118 {
3119 while (emptyLines>0) // write skipped empty lines
3120 {
3121 // add empty line
3122 out+="\n";
3123 emptyLines--;
3124 }
3125 // add code line minus the indent
3126 size_t offset = i+refIndent+codeBlockIndent;
3127 std::string lineLoc;
3128 if (skipOverFileAndLineCommands(data,codeBlockIndent,offset,lineLoc))
3129 {
3130 location = lineLoc;
3131 }
3132 out+=data.substr(offset,end-offset);
3133 i=end;
3134 }
3135 else // end of code block
3136 {
3137 break;
3138 }
3139 }
3140 out+="@endiverbatim";
3141 if (!location.empty())
3142 {
3143 out+=location;
3144 }
3145 else
3146 {
3147 out+="\\ilinebr ";
3148 }
3149 while (emptyLines>0) // write skipped empty lines
3150 {
3151 // add empty line
3152 out+="\n";
3153 emptyLines--;
3154 }
3155 AUTO_TRACE_EXIT("i={}",i);
3156 return i;
3157}
bool skipOverFileAndLineCommands(std::string_view data, size_t indent, size_t &offset, std::string &location)

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

Referenced by processBlocks().

◆ writeFencedCodeBlock()

void Markdown::Private::writeFencedCodeBlock ( std::string_view data,
std::string_view lang,
size_t blockStart,
size_t blockEnd )

Definition at line 3241 of file markdown.cpp.

3243{
3244 AUTO_TRACE("data='{}' lang={} blockStart={} blockEnd={}",Trace::trunc(data),lang,blockStart,blockEnd);
3245 if (!lang.empty() && lang[0]=='.') lang=lang.substr(1);
3246 const size_t size=data.size();
3247 size_t i=0;
3248 while (i<size && (data[i]==' ' || data[i]=='\t'))
3249 {
3250 out+=data[i++];
3251 blockStart--;
3252 blockEnd--;
3253 }
3254 out+="@icode";
3255 if (!lang.empty())
3256 {
3257 out+="{"+lang+"}";
3258 }
3259 out+=" ";
3260 addStrEscapeUtf8Nbsp(data.substr(blockStart+i,blockEnd-blockStart));
3261 out+="@endicode ";
3262}

References addStrEscapeUtf8Nbsp(), AUTO_TRACE, out, and Trace::trunc().

Referenced by processBlocks(), and processQuotations().

◆ writeMarkdownImage()

void Markdown::Private::writeMarkdownImage ( std::string_view fmt,
bool inline_img,
bool explicitTitle,
const DString & title,
const DString & content,
const DString & link,
const DString & attributes,
const FileDef * fd )

Definition at line 1204 of file markdown.cpp.

1209{
1210 AUTO_TRACE("fmt={} inline_img={} explicitTitle={} title={} content={} link={} attrs={}",
1211 fmt,inline_img,explicitTitle,Trace::trunc(title),Trace::trunc(content),link,attrs);
1212 DString attributes = getFilteredImageAttributes(fmt, attrs);
1213 out+="@image";
1214 if (inline_img)
1215 {
1216 out+="{inline}";
1217 }
1218 out+=" ";
1219 out+=fmt;
1220 out+=" ";
1221 out+=link.mid(fd ? 0 : 5);
1222 if (!explicitTitle && !content.empty())
1223 {
1224 out+=" \"";
1225 out+=escapeDoubleQuotes(content);
1226 out+="\"";
1227 }
1228 else if ((content.empty() || explicitTitle) && !title.empty())
1229 {
1230 out+=" \"";
1231 out+=escapeDoubleQuotes(title);
1232 out+="\"";
1233 }
1234 else
1235 {
1236 out+=" ";// so the line break will not be part of the image name
1237 }
1238 if (!attributes.empty())
1239 {
1240 out+=" ";
1241 out+=attributes;
1242 out+=" ";
1243 }
1244 out+="\\ilinebr ";
1245}
static DString getFilteredImageAttributes(std::string_view fmt, const DString &attrs)
parse the image attributes and return attributes for given format
Definition markdown.cpp:348
static DString escapeDoubleQuotes(const DString &s)
Definition markdown.cpp:245

References AUTO_TRACE, DString::empty(), escapeDoubleQuotes(), getFilteredImageAttributes(), DString::mid(), out, and Trace::trunc().

Referenced by processLink().

◆ writeOneLineHeaderOrRuler()

void Markdown::Private::writeOneLineHeaderOrRuler ( std::string_view data)

Definition at line 2873 of file markdown.cpp.

2874{
2875 AUTO_TRACE("data='{}'",Trace::trunc(data));
2876 int level=0;
2877 DString header;
2878 DString id;
2879 if (isHRuler(data))
2880 {
2881 out+="<hr>\n";
2882 }
2883 else if ((level=isAtxHeader(data,header,id,true)))
2884 {
2885 DString hTag;
2886 if (!id.empty())
2887 {
2888 switch (level)
2889 {
2890 case SectionType::Section: out+="@section "; break;
2891 case SectionType::Subsection: out+="@subsection "; break;
2892 case SectionType::Subsubsection: out+="@subsubsection "; break;
2893 case SectionType::Paragraph: out+="@paragraph "; break;
2894 case SectionType::Subparagraph: out+="@subparagraph "; break;
2895 case SectionType::Subsubparagraph: out+="@subsubparagraph "; break;
2896 }
2897 out+=id;
2898 out+=" ";
2899 out+=header;
2900 out+="\n";
2901 }
2902 else
2903 {
2904 hTag.sprintf("h%d",level);
2905 out+="<"+hTag+">";
2906 out+=header;
2907 out+="</"+hTag+">\n";
2908 }
2909 }
2910 else if (data.size()>0) // nothing interesting -> just output the line
2911 {
2912 size_t tmpSize = data.size();
2913 if (data[data.size()-1] == '\n') tmpSize--;
2914 out+=data.substr(0,tmpSize);
2915
2916 if (hasLineBreak(data))
2917 {
2918 out+="\\ilinebr<br>";
2919 }
2920 if (tmpSize != data.size()) out+='\n';
2921 }
2922}
DString & sprintf(const char *format,...)
Definition dstring.cpp:34
static constexpr int Section
Definition section.h:33
static constexpr int Subsection
Definition section.h:34
static constexpr int Subsubsection
Definition section.h:35
static constexpr int Paragraph
Definition section.h:36
static constexpr int Subsubparagraph
Definition section.h:38
static constexpr int Subparagraph
Definition section.h:37
static bool hasLineBreak(std::string_view data)
int isAtxHeader(std::string_view data, DString &header, DString &id, bool allowAdjustLevel, bool *pIsIdGenerated=nullptr)

References AUTO_TRACE, hasLineBreak(), isAtxHeader(), isHRuler(), out, SectionType::Paragraph, SectionType::Section, DString::sprintf(), SectionType::Subparagraph, SectionType::Subsection, SectionType::Subsubparagraph, SectionType::Subsubsection, and Trace::trunc().

Referenced by processBlocks().

◆ writeTableBlock()

size_t Markdown::Private::writeTableBlock ( std::string_view data)

Definition at line 2654 of file markdown.cpp.

2655{
2656 AUTO_TRACE("data='{}'",Trace::trunc(data));
2657 const size_t size = data.size();
2658
2659 size_t columns=0, start=0, end=0;
2660 size_t i = findTableColumns(data,start,end,columns);
2661 size_t headerStart = start;
2662 size_t headerEnd = end;
2663
2664 // read cell alignments
2665 size_t cc = 0;
2666 size_t ret = findTableColumns(data.substr(i),start,end,cc);
2667 size_t k=0;
2668 std::vector<Alignment> columnAlignment(columns);
2669
2670 bool leftMarker=false, rightMarker=false, startFound=false;
2671 size_t j=start+i;
2672 while (j<=end+i)
2673 {
2674 if (!startFound)
2675 {
2676 if (data[j]==':') { leftMarker=true; startFound=true; }
2677 if (data[j]=='-') startFound=true;
2678 //printf(" data[%d]=%c startFound=%d\n",j,data[j],startFound);
2679 }
2680 if (data[j]=='-') rightMarker=false;
2681 else if (data[j]==':') rightMarker=true;
2682 if (j<=end+i && (data[j]=='|' && (j==0 || data[j-1]!='\\')))
2683 {
2684 if (k<columns)
2685 {
2686 columnAlignment[k] = markersToAlignment(leftMarker,rightMarker);
2687 //printf("column[%d] alignment=%d\n",k,columnAlignment[k]);
2688 leftMarker=false;
2689 rightMarker=false;
2690 startFound=false;
2691 }
2692 k++;
2693 }
2694 j++;
2695 }
2696 if (k<columns)
2697 {
2698 columnAlignment[k] = markersToAlignment(leftMarker,rightMarker);
2699 //printf("column[%d] alignment=%d\n",k,columnAlignment[k]);
2700 }
2701 // proceed to next line
2702 i+=ret;
2703
2704 // Store the table cell information by row then column. This
2705 // allows us to handle row spanning.
2706 std::vector<std::vector<TableCell> > tableContents;
2707
2708 size_t m = headerStart;
2709 std::vector<TableCell> headerContents(columns);
2710 for (k=0;k<columns;k++)
2711 {
2712 while (m<=headerEnd && (data[m]!='|' || (m>0 && data[m-1]=='\\')))
2713 {
2714 headerContents[k].cellText += data[m++];
2715 }
2716 m++;
2717 // do the column span test before stripping white space
2718 // || is spanning columns, | | is not
2719 headerContents[k].colSpan = headerContents[k].cellText.empty();
2720 headerContents[k].cellText = headerContents[k].cellText.stripWhiteSpace();
2721 }
2722 tableContents.push_back(headerContents);
2723
2724 // write table cells
2725 while (i<size)
2726 {
2727 ret = findTableColumns(data.substr(i),start,end,cc);
2728 if (cc!=columns) break; // end of table
2729
2730 j=start+i;
2731 k=0;
2732 std::vector<TableCell> rowContents(columns);
2733 while (j<=end+i)
2734 {
2735 if (j<=end+i && (data[j]=='|' && (j==0 || data[j-1]!='\\')))
2736 {
2737 // do the column span test before stripping white space
2738 // || is spanning columns, | | is not
2739 rowContents[k].colSpan = rowContents[k].cellText.empty();
2740 rowContents[k].cellText = rowContents[k].cellText.stripWhiteSpace();
2741 k++;
2742 } // if (j<=end+i && (data[j]=='|' && (j==0 || data[j-1]!='\\')))
2743 else
2744 {
2745 rowContents[k].cellText += data[j];
2746 } // else { if (j<=end+i && (data[j]=='|' && (j==0 || data[j-1]!='\\'))) }
2747 j++;
2748 } // while (j<=end+i)
2749 // do the column span test before stripping white space
2750 // || is spanning columns, | | is not
2751 rowContents[k].colSpan = rowContents[k].cellText.empty();
2752 rowContents[k].cellText = rowContents[k].cellText.stripWhiteSpace();
2753 tableContents.push_back(rowContents);
2754
2755 // proceed to next line
2756 i+=ret;
2757 }
2758
2759 out+="<table class=\"markdownTable\">";
2760 DString cellTag("th"), cellClass("class=\"markdownTableHead");
2761 for (size_t row = 0; row < tableContents.size(); row++)
2762 {
2763 if (row)
2764 {
2765 if (row % 2)
2766 {
2767 out+="\n<tr class=\"markdownTableRowOdd\">";
2768 }
2769 else
2770 {
2771 out+="\n<tr class=\"markdownTableRowEven\">";
2772 }
2773 }
2774 else
2775 {
2776 out+="\n <tr class=\"markdownTableHead\">";
2777 }
2778 for (size_t c = 0; c < columns; c++)
2779 {
2780 // save the cell text for use after column span computation
2781 DString cellText(tableContents[row][c].cellText);
2782
2783 // Row span handling. Spanning rows will contain a caret ('^').
2784 // If the current cell contains just a caret, this is part of an
2785 // earlier row's span and the cell should not be added to the
2786 // output.
2787 if (tableContents[row][c].cellText == "^")
2788 {
2789 continue;
2790 }
2791 if (tableContents[row][c].colSpan)
2792 {
2793 int cr = static_cast<int>(c);
2794 while ( cr >= 0 && tableContents[row][cr].colSpan)
2795 {
2796 cr--;
2797 };
2798 if (cr >= 0 && tableContents[row][cr].cellText == "^") continue;
2799 }
2800 size_t rowSpan = 1, spanRow = row+1;
2801 while ((spanRow < tableContents.size()) &&
2802 (tableContents[spanRow][c].cellText == "^"))
2803 {
2804 spanRow++;
2805 rowSpan++;
2806 }
2807
2808 out+=" <" + cellTag + " " + cellClass;
2809 // use appropriate alignment style
2810 switch (columnAlignment[c])
2811 {
2812 case Alignment::Left: out+="Left\""; break;
2813 case Alignment::Right: out+="Right\""; break;
2814 case Alignment::Center: out+="Center\""; break;
2815 case Alignment::None: out+="None\""; break;
2816 }
2817
2818 if (rowSpan > 1)
2819 {
2820 DString spanStr;
2821 spanStr.setNum(rowSpan);
2822 out+=" rowspan=\"" + spanStr + "\"";
2823 }
2824 // Column span handling, assumes that column spans will have
2825 // empty strings, which would indicate the sequence "||", used
2826 // to signify spanning columns.
2827 size_t colSpan = 1;
2828 while ((c+1 < columns) && tableContents[row][c+1].colSpan)
2829 {
2830 c++;
2831 colSpan++;
2832 }
2833 if (colSpan > 1)
2834 {
2835 DString spanStr;
2836 spanStr.setNum(colSpan);
2837 out+=" colspan=\"" + spanStr + "\"";
2838 }
2839 // need at least one space on either side of the cell text in
2840 // order for doxygen to do other formatting
2841 out+="> " + cellText + " \\ilinebr </" + cellTag + ">";
2842 }
2843 cellTag = "td";
2844 cellClass = "class=\"markdownTableBody";
2845 out+=" </tr>";
2846 }
2847 out+="</table>\n";
2848
2849 AUTO_TRACE_EXIT("i={}",i);
2850 return i;
2851}
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:327
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, Center, end(), findTableColumns(), Left, markersToAlignment(), None, out, Right, DString::setNum(), and Trace::trunc().

Referenced by processBlocks().

Member Data Documentation

◆ fileName

DString Markdown::Private::fileName

Definition at line 183 of file markdown.cpp.

Referenced by extractTitleId(), Private(), processBlocks(), processLink(), and processQuotations().

◆ indentLevel

int Markdown::Private::indentLevel =0

Definition at line 185 of file markdown.cpp.

Referenced by isAtxHeader(), isHeaderline(), and Private().

◆ lineNr

int Markdown::Private::lineNr = 0

Definition at line 184 of file markdown.cpp.

Referenced by extractTitleId(), Private(), processBlocks(), and processQuotations().

◆ linkRefs

std::unordered_map<std::string,LinkRef> Markdown::Private::linkRefs

Definition at line 182 of file markdown.cpp.

Referenced by processBlocks(), and processLink().

◆ out


The documentation for this struct was generated from the following file: