Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace Tabular with EPPlus and CsvHelper #672

Merged
merged 3 commits into from
Jun 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions src/extensions/Wyam.Tables/CsvFile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System.Collections.Generic;
using System.IO;
using CsvHelper;
using CsvHelper.Configuration;

namespace Wyam.Tables
{
internal static class CsvFile
{
public static IEnumerable<IEnumerable<string>> GetAllRecords(Stream stream, string delimiter = null)
{
using (var reader = new StreamReader(stream))
{
return GetAllRecords(reader, delimiter);
}
}

public static IEnumerable<IEnumerable<string>> GetAllRecords(TextReader reader, string delimiter = null)
{
List<IEnumerable<string>> records = new List<IEnumerable<string>>();
var configuration = delimiter == null ? new Configuration { HasHeaderRecord = false } : new Configuration { HasHeaderRecord = false, Delimiter = delimiter };

using (var csv = new CsvReader(reader, configuration))
{
while (csv.Read())
{
var currentRecord = csv.Context.Record;
records.Add(currentRecord);
}
}

return records;
}

public static void WriteAllRecords(IEnumerable<IEnumerable<string>> records, Stream stream)
{
var writer = new StreamWriter(stream);
WriteAllRecords(records, writer);
writer.Flush();
}

public static void WriteAllRecords(IEnumerable<IEnumerable<string>> records, TextWriter writer)
{
if (records == null)
{
return;
}

var csv = new CsvWriter(writer, new Configuration { QuoteAllFields = true });
{
foreach (var row in records)
{
foreach (var cell in row)
{
csv.WriteField(cell ?? string.Empty);
}
csv.NextRecord();
}
}
}
}
}
13 changes: 5 additions & 8 deletions src/extensions/Wyam.Tables/CsvToHtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Wyam.Common.Documents;
using Wyam.Common.Modules;
using Wyam.Common.Execution;
using Wyam.Common.Modules;
using Wyam.Common.Tracing;
using Wyam.Common.Util;

