Doxygen
Toggle main menu visibility
Loading...
Searching...
No Matches
anchor.cpp
Go to the documentation of this file.
1
/******************************************************************************
2
*
3
* Copyright (C) 1997-2023 by Dimitri van Heesch.
4
*
5
* Permission to use, copy, modify, and distribute this software and its
6
* documentation under the terms of the GNU General Public License is hereby
7
* granted. No representations are made about the suitability of this software
8
* for any purpose. It is provided "as is" without express or implied warranty.
9
* See the GNU General Public License for more details.
10
*
11
* Documents produced by Doxygen are derivative works derived from the
12
* input used in their production; they are not affected by this license.
13
*
14
*/
15
16
// own include
17
#include "
anchor.h
"
18
19
// standard includes
20
#include <algorithm>
21
#include <mutex>
22
#include <string>
23
#include <unordered_map>
24
25
// other includes
26
#include "
config.h
"
27
#include "
utf8.h
"
28
29
struct
AnchorGenerator::Private
30
{
31
StringUnorderedSet
anchorsUsed
;
32
int
anchorCount
=0;
33
std::mutex
mutex
;
34
std::unordered_map<std::string,int>
idCount
;
35
};
36
37
AnchorGenerator::AnchorGenerator
() :
p
(
std
::make_unique<
Private
>()) {}
38
39
AnchorGenerator::~AnchorGenerator
() =
default
;
40
41
AnchorGenerator
&
AnchorGenerator::instance
()
42
{
43
static
AnchorGenerator
am;
44
return
am;
45
}
46
47
constexpr
auto
prefix
=
"autotoc_md"
;
48
49
std::string
AnchorGenerator::addPrefixIfNeeded
(
const
std::string &anchor)
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
}
55
56
std::string
AnchorGenerator::generate
(
const
std::string &label)
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
}
124
125
bool
AnchorGenerator::isGenerated
(
const
std::string &anchor)
const
126
{
127
std::lock_guard lock(
p
->mutex);
128
return
p
->anchorsUsed.find(anchor)!=
p
->anchorsUsed.end();
129
}
130
131
int
AnchorGenerator::reserve
(
const
std::string &anchor)
132
{
133
std::lock_guard lock(
p
->mutex);
134
return
p
->idCount[anchor]++;
135
}
136
137
bool
AnchorGenerator::looksGenerated
(
const
std::string &anchor)
138
{
139
return
Config_getEnum
(MARKDOWN_ID_STYLE)==MARKDOWN_ID_STYLE_t::DOXYGEN &&
140
DString
(anchor).
startsWith
(
prefix
);
141
}
prefix
constexpr auto prefix
Definition
anchor.cpp:47
anchor.h
AnchorGenerator::isGenerated
bool isGenerated(const std::string &anchor) const
Returns true iff anchor is one of the generated anchors.
Definition
anchor.cpp:125
AnchorGenerator::instance
static AnchorGenerator & instance()
Returns the singleton instance.
Definition
anchor.cpp:41
AnchorGenerator::~AnchorGenerator
~AnchorGenerator()
AnchorGenerator::AnchorGenerator
AnchorGenerator()
Definition
anchor.cpp:37
AnchorGenerator::addPrefixIfNeeded
static std::string addPrefixIfNeeded(const std::string &anchor)
Definition
anchor.cpp:49
AnchorGenerator::generate
std::string generate(const std::string &title)
generates an anchor for a section with title.
Definition
anchor.cpp:56
AnchorGenerator::reserve
int reserve(const std::string &anchor)
Reserves a non-generated anchor.
Definition
anchor.cpp:131
AnchorGenerator::looksGenerated
static bool looksGenerated(const std::string &anchor)
Returns true if anchor is a potentially generated anchor.
Definition
anchor.cpp:137
AnchorGenerator::p
std::unique_ptr< Private > p
Definition
anchor.h:54
DString
A String class for use with Doxygen wrapping std::string and adding some additional functionality off...
Definition
dstring.h:84
DString::startsWith
bool startsWith(const char *s) const
Definition
dstring.h:600
config.h
Config_getEnum
#define Config_getEnum(name)
Definition
config.h:35
StringUnorderedSet
std::unordered_set< std::string > StringUnorderedSet
Definition
containers.h:29
disspace
bool disspace(char c)
Definition
dstring.h:62
std
Definition
dstring.h:913
AnchorGenerator::Private
Definition
anchor.cpp:30
AnchorGenerator::Private::anchorsUsed
StringUnorderedSet anchorsUsed
Definition
anchor.cpp:31
AnchorGenerator::Private::anchorCount
int anchorCount
Definition
anchor.cpp:32
AnchorGenerator::Private::idCount
std::unordered_map< std::string, int > idCount
Definition
anchor.cpp:34
AnchorGenerator::Private::mutex
std::mutex mutex
Definition
anchor.cpp:33
isUTF8PunctuationCharacter
bool isUTF8PunctuationCharacter(uint32_t unicode)
Check if the given Unicode character represents a punctuation character.
Definition
utf8.cpp:238
getUnicodeForUTF8CharAt
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
convertUTF8ToLower
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
getUTF8CharNumBytes
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
getUTF8CharAt
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
utf8.h
Various UTF8 related helper functions.
src
anchor.cpp
Generated by
1.19.0