示例#1
0
EntitiesInText ConvertTextTagsToEntities(const TextWithTags::Tags &tags) {
	EntitiesInText result;
	if (tags.isEmpty()) {
		return result;
	}

	result.reserve(tags.size());
	for (const auto &tag : tags) {
		const auto push = [&](
				EntityInTextType type,
				const QString &data = QString()) {
			result.push_back(
				EntityInText(type, tag.offset, tag.length, data));
		};
		if (IsMentionLink(tag.id)) {
			if (auto match = qthelp::regex_match("^(\\d+\\.\\d+)(/|$)", tag.id.midRef(kMentionTagStart.size()))) {
				push(EntityInTextMentionName, match->captured(1));
			}
		} else if (tag.id == Ui::InputField::kTagBold) {
			push(EntityInTextBold);
		} else if (tag.id == Ui::InputField::kTagItalic) {
			push(EntityInTextItalic);
		} else if (tag.id == Ui::InputField::kTagCode) {
			push(EntityInTextCode);
		} else if (tag.id == Ui::InputField::kTagPre) {
			push(EntityInTextPre);
		} else /*if (ValidateUrl(tag.id)) */{ // We validate when we insert.
			push(EntityInTextCustomUrl, tag.id);
		}
	}
	return result;
}