Doxygen
Loading...
Searching...
No Matches
Markdown Class Reference

Helper class to process markdown formatted text. More...

#include <src/markdown.h>

Classes

struct  Private

Public Member Functions

 Markdown (const QCString &fileName, int lineNr, int indentLevel=0)
 ~Markdown ()
QCString process (const QCString &input, int &startNewlines, bool fromParseInput=false)
QCString extractPageTitle (QCString &docs, QCString &id, int &prepend, bool &isIdGenerated)
void setIndentLevel (int level)

Private Types

using Action_t = std::function<int(Private&,std::string_view,size_t)>
using ActionTable_t = std::array<Action_t,256>

Static Private Member Functions

static ActionTable_t fill_table ()

Private Attributes

std::unique_ptr< Privateprv

Static Private Attributes

static ActionTable_t actions = Markdown::fill_table()

Detailed Description

Helper class to process markdown formatted text.

Definition at line 31 of file markdown.h.

Member Typedef Documentation

◆ Action_t

using Markdown::Action_t = std::function<int(Private&,std::string_view,size_t)>
private

Definition at line 44 of file markdown.h.

◆ ActionTable_t

using Markdown::ActionTable_t = std::array<Action_t,256>
private

Definition at line 45 of file markdown.h.

Constructor & Destructor Documentation

◆ Markdown()

Markdown::Markdown ( const QCString & fileName,
int lineNr,
int indentLevel = 0 )

Definition at line 203 of file markdown.cpp.

204 : prv(std::make_unique<Private>(fileName,lineNr,indentLevel))
205{
206 using namespace std::placeholders;
207 (void)lineNr; // not used yet
208}
std::unique_ptr< Private > prv
Definition markdown.h:43

References prv.

Referenced by ~Markdown().

◆ ~Markdown()

Markdown::~Markdown ( )
default

References Markdown().

Member Function Documentation

◆ extractPageTitle()

QCString Markdown::extractPageTitle ( QCString & docs,
QCString & id,
int & prepend,
bool & isIdGenerated )

Definition at line 3685 of file markdown.cpp.

3686{
3687 AUTO_TRACE("docs={} prepend={}",Trace::trunc(docs),id,prepend);
3688 // first first non-empty line
3689 prepend = 0;
3690 QCString title;
3691 size_t i=0;
3692 QCString docs_org(docs);
3693 std::string_view data(docs_org.str());
3694 const size_t size = data.size();
3695 docs.clear();
3696 while (i<size && (data[i]==' ' || data[i]=='\n'))
3697 {
3698 if (data[i]=='\n') prepend++;
3699 i++;
3700 }
3701 if (i>=size) { return QCString(); }
3702 size_t end1=i+1;
3703 while (end1<size && data[end1-1]!='\n') end1++;
3704 //printf("i=%d end1=%d size=%d line='%s'\n",i,end1,size,docs.mid(i,end1-i).data());
3705 // first line from i..end1
3706 if (end1<size)
3707 {
3708 // second line form end1..end2
3709 size_t end2=end1+1;
3710 while (end2<size && data[end2-1]!='\n') end2++;
3711 if (prv->isHeaderline(data.substr(end1),FALSE))
3712 {
3713 title = data.substr(i,end1-i-1);
3714 docs+="\n\n"+docs_org.mid(end2);
3715 id = prv->extractTitleId(title, 0, &isIdGenerated);
3716 //printf("extractPageTitle(title='%s' docs='%s' id='%s')\n",title.data(),docs.data(),id.data());
3717 AUTO_TRACE_EXIT("result={} id={} isIdGenerated={}",Trace::trunc(title),id,isIdGenerated);
3718 return title;
3719 }
3720 }
3721 if (i<end1 && prv->isAtxHeader(data.substr(i,end1-i),title,id,FALSE,&isIdGenerated)>0)
3722 {
3723 docs+="\n";
3724 docs+=docs_org.mid(end1);
3725 }
3726 else
3727 {
3728 docs=docs_org;
3729 id = prv->extractTitleId(title, 0, &isIdGenerated);
3730 }
3731 AUTO_TRACE_EXIT("result={} id={} isIdGenerated={}",Trace::trunc(title),id,isIdGenerated);
3732 return title;
3733}
QCString mid(size_t index, size_t len=static_cast< size_t >(-1)) const
Definition qcstring.h:241
void clear()
Definition qcstring.h:182
#define AUTO_TRACE(...)
Definition docnode.cpp:48
#define AUTO_TRACE_EXIT(...)
Definition docnode.cpp:50
QCString trunc(const QCString &s, size_t numChars=15)
Definition trace.h:56
#define FALSE
Definition qcstring.h:34

