Skip to content

Commit

Permalink
Fix table format (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
josefpihrt authored Aug 7, 2023
1 parent b2b87a5 commit fdbead1
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 20 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Add overload of `DocusaurusMarkdownWriter.WriteDocusaurusFrontMatter` ([#38](https://github.com/josefpihrt/dotmarkdown/pull/38)).

### Changed

- Make `ITableAnalyzer` obsolete ([#37](https://github.com/josefpihrt/dotmarkdown/pull/37)).
- Add abstract property `MarkdownWriter.FormatProvider` (BREAKING CHANGE)

### Fixed

- Fix table formatting ([#37](https://github.com/josefpihrt/dotmarkdown/pull/37)).

## [0.3.0-beta] - 2023-07-27

### Added
Expand Down
4 changes: 4 additions & 0 deletions src/DotMarkdown.Docusaurus/DocusaurusMarkdownWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ public void WriteEndDocusaurusAdmonition()
}

#region Decorator
public override MarkdownWriterSettings Settings => Writer.Settings;

public override IFormatProvider FormatProvider => Writer.FormatProvider;

public override WriteState WriteState => Writer.WriteState;

public override void Flush() => Writer.Flush();
Expand Down
2 changes: 2 additions & 0 deletions src/DotMarkdown/Linq/ITableAnalyzer.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// Copyright (c) Josef Pihrt. All rights reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;

namespace DotMarkdown.Linq;

[Obsolete("This API is obsolete.")]
public interface ITableAnalyzer
{
IReadOnlyList<TableColumnInfo>? AnalyzeTable(IEnumerable<MElement> rows);
Expand Down
2 changes: 1 addition & 1 deletion src/DotMarkdown/Linq/MTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public override void WriteTo(MarkdownWriter writer)
{
IEnumerable<MElement> rows = Elements();

IReadOnlyList<TableColumnInfo>? columns = (writer as ITableAnalyzer)?.AnalyzeTable(rows);
IReadOnlyList<TableColumnInfo>? columns = TableAnalyzer.Analyze(rows, writer.Settings, writer.FormatProvider);

if (columns is not null)
{
Expand Down
11 changes: 2 additions & 9 deletions src/DotMarkdown/MarkdownStringWriter.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
// Copyright (c) Josef Pihrt. All rights reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using DotMarkdown.Linq;

namespace DotMarkdown;

internal class MarkdownStringWriter : MarkdownBaseWriter, ITableAnalyzer
internal class MarkdownStringWriter : MarkdownBaseWriter
{
private readonly StringBuilder _sb;
private readonly IFormatProvider _formatProvider;
Expand Down Expand Up @@ -42,7 +40,7 @@ protected internal virtual StringBuilder GetStringBuilder()
return _sb;
}

public virtual IFormatProvider FormatProvider
public override IFormatProvider FormatProvider
{
get { return _formatProvider ?? CultureInfo.CurrentCulture; }
}
Expand Down Expand Up @@ -273,9 +271,4 @@ private void ThrowIfClosed()
if (!_isOpen)
throw new ObjectDisposedException(null, "Cannot write to a closed writer.");
}

public IReadOnlyList<TableColumnInfo>? AnalyzeTable(IEnumerable<MElement> rows)
{
return TableAnalyzer.Analyze(rows, Settings, FormatProvider)?.AsReadOnly();
}
}
11 changes: 3 additions & 8 deletions src/DotMarkdown/MarkdownTextWriter.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
// Copyright (c) Josef Pihrt. All rights reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using DotMarkdown.Linq;

namespace DotMarkdown;

internal class MarkdownTextWriter : MarkdownBaseWriter, ITableAnalyzer
internal class MarkdownTextWriter : MarkdownBaseWriter
{
private const int BufferSize = 1024 * 6;
private const int BufferOverflow = 32;
Expand All @@ -30,6 +28,8 @@ public MarkdownTextWriter(TextWriter writer, MarkdownWriterSettings? settings =

protected internal override int Length { get; set; }

public override IFormatProvider FormatProvider => _writer.FormatProvider;

public override void WriteString(string text)
{
try
Expand Down Expand Up @@ -379,9 +379,4 @@ public override void Close()
}
}
}

public IReadOnlyList<TableColumnInfo>? AnalyzeTable(IEnumerable<MElement> rows)
{
return TableAnalyzer.Analyze(rows, Settings, _writer.FormatProvider)?.AsReadOnly();
}
}
2 changes: 2 additions & 0 deletions src/DotMarkdown/MarkdownWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ protected MarkdownWriter(MarkdownWriterSettings? settings = null)

public abstract WriteState WriteState { get; }

public abstract IFormatProvider FormatProvider { get; }

public virtual MarkdownWriterSettings Settings { get; }

public MarkdownFormat Format => Settings.Format;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
// Copyright (c) Josef Pihrt. All rights reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using DotMarkdown.Linq;
using DotMarkdown.Tests;
using Xunit;
using static DotMarkdown.Docusaurus.DocusaurusMarkdownFactory;
using static DotMarkdown.Docusaurus.Tests.DocusaurusTestHelpers;
using static DotMarkdown.Linq.MFactory;
using static DotMarkdown.Tests.TestHelpers;

namespace DotMarkdown.Docusaurus.Tests;
Expand Down Expand Up @@ -230,6 +232,27 @@ public static void MarkdownWriter_Write_FrontMatter_MultiValue()
- 2
---
";

Assert.Equal(expected.NormalizeNewLine(), mw.ToStringAndClear());
}

[Fact]
public static void MarkdownWriter_CustomFormat()
{
DocusaurusMarkdownWriter mw = CreateDocusaurusWriter(format: new MarkdownFormat(tableOptions: MarkdownFormat.Default.TableOptions | TableOptions.FormatHeaderAndContent));

MTable table = Table(TableRow("xxx", "yyy"), TableRow("a", "b"), TableRow("aaaa", "bbbbb"));

table.WriteTo(mw);

Console.WriteLine("x");

const string expected = @"| xxx | yyy |
| ---- | ----- |
| a | b |
| aaaa | bbbbb |
";

Assert.Equal(expected.NormalizeNewLine(), mw.ToStringAndClear());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public static DocusaurusMarkdownWriter CreateWriterWithCodeBlockOptions(CodeBloc
return CreateDocusaurusWriter(CreateWriter(new MarkdownFormat(codeBlockOptions: options)));
}

public static DocusaurusMarkdownWriter CreateDocusaurusWriter(DocusaurusMarkdownFormat? format = null)
public static DocusaurusMarkdownWriter CreateDocusaurusWriter(DocusaurusMarkdownFormat? docusaurusFormat = null, MarkdownFormat? format = null)
{
return new DocusaurusMarkdownWriter(CreateWriter(), format);
return new DocusaurusMarkdownWriter(CreateWriter(format), docusaurusFormat);
}

public static DocusaurusMarkdownWriter CreateDocusaurusWriter(MarkdownWriter writer, DocusaurusMarkdownFormat? format = null)
Expand Down

0 comments on commit fdbead1

Please sign in to comment.