private static void EditAllEntry( IReadOnlyList <PostedEntry> entries, IHatenaBlogEntryEditor editor, out IReadOnlyList <PostedEntry> updatedEntries, out IReadOnlyList <PostedEntry> modifiedEntries ) { var hatenaBlog = HatenaBlogAtomPubClient.Create(entries); HatenaBlogFunctions.EditAllEntry( hatenaBlog, HatenaBlogFunctions.PostMode.PostIfModified, editor, DiffGenerator.Create(silent: true, null, null, null, null), null, null, out updatedEntries, out modifiedEntries ); }
public void Run(string[] args) { if (!ParseCommonCommandLineArgs(ref args, new[] { "-diff-test" }, out var credential)) { return; } string replaceFromText = null; string replaceToText = null; bool replaceAsRegex = false; bool replaceTitleInsteadOfContent = false; string diffCommand = null; string diffCommandArgs = null; bool testDiffCommand = false; bool dryrun = false; bool confirm = false; for (var i = 0; i < args.Length; i++) { switch (args[i]) { case "--from": case "-from": replaceFromText = args[++i]; break; case "--to": case "-to": replaceToText = args[++i]; break; case "--regex": case "-regex": replaceAsRegex = true; break; case "--diff-cmd": diffCommand = args[++i]; break; case "--diff-cmd-args": diffCommandArgs = args[++i]; break; case "--diff-test": testDiffCommand = true; break; case "--replace-title-only": replaceTitleInsteadOfContent = true; break; case "--dry-run": case "-n": dryrun = true; break; case "--interactive": case "-i": confirm = true; break; } } var descriptionOfReplacementTarget = replaceTitleInsteadOfContent ? "タイトル" : "本文"; var diffGenerator = DiffGenerator.Create(false, diffCommand, diffCommandArgs, $"置換前の{descriptionOfReplacementTarget}", $"置換前の{descriptionOfReplacementTarget}"); if (testDiffCommand) { DiffGenerator.Test(diffGenerator); return; } if (string.IsNullOrEmpty(replaceFromText)) { Usage("置換する文字列を指定してください"); return; } if (replaceToText == null) { replaceToText = string.Empty; // delete } var modifier = replaceTitleInsteadOfContent ? (EntryTextModifier) new EntryTitleModifier() : (EntryTextModifier) new EntryContentModifier(); var editor = replaceAsRegex ? (IHatenaBlogEntryEditor) new RegexEntryEditor(replaceFromText, replaceToText, modifier) : (IHatenaBlogEntryEditor) new EntryEditor(replaceFromText, replaceToText, modifier); var postMode = dryrun ? HatenaBlogFunctions.PostMode.PostNever : HatenaBlogFunctions.PostMode.PostIfModified; Func <bool> confirmBeforePosting = null; if (confirm) { confirmBeforePosting = () => ConsoleUtils.AskYesNo(false, "更新しますか"); } if (!Login(credential, out var hatenaBlog)) { return; } var success = true; try { HatenaBlogFunctions.EditAllEntry( hatenaBlog, postMode, editor, diffGenerator, entryUrlSkipTo: null, confirmBeforePosting, out _, out _ ); } catch (PostEntryFailedException ex) { success = false; Console.Error.WriteLine(ex); Console.ForegroundColor = ConsoleColor.Red; if (ex.CausedEntry is PostedEntry entry) { Console.WriteLine($"エントリの更新に失敗しました ({entry.EntryUri} \"{entry.Title}\")"); } else { Console.WriteLine($"エントリの投稿に失敗しました"); } Console.ResetColor(); } if (success) { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("完了"); Console.ResetColor(); } else { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("エラーにより中断しました"); Console.ResetColor(); } }
public void Run(string[] args) { if (!ParseCommonCommandLineArgs(ref args, new[] { "--diff-test", "--input-content", "--output-content" }, out var credential)) { return; } bool postAlways = false; bool fixMixedContent = false; var fixMixedContentDomainsInclude = new HashSet <string>(StringComparer.Ordinal); var fixMixedContentDomainsExclude = new HashSet <string>(StringComparer.Ordinal); bool fixBlogUrl = false; string customBlogDomain = null; Uri entryUrlSkipTo = null; string diffCommand = null; string diffCommandArgs = null; bool testDiffCommand = false; string contentInput = null; string contentOutput = null; bool editLocalContent = false; bool dryRun = false; bool confirm = false; bool listFixedEntries = false; for (var i = 0; i < args.Length; i++) { switch (args[i]) { case "--update-content": postAlways = true; break; case "--fix-mixed-content": fixMixedContent = true; break; case "--include-domain": fixMixedContentDomainsInclude.Add(args[++i]); break; case "--exclude-domain": fixMixedContentDomainsExclude.Add(args[++i]); break; case "--fix-blog-url": fixBlogUrl = true; break; case "--custom-domain": customBlogDomain = args[++i]; break; case "--entry-url-skip-to": entryUrlSkipTo = new Uri(args[++i]); break; case "--diff-cmd": diffCommand = args[++i]; break; case "--diff-cmd-args": diffCommandArgs = args[++i]; break; case "--diff-test": testDiffCommand = true; break; case "--input-content": contentInput = args[++i]; editLocalContent = true; break; case "--output-content": contentOutput = args[++i]; editLocalContent = true; break; case "--dry-run": case "-n": dryRun = true; break; case "--interactive": case "-i": confirm = true; break; case "--list-fixed-entry": listFixedEntries = true; break; } } var diffGenerator = DiffGenerator.Create(false, diffCommand, diffCommandArgs, "変更前の本文", "変更後の本文"); if (testDiffCommand) { DiffGenerator.Test(diffGenerator); return; } if (editLocalContent && fixBlogUrl && string.IsNullOrEmpty(customBlogDomain)) { Usage("--custom-domainを指定してください"); return; } Predicate <Html.HtmlAttribute> predicateForFixMixedContent = null; if (fixMixedContent) { if (0 < fixMixedContentDomainsInclude.Count && 0 < fixMixedContentDomainsExclude.Count) { Usage("--exclude-domainと--include-domainを同時に指定することはできません"); return; } else { var include = (0 < fixMixedContentDomainsInclude.Count); var domainList = include ? fixMixedContentDomainsInclude : fixMixedContentDomainsExclude; var domainPrefixList = domainList.Select(domain => "//" + domain + "/").ToList(); predicateForFixMixedContent = attr => { foreach (var domainPrefix in domainPrefixList) { if (attr.Value.Contains(domainPrefix)) { return(include); } } return(!include); }; } } var editor = new EntryEditor(blogDomain: credential?.BlogId, customBlogDomain: customBlogDomain, fixMixedContent: fixMixedContent, predicateForFixMixedContent: predicateForFixMixedContent, replaceBlogUrl: fixBlogUrl); if (editLocalContent) { EditContent(editor, contentInput, contentOutput); return; } var postMode = HatenaBlogFunctions.PostMode.PostIfModified; if (postAlways) { postMode = HatenaBlogFunctions.PostMode.PostAlways; } if (dryRun) { postMode = HatenaBlogFunctions.PostMode.PostNever; } Func <bool> confirmBeforePosting = null; if (confirm) { confirmBeforePosting = () => ConsoleUtils.AskYesNo(false, "更新しますか"); } if (!Login(credential, out var hatenaBlog)) { return; } IReadOnlyList <PostedEntry> updatedEntries = null; IReadOnlyList <PostedEntry> modifiedEntries = null; var success = true; try { HatenaBlogFunctions.EditAllEntry( hatenaBlog, postMode, editor, diffGenerator, entryUrlSkipTo, confirmBeforePosting, out updatedEntries, out modifiedEntries ); } catch (PostEntryFailedException ex) { success = false; Console.Error.WriteLine(ex); Console.ForegroundColor = ConsoleColor.Red; if (ex.CausedEntry is PostedEntry entry) { Console.WriteLine($"エントリの更新に失敗しました ({entry.EntryUri} \"{entry.Title}\")"); } else { Console.WriteLine($"エントリの投稿に失敗しました"); } Console.ResetColor(); } if (listFixedEntries && modifiedEntries != null && 0 < modifiedEntries.Count) { Console.WriteLine(); Console.WriteLine("下記エントリに対して修正を行い再投稿しました。"); foreach (var modifiedEntry in modifiedEntries) { Console.WriteLine("{0} \"{1}\"", modifiedEntry.EntryUri, modifiedEntry.Title); } } if (success) { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("完了"); Console.ResetColor(); } else { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("エラーにより中断しました"); Console.ResetColor(); } }