Example #1
0
            public static void Convert(DiscordSocketClient discord, Node node, TelegramMessage message)
            {
                var visitor = new HtmlVisitor(discord, message);

                visitor.Visit(node);
                visitor.UpdateMessage();
            }
Example #2
0
        public string Write(Node node)
        {
            var visitor = new HtmlVisitor();

            visitor.Visit(node);
            return(visitor.ToString());
        }
        public IActionResult Index(
            [NotNull] [ItemNotNull] IEnumerable<IFormFile> files,
            [CanBeNull] string format,
            [CanBeNull] string title,
            [CanBeNull] string publisher,
            [CanBeNull] string website,
            [CanBeNull] string stylesheetUrl,
            [CanBeNull] IFormFile stylesheet)
        {
            if (files is null)
                throw new ArgumentNullException(nameof(files));

            IFormFile[] uploadedFiles = files.ToArray();

            if (uploadedFiles.Length == 0)
                return BadRequest("No files uploaded.");

            if (uploadedFiles.Any(x => x.Length <= 0))
                return BadRequest("Invalid file length.");

            if (uploadedFiles.Any(x => x.ContentType != MicrosoftWordDocument.ToString() &&
                                       x.FileName.EndsWith(".docx", StringComparison.OrdinalIgnoreCase) &&
                                       x.FileName.EndsWith(".md", StringComparison.OrdinalIgnoreCase)))
                return BadRequest("Invalid file format.");

            if (stylesheet is IFormFile s && !s.FileName.EndsWith(".css"))
                return BadRequest($"Invalid stylesheet:{stylesheet.FileName}.");

            Queue<Package> packagesQueue = new Queue<Package>(uploadedFiles.Length);

            foreach (IFormFile file in uploadedFiles)
            {
                if (file.FileName.EndsWith(".docx", StringComparison.OrdinalIgnoreCase))
                {
                    packagesQueue.Enqueue(Package.Open(file.OpenReadStream()));
                }
                else if (file.FileName.EndsWith(".md", StringComparison.OrdinalIgnoreCase))
                {
                    MDocument mDocument = new MDocument();
                    using (StreamReader reader = new StreamReader(file.OpenReadStream()))
                    {
                        MarkdownVisitor visitor = new MarkdownVisitor();
                        ReadOnlySpan<char> span;
                        while ((span = reader.ReadLine()) != null)
                        {
                            mDocument.Append(visitor.Visit(in span));
                        }
                    }

                    Package package = DocxFilePath.Create().ToPackage(FileAccess.ReadWrite);

                    mDocument.ToOpenXml().WriteTo(package.GetPart(Document.PartUri));

                    packagesQueue.Enqueue(package);
                }
            }

            Package output =
                Process(
                    packagesQueue,
                    title ?? "[REPORT TITLE]",
                    publisher ?? "[PUBLISHER]",
                    website ?? "[PUBLISHER WEBSITE]");

            foreach (Package package in packagesQueue)
            {
                package.Close();
            }

            switch (format)
            {
                case "docx":
                {
                    return File(output.ToStream(), MicrosoftWordDocument, $"{title ?? "result"}.docx");
                }
                case "html":
                {
                    string styles = stylesheet is null ? null : new StreamReader(stylesheet.OpenReadStream()).ReadToEnd();
                    OpenXmlPackageVisitor ooxml = new OpenXmlPackageVisitor(output);
                    HtmlVisitor html = new HtmlVisitor(ooxml.Document.ChartReferences, ooxml.Document.ImageReferences);
                    XObject htmlResult = html.Visit(ooxml.Document.Content, ooxml.Footnotes.Content, title, stylesheetUrl, styles);
                    return File(Encoding.UTF8.GetBytes(htmlResult.ToString()), "text/html", $"{title ?? "result"}.html");
                }
                case "xml":
                {
                    OpenXmlPackageVisitor xml = new OpenXmlPackageVisitor(output);
                    XElement xmlResult = xml.Document.Content;
                    return File(Encoding.UTF8.GetBytes(xmlResult.ToString()), "application/xml", $"{title ?? "result"}.xml");
                }
                default:
                {
                    return BadRequest(ModelState);
                }
            }
        }
Example #4
0
        static void Main(string[] args)
        {
            var opt       = CliParser.StrictParse <Options>(args);
            var stopwatch = new Stopwatch();

            XDocument leftDoc  = null;
            XDocument rightDoc = null;

            if (opt.Verbose)
            {
                Console.WriteLine("Loading \"{0}\"...", opt.LeftFile);
            }
            leftDoc = XDocument.Load(opt.LeftFile);
            if (opt.Verbose)
            {
                Console.WriteLine("Loading \"{0}\"...", opt.RightFile);
            }
            rightDoc = XDocument.Load(opt.RightFile);
            if (opt.Verbose)
            {
                Console.WriteLine("Comparing differences...");
            }
            stopwatch.Start();

            var comparer = new XmlComparer();
            var diff     = comparer.Compare(leftDoc.Root, rightDoc.Root);

            if (!diff.IsChanged && opt.Verbose)
            {
                Console.WriteLine("No changes detected!");
            }

            if (opt.Verbose)
            {
                Console.WriteLine("Compared in {0} ms.", stopwatch.ElapsedMilliseconds);
            }

            if (!string.IsNullOrEmpty(opt.OutputHtmlFile))
            {
                if (opt.Verbose)
                {
                    Console.WriteLine("Creating HTML output...");
                    stopwatch.Restart();
                }

                var visitor = new HtmlVisitor();
                visitor.Visit(diff);

                if (opt.Verbose)
                {
                    Console.WriteLine("Writing HTML output to \"{0}\"...", opt.OutputHtmlFile);
                }
                File.WriteAllText(opt.OutputHtmlFile, visitor.Result);

                if (opt.Verbose)
                {
                    Console.WriteLine("HTML output file created in {0} ms.", stopwatch.ElapsedMilliseconds);
                }
            }

            if (!string.IsNullOrEmpty(opt.OutputXdtFile))
            {
                if (opt.Verbose)
                {
                    Console.WriteLine("Creating XDT output...");
                    stopwatch.Restart();
                }

                var visitor = new XdtVisitor();
                visitor.Visit(diff);

                if (opt.Verbose)
                {
                    Console.WriteLine("Writing XDT output to \"{0}\"...", opt.OutputXdtFile);
                }
                File.WriteAllText(opt.OutputXdtFile, visitor.Result);

                if (opt.Verbose)
                {
                    Console.WriteLine("XDT output file created in {0} ms.", stopwatch.ElapsedMilliseconds);
                }
            }
            stopwatch.Stop();

            if (opt.Verbose)
            {
                Console.WriteLine("\nShowing text diff:");
            }
            if (opt.Verbose || (string.IsNullOrEmpty(opt.OutputHtmlFile) && string.IsNullOrEmpty(opt.OutputXdtFile)))
            {
                var vistor = new ToStringVisitor();
                vistor.Visit(diff);
                Console.WriteLine(vistor.Result);
            }
        }
        public static string ToHTML(this DocumentPart part)
        {
            Visitor visitor = new HtmlVisitor();

            return(visitor.Visit(part));
        }