-
Notifications
You must be signed in to change notification settings - Fork 27
Add CSV include 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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
16 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 5cc5c07
Reinstate old unsupported statement
theletterf f40feb4
Merge branch 'main' into theletterf-add-csv-directivee
theletterf 1def0e1
Add also to dict
theletterf 31f1d1e
Merge branch 'main' into theletterf-add-csv-directivee
theletterf 2fa9598
Change columns default
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
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,50 @@ | ||
# 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: ; | ||
::: | ||
``` |
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
127 changes: 127 additions & 0 deletions
127
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,127 @@ | ||
// 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 List<string[]> CsvData { get; private set; } = []; | ||
|
||
public override void FinalizeAndValidate(ParserContext context) | ||
{ | ||
Caption = Prop("caption"); | ||
|
||
var separator = Prop("separator", "delimiter"); | ||
if (!string.IsNullOrEmpty(separator)) | ||
Separator = separator; | ||
|
||
ExtractCsvPath(context); | ||
if (Found) | ||
ParseCsvFile(); | ||
} | ||
|
||
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)) | ||
Found = true; | ||
else | ||
this.EmitError($"CSV file `{CsvFilePath}` does not exist."); | ||
} | ||
|
||
private void ParseCsvFile() | ||
{ | ||
try | ||
{ | ||
var file = Build.ReadFileSystem.FileInfo.New(CsvFilePath!); | ||
var content = file.FileSystem.File.ReadAllText(file.FullName); | ||
|
||
// Split into lines and parse each line | ||
var lines = content.Split(['\r', '\n'], StringSplitOptions.RemoveEmptyEntries); | ||
|
||
foreach (var line in lines) | ||
{ | ||
if (string.IsNullOrWhiteSpace(line.Trim())) | ||
continue; | ||
|
||
var fields = ParseCsvLine(line); | ||
CsvData.Add(fields); | ||
} | ||
} | ||
catch (Exception e) | ||
{ | ||
this.EmitError($"Failed to parse CSV file: {e.Message}"); | ||
} | ||
} | ||
|
||
private string[] ParseCsvLine(string line) | ||
{ | ||
var fields = new List<string>(); | ||
var currentField = ""; | ||
var inQuotes = false; | ||
var i = 0; | ||
|
||
while (i < line.Length) | ||
{ | ||
var c = line[i]; | ||
|
||
if (c == '"') | ||
{ | ||
if (inQuotes && i + 1 < line.Length && line[i + 1] == '"') | ||
{ | ||
// Escaped quote | ||
currentField += '"'; | ||
i += 2; | ||
} | ||
else | ||
{ | ||
// Toggle quote state | ||
inQuotes = !inQuotes; | ||
i++; | ||
} | ||
} | ||
else if (c.ToString() == Separator && !inQuotes) | ||
{ | ||
// End of field | ||
fields.Add(currentField.Trim()); | ||
currentField = ""; | ||
i++; | ||
} | ||
else | ||
{ | ||
currentField += c; | ||
i++; | ||
} | ||
} | ||
|
||
// Add the last field | ||
fields.Add(currentField.Trim()); | ||
|
||
return fields.ToArray(); | ||
} | ||
} |
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
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.