Skip to content

Commit e365218

Browse files
authored
Support auto-generate publish contents (#15)
* add --base_template It can convert markdown file to other files * update gha * csproj: remove RestoreSources * Update README.md * ConvertMarkdownToHtml use GFM line breaks Not need two-space line breaks
1 parent 80c5b7b commit e365218

File tree

11 files changed

+200
-108
lines changed

11 files changed

+200
-108
lines changed

.github/workflows/dotnet-ci.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ jobs:
2222
with:
2323
dotnet-version: 8.x
2424

25+
- name: Prepare
26+
run: |
27+
dotnet nuget add source --username sun128764 --password ${{ secrets.GITHUB_TOKEN }} --store-password-in-clear-text --name github "https://nuget.pkg.github.com/MIRIMIRIM/index.json"
28+
dotnet restore
29+
2530
- name: Publish the application
2631
run: dotnet publish OKP.Core --configuration ${{ matrix.configuration }} --runtime ${{ matrix.runtime-identifier }} /p:PublishAot=false
2732

OKP.Core/Interface/Acgnx/AcgnxAdapter.cs

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -186,23 +186,7 @@ private bool Valid()
186186
throw new ArgumentNullException(nameof(torrent.Data.TorrentObject));
187187
}
188188

189-
if (template.Content != null && template.Content.ToLower().EndsWith(".html"))
190-
{
191-
Log.Debug("开始寻找{Site} .html文件 {File}", site, template.Content);
192-
var templateFile = FileHelper.ParseFileFullPath(template.Content, torrent.SettingPath);
193-
if (File.Exists(templateFile))
194-
{
195-
Log.Debug("找到了{Site} .html文件 {File}", site, template.Content);
196-
template.Content = File.ReadAllText(templateFile);
197-
}
198-
else
199-
{
200-
Log.Error("发布模板看起来是个.html文件,但是这个.html文件不存在{NewLine}" +
201-
"{Source}-->{Dest}", Environment.NewLine, template.Content, templateFile);
202-
return false;
203-
}
204-
}
205-
return true;
189+
return ValidTemplate(template, site, torrent.SettingPath);
206190
}
207191
}
208192

OKP.Core/Interface/Acgrip/AcgripAdapter.cs

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -154,23 +154,7 @@ private bool Valid()
154154
return false;
155155
}
156156
}
157-
if (template.Content != null && template.Content.ToLower().EndsWith(".bbcode"))
158-
{
159-
Log.Debug("开始寻找{Site} bbcode文件 {File}", site, template.Content);
160-
var templateFile = FileHelper.ParseFileFullPath(template.Content, torrent.SettingPath);
161-
if (File.Exists(templateFile))
162-
{
163-
Log.Debug("找到了{Site} bbcode文件 {File}", site, templateFile);
164-
template.Content = File.ReadAllText(templateFile);
165-
}
166-
else
167-
{
168-
Log.Error("发布模板看起来是个.bbcode文件,但是这个.bbcode文件不存在{NewLine}" +
169-
"{Source}-->{Dest}", Environment.NewLine, template.Content, templateFile);
170-
return false;
171-
}
172-
}
173-
return true;
157+
return ValidTemplate(template, site, torrent.SettingPath);
174158
}
175159
}
176160
}

OKP.Core/Interface/AdapterBase.cs

