Meta Tags Generator — C++ source
Build the SEO head tags for any page — title, description, canonical, robots, Open Graph with smart fallbacks, Twitter card and theme-color — with live length warnings.
This is the C++ implementation — the same logic the interactive tool runs, in a shareable, citable form.
// meta-tags-generator — build the ordered SEO head-tag set: title,
// description, canonical, robots, Open Graph (ogTitle/ogDescription/ogUrl
// falling back to the base fields, og:type website whenever the block is
// present), Twitter card, theme-color. C++ port (canonical TS:
// src/lib/meta-tags.ts; Go twin:
// cli/meta-tags-generator/meta-tags-generator.go). Same fields, same
// fallbacks, same emission order, same warnings (title > 60,
// description > 160) and the same hard errors (missing title/description,
// non-absolute og:image, non-hex theme-color suppress emission entirely).
#include <regex>
#include <string>
#include <vector>
namespace metatags {
struct MetaTagsInput {
std::string title;
std::string description;
std::string canonical_url;
std::string robots;
std::string og_title;
std::string og_description;
std::string og_image;
std::string og_url;
std::string twitter_card;
std::string twitter_site;
std::string theme_color;
};
struct MetaTagsResult {
bool ok = false;
std::vector<std::string> errors;
std::vector<std::string> warnings;
std::vector<std::string> tags;
};
namespace {
const std::regex &hexColor() {
static const std::regex re("^#[0-9a-fA-F]{6}$");
return re;
}
bool isAbsoluteHttp(const std::string &s) {
return s.rfind("http://", 0) == 0 || s.rfind("https://", 0) == 0;
}
} // namespace
/// Build the ordered head-tag set. Blank optional fields are skipped; hard
/// errors suppress emission entirely; length warnings never block emission.
inline MetaTagsResult buildMetaTags(const MetaTagsInput &in) {
MetaTagsResult r;
std::vector<std::string> errors;
std::vector<std::string> warnings;
auto trim = [](std::string s) -> std::string {
const char *ws = " \t\r\n";
size_t b = s.find_first_not_of(ws);
if (b == std::string::npos) return "";
size_t e = s.find_last_not_of(ws);
return s.substr(b, e - b + 1);
};
const std::string title_ = trim(in.title);
const std::string description = trim(in.description);
const std::string canonical = trim(in.canonical_url);
const std::string ogTitleIn = trim(in.og_title);
const std::string ogTitle = !ogTitleIn.empty() ? ogTitleIn : title_;
const std::string ogDescriptionIn = trim(in.og_description);
const std::string ogDescription = !ogDescriptionIn.empty() ? ogDescriptionIn : description;
const std::string ogImage = trim(in.og_image);
const std::string ogUrlIn = trim(in.og_url);
const std::string ogUrl = !ogUrlIn.empty() ? ogUrlIn : canonical;
const std::string themeColor = trim(in.theme_color);
if (title_.empty()) errors.push_back("Title is required.");
if (description.empty()) errors.push_back("Description is required.");
if (!ogImage.empty() && !isAbsoluteHttp(ogImage))
errors.push_back("og:image must be an absolute http(s) URL.");
if (!themeColor.empty() && !std::regex_search(themeColor, hexColor()))
errors.push_back("theme-color must be a #rrggbb hex value.");
if (!errors.empty()) {
r.ok = false;
r.errors = std::move(errors);
return r;
}
if (title_.size() > 60)
warnings.push_back("Title is " + std::to_string(title_.size()) +
" characters — search results truncate around 60.");
if (description.size() > 160)
warnings.push_back("Description is " + std::to_string(description.size()) +
" characters — search results truncate around 160.");
std::vector<std::string> tags;
tags.push_back("<title>" + title_ + "</title>");
tags.push_back("<meta name=\"description\" content=\"" + description + "\" />");
if (!canonical.empty()) tags.push_back("<link rel=\"canonical\" href=\"" + canonical + "\" />");
const std::string robots = trim(in.robots);
if (!robots.empty()) tags.push_back("<meta name=\"robots\" content=\"" + robots + "\" />");
const bool hasOg = !ogTitleIn.empty() || !ogDescriptionIn.empty() || !ogImage.empty() || !ogUrl.empty();
if (hasOg) {
tags.push_back("<meta property=\"og:title\" content=\"" + ogTitle + "\" />");
tags.push_back("<meta property=\"og:description\" content=\"" + ogDescription + "\" />");
if (!ogImage.empty()) tags.push_back("<meta property=\"og:image\" content=\"" + ogImage + "\" />");
if (!ogUrl.empty()) tags.push_back("<meta property=\"og:url\" content=\"" + ogUrl + "\" />");
tags.push_back("<meta property=\"og:type\" content=\"website\" />");
}
if (!in.twitter_card.empty())
tags.push_back("<meta name=\"twitter:card\" content=\"" + in.twitter_card + "\" />");
const std::string twitterSite = trim(in.twitter_site);
if (!twitterSite.empty())
tags.push_back("<meta name=\"twitter:site\" content=\"" + twitterSite + "\" />");
if (!themeColor.empty())
tags.push_back("<meta name=\"theme-color\" content=\"" + themeColor + "\" />");
r.ok = true;
r.warnings = std::move(warnings);
r.tags = std::move(tags);
return r;
}
} // namespace metatags
Also available in 13 other languages
Every CosmoDev tool ships its pure logic in TypeScript (web) and Go (CLI), with authored implementations in a dozen-plus languages — the same contract, ported. Compare all languages side by side →