References AUTO_TRACE, AUTO_TRACE_EXIT, QCString::clear(), FALSE, QCString::mid(), prv, QCString::str(), and Trace::trunc().

Referenced by MarkdownOutlineParser::parseInput().

◆ fill_table()

Markdown::ActionTable_t Markdown::fill_table ( )
staticprivate

Definition at line 184 of file markdown.cpp.

185{
187 a[static_cast<unsigned int>('_')] = [](Markdown::Private &obj,std::string_view data,size_t offset) { return obj.processEmphasis (data,offset); };
188 a[static_cast<unsigned int>('*')] = [](Markdown::Private &obj,std::string_view data,size_t offset) { return obj.processEmphasis (data,offset); };
189 a[static_cast<unsigned int>('~')] = [](Markdown::Private &obj,std::string_view data,size_t offset) { return obj.processEmphasis (data,offset); };
190 a[static_cast<unsigned int>('`')] = [](Markdown::Private &obj,std::string_view data,size_t offset) { return obj.processCodeSpan (data,offset); };
191 a[static_cast<unsigned int>('\\')]= [](Markdown::Private &obj,std::string_view data,size_t offset) { return obj.processSpecialCommand(data,offset); };
192 a[static_cast<unsigned int>('@')] = [](Markdown::Private &obj,std::string_view data,size_t offset) { return obj.processSpecialCommand(data,offset); };
193 a[static_cast<unsigned int>('[')] = [](Markdown::Private &obj,std::string_view data,size_t offset) { return obj.processLink (data,offset); };
194 a[static_cast<unsigned int>('!')] = [](Markdown::Private &obj,std::string_view data,size_t offset) { return obj.processLink (data,offset); };
195 a[static_cast<unsigned int>('<')] = [](Markdown::Private &obj,std::string_view data,size_t offset) { return obj.processHtmlTag (data,offset); };
196 a[static_cast<unsigned int>('-')] = [](Markdown::Private &obj,std::string_view data,size_t offset) { return obj.processNmdash (data,offset); };
197 a[static_cast<unsigned int>('"')] = [](Markdown::Private &obj,std::string_view data,size_t offset) { return obj.processQuoted (data,offset); };
198 return a;
199}
std::array< Action_t, 256 > ActionTable_t
Definition markdown.h:45
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 processHtmlTag(std::string_view data, size_t offset)
int processEmphasis(std::string_view data, size_t offset)
int processLink(std::string_view data, size_t offset)
int processNmdash(std::string_view data, size_t offset)
Process ndash and mdashes.
Definition markdown.cpp:981

References Markdown::Private::processCodeSpan(), Markdown::Private::processEmphasis(), Markdown::Private::processHtmlTag(), Markdown::Private::processLink(), Markdown::Private::processNmdash(), Markdown::Private::processQuoted(), and Markdown::Private::processSpecialCommand().

◆ process()

QCString Markdown::process ( const QCString & input,
int & startNewlines,
bool fromParseInput = false )

Definition at line 3738 of file markdown.cpp.