Lines changed: 124 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,132 @@
1-
namespace OKP.Core.Interface
1+
using Serilog;
2+
using Markdig;
3+
using Converter.MarkdownToBBCode.Shared;
4+
using Markdig.Extensions.EmphasisExtras;
5+
using Markdig.Parsers;
6+
using OKP.Core.Utils;
7+
8+
namespace OKP.Core.Interface
29
{
310
internal abstract class AdapterBase
411
{
512
public abstract Task<HttpResult> PingAsync();
613
public abstract Task<HttpResult> PostAsync();
14+
15+
internal static string ConvertMarkdownToHtml(string mdContent)
16+
{
17+
var pipeline = new MarkdownPipelineBuilder().UseSoftlineBreakAsHardlineBreak().Build();
18+
return Markdown.ToHtml(mdContent, pipeline);
19+
}
20+
21+
internal static string ConvertMarkdownToBBCode(string mdContent)
22+
{
23+
var pipeline = new MarkdownPipelineBuilder().EnableTrackTrivia().UseEmphasisExtras(EmphasisExtraOptions.Strikethrough).UseSoftlineBreakAsHardlineBreak().Build();
24+
var document = MarkdownParser.Parse(mdContent, pipeline);
25+
26+
using var sw = new StringWriter();
27+
var renderer = new BBCodeRenderer(BBCodeType.NexusMods, pipeline, false, false, sw);
28+
renderer.Render(document);
29+
renderer.Writer.Flush();
30+
31+
return renderer.Writer.ToString() ?? string.Empty;
32+
}
33+
34+
private enum ContentType
35+
{
36+
Text,
37+
MarkdownFile,
38+
HtmlFile,
39+
BBCodeFile,
40+
}
41+
42+
private static string GetContentExt(ContentType contentType)
43+
{
44+
return contentType switch
45+
{
46+
ContentType.MarkdownFile => ".md",
47+
ContentType.HtmlFile => ".html",
48+
ContentType.BBCodeFile => ".bbcode",
49+
_ => throw new ArgumentOutOfRangeException(nameof(contentType), contentType, null)
50+
};
51+
}
52+
53+
private static ContentType GetContentType(string site)
54+
{
55+
return site switch
56+
{
57+
"dmhy" => ContentType.HtmlFile,
58+
"acgnx_asia" => ContentType.HtmlFile,
59+
"acgnx_global" => ContentType.HtmlFile,
60+
"acgrip" => ContentType.BBCodeFile,
61+
"bangumi" => ContentType.HtmlFile,
62+
"nyaa" => ContentType.MarkdownFile,
63+
_ => throw new ArgumentOutOfRangeException(nameof(site), site, null)
64+
};
65+
}
66+
67+
internal static bool ValidTemplate(TorrentContent.Template template, string site, string? settingPath)
68+
{
69+
if (template.Content == null) return false;
70+
var contentType = ContentType.Text;
71+
72+
var span = template.Content.AsSpan();
73+
if (span.IndexOf('\n') != -1) return true;
74+
75+
if (span.EndsWith(".md", StringComparison.OrdinalIgnoreCase))
76+
{
77+
contentType = ContentType.MarkdownFile;
78+
}
79+
else if (span.EndsWith(".html", StringComparison.OrdinalIgnoreCase))
80+
{
81+
contentType = ContentType.HtmlFile;
82+
}
83+
else if (span.EndsWith(".bbcode", StringComparison.OrdinalIgnoreCase))
84+
{
85+
contentType = ContentType.BBCodeFile;
86+
}
87+
else
88+
{
89+
return true;
90+
}
91+
92+
var siteContentType = GetContentType(site);
93+
var siteContentExt = GetContentExt(siteContentType);
94+
var contentExt = GetContentExt(contentType);
95+
if (contentType != siteContentType)
96+
{
97+
Log.Debug("不存在{Site} {FileExt}文件,将用 {File} 生成", site, siteContentExt, template.Content);
98+
}
99+
else
100+
{
101+
Log.Debug("开始寻找{Site} {FileExt}文件 {File}", site, siteContentExt, template.Content);
102+
}
103+
var templateFile = FileHelper.ParseFileFullPath(template.Content, settingPath);
104+
if (File.Exists(templateFile))
105+
{
106+
Log.Debug("找到了{FileExt}文件 {File}", contentExt, templateFile);
107+
var text = File.ReadAllText(templateFile);
108+
if (contentType == siteContentType)
109+
{
110+
template.Content = text;
111+
}
112+
else
113+
{
114+
template.Content = siteContentType switch
115+
{
116+
ContentType.HtmlFile => ConvertMarkdownToHtml(text),
117+
ContentType.BBCodeFile => ConvertMarkdownToBBCode(text),
118+
_ => throw new ArgumentOutOfRangeException()
119+
};
120+
}
121+
}
122+
else
123+
{
124+
Log.Error("发布模板不存在{NewLine}{Source}-->{Dest}", Environment.NewLine, template.Content, templateFile);
125+
return false;
126+
}
127+
128+
return true;
129+
}
7130
}
8131
public class HttpResult
9132
{

OKP.Core/Interface/Bangumi/BangumiAdapter.cs

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -155,23 +155,7 @@ private bool Valid()
155155
Log.Fatal("{Site} torrent.Data?.TorrentObject is null", site);
156156
throw new ArgumentNullException(nameof(torrent.Data.TorrentObject));
157157
}
158-
if (template.Content != null && template.Content.ToLower().EndsWith(".html"))
159-
{
160-
Log.Debug("开始寻找{Site} html文件 {File}", site, template.Content);
161-
var templateFile = FileHelper.ParseFileFullPath(template.Content, torrent.SettingPath);
162-
if (File.Exists(templateFile))
163-
{
164-
Log.Debug("找到了{Site} html文件 {File}", site, templateFile);
165-
template.Content = File.ReadAllText(templateFile);
166-
}
167-
else
168-
{
169-
Log.Error("发布模板看起来是个.html文件,但是这个.html文件不存在{NewLine}" +
170-
"{Source}-->{Dest}", template.Content, Environment.NewLine, templateFile);
171-
return false;
172-
}
173-
}
174-
return true;
158+
return ValidTemplate(template, site, torrent.SettingPath);
175159
}
176160
private static List<string?> CastTags(List<ContentTypes>? tags)
177161
{

OKP.Core/Interface/Dmhy/DmhyAdapter.cs

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -148,23 +148,8 @@ private bool Valid()
148148
Log.Fatal("{Site} torrent.Data?.TorrentObject is null", site);
149149
throw new ArgumentNullException(nameof(torrent.Data.TorrentObject));
150150
}
151-
if (template.Content != null && template.Content.ToLower().EndsWith(".html"))
152-
{
153-
Log.Debug("开始寻找{Site} html文件 {File}", site, template.Content);
154-
var templateFile = FileHelper.ParseFileFullPath(template.Content, torrent.SettingPath);
155-
if (File.Exists(templateFile))
156-
{
157-
Log.Debug("找到了{Site} html文件 {File}", site, templateFile);
158-
template.Content = File.ReadAllText(templateFile);
159-
}
160-
else
161-
{
162-
Log.Error("发布模板看起来是个.html文件,但是这个.html文件不存在{NewLine}" +
163-
"{Source}-->{Dest}", template.Content, Environment.NewLine, templateFile);
164-
return false;
165-
}
166-
}
167-
return true;
151+
152+
return ValidTemplate(template, site, torrent.SettingPath);
168153
}
169154
}
170155
}

