Skip to content

Commit 208677d

Browse files
committed
create component for examples
1 parent 5c1e238 commit 208677d

11 files changed

+35
-72
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
import {Code} from '@astrojs/starlight/components';
3+
const { filename, highlights } = Astro.props;
4+
5+
import fs from 'node:fs/promises';
6+
const url = new URL(`../../../example/src/main/java/example/${filename}`, import.meta.url);
7+
const sourceCode = await fs.readFile(url, 'utf-8');
8+
---
9+
10+
<Code code={sourceCode} title={filename} lang="java" mark={highlights}/>
11+
12+
<p>You also find this source code example in the
13+
<a href={`https://github.com/osiegmar/FastCSV/blob/main/example/src/main/java/example/${filename}`}>FastCSV GitHub repository</a>.</p>

docs/src/content/docs/guides/Examples/byte-order-mark.mdx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
title: Byte order mark (BOM) header
33
---
44

5-
import {Code} from '@astrojs/starlight/components';
6-
import sourceCode from '../../../../../../example/src/main/java/example/ExampleCsvReaderWithBomHeader.java?raw';
7-
export const highlights = ['.detectBomHeader(true)']
5+
import SourceExample from '../../../../components/SourceExample.astro';
86

97
FastCSV is capable of reading CSV files with a [Byte order mark](https://en.wikipedia.org/wiki/Byte_order_mark)
108
(BOM) header.
@@ -28,10 +26,7 @@ for more information.
2826

2927
In the following example, a CSV file with a BOM header is created and read using FastCSV.
3028

31-
<Code code={sourceCode} title="ExampleCsvReaderWithBomHeader.java" lang="java" mark={highlights}/>
32-
33-
You also find this source code example in the
34-
[FastCSV GitHub repository](https://github.com/osiegmar/FastCSV/blob/main/example/src/main/java/example/ExampleCsvReaderWithBomHeader.java).
29+
<SourceExample filename="ExampleCsvReaderWithBomHeader.java" highlights={['.detectBomHeader(true)']}/>
3530

3631
:::tip
3732
To **create** CSV files with a BOM header using FastCSV, you have to write the BOM header manually,

docs/src/content/docs/guides/Examples/custom-callback-handler.mdx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
title: Custom callback handler
33
---
44

5-
import {Code} from '@astrojs/starlight/components';
6-
import sourceCode from '../../../../../../example/src/main/java/example/ExampleCsvReaderWithCustomCallbackHandler.java?raw';
5+
import SourceExample from '../../../../components/SourceExample.astro';
76

87
The typical way to read CSV data with FastCSV is to either use one of the `ofCsvRecord()` or `ofNamedCsvRecord()`
98
methods of the `CsvReader` class. Those methods provide convenient access to the CSV data as a stream of
@@ -17,7 +16,4 @@ processing the CSV data and to materialize the objects that represent the CSV re
1716
The following example demonstrates how to read CSV data using a custom callback handler. This allows you to
1817
process the CSV data in a more flexible way.
1918

20-
<Code code={sourceCode} title="ExampleCsvReaderWithCustomCallbackHandler.java" lang="java"/>
21-
22-
You also find this source code example in the
23-
[FastCSV GitHub repository](https://github.com/osiegmar/FastCSV/blob/main/example/src/main/java/example/ExampleCsvReaderWithCustomCallbackHandler.java).
19+
<SourceExample filename="ExampleCsvReaderWithCustomCallbackHandler.java"/>

docs/src/content/docs/guides/Examples/handle-comments.mdx

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
title: Handle comments
33
---
44

5-
import {Code} from '@astrojs/starlight/components';
6-
import readSourceCode from '../../../../../../example/src/main/java/example/ExampleCsvReaderWithComments.java?raw';
7-
import writeSourceCode from '../../../../../../example/src/main/java/example/ExampleCsvWriterWithComments.java?raw';
5+
import SourceExample from '../../../../components/SourceExample.astro';
86

97
Although comments are not part of the CSV standard (per [RFC 4180](https://datatracker.ietf.org/doc/html/rfc4180)),
108
many applications make use of comments in CSV files to provide additional information or to add context to the data.
@@ -41,10 +39,7 @@ More information about the `CommentStrategy` enum can be found in the correspond
4139
By default, FastCSV treats comments as part of the data to ensure that no data is lost and to maintain
4240
maximum compatibility with the RFC 4180 standard.
4341

44-
<Code code={readSourceCode} title="ExampleCsvReaderWithComments.java" lang="java" />
45-
46-
You also find this source code example in the
47-
[FastCSV GitHub repository](https://github.com/osiegmar/FastCSV/blob/main/example/src/main/java/example/ExampleCsvReaderWithComments.java).
42+
<SourceExample filename="ExampleCsvReaderWithComments.java"/>
4843

4944
## Writing CSV files with comments
5045

@@ -54,7 +49,4 @@ Writing comments to a CSV file is straightforward, as FastCSV takes care of any
5449
Be aware that comments are not part of the CSV standard and may not be supported by all applications that read CSV files.
5550
:::
5651

57-
<Code code={writeSourceCode} title="ExampleCsvWriterWithComments.java" lang="java" />
58-
59-
You also find this source code example in the
60-
[FastCSV GitHub repository](https://github.com/osiegmar/FastCSV/blob/main/example/src/main/java/example/ExampleCsvWriterWithComments.java).
52+
<SourceExample filename="ExampleCsvWriterWithComments.java"/>

docs/src/content/docs/guides/Examples/indexed-read.mdx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
title: Indexed reading
33
---
44

5-
import {Code} from '@astrojs/starlight/components';
6-
import sourceCode from '../../../../../../example/src/main/java/example/ExampleIndexedCsvReader.java?raw';
5+
import SourceExample from '../../../../components/SourceExample.astro';
76

87
CSV files are most commonly read sequentially from start to end. In some situations, like in graphical user interfaces,
98
you might need to read specific rows directly without having to read the entire file. FastCSV supports indexed reading
@@ -41,7 +40,4 @@ try (csv) {
4140

4241
The following example demonstrates how to read a CSV file using FastCSV's indexed reader.
4342

44-
<Code code={sourceCode} title="ExampleIndexedCsvReader.java" lang="java" />
45-
46-
You also find this source code example in the
47-
[FastCSV GitHub repository](https://github.com/osiegmar/FastCSV/blob/main/example/src/main/java/example/ExampleIndexedCsvReader.java).
43+
<SourceExample filename="ExampleIndexedCsvReader.java"/>

docs/src/content/docs/guides/Examples/modifying-input.mdx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
title: Modifying input
33
---
44

5-
import {Code} from '@astrojs/starlight/components';
6-
import sourceCode from '../../../../../../example/src/main/java/example/ExampleCsvReaderWithFieldModifier.java?raw';
5+
import SourceExample from '../../../../components/SourceExample.astro';
76

87
The CSV specification ([section 2.4. of RFC 4180](https://www.rfc-editor.org/rfc/rfc4180#section-2)) explicitly states:
98

@@ -22,7 +21,4 @@ you should check out the [Custom Callback handler example](/guides/examples/cust
2221

2322
In the following example, fields are modified while reading a CSV file.
2423

25-
<Code code={sourceCode} title="ExampleCsvReaderWithFieldModifier.java" lang="java"/>
26-
27-
You also find this source code example in the
28-
[FastCSV GitHub repository](https://github.com/osiegmar/FastCSV/blob/main/example/src/main/java/example/ExampleCsvReaderWithFieldModifier.java).
24+
<SourceExample filename="ExampleCsvReaderWithFieldModifier.java"/>

docs/src/content/docs/guides/Examples/non-standard-control-characters.mdx

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
title: Non-standard control characters
33
---
44

5-
import {Code} from '@astrojs/starlight/components';
6-
import readSourceCode from '../../../../../../example/src/main/java/example/ExampleCsvReaderWithNonStandardControlCharacters.java?raw';
7-
import writeSourceCode from '../../../../../../example/src/main/java/example/ExampleCsvWriterWithNonStandardControlCharacters.java?raw';
5+
import SourceExample from '../../../../components/SourceExample.astro';
86

97
The CSV specification ([section 2.6. of RFC 4180](https://www.rfc-editor.org/rfc/rfc4180#section-2)) defines the following control characters:
108

@@ -19,10 +17,7 @@ FastCSV uses these control characters by default. However, you can change them u
1917

2018
In the following example, a CSV file with non-standard control characters is read using FastCSV.
2119

22-
<Code code={readSourceCode} title="ExampleCsvReaderWithNonStandardControlCharacters.java" lang="java"/>
23-
24-
You also find this source code example in the
25-
[FastCSV GitHub repository](https://github.com/osiegmar/FastCSV/blob/main/example/src/main/java/example/ExampleCsvReaderWithNonStandardControlCharacters.java).
20+
<SourceExample filename="ExampleCsvReaderWithNonStandardControlCharacters.java"/>
2621

2722
:::note
2823
The record delimiter cannot be configured when reading CSV files. FastCSV automatically detects the record delimiter
@@ -33,7 +28,4 @@ while reading the CSV file.
3328

3429
In the following example, a CSV file with non-standard control characters is created using FastCSV.
3530

36-
<Code code={writeSourceCode} title="ExampleCsvWriterWithNonStandardControlCharacters.java" lang="java"/>
37-
38-
You also find this source code example in the
39-
[FastCSV GitHub repository](https://github.com/osiegmar/FastCSV/blob/main/example/src/main/java/example/ExampleCsvWriterWithNonStandardControlCharacters.java).
31+
<SourceExample filename="ExampleCsvWriterWithNonStandardControlCharacters.java"/>

docs/src/content/docs/guides/Examples/quote-strategies.mdx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
title: Quote strategies
33
---
44

5-
import {Code} from '@astrojs/starlight/components';
6-
import sourceCode from '../../../../../../example/src/main/java/example/ExampleCsvWriterWithQuoteStrategy.java?raw';
7-
export const highlights = [/\.quoteStrategy\(.*\)/g];
5+
import SourceExample from '../../../../components/SourceExample.astro';
86

97
Fields in CSV files can be enclosed in quotes.
108
Although the CSV specification requires quotes only when a field contains a separator, delimiter,
@@ -35,10 +33,7 @@ for more information.
3533

3634
In the following example, CSV output is created using different quote strategies.
3735

38-
<Code code={sourceCode} title="ExampleCsvWriterWithQuoteStrategy.java" lang="java" mark={highlights}/>
39-
40-
You also find this source code example in the
41-
[FastCSV GitHub repository](https://github.com/osiegmar/FastCSV/blob/main/example/src/main/java/example/ExampleCsvWriterWithQuoteStrategy.java).
36+
<SourceExample filename="ExampleCsvWriterWithQuoteStrategy.java" highlights={[/\.quoteStrategy\(.*\)/g]}/>
4237

4338
:::tip
4439
If you want to know if a field is enclosed in quotes when reading a CSV file, you may implement a

docs/src/content/docs/guides/Examples/read-from-classpath.mdx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
title: Read from classpath
33
---
44

5-
import {Code} from '@astrojs/starlight/components';
6-
import sourceCode from '../../../../../../example/src/main/java/example/ExampleCsvReaderWithClasspathInput.java?raw';
5+
import SourceExample from '../../../../components/SourceExample.astro';
76

87
As you can pass any `java.io.Reader` to the `CsvReader` class, you can read CSV data from any source that
98
provides a `Reader` or can be converted to a `Reader`.
@@ -13,7 +12,4 @@ This includes reading from the classpath.
1312

1413
In the following example, a CSV file is read from the classpath.
1514

16-
<Code code={sourceCode} title="ExampleCsvReaderWithClasspathInput.java" lang="java"/>
17-
18-
You also find this source code example in the
19-
[FastCSV GitHub repository](https://github.com/osiegmar/FastCSV/blob/main/example/src/main/java/example/ExampleCsvReaderWithClasspathInput.java).
15+
<SourceExample filename="ExampleCsvReaderWithClasspathInput.java"/>

docs/src/content/docs/guides/Examples/reading-ambiguous-data.mdx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
title: Reading ambiguous data
33
---
44

5-
import {Code} from '@astrojs/starlight/components';
6-
import sourceCode from '../../../../../../example/src/main/java/example/ExampleCsvReaderWithFaultyData.java?raw';
5+
import SourceExample from '../../../../components/SourceExample.astro';
76

87
There are a few widespread ambiguities in CSV files:
98

@@ -29,7 +28,4 @@ libraries and how they handle these ambiguities.
2928

3029
In the following example, a CSV file with various ambiguous data is read using FastCSV.
3130

32-
<Code code={sourceCode} title="ExampleCsvReaderWithFaultyData.java" lang="java"/>
33-
34-
You also find this source code example in the
35-
[FastCSV GitHub repository](https://github.com/osiegmar/FastCSV/blob/main/example/src/main/java/example/ExampleCsvReaderWithFaultyData.java).
31+
<SourceExample filename="ExampleCsvReaderWithFaultyData.java"/>

docs/src/content/docs/guides/Examples/skip-non-csv-head.mdx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
title: Skip non-CSV head
33
---
44

5-
import {Code} from '@astrojs/starlight/components';
6-
import sourceCode from '../../../../../../example/src/main/java/example/ExampleCsvReaderWithNonCsvAtStart.java?raw';
5+
import SourceExample from '../../../../components/SourceExample.astro';
76

87
Some CSV files contain one or more lines of text before the actual CSV data starts.
98
For example, it could look like this:
@@ -41,7 +40,4 @@ See the example on how to [handle comments](/guides/examples/handle-comments/) f
4140

4241
This example demonstrates how to skip non-CSV head lines when reading such a CSV file with FastCSV.
4342

44-
<Code code={sourceCode} title="ExampleCsvReaderWithNonCsvAtStart.java" lang="java"/>
45-
46-
You also find this source code example in the
47-
[FastCSV GitHub repository](https://github.com/osiegmar/FastCSV/blob/main/example/src/main/java/example/ExampleCsvReaderWithNonCsvAtStart.java).
43+
<SourceExample filename="ExampleCsvReaderWithNonCsvAtStart.java"/>

0 commit comments

Comments
 (0)