Skip to content

Commit

Permalink
convert summary to standard markdown
Browse files Browse the repository at this point in the history
  • Loading branch information
kookxiang committed Oct 6, 2024
1 parent beddecf commit c98b83b
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 24 deletions.
8 changes: 0 additions & 8 deletions Emby.Plugin.Bangumi/Configuration/ConfigPage.html
Original file line number Diff line number Diff line change
Expand Up @@ -180,14 +180,6 @@ <h2 class="sectionTitle">元数据</h2>
</label>
</div>
</div>
<div class="selectContainer">
<div class="checkboxContainer checkboxContainer-withDescription">
<label class="emby-checkbox-label">
<input id="ConvertLineBreaks" is="emby-checkbox" type="checkbox"/>
<span>转换元数据信息中的换行符</span>
</label>
</div>
</div>
<div class="selectContainer">
<div class="checkboxContainer checkboxContainer-withDescription">
<label class="emby-checkbox-label">
Expand Down
24 changes: 22 additions & 2 deletions Emby.Plugin.Bangumi/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,30 @@

namespace Jellyfin.Plugin.Bangumi;

public static class Extensions
public static class DictionaryExtensions
{
public static T? GetOrDefault<TKey, T>(this IDictionary<TKey, T> dict, TKey key)
{
return dict.TryGetValue(key, out var value) ? value : default;
}
}
}

public static class StringExtensions
{
private const string MarkdownForcedLineBreak = "<br>\n";

public static string ToMarkdown(this string input)
{
var content = input.ReplaceLineEndings(MarkdownForcedLineBreak);

while (content.Contains(MarkdownForcedLineBreak + MarkdownForcedLineBreak + MarkdownForcedLineBreak))
content = content.Replace(
MarkdownForcedLineBreak + MarkdownForcedLineBreak + MarkdownForcedLineBreak,
MarkdownForcedLineBreak + MarkdownForcedLineBreak
);

content = content.Replace(MarkdownForcedLineBreak + MarkdownForcedLineBreak, "\n\n");

return content;
}
}
2 changes: 1 addition & 1 deletion Jellyfin.Plugin.Bangumi.Test/Person.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public async Task LineBreaksConversion()
ProviderIds = new Dictionary<string, string> { { Constants.ProviderName, "389" } }
}, _token);
Assert.IsNotNull(result.Item, "person info should not be null");
Assert.IsTrue(result.Item.Overview.Contains("<br>"), "should convert line breaks to html tag");
Assert.IsTrue(result.Item.Overview.Contains("<br>") || result.Item.Overview.Contains("\n\n"), "should convert line breaks to html tag");
Assert.IsNotNull(result.Item.ProviderIds[Constants.ProviderName], "should have plugin provider id");
}

Expand Down
6 changes: 0 additions & 6 deletions Jellyfin.Plugin.Bangumi/Configuration/ConfigPage.html
Original file line number Diff line number Diff line change
Expand Up @@ -228,12 +228,6 @@ <h2 class="sectionTitle">元数据</h2>
启用后不再更新,直到剧集元数据中的 ID 被清除才会查询。
</div>
</div>
<div class="checkboxContainer checkboxContainer-withDescription">
<label class="emby-checkbox-label">
<input id="ConvertLineBreaks" is="emby-checkbox" type="checkbox"/>
<span>转换元数据信息中的换行符</span>
</label>
</div>
<div class="checkboxContainer checkboxContainer-withDescription">
<label class="emby-checkbox-label">
<input id="UseTestingSearchApi" is="emby-checkbox" type="checkbox"/>
Expand Down
2 changes: 0 additions & 2 deletions Jellyfin.Plugin.Bangumi/Configuration/PluginConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,5 @@ public class PluginConfiguration : BasePluginConfiguration

public bool UseTestingSearchApi { get; set; }

public bool ConvertLineBreaks { get; set; } = true;

public int SeasonGuessMaxSearchCount { get; set; } = 2;
}
22 changes: 21 additions & 1 deletion Jellyfin.Plugin.Bangumi/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,34 @@

namespace Jellyfin.Plugin.Bangumi;

public static class Extensions
public static class DictionaryExtensions
{
public static T? GetOrDefault<TKey, T>(this IDictionary<TKey, T> dict, TKey key)
{
return dict.TryGetValue(key, out var value) ? value : default;
}
}

public static class StringExtensions
{
private const string MarkdownForcedLineBreak = "<br>\n";

public static string ToMarkdown(this string input)
{
var content = input.ReplaceLineEndings(MarkdownForcedLineBreak);

while (content.Contains(MarkdownForcedLineBreak + MarkdownForcedLineBreak + MarkdownForcedLineBreak))
content = content.Replace(
MarkdownForcedLineBreak + MarkdownForcedLineBreak + MarkdownForcedLineBreak,
MarkdownForcedLineBreak + MarkdownForcedLineBreak
);

content = content.Replace(MarkdownForcedLineBreak + MarkdownForcedLineBreak, "\n\n");

return content;
}
}

/// <summary>
/// Class providing extension methods for working with paths.
/// From: https://github.com/jellyfin/jellyfin/blob/master/Emby.Server.Implementations/Library/PathExtensions.cs#L10
Expand Down
2 changes: 1 addition & 1 deletion Jellyfin.Plugin.Bangumi/Model/Episode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public class Episode
public string? Duration { get; set; }

[JsonIgnore]
public string? Description => Configuration.ConvertLineBreaks ? DescriptionRaw?.ReplaceLineEndings(Constants.HtmlLineBreak).TrimStart() : DescriptionRaw;
public string? Description => DescriptionRaw?.ToMarkdown();

[JsonPropertyName("desc")]
public string? DescriptionRaw { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion Jellyfin.Plugin.Bangumi/Model/PersonDetail.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class PersonDetail : Person
private static PluginConfiguration Configuration => Plugin.Instance!.Configuration;

[JsonIgnore]
public string Summary => Configuration.ConvertLineBreaks ? SummaryRaw.ReplaceLineEndings(Constants.HtmlLineBreak) : SummaryRaw;
public string Summary => SummaryRaw.ToMarkdown();

[JsonPropertyName("summary")]
public string SummaryRaw { get; set; } = "";
Expand Down
5 changes: 3 additions & 2 deletions Jellyfin.Plugin.Bangumi/Model/Subject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class Subject
public string? ChineseNameRaw { get; set; }

[JsonIgnore]
public string? Summary => Configuration.ConvertLineBreaks ? SummaryRaw?.ReplaceLineEndings(Constants.HtmlLineBreak).TrimStart() : SummaryRaw;
public string? Summary => SummaryRaw?.ToMarkdown();

[JsonPropertyName("summary")]
public string? SummaryRaw { get; set; }
Expand Down Expand Up @@ -96,7 +96,8 @@ public DateTime? EndDate
get
{
var dateStr = InfoBox?.GetString("播放结束");
if (dateStr != null && DateTime.TryParseExact(dateStr, "yyyy年MM月dd日", CultureInfo.GetCultureInfo("zh-CN"), DateTimeStyles.None, out var date))
if (dateStr != null && DateTime.TryParseExact(dateStr, "yyyy年MM月dd日", CultureInfo.GetCultureInfo("zh-CN"),
DateTimeStyles.None, out var date))
return date;
return null;
}
Expand Down

0 comments on commit c98b83b

Please sign in to comment.