Esempio n. 1
0
        public void TestStuff(string input, string expected)
        {
            var outputter = new StringBuilderOutputter();
            var formatter = new TextileFormatter(outputter);

            formatter.Format(input);
            var actual = outputter.GetFormattedText();

            Bitmap diff;

            if (!actual.RendersEqual(expected, out diff))
            {
                var path = Path.Combine(Path.GetTempPath(), "Textile.Test");

                if (Directory.Exists(path))
                {
                }

                Directory.CreateDirectory(path);
                var filename = Path.Combine(path, $"{TestContext.CurrentContext.Test.Name}.png");

                diff.Save(filename);

                Assert.Fail($"HTML does not match.\r\nExpected:\r\n{expected}\r\n\r\nActual:\r\n{actual}\r\n\r\nDiff image: {filename}");
            }
        }
Esempio n. 2
0
        protected override void ExecuteTask()
        {
            // ensure base directory is set, even if fileset was not initialized from XML
            if (Inputs.BaseDirectory == null)
            {
                Inputs.BaseDirectory = new DirectoryInfo(Project.BaseDirectory);
            }
            // get the complete path of the base directory of the fileset, ie, c:\work\nant\src
            DirectoryInfo srcBaseInfo = Inputs.BaseDirectory;

            #region Configure TextileFormatter
            StringBuilderOutputter outputter = new StringBuilderOutputter();
            TextileFormatter       tf        = new TextileFormatter(outputter);
            tf.FormatFootNotes = this.FormatFootNotes;
            tf.FormatImages    = this.FormatImages;
            tf.FormatLinks     = this.FormatLinks;
            tf.FormatLists     = this.FormatLists;
            tf.FormatTables    = this.FormatTables;
            tf.HeaderOffset    = this.HeaderOffset;
            tf.Rel             = this.LinkRel;
            #endregion

            int numConvertedFiles = 0;
            foreach (string pathname in Inputs.FileNames)
            {
                FileInfo srcInfo = new FileInfo(pathname);
                if (srcInfo.Exists)
                {
                    #region Compute destination path
                    // Gets the relative path and file info from the full source filepath
                    // pathname = C:\f2\f3\file1, srcBaseInfo=C:\f2, then
                    // dstRelFilePath=f3\file1
                    string dstRelFilePath = "";
                    if (srcInfo.FullName.IndexOf(srcBaseInfo.FullName, 0) != -1)
                    {
                        dstRelFilePath = srcInfo.FullName.Substring(srcBaseInfo.FullName.Length);
                    }
                    else
                    {
                        dstRelFilePath = srcInfo.Name;
                    }

                    if (dstRelFilePath[0] == Path.DirectorySeparatorChar)
                    {
                        dstRelFilePath = dstRelFilePath.Substring(1);
                    }
                    #endregion

                    // The full filepath of the destination
                    string dstFilePath = Path.Combine(ToDirectory.FullName, dstRelFilePath);

                    string parentFolder = Path.GetDirectoryName(dstFilePath);
                    if (!Directory.Exists(parentFolder))
                    {
                        Directory.CreateDirectory(parentFolder);
                    }

                    #region Read, format and write
                    string inputTextile;

                    Log(Level.Debug, "Reading input file '{0}'", srcInfo.FullName);
                    using (StreamReader sr = new StreamReader(srcInfo.FullName))
                    {
                        inputTextile = sr.ReadToEnd();
                    }

                    Log(Level.Verbose, "Converting textile from file '{0}' to an HTML string...", srcInfo.FullName);
                    tf.Format(inputTextile);
                    string outputHtml = outputter.GetFormattedText();

                    // TODO: make output extension configurable
                    string outputPath = Path.ChangeExtension(dstFilePath, ".html");
                    // TODO: Add a switch to optionally hide (or at least make it verbose) the following log statement
                    Log(Level.Info, "Generating {0}...", outputPath);
                    using (StreamWriter sw = new StreamWriter(outputPath))
                    {
                        sw.Write(outputHtml);
                    }
                    Log(Level.Verbose, "Generated {0}.", outputPath);
                    #endregion
                    numConvertedFiles++;
                }
                else
                {
                    throw new BuildException(string.Format(CultureInfo.InvariantCulture,
                                                           "Could not find file '{0}' to convert with Textile.", srcInfo.FullName),
                                             Location);
                }
            }
            Log(Level.Verbose, "Converted {0} files.", numConvertedFiles);
        }