//===================================================================== /// <summary> /// Constructor /// </summary> /// <param name="source">The source file or null to create an empty container node with no /// associated topic</param> public Topic(string source) { id = Guid.NewGuid(); revisionNumber = "1"; sortOrder = Int32.MaxValue; if (source != null) { sourceFile = new FilePath(source, HtmlToMaml.PathProvider); } subtopics = new TopicCollection(); }
//===================================================================== /// <summary> /// Constructor /// </summary> /// <param name="source">The source path containing the HTML files</param> /// <param name="dest">The destination path for the MAML topics and /// supporting files</param> /// <param name="createCompanion">True to create companion files for /// all topic files or false to not create them.</param> /// <param name="moveIntroText">If true, move text before the first section into an introduction element. /// If false, insert a place holder introduction element.</param> public HtmlToMaml(string source, string dest, bool createCompanion, bool moveIntroText) { XPathDocument rulesFile; XPathNavigator navRules; StringBuilder sb; string name; sourcePath = source; destPath = dest; createCompanionFile = createCompanion; replaceIntro = moveIntroText; pathProvider = this; if (sourcePath.EndsWith("\\", StringComparison.Ordinal)) { sourcePath = sourcePath.Substring(0, sourcePath.Length - 1); } if (destPath.EndsWith("\\", StringComparison.Ordinal)) { destPath = destPath.Substring(0, destPath.Length - 1); } sourcePath = Path.GetFullPath(sourcePath); destPath = Path.GetFullPath(destPath); topics = new TopicCollection(); images = new ImageReferenceCollection(); topicDictionary = new Dictionary <FilePath, Topic>(); imageDictionary = new Dictionary <FilePath, ImageReference>(); conversionRules = new Dictionary <string, TagOptions>(); markupSections = new List <string>(); entities = new Dictionary <string, string>(); tokens = new Dictionary <string, string>(); // Load the conversion rules rulesFile = new XPathDocument(Path.Combine(Path.GetDirectoryName( Assembly.GetExecutingAssembly().Location), "ConversionRules.xml")); navRules = rulesFile.CreateNavigator(); XPathNavigator bodyExpr = navRules.SelectSingleNode("//BodyExtract"); if (bodyExpr != null) { FileParser.BodyExtractExpression = bodyExpr.GetAttribute( "expression", string.Empty); } // Add the tags we will handle internally conversionRules.Add("a", null); conversionRules.Add("code", null); conversionRules.Add("h1", null); conversionRules.Add("h2", null); conversionRules.Add("h3", null); conversionRules.Add("h4", null); conversionRules.Add("h5", null); conversionRules.Add("h6", null); conversionRules.Add("img", null); conversionRules.Add("see", null); // Get the rules to process sb = new StringBuilder(); foreach (XPathNavigator nav in navRules.Select("//Entities/Entity")) { entities.Add(nav.GetAttribute("name", String.Empty), nav.GetAttribute("value", String.Empty)); } foreach (XPathNavigator nav in navRules.Select("//MarkupWrapper/Tag")) { if (sb.Length != 0) { sb.Append("|"); } name = nav.GetAttribute("name", String.Empty).ToLower( CultureInfo.InvariantCulture); conversionRules.Add(name, null); sb.Append(name); } name = sb.ToString(); sb.Insert(0, @"<\s*("); sb.Append(@")[^>]*?>.*?<\s*/\s*(\1)[^>]*?>|<\s*("); sb.Append(name); sb.Append(@")\s*?((\s|/)[^>]*?)?>"); reMarkupWrapper = new Regex(sb.ToString(), RegexOptions.IgnoreCase | RegexOptions.Singleline); reReplaceMarker = new Regex("\xFF"); matchMarkupWrapper = new MatchEvaluator(OnMatchMarkupWrapper); matchMarker = new MatchEvaluator(OnMatchMarker); sb.Remove(0, sb.Length); foreach (XPathNavigator nav in navRules.Select("//Remove/Tag")) { if (sb.Length != 0) { sb.Append("|"); } name = nav.GetAttribute("name", String.Empty).ToLower( CultureInfo.InvariantCulture); conversionRules.Add(name, null); sb.Append(name); } sb.Insert(0, @"<\s*/?\s*("); sb.Append(@")\s*?((\s|/)[^>]*?)?>"); reRemoveTag = new Regex(sb.ToString(), RegexOptions.IgnoreCase | RegexOptions.Singleline); sb.Remove(0, sb.Length); foreach (XPathNavigator nav in navRules.Select("//Replace/Tag")) { if (sb.Length != 0) { sb.Append("|"); } name = nav.GetAttribute("name", String.Empty).ToLower( CultureInfo.InvariantCulture); conversionRules.Add(name, new TagOptions(nav)); sb.Append(name); } sb.Insert(0, @"<\s*(?<Closing>/?)\s*(?<Tag>"); sb.Append(@")\s*(?<Attributes>(\s|/)[^>]*?)?>"); reReplaceTag = new Regex(sb.ToString(), RegexOptions.IgnoreCase | RegexOptions.Singleline); matchReplace = new MatchEvaluator(OnMatchReplace); matchCode = new MatchEvaluator(OnMatchCode); matchSee = new MatchEvaluator(OnMatchSee); matchAnchor = new MatchEvaluator(OnMatchAnchor); matchImage = new MatchEvaluator(OnMatchImage); matchHeading = new MatchEvaluator(OnMatchHeading); matchIntroduction = new MatchEvaluator(OnMatchIntroduction); matchEntity = new MatchEvaluator(OnMatchEntity); matchToken = new MatchEvaluator(OnMatchToken); }