From c9c992b93c4ba381873ca77e48dd7bb9c9c8b0ce Mon Sep 17 00:00:00 2001 From: Still Hsu <341464@gmail.com> Date: Sat, 11 Aug 2018 02:50:39 +0800 Subject: [PATCH] Refactor & cleanup --- .../ArticleType.cs | 11 +++ .../DescriptionPostProcessor.cs | 73 +++++-------------- .../StringHelper.cs | 32 ++++++++ 3 files changed, 60 insertions(+), 56 deletions(-) create mode 100644 DocFX.Plugin.DescriptionGenerator/ArticleType.cs create mode 100644 DocFX.Plugin.DescriptionGenerator/StringHelper.cs diff --git a/DocFX.Plugin.DescriptionGenerator/ArticleType.cs b/DocFX.Plugin.DescriptionGenerator/ArticleType.cs new file mode 100644 index 0000000..d0274eb --- /dev/null +++ b/DocFX.Plugin.DescriptionGenerator/ArticleType.cs @@ -0,0 +1,11 @@ +namespace DocFX.Plugin.DescriptionGenerator +{ + /// + /// Defines the type of article. + /// + internal enum ArticleType + { + Conceptual, + Reference + } +} \ No newline at end of file diff --git a/DocFX.Plugin.DescriptionGenerator/DescriptionPostProcessor.cs b/DocFX.Plugin.DescriptionGenerator/DescriptionPostProcessor.cs index f3f5727..3345ddf 100644 --- a/DocFX.Plugin.DescriptionGenerator/DescriptionPostProcessor.cs +++ b/DocFX.Plugin.DescriptionGenerator/DescriptionPostProcessor.cs @@ -1,6 +1,9 @@ using System; +using System.Collections.Generic; using System.Collections.Immutable; +using System.Collections.ObjectModel; using System.Composition; +using System.Diagnostics; using System.IO; using System.Linq; using System.Reflection; @@ -12,7 +15,7 @@ namespace DocFX.Plugin.DescriptionGenerator { [Export(nameof(DescriptionPostProcessor), typeof(IPostProcessor))] - public class DescriptionPostProcessor : IPostProcessor + public partial class DescriptionPostProcessor : IPostProcessor { private const int FixedDescriptionLength = 150; private const string FullStopDelimiter = ". "; @@ -37,9 +40,9 @@ public Manifest Process(Manifest manifest, string outputFolder) string sourcePath = Path.Combine(manifest.SourceBasePath, manifestItem.SourceRelativePath); string outputPath = Path.Combine(outputFolder, manifestItemOutputFile.Value.RelativePath); if (manifestItem.DocumentType == "Conceptual") - WriteDescriptionTag(sourcePath, outputPath, ArticleType.Conceptual); + WriteMetadataTag(sourcePath, outputPath, ArticleType.Conceptual); if (manifestItem.DocumentType == "ManagedReference") - WriteDescriptionTag(sourcePath, outputPath, ArticleType.Reference); + WriteMetadataTag(sourcePath, outputPath, ArticleType.Reference); } } @@ -53,12 +56,13 @@ public Manifest Process(Manifest manifest, string outputFolder) /// The original article path. /// The output path. /// The type of document. - private void WriteDescriptionTag(string sourcePath, string outputPath, ArticleType type) + private static void WriteMetadataTag(string sourcePath, string outputPath, ArticleType type) { Logger.LogVerbose($"Processing metadata from {sourcePath} to {outputPath}..."); var htmlDoc = new HtmlDocument(); htmlDoc.Load(outputPath); + // Write description string descriptionText = string.Empty; switch (type) { @@ -69,78 +73,35 @@ private void WriteDescriptionTag(string sourcePath, string outputPath, ArticleTy int articlePunctuationPos = articleInnerText.IndexOf(FullStopDelimiter, StringComparison.Ordinal); descriptionText = articlePunctuationPos <= FixedDescriptionLength && articlePunctuationPos > 0 ? articleInnerText.Remove(articlePunctuationPos + FullStopDelimiter.Length).Trim() - : Truncate(articleInnerText, FixedDescriptionLength, "..."); + : articleInnerText.Truncate(FixedDescriptionLength, "..."); break; case ArticleType.Reference: - var sb = new StringBuilder(); - - var memberName = htmlDoc.DocumentNode.SelectSingleNode("//article[@id='_content']/h1")?.InnerText; - if (!string.IsNullOrEmpty(memberName)) sb.Append(memberName.Trim()); - var memberDescription = htmlDoc.DocumentNode.SelectSingleNode("//div[contains(@class, 'level0 summary')]/p")?.InnerText; - if (!string.IsNullOrEmpty(memberDescription)) sb.Append($" {char.ToLowerInvariant(memberDescription[0]) + memberDescription.Substring(1)}"); - - descriptionText = sb.ToString(); + if (!string.IsNullOrEmpty(memberDescription)) descriptionText = memberDescription; break; } - if (string.IsNullOrEmpty(descriptionText)) return; - AppendDescription(htmlDoc, descriptionText).Save(outputPath); - _savedFiles++; + if (!string.IsNullOrEmpty(descriptionText)) + AppendMetadata(htmlDoc, descriptionText).Save(outputPath); } /// /// Appends the description text into the meta tag of the document. /// /// The document to be modified. - /// The text to append. + /// The type of metadata to inject. + /// The text to append. /// - /// The modified . + /// The modified . /// - private static HtmlDocument AppendDescription(HtmlDocument htmlDoc, string descriptionText) + private static HtmlDocument AppendMetadata(HtmlDocument htmlDoc, string value) { var headerNode = htmlDoc.DocumentNode.SelectSingleNode("//head"); var metaDescriptionNode = htmlDoc.CreateElement("meta"); metaDescriptionNode.SetAttributeValue("name", "description"); - metaDescriptionNode.SetAttributeValue("content", descriptionText); + metaDescriptionNode.SetAttributeValue("content", value); headerNode.AppendChild(metaDescriptionNode); return htmlDoc; } - - /// - /// Truncates the specified string. - /// - /// - /// This method truncates the input string. - /// - /// This snippet is fetched from the Humanizer project, which is licensed under the MIT license. - /// Copyright(c) .NET Foundation and Contributors - /// - /// - /// - private static string Truncate(string value, int length, string truncationString) - { - if (value == null) - return null; - - if (value.Length == 0) - return value; - - if (truncationString == null || truncationString.Length > length) - return value.Substring(0, length); - - return value.Length > length - ? value.Substring(0, length - truncationString.Length) + truncationString - : value; - } - } - - /// - /// Defines the type of article. - /// - internal enum ArticleType - { - Conceptual, - Reference } } \ No newline at end of file diff --git a/DocFX.Plugin.DescriptionGenerator/StringHelper.cs b/DocFX.Plugin.DescriptionGenerator/StringHelper.cs new file mode 100644 index 0000000..0b18839 --- /dev/null +++ b/DocFX.Plugin.DescriptionGenerator/StringHelper.cs @@ -0,0 +1,32 @@ +namespace DocFX.Plugin.DescriptionGenerator +{ + public static class StringHelper + { + /// + /// Truncates the specified string. + /// + /// + /// This method truncates the input string. + /// + /// This snippet is fetched from the Humanizer project, which is licensed under the MIT license. + /// Copyright(c) .NET Foundation and Contributors + /// + /// + /// + public static string Truncate(this string value, int length, string truncationString) + { + if (value == null) + return null; + + if (value.Length == 0) + return value; + + if (truncationString == null || truncationString.Length > length) + return value.Substring(0, length); + + return value.Length > length + ? value.Substring(0, length - truncationString.Length) + truncationString + : value; + } + } +} \ No newline at end of file