public override bool OpenNode(HtmlParser.Node node, StringBuilder bodyBuilder) { var content = MarkdownVisitor.Process(node).Trim('\n', '\r'); // TODO: figure out of this is a problem content = content.Replace("<", "<").Replace(">", ">"); var useBlockFormat = content.IndexOf('\n') > 0; if (useBlockFormat) { // TODO: detect language var attClass = node.Attributes.GetValueOrDefault("class", ""); var language = string.Empty; foreach (var att in attClass.Split(' ')) { if (att.StartsWith("lang:")) { language = att.Replace("lang:", ""); break; } } bodyBuilder.AppendLine().Append("```"); bodyBuilder.AppendLine(language); bodyBuilder.AppendLine(content); bodyBuilder.AppendLine("```"); } else { bodyBuilder.Append("`").Append(content).Append("`"); } return(false); }
public void GivenText_WhenValid_ReturnsMBulletListItem(string markdown, string normalized, string text, string html) { MNode node = new MarkdownVisitor().Visit(markdown); MBulletListItem item = Assert.IsType <MBulletListItem>(node); Assert.Equal(normalized, node.ToString()); Assert.Equal(text, item.Item.ToString()); Assert.Equal(html, item.ToHtml().ToString()); }
public void GivenText_WhenValid_ReturnsMParagraph(string markdown, string normalized, string html) { MNode node = new MarkdownVisitor().Visit(markdown); MParagraph paragraph = Assert.IsType <MParagraph>(node); Assert.Equal(normalized, paragraph.Text.ToString()); Assert.Equal(normalized, node.ToString()); Assert.Equal(html, paragraph.ToHtml().ToString()); }
public void GivenText_WhenValid_ReturnsMHeading(string markdown, string normalized, string text, string html) { MNode node = new MarkdownVisitor().Visit(markdown); MHeading heading = Assert.IsType <MHeading>(node); Assert.Equal(text, heading.Heading.ToString()); Assert.Equal(html, heading.ToHtml().ToString()); Assert.Equal(normalized, node.ToString()); }
static void Main(string[] args) { #region Employee Management // Setup employee collection Employees e = new Employees(); e.Attach(new Clerk()); e.Attach(new Director()); e.Attach(new President()); // Employees are 'visited' e.Accept(new IncomeVisitor()); e.Accept(new VacationVisitor()); // Wait for user Console.ReadKey(); #endregion #region Document Formatting // Create a new document. var document = new Document(); // Add some elements to the document. document.Elements.Add(new Text("This is plain text.")); document.Elements.Add(new Hyperlink("Hyperlink to Airbrake.io", "http://airbrake.io")); document.Elements.Add(new Paragraph("Lorem ipsum dolor sit amet, consectetur adipiscing elit.")); document.Elements.Add(new BoldText("Important text to bold!")); // Create a few visitors. var html = new HtmlVisitor(); var markdown = new MarkdownVisitor(); var bbCode = new BbVisitor(); // Force document to accept passed visitors, // which will each uniquely alter output. document.Accept(html); document.Accept(markdown); document.Accept(bbCode); // Log each visitor's output. Console.WriteLine("HTML"); Console.WriteLine(html.ToString()); Console.WriteLine("Markdown"); Console.WriteLine(markdown.ToString()); Console.WriteLine("BBCode"); Console.WriteLine(bbCode.ToString()); #endregion }
private string ElementsToMarkdown(IEnumerable <IElement> elements) { using (var writer = new StringWriter()) { var md = new MarkdownVisitor(writer); foreach (var element in elements) { element.Visit(md); } return(writer.ToString()); } }
public override bool OpenNode(HtmlParser.Node node, StringBuilder bodyBuilder) { var content = MarkdownVisitor.Process(node).Trim('\n', '\r', ' '); bodyBuilder.AppendLine().Append(">"); content = content.Replace("\n", "\n>"); bodyBuilder.AppendLine(content).AppendLine(); return(false); }
public override bool OpenNode(HtmlParser.Node node, StringBuilder bodyBuilder) { string content = string.Empty; switch (node.Name) { case "ul": case "ol": _lists.Push(node.Name); //if (_lists.Count == 1) { bodyBuilder.AppendLine(); } // this is a weird hack, but we're doing it because HTML is weird // but we only care about the li elements, and we treat the rest as problematic (non-existant) // and if we loose content, so be it... foreach (var child in node.Children) { if (child.GetNameSafely() != "li") { Console.WriteLine("!! Content ignored: " + child.ToString()); continue; } content = MarkdownVisitor.Process(child).Trim('\r', '\n', ' '); if (content.IndexOf('\n') >= 0) { // we need to add a \t at every new line content = content.Replace("\n", "\n\t"); } bodyBuilder.AppendFormat("{0} {1}", node.Name == "ul" ? "*" : "1.", content).AppendLine(); } return(false); case "li": content = MarkdownVisitor.Process(node).Trim('\n', ' '); bodyBuilder.AppendFormat("{0} {1}", _lists.Peek() == "ul" ? "*" : "1.", content).AppendLine(); return(false); } return(true); }
static void Main(string[] args) { var options = new ReaderOptions() { KeepNewLinesInText = true }; var members = DocReader.Read(typeof(Template).Assembly, options); var builtinClassNames = new Dictionary <string, string>() { [nameof(ArrayFunctions)] = "array", [nameof(DateTimeFunctions)] = "date", [nameof(HtmlFunctions)] = "html", [nameof(MathFunctions)] = "math", [nameof(ObjectFunctions)] = "object", [nameof(RegexFunctions)] = "regex", [nameof(StringFunctions)] = "string", [nameof(TimeSpanFunctions)] = "timespan" }; var writer = new StreamWriter("../../../../doc/builtins.md"); writer.WriteLine(@"# Builtins This document describes the various built-in functions available in scriban. "); var visitor = new MarkdownVisitor(builtinClassNames); members.Accept(visitor); writer.WriteLine(visitor.Toc); foreach (var classWriter in visitor.ClassWriters.OrderBy(c => c.Key).Select(c => c.Value)) { writer.Write(classWriter.Head); writer.Write(classWriter.Body); } writer.WriteLine(); writer.WriteLine("> Note: This document was automatically generated from the sourcecode using `Scriban.DocGen` program"); writer.Flush(); writer.Close(); }
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); } } }
public void GivenText_WhenInvalid_DoesNotReturnMBulletListItem(string text) { MNode node = new MarkdownVisitor().Visit(text); Assert.IsNotType <MBulletListItem>(node); }
public void GivenText_WhenInvalid_DoesNotReturnMHeading(string text) { MNode node = new MarkdownVisitor().Visit(text); Assert.IsNotType <MHeading>(node); }