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

Singleton class used to generate anchors for Markdown headers. More...

#include <src/anchor.h>

Classes

struct  Private

Public Member Functions

std::string generate (const std::string &title)
 generates an anchor for a section with title.
bool isGenerated (const std::string &anchor) const
 Returns true iff anchor is one of the generated anchors.
int reserve (const std::string &anchor)
 Reserves a non-generated anchor.

Static Public Member Functions

static AnchorGeneratorinstance ()
 Returns the singleton instance.
static bool looksGenerated (const std::string &anchor)
 Returns true if anchor is a potentially generated anchor.
static std::string addPrefixIfNeeded (const std::string &anchor)

Private Member Functions

 AnchorGenerator ()
 ~AnchorGenerator ()

Private Attributes

std::unique_ptr< Privatep

Detailed Description

Singleton class used to generate anchors for Markdown headers.

Definition at line 25 of file anchor.h.

Constructor & Destructor Documentation

◆ AnchorGenerator()

AnchorGenerator::AnchorGenerator ( )
private

Definition at line 37 of file anchor.cpp.

37: p(std::make_unique<Private>()) {}
std::unique_ptr< Private > p
Definition anchor.h:54

References p.

Referenced by instance(), and ~AnchorGenerator().

◆ ~AnchorGenerator()

AnchorGenerator::~AnchorGenerator ( )
privatedefault

References AnchorGenerator().

Member Function Documentation

◆ addPrefixIfNeeded()

std::string AnchorGenerator::addPrefixIfNeeded ( const std::string & anchor)
static

Definition at line 49 of file anchor.cpp.

50{
51 return (Config_getEnum(MARKDOWN_ID_STYLE) == MARKDOWN_ID_STYLE_t::GITHUB &&
52 (anchor.empty() || anchor.front() == '-' || std::isdigit(anchor.front())))
53 ? prefix + anchor : anchor;
54}
constexpr auto prefix
Definition anchor.cpp:47
#define Config_getEnum(name)
Definition config.h:35

References Config_getEnum, and prefix.

Referenced by generate(), and Markdown::Private::processLink().

◆ generate()

std::string AnchorGenerator::generate ( const std::string & title)

generates an anchor for a section with title.

Returns the anchor.

Definition at line 56 of file anchor.cpp.

57{
58 std::lock_guard lock(p->mutex);
59
60 std::string result;
61
62 auto createDoxygenStyleAnchor = [&]()
63 {
64 // overwrite result with the doxygen style anchor
65 result = prefix+std::to_string(p->anchorCount++);
66 };
67
68 auto createGitHubStyleAnchor = [&]()
69 {
70 result.clear();
71 size_t pos=0;
72 while (pos<label.length())
73 {
74 uint8_t bytes = getUTF8CharNumBytes(label[pos]);
75 std::string charStr = getUTF8CharAt(label,pos);
76 uint32_t cUnicode = getUnicodeForUTF8CharAt(label,pos);
77 char c = charStr[0];
78 if (disspace(c) || c=='-')
79 {
80 result+='-';
81 }
82 else if (c!='_' && isUTF8PunctuationCharacter(cUnicode))
83 {
84 // skip punctuation characters
85 }
86 else // normal UTF8 character
87 {
88 result+=convertUTF8ToLower(charStr);
89 }
90 pos+=bytes;
91 }
92 //printf("label='%s' result='%s'\n",qPrint(label),qPrint(result));
93 if (result.empty()) // fallback use doxygen style anchor
94 {
95 createDoxygenStyleAnchor();
96 }
97 else
98 {
99 result = addPrefixIfNeeded(result);
100 int &count = p->idCount[result];
101 // Add end digits if an identical header already exists
102 if (count>0)
103 {
104 result+="-"+std::to_string(count);
105 }
106 count++;
107 }
108 };
109
110 switch (Config_getEnum(MARKDOWN_ID_STYLE))
111 {
112 case MARKDOWN_ID_STYLE_t::DOXYGEN:
113 createDoxygenStyleAnchor();
114 break;
115 case MARKDOWN_ID_STYLE_t::GITHUB:
116 createGitHubStyleAnchor();
117 break;
118 }
119
120 p->anchorsUsed.insert(result);
121
122 return result;
123}
static std::string addPrefixIfNeeded(const std::string &anchor)
Definition anchor.cpp:49
bool disspace(char c)
Definition dstring.h:62
bool isUTF8PunctuationCharacter(uint32_t unicode)
Check if the given Unicode character represents a punctuation character.
Definition utf8.cpp:238
uint32_t getUnicodeForUTF8CharAt(const std::string &input, size_t pos)
Returns the 32bit Unicode value matching character at byte position pos in the UTF8 encoded input.
Definition utf8.cpp:139
std::string convertUTF8ToLower(const std::string &input)
Converts the input string into a lower case version, also taking into account non-ASCII characters th...
Definition utf8.cpp:191
uint8_t getUTF8CharNumBytes(char c)
Returns the number of bytes making up a single UTF8 character given the first byte in the sequence.
Definition utf8.cpp:27
std::string getUTF8CharAt(const std::string &input, size_t pos)
Returns the UTF8 character found at byte position pos in the input string.
Definition utf8.cpp:131

References addPrefixIfNeeded(), Config_getEnum, convertUTF8ToLower(), disspace(), getUnicodeForUTF8CharAt(), getUTF8CharAt(), getUTF8CharNumBytes(), isUTF8PunctuationCharacter(), p, and prefix.

Referenced by Markdown::Private::extractTitleId(), and MarkdownOutlineParser::parseInput().

◆ instance()

AnchorGenerator & AnchorGenerator::instance ( )
static

Returns the singleton instance.

Definition at line 41 of file anchor.cpp.

42{
43 static AnchorGenerator am;
44 return am;
45}

References AnchorGenerator().

Referenced by Markdown::Private::extractTitleId(), DocRoot::parse(), DocSection::parse(), MarkdownOutlineParser::parseInput(), and DefinitionImpl::writeDocAnchorsToTagFile().

◆ isGenerated()

bool AnchorGenerator::isGenerated ( const std::string & anchor) const

Returns true iff anchor is one of the generated anchors.

Definition at line 125 of file anchor.cpp.

126{
127 std::lock_guard lock(p->mutex);
128 return p->anchorsUsed.find(anchor)!=p->anchorsUsed.end();
129}

References p.

Referenced by DefinitionImpl::writeDocAnchorsToTagFile().

◆ looksGenerated()

bool AnchorGenerator::looksGenerated ( const std::string & anchor)
static

Returns true if anchor is a potentially generated anchor.

Note this is a much weaker check than isGenerated() and may not always work.

Definition at line 137 of file anchor.cpp.

138{
139 return Config_getEnum(MARKDOWN_ID_STYLE)==MARKDOWN_ID_STYLE_t::DOXYGEN &&
140 DString(anchor).startsWith(prefix);
141}

References Config_getEnum, prefix, and DString::startsWith().

Referenced by anonymous_namespace{tagreader.cpp}::TagFileParser::endDocAnchor().

◆ reserve()

int AnchorGenerator::reserve ( const std::string & anchor)

Reserves a non-generated anchor.

Definition at line 131 of file anchor.cpp.

132{
133 std::lock_guard lock(p->mutex);
134 return p->idCount[anchor]++;
135}

References p.

Member Data Documentation

◆ p

std::unique_ptr<Private> AnchorGenerator::p
private

Definition at line 54 of file anchor.h.

Referenced by AnchorGenerator(), generate(), isGenerated(), and reserve().


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