/// <summary> /// Loads the specified config file. /// </summary> /// <param name="file">The config file.</param> /// <returns></returns> public static Config Load(string file, TopicContentLoaderDelegate contentLoader) { var deserializer = new XmlSerializer(typeof(Config)); var config = (Config)deserializer.Deserialize(new StringReader(File.ReadAllText(file))); config.FilePath = file; // Update Config for all topics if (config.RootTopic != null) { config.RootTopic.ForEachTopic(topic => topic.Config = config); config.RootTopic.Init(contentLoader); } return(config); }
/// <summary> /// Loads the specified config file. /// </summary> /// <param name="file">The config file.</param> /// <returns></returns> public static Config Load(string file, TopicContentLoaderDelegate contentLoader) { var deserializer = new XmlSerializer(typeof(Config)); var config = (Config)deserializer.Deserialize(new StringReader(File.ReadAllText(file))); config.FilePath = file; // Update Config for all topics if (config.RootTopic != null) { config.RootTopic.ForEachTopic(topic => topic.Config = config); config.RootTopic.Init(contentLoader); } return config; }
/// <summary> /// Loads the content of this topic. /// </summary> /// <param name="contentLoader">The template factory.</param> /// <exception cref="System.ArgumentNullException">contentLoader</exception> public void Init(TopicContentLoaderDelegate contentLoader) { if (contentLoader == null) throw new ArgumentNullException("contentLoader"); // Check that id is valid if (string.IsNullOrEmpty(Id)) Logger.Error("Missing id for topic [{0}]", this); // Check that name is valid if (string.IsNullOrEmpty(Name)) { if (Id == ClassLibraryTopicId) { Name = "Class Library"; } else { Logger.Error("Missing name for topic [{0}]", this); } } // Copy Name to Fullname if empty if (string.IsNullOrEmpty(FullName)) FullName = Name; if (string.IsNullOrEmpty(PageTitle)) PageTitle = Name; var rootPath = Path.GetDirectoryName(Config.FilePath); rootPath = rootPath ?? ""; // Initialize sub topics foreach(var topic in SubTopics) topic.Init(contentLoader); // Create non existing topic files based on template if (Config.TopicTemplate != null && File.Exists(Config.TopicTemplate)) { var regex = new Regex(@"(\$\w+)"); if (FileName != null && !File.Exists(FileName)) { var content = File.ReadAllText(Config.TopicTemplate); content = regex.Replace( content, match => { var propertyName = match.Groups[1].Value.Substring(1); var value = this.GetType().GetProperty(propertyName).GetValue(this, null); return value == null ? string.Empty : value.ToString(); }); File.WriteAllText(FileName, content); } } if (Id != ClassLibraryTopicId) { // Load content file if (!string.IsNullOrEmpty(FileName)) { string filePath = null; try { filePath = Path.Combine(rootPath, FileName); var rawContent = contentLoader(filePath); if (rawContent == null) { Logger.Warning("Cannot use template documentation [{0}] not supported", FileName); return; } var htmlDocument = new HtmlDocument(); htmlDocument.LoadHtml(rawContent); // Override title var titleNode = htmlDocument.DocumentNode.SelectSingleNode("html/head/title"); if (titleNode != null) { PageTitle = titleNode.InnerText; Name = titleNode.InnerText; } // Get body var bodyNode = htmlDocument.DocumentNode.Descendants("body").FirstOrDefault(); Content = bodyNode == null ? rawContent : bodyNode.InnerHtml; Content = Content.Trim(); } catch (Exception ex) { Logger.Error("Cannot load content for topic [{0}] from path [{1}]. Reason: {2}", this, filePath, ex.Message); } } else if (!string.IsNullOrEmpty(WebDoc)) { Content = "<webdoc>" + WebDoc + "</webdoc>"; } else { // Check that filename is valid Logger.Error("Filname or WebDoc for topic [{0}] cannot be empty", this); } } }
/// <summary> /// Loads the content of this topic. /// </summary> /// <param name="contentLoader">The template factory.</param> /// <exception cref="System.ArgumentNullException">contentLoader</exception> public void Init(TopicContentLoaderDelegate contentLoader) { if (contentLoader == null) { throw new ArgumentNullException("contentLoader"); } // Check that id is valid if (string.IsNullOrEmpty(Id)) { Logger.Error("Missing id for topic [{0}]", this); } // Check that name is valid if (string.IsNullOrEmpty(Name)) { if (Id == ClassLibraryTopicId) { Name = "Class Library"; } else { Logger.Error("Missing name for topic [{0}]", this); } } // Copy Name to Fullname if empty if (string.IsNullOrEmpty(FullName)) { FullName = Name; } if (string.IsNullOrEmpty(PageTitle)) { PageTitle = Name; } var rootPath = Path.GetDirectoryName(Config.FilePath); rootPath = rootPath ?? ""; // Initialize sub topics foreach (var topic in SubTopics) { topic.Init(contentLoader); } // Create non existing topic files based on template if (Config.TopicTemplate != null && File.Exists(Config.TopicTemplate)) { var regex = new Regex(@"(\$\w+)"); if (FileName != null && !File.Exists(FileName)) { var content = File.ReadAllText(Config.TopicTemplate); content = regex.Replace( content, match => { var propertyName = match.Groups[1].Value.Substring(1); var value = this.GetType().GetProperty(propertyName).GetValue(this, null); return(value == null ? string.Empty : value.ToString()); }); File.WriteAllText(FileName, content); } } if (Id != ClassLibraryTopicId) { // Load content file if (!string.IsNullOrEmpty(FileName)) { string filePath = null; try { filePath = Path.Combine(rootPath, FileName); var rawContent = contentLoader(filePath); if (rawContent == null) { Logger.Warning("Cannot use template documentation [{0}] not supported", FileName); return; } var htmlDocument = new HtmlDocument(); htmlDocument.LoadHtml(rawContent); // Override title var titleNode = htmlDocument.DocumentNode.SelectSingleNode("html/head/title"); if (titleNode != null) { PageTitle = titleNode.InnerText; Name = titleNode.InnerText; } // Get body var bodyNode = htmlDocument.DocumentNode.Descendants("body").FirstOrDefault(); Content = bodyNode == null ? rawContent : bodyNode.InnerHtml; Content = Content.Trim(); } catch (Exception ex) { Logger.Error("Cannot load content for topic [{0}] from path [{1}]. Reason: {2}", this, filePath, ex.Message); } } else if (!string.IsNullOrEmpty(WebDoc)) { Content = "<webdoc>" + WebDoc + "</webdoc>"; } else { // Check that filename is valid Logger.Error("Filname or WebDoc for topic [{0}] cannot be empty", this); } } }