Skip to main content

The AnchorGenerator Class Reference

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

Declaration

class AnchorGenerator { ... }

Included Headers

#include <src/anchor.h>

Private Constructors Index

AnchorGenerator ()

Private Destructor Index

~AnchorGenerator ()

Public Member Functions Index

std::stringgenerate (const std::string &title)

generates an anchor for a section with title. More...

boolisGenerated (const std::string &anchor) const

Returns true iff anchor is one of the generated anchors. More...

intreserve (const std::string &anchor)

Reserves a non-generated anchor. More...

Private Member Attributes Index

std::unique_ptr< Private >p

Public Static Functions Index

static AnchorGenerator &instance ()

Returns the singleton instance. More...

static boollooksGenerated (const std::string &anchor)

Returns true if anchor is a potentially generated anchor. More...

static std::stringaddPrefixIfNeeded (const std::string &anchor)

Description

Singleton class used to generate anchors for Markdown headers.

Definition at line 25 of file anchor.h.

Private Constructors

AnchorGenerator()

AnchorGenerator::AnchorGenerator ()

Declaration at line 50 of file anchor.h, definition at line 34 of file anchor.cpp.

34AnchorGenerator::AnchorGenerator() : p(std::make_unique<Private>()) {}

Reference p.

Referenced by instance and ~AnchorGenerator.

Private Destructor

~AnchorGenerator()

AnchorGenerator::~AnchorGenerator ()

Definition at line 51 of file anchor.h.

Reference AnchorGenerator.

Public Member Functions

generate()

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

generates an anchor for a section with title.

Returns the anchor.

Declaration at line 34 of file anchor.h, definition at line 59 of file anchor.cpp.

59std::string AnchorGenerator::generate(const std::string &label)
60{
61 std::lock_guard lock(p->mutex);
62
63 std::string result;
64
65 auto createDoxygenStyleAnchor = [&]()
66 {
67 // overwrite result with the doxygen style anchor
68 result = prefix+std::to_string(p->anchorCount++);
69 };
70
71 auto createGitHubStyleAnchor = [&]()
72 {
73 result.clear();
74 size_t pos=0;
75 while (pos<label.length())
76 {
77 uint8_t bytes = getUTF8CharNumBytes(label[pos]);
78 std::string charStr = getUTF8CharAt(label,pos);
79 uint32_t cUnicode = getUnicodeForUTF8CharAt(label,pos);
80 char c = charStr[0];
81 if (qisspace(c) || c=='-')
82 {
83 result+='-';
84 }
85 else if (c!='_' && isUTF8PunctuationCharacter(cUnicode))
86 {
87 // skip punctuation characters
88 }
89 else // normal UTF8 character
90 {
91 result+=convertUTF8ToLower(charStr);
92 }
93 pos+=bytes;
94 }
95 //printf("label='%s' result='%s'\n",qPrint(label),qPrint(result));
96 if (result.empty()) // fallback use doxygen style anchor
97 {
98 createDoxygenStyleAnchor();
99 }
100 else
101 {
102 result = addPrefixIfNeeded(result);
103 int &count = p->idCount[result];
104 // Add end digits if an identical header already exists
105 if (count>0)
106 {
107 result+="-"+std::to_string(count);
108 }
109 count++;
110 }
111 };
112
113 switch (Config_getEnum(MARKDOWN_ID_STYLE))
114 {
115 case MARKDOWN_ID_STYLE_t::DOXYGEN:
116 createDoxygenStyleAnchor();
117 break;
118 case MARKDOWN_ID_STYLE_t::GITHUB:
119 createGitHubStyleAnchor();
120 break;
121 }
122
123 p->anchorsUsed.insert(result);
124
125 return result;
126}

References addPrefixIfNeeded, Config_getEnum, convertUTF8ToLower, getUnicodeForUTF8CharAt, getUTF8CharAt, getUTF8CharNumBytes, isUTF8PunctuationCharacter, p, prefix and qisspace.

Referenced by Markdown::Private::extractTitleId and MarkdownOutlineParser::parseInput.

isGenerated()

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

Returns true iff anchor is one of the generated anchors.

Declaration at line 37 of file anchor.h, definition at line 128 of file anchor.cpp.

128bool AnchorGenerator::isGenerated(const std::string &anchor) const
129{
130 std::lock_guard lock(p->mutex);
131 return p->anchorsUsed.find(anchor)!=p->anchorsUsed.end();
132}

Reference p.

reserve()

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

Reserves a non-generated anchor.

Declaration at line 40 of file anchor.h, definition at line 134 of file anchor.cpp.

134int AnchorGenerator::reserve(const std::string &anchor)
135{
136 std::lock_guard lock(p->mutex);
137 return p->idCount[anchor]++;
138}

Reference p.

Private Member Attributes

p

std::unique_ptr<Private> AnchorGenerator::p

Definition at line 54 of file anchor.h.

54 std::unique_ptr<Private> p;

Referenced by AnchorGenerator, generate, isGenerated and reserve.

Public Static Functions

addPrefixIfNeeded()

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

Declaration at line 47 of file anchor.h, definition at line 46 of file anchor.cpp.

46std::string AnchorGenerator::addPrefixIfNeeded(const std::string &anchor)
47{
48 if (Config_getEnum(MARKDOWN_ID_STYLE)==MARKDOWN_ID_STYLE_t::GITHUB &&
49 (anchor.empty() || anchor.front() == '-' || std::isdigit(anchor.front())))
50 {
51 return prefix+anchor;
52 }
53 else
54 {
55 return anchor;
56 }
57}

References Config_getEnum and prefix.

Referenced by generate and Markdown::Private::processLink.

instance()

AnchorGenerator & AnchorGenerator::instance ()
static

Returns the singleton instance.

Declaration at line 29 of file anchor.h, definition at line 38 of file anchor.cpp.

39{
40 static AnchorGenerator am;
41 return am;
42}

Reference AnchorGenerator.

Referenced by Markdown::Private::extractTitleId, DocRoot::parse, DocSection::parse, MarkdownOutlineParser::parseInput and 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.

Declaration at line 45 of file anchor.h, definition at line 140 of file anchor.cpp.

140bool AnchorGenerator::looksGenerated(const std::string &anchor)
141{
142 return Config_getEnum(MARKDOWN_ID_STYLE)==MARKDOWN_ID_STYLE_t::DOXYGEN &&
143 QCString(anchor).startsWith("autotoc_md");
144}

References Config_getEnum and QCString::startsWith.

Referenced by anonymous{tagreader.cpp}::TagFileParser::endDocAnchor.


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


Generated via doxygen2docusaurus by Doxygen 1.14.0.