lipsum-cpp 0.5.4
A basic library written in C++ for generating placeholder Lorem Ipsum text.
Loading...
Searching...
No Matches
Advanced.cpp
// This was generated by an LLM unlike the rest of these examples.
#ifndef LIPSUM_BUILD_STATIC
# define LIPSUM_IMPLEMENTATION // only for header-only usage
#endif
#include <lipsum.hpp>
// A small helper to replace all occurrences of a token in a string.
static std::string
ReplaceAll(std::string& s, const std::string& from, const std::string& to)
{
if (from.empty())
return s;
size_t pos = 0;
while ((pos = s.find(from, pos)) != std::string::npos)
{
s.replace(pos, from.length(), to);
pos += to.length();
}
return s;
}
int main()
{
std::cout << "Notice: this example was AI-generated.\n";
// Use the object-oriented generator.
gen.change_setting("wordFmt", 3, 6);
gen.change_setting("wordURL", 2, 4);
gen.change_setting("word", 4, 8);
gen.change_setting("frag", 1, 2);
// Build a template Markdown document with tokens to fill.
std::string mdTemplate =
"{TITLE}\n\n"
"## About\n\n"
"{ABOUT}\n\n"
"## Highlights\n\n"
"{HIGHLIGHTS}\n\n"
"## Quick Example\n\n"
"```cpp\n"
"// This is a tiny snippet; it's intentionally silly\n"
"auto awesome = \"{WORD}\";\n"
"std::cout << \"Behold: \" << awesome << std::endl;\n"
"```\n\n"
"## Related Link\n\n"
"{MDLINK}\n\n"
"## Gallery\n\n"
"{GALLERY}\n";
// Fill tokens using the library:
std::string title = gen.fmt_header(1, lpsm::MARKDOWN); // short title
// About: a short Markdown paragraph (use Markdown generator to add some
// formatting)
std::string about = gen.fmt_paragraph(1, lpsm::USELIPSUM, lpsm::MARKDOWN);
// Highlights: a markdown list (unordered)
std::string highlights = gen.fmt_list(lpsm::UNORDERED, lpsm::MARKDOWN);
// A single random word used in the code block
std::string word = gen.word();
// A markdown link
std::string mdLink = gen.fmt_link(lpsm::MARKDOWN);
// Gallery: several short paragraphs
std::string gallery = gen.paragraph(3, lpsm::NO_USELIPSUM);
gen.change_setting("word", 2, 4);
gen.change_setting("frag", 1, 1);
// Do a few playful replacements in the gallery to mimic captions
std::string caption = std::string("> Caption: ") + gen.sentence() + "\n\n";
gallery = caption + gallery;
// Now replace tokens in template
std::string md = mdTemplate;
md = ReplaceAll(md, "{TITLE}", title);
md = ReplaceAll(md, "{ABOUT}", about);
md = ReplaceAll(md, "{HIGHLIGHTS}", highlights);
md = ReplaceAll(md, "{WORD}", word);
md = ReplaceAll(md, "{MDLINK}", mdLink);
md = ReplaceAll(md, "{GALLERY}", gallery);
// Print the generated Markdown document
std::cout << "----- Generated Markdown -----\n\n";
std::cout << md << "\n";
// Show some simple statistics
int sentences = lpsm::CountSentences(md);
int words = lpsm::CountWords(md);
std::cout << "----- Stats -----\n";
std::cout << "Sentences: " << sentences << "\n";
std::cout << "Words: " << words << "\n";
return 0;
}
Main class of lipsum-cpp.
Definition generator.hpp:92
void change_setting(const std::string &setting, const ArgVec2 &value)
Change a setting.
Definition generatorcore.inl:131
std::string fmt_paragraph(int num=1, bool useLipsum=true, bool useHtml=false)
Generate Markdown or HTML paragraphs.
Definition generatorformats.inl:27
std::string sentence(int num=1, bool useLipsum=true)
Generate sentences.
Definition generator.inl:98
std::string fmt_header(int level=1, bool useHtml=false)
Generate a Markdown or HTML header.
Definition generatorformats.inl:110
std::string word(int num=1)
Generate words.
Definition generator.inl:67
std::string fmt_list(bool ordered=false, bool useHtml=false)
Generate a Markdown or HTML list.
Definition generatorformats.inl:186
std::string fmt_link(bool useHtml=false)
Generate a Markdown or HTML link.
Definition generatorformats.inl:173
std::string paragraph(int num=1, bool useLipsum=true)
Generate paragraphs.
Definition generator.inl:125
Main header of lipsum-cpp.
LIPSUM_API int CountSentences(const std::string &str)
Count the number of sentences in a string.
Definition misc.inl:127
LIPSUM_API int CountWords(const std::string &str)
Count the number of words in a string.
Definition misc.inl:43