3739{
3740 if (input.isEmpty()) return input;
3741 size_t refIndent=0;
3742
3743 // for replace tabs by spaces
3744 QCString s = input;
3745 if (s.at(s.length()-1)!='\n') s += "\n"; // see PR #6766
3746 s = detab(s,refIndent);
3747 //printf("======== DeTab =========\n---- output -----\n%s\n---------\n",qPrint(s));
3748
3749 // then process quotation blocks (as these may contain other blocks)
3750 s = prv->processQuotations(s.view(),refIndent);
3751 //printf("======== Quotations =========\n---- output -----\n%s\n---------\n",qPrint(s));
3752
3753 // then process block items (headers, rules, and code blocks, references)
3754 s = prv->processBlocks(s.view(),refIndent);
3755 //printf("======== Blocks =========\n---- output -----\n%s\n---------\n",qPrint(s));
3756
3757 // finally process the inline markup (links, emphasis and code spans)
3758 prv->out.clear();
3759 prv->out.reserve(s.length());
3760 prv->processInline(s.view());
3761 if (fromParseInput)
3762 {
3763 Debug::print(Debug::Markdown,0,"---- output -----\n{}\n=========\n",qPrint(prv->out));
3764 }
3765 else
3766 {
3767 Debug::print(Debug::Markdown,0,"======== Markdown =========\n---- input ------- \n{}\n---- output -----\n{}\n=========\n",input,prv->out);
3768 }
3769
3770 // post processing
3771 QCString result = substitute(prv->out,g_doxy_nbsp,"&nbsp;");
3772 const char *p = result.data();
3773 if (p)
3774 {
3775 while (*p==' ') p++; // skip over spaces
3776 while (*p=='\n') {startNewlines++;p++;}; // skip over newlines
3777 if (literal_at(p,"<br>")) p+=4; // skip over <br>
3778 }
3779 if (p>result.data())
3780 {
3781 // strip part of the input
3782 result = result.mid(static_cast<int>(p-result.data()));
3783 }
3784 return result;
3785}
@ Markdown
Definition debug.h:37
static void print(DebugMask mask, int prio, fmt::format_string< Args... > fmt, Args &&... args)
Definition debug.h:77
size_t length() const
Returns the length of the string, not counting the 0-terminator.
Definition qcstring.h:166
char & at(size_t i)
Returns a reference to the character at index i.
Definition qcstring.h:593
bool isEmpty() const
Returns TRUE iff the string is empty.
Definition qcstring.h:163
const char * data() const
Returns a pointer to the contents of the string in the form of a 0-terminated C string.
Definition qcstring.h:172
std::string_view view() const
Definition qcstring.h:174
static const char * g_doxy_nbsp
Definition markdown.cpp:220
QCString substitute(const QCString &s, const QCString &src, const QCString &dst)
substitute all occurrences of src in s by dst
Definition qcstring.cpp:571
const char * qPrint(const char *s)
Definition qcstring.h:687
bool literal_at(const char *data, const char(&str)[N])
returns TRUE iff data points to a substring that matches string literal str
Definition stringutil.h:98
QCString detab(const QCString &s, size_t &refIndent)
Definition util.cpp:6738

References QCString::at(), QCString::data(), detab(), g_doxy_nbsp, QCString::isEmpty(), QCString::length(), literal_at(), Debug::Markdown, QCString::mid(), Debug::print(), prv, qPrint(), substitute(), and QCString::view().

Referenced by generateHtmlForComment(), handleCommentBlock(), handleCommentBlock(), handleCommentBlock(), VHDLOutlineParser::handleCommentBlock(), handleParametersCommentBlocks(), and MarkdownOutlineParser::parseInput().

◆ setIndentLevel()

void Markdown::setIndentLevel ( int level)

Definition at line 212 of file markdown.cpp.

212{ prv->indentLevel = level; }

References prv.

Referenced by MarkdownOutlineParser::parseInput().

Member Data Documentation

◆ actions

Markdown::ActionTable_t Markdown::actions = Markdown::fill_table()
staticprivate

Definition at line 47 of file markdown.h.

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

◆ prv

std::unique_ptr<Private> Markdown::prv
private

Definition at line 43 of file markdown.h.

Referenced by extractPageTitle(), Markdown(), process(), and setIndentLevel().


The documentation for this class was generated from the following files: