public void TestReplaceContent() { var entries = new [] { new PseudoPostedEntry() { Content = "foobar", Title = "foobar" }, }; var editor = new EntryEditor("foo", "bar", new EntryContentModifier()); EditAllEntry( entries, editor, out _, out var modifiedEntries ); Assert.AreEqual(1, modifiedEntries.Count); Assert.AreEqual("barbar", modifiedEntries[0].Content); Assert.AreEqual("foobar", modifiedEntries[0].Title); }
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(); } }