OKP.Core/Interface/Nyaa/NyaaAdapter.cs

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -147,23 +147,7 @@ private bool Valid()
147147
return false;
148148
}
149149
}
150-
if (template.Content != null && template.Content.ToLower().EndsWith(".md"))
151-
{
152-
Log.Debug("开始寻找{Site} .md文件 {File}", site, template.Content);
153-
var templateFile = FileHelper.ParseFileFullPath(template.Content, torrent.SettingPath);
154-
if (File.Exists(templateFile))
155-
{
156-
Log.Debug("找到了{Site} .md文件 {File}", site, template.Content);
157-
template.Content = File.ReadAllText(templateFile);
158-
}
159-
else
160-
{
161-
Log.Error("发布模板看起来是个.md文件,但是这个.md文件不存在{NewLine}" +
162-
"{Source}-->{Dest}", Environment.NewLine, template.Content, templateFile);
163-
return false;
164-
}
165-
}
166-
return true;
150+
return ValidTemplate(template, site, torrent.SettingPath);
167151
}
168152

169153
private NyaaTorrentFlags SetFlags(List<NyaaTorrentFlags>? flags, List<ContentTypes>? tags)

OKP.Core/Interface/TorrentContent.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public enum ContentTypes
9494
App,
9595
Game
9696
}
97-
public static TorrentContent Build(string filename, string settingFile, string appLocation)
97+
public static TorrentContent Build(string filename, string settingFile, string? baseTemplate, string appLocation)
9898
{
9999
var settingFilePath = settingFile;
100100

@@ -116,6 +116,7 @@ public static TorrentContent Build(string filename, string settingFile, string a
116116
}
117117

118118
var torrentC = TomlParseHelper.DeserializeTorrentContent(settingFilePath);
119+
ProcessTemplate(torrentC, baseTemplate);
119120
torrentC.SettingPath = Path.GetDirectoryName(settingFilePath);
120121
//if (!File.Exists(torrentC.CookiePath))
121122
//{
@@ -194,6 +195,24 @@ public static TorrentContent Build(string filename, string settingFile, string a
194195

195196
return torrentC;
196197
}
198+
199+
private static void ProcessTemplate(TorrentContent torrentC, string? baseTemplate)
200+
{
201+
if (baseTemplate is null) return;
202+
if (torrentC.IntroTemplate is null) return;
203+
204+
if (Path.GetDirectoryName(baseTemplate) == "")
205+
{
206+
baseTemplate = Path.Combine(Environment.CurrentDirectory, baseTemplate);
207+
}
208+
209+
// Automatically generate for sites with missing publishing content
210+
foreach (var site in torrentC.IntroTemplate.Where(site => string.IsNullOrEmpty(site.Content)))
211+
{
212+
site.Content = baseTemplate;
213+
}
214+
}
215+
197216
public bool IsV2()
198217
{
199218
if (Data?.TorrentObject is null)

OKP.Core/OKP.Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Released under the GNU GPLv3+.
1515
</PropertyGroup>
1616

1717
<ItemGroup>
18+
<PackageReference Include="AC.Converter.MarkdownToBBCode.Shared" Version="1.0.0" />
1819
<PackageReference Include="BencodeNET" Version="5.0.0" />
1920
<PackageReference Include="Polly.Extensions.Http" Version="3.0.0" />
2021
<PackageReference Include="Serilog" Version="4.0.1" />

0 commit comments

Comments
 (0)