Skip to content

Commit

Permalink
#15: I joined all the Markdown classes into single file - so it can b…
Browse files Browse the repository at this point in the history
…e internalized into different projects as linked file
  • Loading branch information
MarcinCelej committed Feb 18, 2022
1 parent bba9498 commit e11fac7
Show file tree
Hide file tree
Showing 8 changed files with 233 additions and 235 deletions.
30 changes: 0 additions & 30 deletions Markdown/Synergy.Markdowns/Code.cs

This file was deleted.

52 changes: 0 additions & 52 deletions Markdown/Synergy.Markdowns/Document.cs

This file was deleted.

43 changes: 0 additions & 43 deletions Markdown/Synergy.Markdowns/Headers.cs

This file was deleted.

230 changes: 230 additions & 0 deletions Markdown/Synergy.Markdowns/Markdown.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using Synergy.Catalogue;
using Synergy.Contracts;

namespace Synergy.Markdowns
{
#if INTERNAL_MARKDOWN
internal
#else
public
#endif
class Markdown
{
// ReSharper disable once InconsistentNaming
private static readonly string NL = Environment.NewLine;

public interface IElement{}

public class Document : IEnumerable<IElement>
{
private readonly List<IElement> elements = new();

public Document Append(IElement element)
{
Fail.IfArgumentNull(element, nameof(element));
this.elements.Add(element);
return this;
}

public Document Append(IEnumerable<IElement> newElements)
{
Fail.IfArgumentNull(newElements, nameof(newElements));
this.elements.AddRange(newElements);
return this;
}

/// <inheritdoc />
public override string ToString()
{
StringBuilder markdown = new StringBuilder();
foreach (var element in this.elements)
{
markdown.AppendLine(element.ToString());
}

return markdown.ToString();
}

/// <inheritdoc />
public IEnumerator<IElement> GetEnumerator()
=> this.elements.GetEnumerator();

/// <inheritdoc />
IEnumerator IEnumerable.GetEnumerator()
=> this.GetEnumerator();
}

#region Headers

public abstract class Header : IElement
{
private readonly int level;
private readonly string header;

protected Header(int level, string header)
{
this.level = level;
this.header = header.OrFail(nameof(header))
.Trim();
}

public override string ToString() => $"{new string('#', this.level)} {this.header}{NL}";
}

public class Header1 : Header
{
public Header1(string header) : base(1, header)
{
}
}

public class Header2 : Header
{
public Header2(string header) : base(2, header)
{
}
}

public class Header3 : Header
{
public Header3(string header) : base(3, header)
{
}
}

#endregion

#region Code

public class Code : IElement
{
private readonly string? language;
private readonly string? text;

public Code(string? text = null, string? language = "csharp")
{
this.language = language?.TrimToNull();
this.text = text;
}

public Code Line(string line)
{
Fail.IfNull(line, nameof(line));

if (this.text == null)
return new Code(line, this.language);
return new Code(this.text + Markdown.NL + line, this.language);
}

public override string ToString() => $"``` {this.language ?? ""}{Markdown.NL}" +
$"{this.text}{Markdown.NL}" +
$"```{Markdown.NL}";
}

#endregion

#region Paragraph

public class Paragraph : IElement
{
private readonly string text;

public Paragraph(string text)
=> this.text = text.OrFail(nameof(text))
.Trim();

public Paragraph Line(string line)
{
Fail.IfArgumentWhiteSpace(line, nameof(line));
return new Paragraph(this.text + NL + line);
}

public override string ToString() => $"{this.text}{NL}";
}

#endregion

#region Quote

public class Quote : IElement
{
private readonly List<string> lines;

public Quote(string? text)
{
if (text == null)
this.lines = new List<string>();

this.lines = new List<string>()
{
text.OrFail(nameof(text))
.Trim()
};
}

public Quote Line(string line)
{
this.lines.Add(line.OrFail(nameof(line))
.Trim());
return this;
}

public override string ToString() => $"> {String.Join(NL + "> ", this.lines)}{NL}";
}

#endregion

#region Table

public class Table : IElement
{
private readonly string[] headers;
private readonly List<string[]> rows;

public Table(params string[] headers)
{
this.headers = headers;
this.rows = new List<string[]>();
}

public void Append(params string[] cells)
{
this.rows.Add(cells);
}

public override string ToString()
{
var table = new StringBuilder();
foreach (var header in this.headers)
{
table.Append($"| {header.Trim()} ");
}
table.AppendLine("|");

foreach (var header in this.headers)
{
table.Append($"|{new string('-', header.Length + 2)}");
}
table.AppendLine("|");

foreach (var row in this.rows)
{
foreach (var cell in row)
{
table.Append($"| {cell?.Trim()} ");
}
table.AppendLine("|");
}

return table.ToString();
}
}


#endregion
}
}
23 changes: 0 additions & 23 deletions Markdown/Synergy.Markdowns/Paragraph.cs

This file was deleted.

Loading

0 comments on commit e11fac7

Please sign in to comment.