コード例 #1
0
        //=====================================================================

        /// <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);
        }
コード例 #2
0
ファイル: HtmlToMaml.cs プロジェクト: julianhaslinger/SHFB
        //=====================================================================

        /// <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);
        }
コード例 #3
0
        /// <summary>
        /// Main program entry point
        /// </summary>
        /// <param name="args">The command line arguments (the source folder,
        /// the destination folder, and the optional "/companion" option to
        /// create companion files for each topic file).</param>
        /// <returns>Zero on success, non-zero on failure</returns>
        public static int Main(string[] args)
        {
            string sourceFolder = null, destFolder = null;
            bool success = false, createCompanionFile = false, moveIntro = false, badArgs = false;

            Assembly asm = Assembly.GetExecutingAssembly();

            FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(asm.Location);
            Console.WriteLine("{0}, version {1}\r\n{2}\r\nE-Mail: [email protected]\r\n", fvi.ProductName,
                fvi.ProductVersion, fvi.LegalCopyright);

            try
            {
                if(args.Length < 2 || args.Length > 4)
                    badArgs = true;
                else
                {
                    sourceFolder = Path.GetFullPath(args[0]);
                    destFolder = Path.GetFullPath(args[1]);

                    for(int i = 2; i < args.Length; i++)
                        if(args[i].Length > 1 && args[i].Substring(1).Equals("companion",
                          StringComparison.OrdinalIgnoreCase))
                            createCompanionFile = true;
                        else
                            if(args[i].Length > 1 && args[i].Substring(1).Equals("moveIntro",
                              StringComparison.OrdinalIgnoreCase))
                                moveIntro = true;
                            else
                                badArgs = true;
                }

                if(badArgs)
                {
                    Console.WriteLine("Syntax: ConvertHtmlToMaml sourceFolder destFolder " +
                        "[/companion] [/moveIntro]");
                    return 1;
                }

                if(!Directory.Exists(sourceFolder))
                {
                    Console.WriteLine("Source folder does not exist");
                    return 2;
                }

                if(String.Compare(sourceFolder, destFolder, StringComparison.OrdinalIgnoreCase) == 0)
                {
                    Console.WriteLine("Destination folder cannot match source folder");
                    return 3;
                }

                if(!Directory.Exists(destFolder))
                    Directory.CreateDirectory(destFolder);

                swLog = new StreamWriter(Path.Combine(destFolder, "_ConversionLog.log"));

                HtmlToMaml htmlToMaml = new HtmlToMaml(sourceFolder, destFolder, createCompanionFile,
                    moveIntro);

                htmlToMaml.ConversionProgress += htmlToMaml_ConversionProgress;

                htmlToMaml.ConvertTopics();
                success = true;
            }
            catch(Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.ToString());
                Console.WriteLine(ex.ToString());
            }
            finally
            {
                if(swLog != null)
                    swLog.Close();

#if DEBUG
                if(System.Diagnostics.Debugger.IsAttached)
                {
                    Console.WriteLine("Hit ENTER...");
                    Console.ReadLine();
                }
#endif
            }

            return (success) ? 0 : 4;
        }
コード例 #4
0
ファイル: ConvertHtmlToMaml.cs プロジェクト: Tyler-Zhou/SHFB
        /// <summary>
        /// Main program entry point
        /// </summary>
        /// <param name="args">The command line arguments (the source folder, the destination folder, and the
        /// optional "/companion" option to create companion files for each topic file).</param>
        /// <returns>Zero on success, non-zero on failure</returns>
        public static int Main(string[] args)
        {
            string sourceFolder = null, destFolder = null;
            bool   success = false, createCompanionFile = false, moveIntro = false, badArgs = false;

            Assembly asm = Assembly.GetExecutingAssembly();

            FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(asm.Location);

            Console.WriteLine("{0}, version {1}\r\n{2}\r\nE-Mail: [email protected]\r\n", fvi.ProductName,
                              fvi.ProductVersion, fvi.LegalCopyright);

            try
            {
                if (args.Length < 2 || args.Length > 4)
                {
                    badArgs = true;
                }
                else
                {
                    sourceFolder = Path.GetFullPath(args[0]);
                    destFolder   = Path.GetFullPath(args[1]);

                    for (int i = 2; i < args.Length; i++)
                    {
                        if (args[i].Length > 1 && args[i].Substring(1).Equals("companion", StringComparison.OrdinalIgnoreCase))
                        {
                            createCompanionFile = true;
                        }
                        else
                        if (args[i].Length > 1 && args[i].Substring(1).Equals("moveIntro", StringComparison.OrdinalIgnoreCase))
                        {
                            moveIntro = true;
                        }
                        else
                        {
                            badArgs = true;
                        }
                    }
                }

                if (badArgs)
                {
                    Console.WriteLine("Syntax: ConvertHtmlToMaml sourceFolder destFolder [/companion] [/moveIntro]");
                    return(1);
                }

                if (!Directory.Exists(sourceFolder))
                {
                    Console.WriteLine("Source folder does not exist");
                    return(2);
                }

                if (sourceFolder.Equals(destFolder, StringComparison.OrdinalIgnoreCase))
                {
                    Console.WriteLine("Destination folder cannot match source folder");
                    return(3);
                }

                if (!Directory.Exists(destFolder))
                {
                    Directory.CreateDirectory(destFolder);
                }

                swLog = new StreamWriter(Path.Combine(destFolder, "_ConversionLog.log"));

                HtmlToMaml htmlToMaml = new HtmlToMaml(sourceFolder, destFolder, createCompanionFile, moveIntro);

                htmlToMaml.ConversionProgress += htmlToMaml_ConversionProgress;

                htmlToMaml.ConvertTopics();
                success = true;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.ToString());
                Console.WriteLine(ex.ToString());
            }
            finally
            {
                if (swLog != null)
                {
                    swLog.Close();
                }

#if DEBUG
                if (Debugger.IsAttached)
                {
                    Console.WriteLine("Hit ENTER...");
                    Console.ReadLine();
                }
#endif
            }

            return((success) ? 0 : 4);
        }