Last active
March 29, 2025 06:05
-
-
Save guitarrapc/ccb1b238be9ac326ed4c0f865fb494c3 to your computer and use it in GitHub Desktop.
HatenaBlog Content copy to draft_entries, if draft title is same as customPath
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var draftBasePath = @"{YOUR_GITHUB_PATH}/blogdraft_entries\"; | |
var basePath = @"{YOUR_GITHUB_PATH}/entries/{YOUR_ACCOUNT}.hatenablog.com/entry/"; | |
var targetMonths = new[] { | |
// 持ってきたい日付を並べる | |
"2025/03" | |
}; | |
var titlePattern = new Regex("201[2,3]/[0-9]{2}/[0-9]{2}/[0-9]{6}", RegexOptions.Compiled); | |
var drafts = Directory.EnumerateFiles(draftBasePath, "*.md"); | |
foreach (var draft in drafts) | |
{ | |
var title = File.ReadLines(draft) | |
.Where(x => x.StartsWith("Title:")) | |
.Select(x => x.Split(":")[1].Trim()) | |
.Single(); | |
if (!titlePattern.IsMatch(title)) | |
continue; | |
var draftContent = File.ReadAllLines(draft); | |
var draftEditUrlLine = draftContent.Where(x => x.StartsWith("EditURL: ")).Single(); | |
var draftPreviewURL = draftContent.Where(x => x.StartsWith("PreviewURL: ")).Single(); | |
foreach (var targetMonth in targetMonths) | |
{ | |
var searchBase = Path.Combine(basePath, targetMonth); | |
var files = Directory.EnumerateFiles(searchBase, "*.md", SearchOption.AllDirectories); | |
foreach (var file in files) | |
{ | |
// _2.md は除外 | |
if (Path.GetFileNameWithoutExtension(file).EndsWith("_2")) | |
continue; | |
var lines = File.ReadAllLines(file); | |
if (lines.Any(x => x.Contains($"CustomPath: {title}"))) | |
{ | |
var sectionLines = GetHeaderSectionLines(lines); | |
var titleLine = sectionLines.Where(x => x.StartsWith("Title: ")).Single(); | |
var categoryLine = GetCategories(sectionLines); | |
var dateLine = sectionLines.Where(x => x.StartsWith("Date: ")).Single(); | |
var urlLine = sectionLines.Where(x => x.StartsWith("URL: ")).Single(); | |
var customPathLine = sectionLines.Where(x => x.StartsWith("CustomPath: ")).Single(); | |
var contentBuilder = new StringBuilder(); | |
// ヘッダー | |
contentBuilder.AppendLine("---"); | |
contentBuilder.AppendLine(titleLine); | |
contentBuilder.AppendLine(categoryLine); | |
contentBuilder.AppendLine(dateLine); | |
contentBuilder.AppendLine(draftEditUrlLine); | |
contentBuilder.AppendLine(draftPreviewURL); | |
contentBuilder.AppendLine(customPathLine); | |
contentBuilder.AppendLine("---"); | |
// コンテンツ | |
var contentsLines = lines.Skip(sectionLines.Length); | |
contentBuilder.AppendLine(); | |
contentBuilder.AppendLine("<!--"); | |
contentBuilder.AppendLine($" {dateLine}"); | |
contentBuilder.AppendLine($" {urlLine}"); | |
contentBuilder.AppendLine("-->"); | |
foreach (var line in contentsLines) | |
{ | |
contentBuilder.AppendLine(line); | |
} | |
var content = contentBuilder.ToString(); | |
// 書き込み | |
if (content != "") | |
{ | |
File.WriteAllText(draft, content); | |
} | |
} | |
} | |
} | |
} | |
static string[] GetHeaderSectionLines(string[] lines) | |
{ | |
int number = 0; | |
var inSection = false; | |
foreach (var line in lines) | |
{ | |
// enter | |
if (!inSection && line == "---") | |
{ | |
inSection = true; | |
number++; | |
continue; | |
} | |
// exit | |
if (inSection && line == "---") | |
{ | |
inSection = false; | |
number++; | |
break; | |
} | |
// inside | |
if (inSection && line != "---") | |
{ | |
number++; | |
continue; | |
} | |
} | |
return lines.Take(number).ToArray(); | |
} | |
static string GetCategories(string[] lines) | |
{ | |
var categoryLines = new StringBuilder(); | |
var inCategory = false; | |
foreach (var line in lines) | |
{ | |
if (line.StartsWith("Category:")) | |
{ | |
inCategory = true; | |
categoryLines.AppendLine(line); | |
continue; | |
} | |
if (inCategory) | |
{ | |
if (!line.StartsWith("- ")) | |
{ | |
inCategory = false; | |
break; | |
} | |
categoryLines.AppendLine(line); | |
continue; | |
} | |
} | |
// 最終行の空行はトリム除去 | |
return categoryLines.ToString().TrimEnd(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment