From 49ea9434f269a28ae5318c553d19f970fc1b5dc2 Mon Sep 17 00:00:00 2001 From: John Preston Date: Thu, 28 Jun 2018 18:52:57 +0100 Subject: [PATCH] Better nested key-value html serialization. --- .../export/output/export_output_html.cpp | 67 +++++++++++-------- 1 file changed, 39 insertions(+), 28 deletions(-) diff --git a/Telegram/SourceFiles/export/output/export_output_html.cpp b/Telegram/SourceFiles/export/output/export_output_html.cpp index 07b2e5552..469663310 100644 --- a/Telegram/SourceFiles/export/output/export_output_html.cpp +++ b/Telegram/SourceFiles/export/output/export_output_html.cpp @@ -172,6 +172,13 @@ QByteArray SerializeKeyValue( return result; } +QByteArray SerializeBlockquote( + std::vector> &&values) { + return "
" + + SerializeKeyValue(std::move(values)) + + "
"; +} + Data::Utf8String FormatUsername(const Data::Utf8String &username) { return username.isEmpty() ? username : ('@' + username); } @@ -283,33 +290,37 @@ QByteArray SerializeMessage( }; using SkipReason = Data::File::SkipReason; - const auto pushPath = [&]( + const auto formatPath = [&]( const Data::File &file, const QByteArray &label, const QByteArray &name = QByteArray()) { Expects(!file.relativePath.isEmpty() || file.skipReason != SkipReason::None); - pushBare(label, [&]() -> QByteArray { - const auto pre = name.isEmpty() - ? QByteArray() - : SerializeString(name + ' '); - switch (file.skipReason) { - case SkipReason::Unavailable: - return pre + "(" + label + " unavailable, " - "please try again later)"; - case SkipReason::FileSize: - return pre + "(" + label + " exceeds maximum size. " - "Change data exporting settings to download.)"; - case SkipReason::FileType: - return pre + "(" + label + " not included. " - "Change data exporting settings to download.)"; - case SkipReason::None: return SerializeLink( - FormatFilePath(file), - relativePath(file.relativePath)); - } - Unexpected("Skip reason while writing file path."); - }()); + const auto pre = name.isEmpty() + ? QByteArray() + : SerializeString(name + ' '); + switch (file.skipReason) { + case SkipReason::Unavailable: + return pre + "(" + label + " unavailable, " + "please try again later)"; + case SkipReason::FileSize: + return pre + "(" + label + " exceeds maximum size. " + "Change data exporting settings to download.)"; + case SkipReason::FileType: + return pre + "(" + label + " not included. " + "Change data exporting settings to download.)"; + case SkipReason::None: return SerializeLink( + FormatFilePath(file), + relativePath(file.relativePath)); + } + Unexpected("Skip reason while writing file path."); + }; + const auto pushPath = [&]( + const Data::File &file, + const QByteArray &label, + const QByteArray &name = QByteArray()) { + pushBare(label, formatPath(file, label, name)); }; const auto pushPhoto = [&](const Image &image) { pushPath(image.file, "Photo"); @@ -486,16 +497,16 @@ QByteArray SerializeMessage( } pushTTL(); }, [&](const SharedContact &data) { - pushBare("Contact information", SerializeKeyValue({ + pushBare("Contact information", SerializeBlockquote({ { "First name", data.info.firstName }, { "Last name", data.info.lastName }, { "Phone number", FormatPhoneNumber(data.info.phoneNumber) }, + { "vCard", (data.vcard.content.isEmpty() + ? QByteArray() + : formatPath(data.vcard, "vCard")) } })); - if (!data.vcard.content.isEmpty()) { - pushPath(data.vcard, "Contact vcard"); - } }, [&](const GeoPoint &data) { - pushBare("Location", data.valid ? SerializeKeyValue({ + pushBare("Location", data.valid ? SerializeBlockquote({ { "Latitude", NumberToString(data.latitude) }, { "Longitude", NumberToString(data.longitude) }, }) : QByteArray("(empty value)")); @@ -504,7 +515,7 @@ QByteArray SerializeMessage( push("Place name", data.title); push("Address", data.address); if (data.point.valid) { - pushBare("Location", SerializeKeyValue({ + pushBare("Location", SerializeBlockquote({ { "Latitude", NumberToString(data.point.latitude) }, { "Longitude", NumberToString(data.point.longitude) }, })); @@ -522,7 +533,7 @@ QByteArray SerializeMessage( } } }, [&](const Invoice &data) { - pushBare("Invoice", SerializeKeyValue({ + pushBare("Invoice", SerializeBlockquote({ { "Title", data.title }, { "Description", data.description }, {