namespace Wyam.Tables
{
Expand Down Expand Up @@ -41,24 +39,23 @@ public IEnumerable<IDocument> Execute(IReadOnlyList<IDocument> inputs, IExecutio
{
try
{
Tabular.Csv csv = new Tabular.Csv() { Data = input.Content };
Tabular.Table table = Tabular.Csv.FromCsv(csv);
var records = CsvFile.GetAllRecords(input.GetStream());
StringBuilder builder = new StringBuilder();

bool firstLine = true;
builder.AppendLine("<table>");
foreach (var row in table.Rows)
foreach (var row in records)
{
builder.AppendLine("<tr>");
foreach (var cell in row)
{
if (_firstLineHeader && firstLine)
{
builder.AppendLine($"<th>{cell.Value}</th>");
builder.AppendLine($"<th>{cell}</th>");
}
else
{
builder.AppendLine($"<td>{cell.Value}</td>");
builder.AppendLine($"<td>{cell}</td>");
}
}
builder.AppendLine("</tr>");
Expand Down
25 changes: 11 additions & 14 deletions src/extensions/Wyam.Tables/CsvToMarkdown.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Wyam.Common.Documents;
using Wyam.Common.Modules;
using Wyam.Common.Execution;
using Wyam.Common.Modules;
using Wyam.Common.Tracing;
using Wyam.Common.Util;

namespace Wyam.Tables
{
Expand Down Expand Up @@ -47,37 +45,36 @@ public IEnumerable<IDocument> Execute(IReadOnlyList<IDocument> inputs, IExecutio
{
try
{
Tabular.Csv csv = new Tabular.Csv() { Data = input.Content };
Tabular.Table table = Tabular.Csv.FromCsv(csv);
var records = CsvFile.GetAllRecords(input.GetStream());
StringBuilder builder = new StringBuilder();

int columnCount = table.First().Count;
int columnCount = records.First().Count();

int[] columnSize = new int[columnCount];

foreach (var row in table.Rows)
foreach (var row in records)
{
for (int i = 0; i < row.Count; i++)
for (int i = 0; i < row.Count(); i++)
{
var cell = row[i];
columnSize[i] = Math.Max(columnSize[i], cell.Value.Length);
var cell = row.ElementAt(i);
columnSize[i] = Math.Max(columnSize[i], cell.Length);
}
}

bool firstLine = true;
WriteLine(builder, columnSize);
foreach (var row in table.Rows)
foreach (var row in records)
{
builder.Append("|");
for (int i = 0; i < columnSize.Length; i++)
{
builder.Append(" ");
builder.Append(row[i].Value);
builder.Append(' ', columnSize[i] - row[i].Value.Length + 1);
builder.Append(row.ElementAt(i));
builder.Append(' ', columnSize[i] - row.ElementAt(i).Length + 1);
builder.Append("|");
}
builder.AppendLine();
WriteLine(builder, columnSize, this._firstLineHeader && firstLine);
WriteLine(builder, columnSize, _firstLineHeader && firstLine);
firstLine = false;
}

Expand Down
54 changes: 54 additions & 0 deletions src/extensions/Wyam.Tables/ExcelFile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using OfficeOpenXml;

namespace Wyam.Tables
{
internal static class ExcelFile
{
public static IEnumerable<IEnumerable<string>> GetAllRecords(Stream stream, int sheetNumber = 0)
{
using (var excel = new ExcelPackage(stream))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looked through the EPPlus source and it doesn't look like ExcelPackage closes external streams. That means we need to make sure the input document stream gets disposed from the call site (IDocument.GetStream() requires the stream be closed as soon as possible since it locks some resources). I'll fix it up, just FYI.

{
excel.Compatibility.IsWorksheets1Based = false;
if (sheetNumber > excel.Workbook.Worksheets.Count)
{
return null;
}

var sheet = excel.Workbook.Worksheets[sheetNumber];

return GetAllRecords(sheet);
}
}

public static IEnumerable<IEnumerable<string>> GetAllRecords(ExcelWorksheet sheet)
{
var dimension = sheet.Dimension;

if (dimension == null)
{
return null;
}

var rowList = new List<List<string>>();
int rowCount = dimension.Rows;
int columnCount = dimension.Columns;

for (var r = 1; r <= rowCount; r++)
{
var rowValues = new List<string>(columnCount);
for (var c = 1; c <= columnCount; c++)
{
var cell = sheet.Cells[r, c].FirstOrDefault();
rowValues.Add(cell?.Value?.ToString());
}

rowList.Add(rowValues);
}

return rowList;
}
}
}
15 changes: 5 additions & 10 deletions src/extensions/Wyam.Tables/ExcelToCsv.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,10 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Wyam.Common.Configuration;
using Wyam.Common.Documents;
using Wyam.Common.Modules;
using Wyam.Common.Execution;
using Wyam.Common.Modules;
using Wyam.Common.Tracing;
using Wyam.Common.Util;

namespace Wyam.Tables
{
Expand All @@ -30,13 +26,12 @@ public IEnumerable<IDocument> Execute(IReadOnlyList<IDocument> inputs, IExecutio
{
try
{
Tabular.Table table;
using (Stream stream = input.GetStream())
var records = ExcelFile.GetAllRecords(input.GetStream());
using (var stream = new MemoryStream())
{
table = Tabular.Excel.ReadFrom(stream, Tabular.ExcelFormat.Excel2007);
CsvFile.WriteAllRecords(records, stream);
return context.GetDocument(input, stream);
}
Tabular.Csv csv = Tabular.Csv.ToCsv(table);
return context.GetDocument(input, context.GetContentStream(csv.Data));
}
catch (Exception e)
{
Expand Down
8 changes: 3 additions & 5 deletions src/extensions/Wyam.Tables/Wyam.Tables.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net462</TargetFramework>
<TargetFramework>netstandard2.0</TargetFramework>
<Description>Wyam is a simple to use, highly modular, and extremely configurable static content generator. This library provides support for importing and exporting CSV and Excel files.</Description>
<PackageTags>Wyam Static StaticContent StaticSite Blog BlogEngine CSV Excel</PackageTags>
</PropertyGroup>
Expand All @@ -10,9 +10,7 @@
</ProjectReference>
</ItemGroup>
<ItemGroup>
<PackageReference Include="EPPlus" Version="4.1.0" />
<PackageReference Include="Tabular.Csv.dll" Version="2.1.5" />
<PackageReference Include="Tabular.dll" Version="2.1.5" />
<PackageReference Include="Tabular.Excel.dll" Version="2.1.5" />
<PackageReference Include="CsvHelper" Version="7.1.0" />
<PackageReference Include="EPPlus" Version="4.5.2.1" />
</ItemGroup>
</Project>
4 changes: 2 additions & 2 deletions tests/extensions/Wyam.Tables.Tests/CsvToHtmlFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class CsvToHtmlFixture : BaseFixture
public class ExecuteTests : CsvToHtmlFixture
{
[Test]
public void TestWithoutHeadder()
public void TestWithoutHeader()
{
// Given
string input = string.Empty
Expand Down Expand Up @@ -330,7 +330,7 @@ public void TestWithoutHeadder()
}

[Test]
public void TestHeadder()
public void TestHeader()
{
// Given
string input = string.Empty
Expand Down
Loading