Skip to content

Commit

Permalink
Merge pull request #1440 from TelegramBots/develop
Browse files Browse the repository at this point in the history
ToHtml/ToMarkdown: support for ExpandableBlockquote
  • Loading branch information
wiz0u authored Nov 20, 2024
2 parents 454f298 + 81fe54c commit 4837cf5
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
4 changes: 2 additions & 2 deletions .azure-pipelines/variables.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
variables:
variables:
- group: Integration Tests Variables
- name: versionPrefix
value: 22.1.0
value: 22.1.1
- name: versionSuffix
value: ''
- name: ciVersionSuffix
Expand Down
8 changes: 6 additions & 2 deletions src/Telegram.Bot/Extensions/FormatExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static string ToMarkdown(string message, MessageEntity[]? entities)
var md = closings[0].md;
closings.RemoveAt(0);
if (i > 0 && md[0] == '_' && sb[i - 1] == '_') md = '\r' + md;
if (md[0] == '>') { inBlockQuote = false; if (lastCh != '\n' && i < sb.Length && sb[i] != '\n') md = "\n"; else continue; }
if (md[0] == '>') { inBlockQuote = false; md = md[1..]; if (lastCh != '\n' && i < sb.Length && sb[i] != '\n') md += '\n'; }
sb.Insert(i, md); i += md.Length;
}
if (i == sb.Length) break;
Expand All @@ -56,7 +56,7 @@ public static string ToMarkdown(string message, MessageEntity[]? entities)
closing.md = $"](tg://emoji?id={nextEntity.CustomEmojiId})";
}
else if (md[0] == '>')
{ inBlockQuote = true; if (lastCh is not '\n' and not '\0') md = "\n>"; }
{ inBlockQuote = true; md = lastCh is not '\n' and not '\0' ? "\n>" : ">"; }
else if (nextEntity.Type is MessageEntityType.Pre)
md = $"```{nextEntity.Language}\n";
int index = ~closings.BinarySearch(closing, Comparer<(int, string)>.Create((x, y) => x.Item1.CompareTo(y.Item1) | 1));
Expand Down Expand Up @@ -92,6 +92,7 @@ public static string ToMarkdown(string message, MessageEntity[]? entities)
[MessageEntityType.Spoiler] = "||",
[MessageEntityType.CustomEmoji] = "![",
[MessageEntityType.Blockquote] = ">",
[MessageEntityType.ExpandableBlockquote] = ">||",
};

/// <summary>Insert backslashes in front of MarkdownV2 reserved characters</summary>
Expand Down Expand Up @@ -168,6 +169,8 @@ public static string ToHtml(string message, MessageEntity[]? entities)
closing.Item2 = "</code></pre>";
tag = $"<pre><code class=\"language-{nextEntity.Language}\">";
}
else if (nextEntity.Type is MessageEntityType.ExpandableBlockquote)
tag = "<blockquote expandable>";
else
tag = $"<{tag}>";
int index = ~closings.BinarySearch(closing, Comparer<(int, string)>.Create((x, y) => x.Item1.CompareTo(y.Item1) | 1));
Expand Down Expand Up @@ -198,6 +201,7 @@ public static string ToHtml(string message, MessageEntity[]? entities)
[MessageEntityType.Spoiler] = "tg-spoiler",
[MessageEntityType.CustomEmoji] = "tg-emoji",
[MessageEntityType.Blockquote] = "blockquote",
[MessageEntityType.ExpandableBlockquote] = "blockquote",
};

/// <summary>Replace special HTML characters with their &amp;xx; equivalent</summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ namespace Telegram.Bot.Tests.Integ.Framework;
internal class RetryTelegramBotClient(TestConfiguration configuration, IMessageSink diagnosticMessageSink, CancellationToken ct = default)
: WTelegramBotClient(MakeOptions(configuration.ApiToken, configuration.ClientApiToken.Split(':')), cancellationToken: ct)
{
private static StreamWriter WTelegramLogs = new StreamWriter("WTelegramBot.log", true, System.Text.Encoding.UTF8) { AutoFlush = true };
private static WTelegramBotClientOptions MakeOptions(string botToken, string[] api)
{
var connection = new Microsoft.Data.Sqlite.SqliteConnection($"Data Source=WTelegramBot.{botToken.Split(':')[0]}.sqlite");
WTelegram.Helpers.Log = (lvl, str) => System.Diagnostics.Trace.WriteLine(str);
WTelegram.Helpers.Log += (lvl, str) => WTelegramLogs.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss} [{"TDIWE!"[lvl]}] {str}");
return new(botToken, int.Parse(api[0]), api[1], connection);
}
public void WithStreams(Stream[] _) { }
Expand Down

0 comments on commit 4837cf5

Please sign in to comment.