カテゴリをきれいに出力するように
This commit is contained in:
1 parent
396366dcd7
commit
e91f2ffdb4
1 file changed
+134
@@ -386,6 +386,67 @@ app.MapGet("/momonga/fanzine", async (HttpContext context, IHttpClientFactory ht
|
||||
// 元のRSSが重い画像や長文を含んでいても、ここでシンプルな説明文に上書きして削ぎ落とす
|
||||
// var simpleContent = SyndicationContent.CreatePlaintextContent("記事の詳細はリンク先で確認してください。");
|
||||
|
||||
List<SyndicationCategory> getAddCategory()
|
||||
{
|
||||
List<SyndicationCategory> result = new List<SyndicationCategory>();
|
||||
|
||||
string orgText = originalItem.Summary.Text;
|
||||
var parser = new HtmlParser();
|
||||
var document = parser.ParseDocument(orgText);
|
||||
|
||||
var firstP = document.QuerySelector("p");
|
||||
if (firstP != null)
|
||||
{
|
||||
var targetText = firstP.TextContent;
|
||||
int tagIndex = targetText.IndexOf("【タグ】");
|
||||
if (tagIndex >= 0)
|
||||
{
|
||||
// 【タグ】が見つかった場合、そこから前の文字列だけを残す
|
||||
targetText = targetText.Substring(0, tagIndex);
|
||||
}
|
||||
// ----------------------------------------------------
|
||||
// 正規表現で各項目の値を抽出する
|
||||
// ----------------------------------------------------
|
||||
// パターン解説:
|
||||
// 【[^】]+】 : 「【」で始まり、「】以外の文字」が続き、「】」で終わる文字列 (例: 【パロディ】)
|
||||
// ([^【]*) : その後、「【」以外の文字が続く限り取得する (Group1としてキャプチャ)
|
||||
string pattern = @"【[^】]+】([^【]*)";
|
||||
var matches = Regex.Matches(targetText, pattern);
|
||||
|
||||
foreach (Match match in matches)
|
||||
{
|
||||
// match.Groups[1].Value には "パロ1, パロ 2 " などの値が入ります
|
||||
// (項目の中身が空の場合は空文字になります)
|
||||
string valuesStr = match.Groups[1].Value;
|
||||
|
||||
if (string.IsNullOrWhiteSpace(valuesStr))
|
||||
{
|
||||
continue; // 中身が空の場合はスキップ
|
||||
}
|
||||
|
||||
// ----------------------------------------------------
|
||||
// 3. カンマで分割し、前後の空白を除去してカテゴリに追加
|
||||
// ----------------------------------------------------
|
||||
// ※もし全角カンマ「、」も考慮したい場合は new[] { ',', '、' } と記述します
|
||||
string[] splitValues = valuesStr.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
|
||||
|
||||
foreach (var val in splitValues)
|
||||
{
|
||||
// 前後の半角・全角スペースを取り除く
|
||||
string categoryName = val.Trim();
|
||||
|
||||
// 空でなければカテゴリとして追加
|
||||
if (!string.IsNullOrEmpty(categoryName))
|
||||
{
|
||||
// domain属性なし(前回回答の手法)でカテゴリを追加
|
||||
result.Add(new SyndicationCategory(categoryName));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
var newItem = new SyndicationItem()
|
||||
{
|
||||
// タイトルは TextSyndicationContent でラップする必要があります
|
||||
@@ -405,9 +466,15 @@ app.MapGet("/momonga/fanzine", async (HttpContext context, IHttpClientFactory ht
|
||||
{
|
||||
newItem.Links.Add(new SyndicationLink(link));
|
||||
}
|
||||
// カテゴリに本文内の作者などの情報を追加する
|
||||
foreach (var category in getAddCategory())
|
||||
{
|
||||
newItem.Categories.Add(category);
|
||||
}
|
||||
// 【追加】元のRSSが持っていたカテゴリ(タグ)をそのまま引き継ぐ
|
||||
foreach (var category in originalItem.Categories)
|
||||
{
|
||||
category.Scheme = null;
|
||||
newItem.Categories.Add(category);
|
||||
}
|
||||
foreach (var author in originalItem.Authors)
|
||||
@@ -505,6 +572,67 @@ app.MapGet("/momonga/magazine", async (HttpContext context, IHttpClientFactory h
|
||||
// 元のRSSが重い画像や長文を含んでいても、ここでシンプルな説明文に上書きして削ぎ落とす
|
||||
// var simpleContent = SyndicationContent.CreatePlaintextContent("記事の詳細はリンク先で確認してください。");
|
||||
|
||||
List<SyndicationCategory> getAddCategory()
|
||||
{
|
||||
List<SyndicationCategory> result = new List<SyndicationCategory>();
|
||||
|
||||
string orgText = originalItem.Summary.Text;
|
||||
var parser = new HtmlParser();
|
||||
var document = parser.ParseDocument(orgText);
|
||||
|
||||
var firstP = document.QuerySelector("p");
|
||||
if (firstP != null)
|
||||
{
|
||||
var targetText = firstP.TextContent;
|
||||
int tagIndex = targetText.IndexOf("【タグ】");
|
||||
if (tagIndex >= 0)
|
||||
{
|
||||
// 【タグ】が見つかった場合、そこから前の文字列だけを残す
|
||||
targetText = targetText.Substring(0, tagIndex);
|
||||
}
|
||||
// ----------------------------------------------------
|
||||
// 正規表現で各項目の値を抽出する
|
||||
// ----------------------------------------------------
|
||||
// パターン解説:
|
||||
// 【[^】]+】 : 「【」で始まり、「】以外の文字」が続き、「】」で終わる文字列 (例: 【パロディ】)
|
||||
// ([^【]*) : その後、「【」以外の文字が続く限り取得する (Group1としてキャプチャ)
|
||||
string pattern = @"【[^】]+】([^【]*)";
|
||||
var matches = Regex.Matches(targetText, pattern);
|
||||
|
||||
foreach (Match match in matches)
|
||||
{
|
||||
// match.Groups[1].Value には "パロ1, パロ 2 " などの値が入ります
|
||||
// (項目の中身が空の場合は空文字になります)
|
||||
string valuesStr = match.Groups[1].Value;
|
||||
|
||||
if (string.IsNullOrWhiteSpace(valuesStr))
|
||||
{
|
||||
continue; // 中身が空の場合はスキップ
|
||||
}
|
||||
|
||||
// ----------------------------------------------------
|
||||
// 3. カンマで分割し、前後の空白を除去してカテゴリに追加
|
||||
// ----------------------------------------------------
|
||||
// ※もし全角カンマ「、」も考慮したい場合は new[] { ',', '、' } と記述します
|
||||
string[] splitValues = valuesStr.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
|
||||
|
||||
foreach (var val in splitValues)
|
||||
{
|
||||
// 前後の半角・全角スペースを取り除く
|
||||
string categoryName = val.Trim();
|
||||
|
||||
// 空でなければカテゴリとして追加
|
||||
if (!string.IsNullOrEmpty(categoryName))
|
||||
{
|
||||
// domain属性なし(前回回答の手法)でカテゴリを追加
|
||||
result.Add(new SyndicationCategory(categoryName));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
var newItem = new SyndicationItem()
|
||||
{
|
||||
// タイトルは TextSyndicationContent でラップする必要があります
|
||||
@@ -524,9 +652,15 @@ app.MapGet("/momonga/magazine", async (HttpContext context, IHttpClientFactory h
|
||||
{
|
||||
newItem.Links.Add(new SyndicationLink(link));
|
||||
}
|
||||
// カテゴリに本文内の作者などの情報を追加する
|
||||
foreach (var category in getAddCategory())
|
||||
{
|
||||
newItem.Categories.Add(category);
|
||||
}
|
||||
// 【追加】元のRSSが持っていたカテゴリ(タグ)をそのまま引き継ぐ
|
||||
foreach (var category in originalItem.Categories)
|
||||
{
|
||||
category.Scheme = null;
|
||||
newItem.Categories.Add(category);
|
||||
}
|
||||
foreach (var author in originalItem.Authors)
|
||||
|
||||
Reference in new issue
Block a user