-
Notifications
You must be signed in to change notification settings - Fork 26
Add CSV file directive #1742
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
Open
theletterf
wants to merge
11
commits into
main
Choose a base branch
from
theletterf-add-csv-directivee
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add CSV file directive #1742
Changes from 5 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
0f169be
Add CSV file directive
theletterf bdb583b
Change to Sep and defer rendering
theletterf f8b604a
Use only Sep
theletterf 8cc307e
Add streaming and max size
theletterf 45a9ce4
Merge branch 'main' into theletterf-add-csv-directivee
theletterf 24fe347
Update docs/syntax/csv-file.md
theletterf 40c24f4
Refactor and other stuff
theletterf 4c77924
Merge branch 'main' into theletterf-add-csv-directivee
theletterf f8c3c60
Remove preview mode
theletterf 7598f5d
Increase rows limit
theletterf d062fec
Formatting
theletterf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
Name,Age,City,Occupation | ||
John Doe,30,New York,Software Engineer | ||
Jane Smith,25,Los Angeles,Product Manager | ||
Bob Johnson,35,Chicago,Data Scientist | ||
Alice Brown,28,San Francisco,UX Designer | ||
Charlie Wilson,32,Boston,DevOps Engineer |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
# CSV file directive | ||
|
||
The `{csv-file}` directive allows you to include and render CSV files as formatted tables in your documentation. The directive automatically parses CSV content and renders it using the standard table styles defined in `table.css`. | ||
|
||
## Usage | ||
|
||
:::::{tab-set} | ||
|
||
::::{tab-item} Output | ||
|
||
:::{csv-file} ../_snippets/sample-data.csv | ||
:caption: Sample user data from the database | ||
::: | ||
|
||
:::: | ||
|
||
::::{tab-item} Markdown | ||
|
||
```markdown | ||
:::{csv-file} _snippets/sample-data.csv | ||
::: | ||
``` | ||
|
||
:::: | ||
|
||
::::: | ||
|
||
## Options | ||
|
||
The CSV file directive supports several options to customize the table rendering: | ||
|
||
### Caption | ||
|
||
Add a descriptive caption above the table: | ||
|
||
```markdown | ||
:::{csv-file} _snippets/sample-data.csv | ||
:caption: Sample user data from the database | ||
::: | ||
``` | ||
|
||
### Custom separator | ||
|
||
Specify a custom field separator (default is comma): | ||
|
||
```markdown | ||
:::{csv-file} _snippets/sample-data.csv | ||
:separator: ; | ||
::: | ||
``` | ||
|
||
### Size and performance limits | ||
|
||
Control how much data is loaded and displayed: | ||
|
||
```markdown | ||
:::{csv-file} _snippets/large-dataset.csv | ||
:max-rows: 5000 | ||
:max-columns: 50 | ||
:max-size: 20MB | ||
::: | ||
``` | ||
|
||
- **max-rows**: Maximum number of rows to display (default: 10,000) | ||
- **max-columns**: Maximum number of columns to display (default: 100) | ||
- **max-size**: Maximum file size to process (default: 10MB). Supports KB, MB, GB units. | ||
|
||
### Preview mode | ||
|
||
For very large files, enable preview mode to show only the first 100 rows: | ||
|
||
```markdown | ||
:::{csv-file} _snippets/huge-dataset.csv | ||
:preview-only: true | ||
::: | ||
``` | ||
|
||
## Performance considerations | ||
|
||
The CSV directive is optimized for large files: | ||
|
||
- Files are processed using streaming to avoid loading everything into memory | ||
- Size validation prevents processing of files that exceed the specified limits | ||
- Row and column limits protect against accidentally rendering massive tables | ||
- Warning messages are displayed when limits are exceeded | ||
|
||
For optimal performance with large CSV files, consider: | ||
- Setting appropriate `max-rows` and `max-columns` limits | ||
- Using `preview-only: true` for exploratory data viewing | ||
- Increasing `max-size` only when necessary |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
src/Elastic.Markdown/Myst/Directives/CsvFile/CsvFileBlock.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
// Licensed to Elasticsearch B.V under one or more agreements. | ||
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. | ||
// See the LICENSE file in the project root for more information | ||
|
||
using System.Globalization; | ||
using System.IO.Abstractions; | ||
using Elastic.Markdown.Diagnostics; | ||
|
||
namespace Elastic.Markdown.Myst.Directives.CsvFile; | ||
|
||
public class CsvFileBlock(DirectiveBlockParser parser, ParserContext context) : DirectiveBlock(parser, context) | ||
{ | ||
public override string Directive => "csv-file"; | ||
|
||
public string? CsvFilePath { get; private set; } | ||
public string? CsvFilePathRelativeToSource { get; private set; } | ||
public bool Found { get; private set; } | ||
public string? Caption { get; private set; } | ||
public string Separator { get; private set; } = ","; | ||
public int MaxRows { get; private set; } = 10000; | ||
public long MaxFileSizeBytes { get; private set; } = 10 * 1024 * 1024; // 10MB | ||
public int MaxColumns { get; private set; } = 100; | ||
public bool PreviewOnly { get; private set; } | ||
|
||
public override void FinalizeAndValidate(ParserContext context) | ||
{ | ||
Caption = Prop("caption"); | ||
|
||
var separator = Prop("separator", "delimiter"); | ||
if (!string.IsNullOrEmpty(separator)) | ||
Separator = separator; | ||
|
||
if (int.TryParse(Prop("max-rows"), out var maxRows) && maxRows > 0) | ||
MaxRows = maxRows; | ||
|
||
if (ParseFileSize(Prop("max-size"), out var maxSize) && maxSize > 0) | ||
MaxFileSizeBytes = maxSize; | ||
|
||
if (int.TryParse(Prop("max-columns"), out var maxColumns) && maxColumns > 0) | ||
MaxColumns = maxColumns; | ||
|
||
PreviewOnly = bool.TryParse(Prop("preview-only"), out var preview) && preview; | ||
|
||
ExtractCsvPath(context); | ||
} | ||
|
||
private void ExtractCsvPath(ParserContext context) | ||
{ | ||
var csvPath = Arguments; | ||
if (string.IsNullOrWhiteSpace(csvPath)) | ||
{ | ||
this.EmitError("csv-file requires an argument specifying the path to the CSV file."); | ||
return; | ||
} | ||
|
||
var csvFrom = context.MarkdownSourcePath.Directory!.FullName; | ||
if (csvPath.StartsWith('/')) | ||
csvFrom = Build.DocumentationSourceDirectory.FullName; | ||
|
||
CsvFilePath = Path.Combine(csvFrom, csvPath.TrimStart('/')); | ||
CsvFilePathRelativeToSource = Path.GetRelativePath(Build.DocumentationSourceDirectory.FullName, CsvFilePath); | ||
|
||
if (Build.ReadFileSystem.File.Exists(CsvFilePath)) | ||
{ | ||
ValidateFileSize(); | ||
Found = true; | ||
} | ||
else | ||
this.EmitError($"CSV file `{CsvFilePath}` does not exist."); | ||
} | ||
|
||
private void ValidateFileSize() | ||
{ | ||
if (CsvFilePath == null) | ||
return; | ||
|
||
try | ||
{ | ||
var fileInfo = Build.ReadFileSystem.FileInfo.New(CsvFilePath); | ||
if (fileInfo.Length > MaxFileSizeBytes) | ||
{ | ||
var sizeMB = fileInfo.Length / (1024.0 * 1024.0); | ||
var maxSizeMB = MaxFileSizeBytes / (1024.0 * 1024.0); | ||
this.EmitError($"CSV file `{CsvFilePath}` is {sizeMB:F1}MB, which exceeds the maximum allowed size of {maxSizeMB:F1}MB. Use :max-size to increase the limit."); | ||
Found = false; | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
this.EmitError($"Could not validate CSV file size: {ex.Message}"); | ||
Found = false; | ||
} | ||
} | ||
|
||
private static bool ParseFileSize(string? sizeString, out long bytes) | ||
{ | ||
bytes = 0; | ||
if (string.IsNullOrEmpty(sizeString)) | ||
return false; | ||
|
||
var multiplier = 1L; | ||
var value = sizeString.ToUpperInvariant(); | ||
|
||
if (value.EndsWith("KB")) | ||
{ | ||
multiplier = 1024; | ||
value = value[..^2]; | ||
} | ||
else if (value.EndsWith("MB")) | ||
{ | ||
multiplier = 1024 * 1024; | ||
value = value[..^2]; | ||
} | ||
else if (value.EndsWith("GB")) | ||
{ | ||
multiplier = 1024 * 1024 * 1024; | ||
value = value[..^2]; | ||
} | ||
|
||
if (double.TryParse(value, out var numericValue)) | ||
{ | ||
bytes = (long)(numericValue * multiplier); | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
} |
40 changes: 40 additions & 0 deletions
40
src/Elastic.Markdown/Myst/Directives/CsvFile/CsvFileView.cshtml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
@using Elastic.Markdown.Myst.Directives.CsvFile | ||
@inherits RazorSlice<CsvFileViewModel> | ||
|
||
@{ | ||
var csvBlock = (CsvFileBlock)Model.DirectiveBlock; | ||
if (!csvBlock.Found || Model.CsvRows.Count == 0) | ||
{ | ||
return; | ||
} | ||
} | ||
|
||
<div class="table-wrapper"> | ||
@if (!string.IsNullOrEmpty(csvBlock.Caption)) | ||
{ | ||
<caption>@csvBlock.Caption</caption> | ||
} | ||
|
||
<table> | ||
<thead> | ||
<tr> | ||
@for (var i = 0; i < Model.CsvRows[0].Length; i++) | ||
{ | ||
<th>@Model.CsvRows[0][i]</th> | ||
} | ||
</tr> | ||
</thead> | ||
|
||
<tbody> | ||
@for (var rowIndex = 1; rowIndex < Model.CsvRows.Count; rowIndex++) | ||
{ | ||
<tr> | ||
@for (var i = 0; i < Model.CsvRows[rowIndex].Length; i++) | ||
{ | ||
<td>@Model.CsvRows[rowIndex][i]</td> | ||
} | ||
</tr> | ||
} | ||
</tbody> | ||
</table> | ||
</div> |
64 changes: 64 additions & 0 deletions
64
src/Elastic.Markdown/Myst/Directives/CsvFile/CsvFileViewModel.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Licensed to Elasticsearch B.V under one or more agreements. | ||
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. | ||
// See the LICENSE file in the project root for more information | ||
|
||
using Elastic.Markdown.Diagnostics; | ||
|
||
namespace Elastic.Markdown.Myst.Directives.CsvFile; | ||
|
||
public class CsvFileViewModel : DirectiveViewModel | ||
{ | ||
public required List<string[]> CsvRows { get; init; } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should also be That way the cshtml can lazily iterate over large csv files too. Let me know if you want me to pick that up in a follow up @theletterf ! |
||
|
||
public static CsvFileViewModel Create(CsvFileBlock csvBlock) | ||
{ | ||
var rows = new List<string[]>(); | ||
|
||
if (csvBlock.Found && !string.IsNullOrEmpty(csvBlock.CsvFilePath)) | ||
{ | ||
var csvData = CsvReader.ReadCsvFile(csvBlock.CsvFilePath, csvBlock.Separator, csvBlock.Build.ReadFileSystem); | ||
var rowCount = 0; | ||
var columnCountExceeded = false; | ||
|
||
foreach (var row in csvData) | ||
{ | ||
if (rowCount >= csvBlock.MaxRows) | ||
{ | ||
csvBlock.EmitWarning($"CSV file contains more than {csvBlock.MaxRows} rows. Only the first {csvBlock.MaxRows} rows will be displayed. Use :max-rows to increase the limit."); | ||
break; | ||
} | ||
|
||
if (row.Length > csvBlock.MaxColumns) | ||
{ | ||
if (!columnCountExceeded) | ||
{ | ||
csvBlock.EmitWarning($"CSV file contains more than {csvBlock.MaxColumns} columns. Only the first {csvBlock.MaxColumns} columns will be displayed. Use :max-columns to increase the limit."); | ||
columnCountExceeded = true; | ||
} | ||
|
||
var trimmedRow = new string[csvBlock.MaxColumns]; | ||
Array.Copy(row, trimmedRow, csvBlock.MaxColumns); | ||
rows.Add(trimmedRow); | ||
} | ||
else | ||
{ | ||
rows.Add(row); | ||
} | ||
|
||
rowCount++; | ||
} | ||
|
||
if (csvBlock.PreviewOnly && rowCount > 100) | ||
{ | ||
csvBlock.EmitWarning("Preview mode is enabled. Showing first 100 rows of CSV data."); | ||
rows = rows.Take(100).ToList(); | ||
} | ||
} | ||
|
||
return new CsvFileViewModel | ||
{ | ||
DirectiveBlock = csvBlock, | ||
CsvRows = rows | ||
}; | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.