Skip to content

Commit 26d24a9

Browse files
committed
docs: prefer literalinclude for complete code examples
- Update SKILL.md to emphasize literalinclude as preferred method - Add file naming convention with underscore prefix (_ClassName.php) - Document file organization structure for code snippets - Add examples for text markers (start-after/end-before) - Update decision guide: literalinclude for 5+ lines - Update pre-commit checklist to prioritize literalinclude
1 parent c098989 commit 26d24a9

File tree

2 files changed

+153
-46
lines changed

2 files changed

+153
-46
lines changed

SKILL.md

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,20 +129,35 @@ See `references/text-roles-inline-code.md` for complete list.
129129

130130
| Content Type | Element |
131131
|--------------|---------|
132-
| Multi-line code | `.. code-block:: <language>` |
133-
| External code files | `.. literalinclude::` |
132+
| **Complete code examples (5+ lines)** | **`.. literalinclude::` (preferred)** |
133+
| Short inline snippets (< 5 lines) | `.. code-block:: <language>` |
134134
| Configuration options | `.. confval::` |
135135
| PHP API documentation | `.. php:class::`, `.. php:method::` |
136136
| Site settings | `.. typo3:site-set-settings::` |
137137

138-
### Code Block Requirements
138+
### literalinclude (Preferred for Code)
139+
140+
Store code as source files with underscore prefix, include via `literalinclude`:
141+
142+
```rst
143+
.. literalinclude:: _TranslationService.php
144+
:language: php
145+
:caption: EXT:my_ext/Classes/Service/TranslationService.php
146+
```
147+
148+
**File naming:** `_ClassName.php`, `_config.yaml`, `_tca-table.php`
149+
150+
**Benefits:** IDE support, syntax validation, reusability, maintainability.
151+
152+
### code-block (Short Snippets Only)
153+
154+
Use only for very short examples (< 5 lines):
139155

140156
```rst
141157
.. code-block:: php
142-
:caption: EXT:my_ext/Classes/Service/MyService.php
158+
:caption: Quick example
143159
144-
<?php
145-
// Always include :caption: with file path
160+
$vault->http()->withAuthentication($key, SecretPlacement::Bearer);
146161
```
147162

148163
**Always include:**

references/code-structure-elements.md

Lines changed: 132 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ Based on:
1313

1414
| Content Type | Element | Example |
1515
|--------------|---------|---------|
16-
| Multi-line code | `code-block` directive | PHP classes, TypoScript setup |
16+
| Short inline examples (< 5 lines) | `code-block` directive | Quick syntax demos |
17+
| **Complete code snippets** | **`literalinclude` directive (preferred)** | PHP classes, services, TCA |
1718
| External code files | `literalinclude` directive | Full example files |
1819
| Configuration options | `confval` directive | Extension settings, TCA fields |
1920
| Inline PHP | `:php:` role | Class names, method calls |
@@ -115,49 +116,137 @@ For XML/HTML, use comments:
115116
<property><!-- your-value --></property>
116117
```
117118

118-
## literalinclude Directive
119+
## literalinclude Directive (Preferred for Code)
119120

120-
Include code from external files. Preferred for longer examples or testable code.
121+
**`literalinclude` is the preferred way to include code examples** in TYPO3 documentation. It provides:
122+
123+
- **Syntax validation**: IDE support catches errors in source files
124+
- **Reusability**: Same snippet can be included in multiple places
125+
- **Maintainability**: Update code in one place, documentation stays in sync
126+
- **Testability**: Code files can be validated/linted separately
127+
128+
### File Naming Convention
129+
130+
Code snippet files use an **underscore prefix** to indicate they are include files:
131+
132+
| File Type | Naming Pattern | Example |
133+
|-----------|----------------|---------|
134+
| PHP classes | `_ClassName.php` | `_TranslationService.php` |
135+
| Configuration | `_config-name.yaml` | `_services.yaml` |
136+
| TCA | `_tca-tablename.php` | `_tca-apiendpoint.php` |
137+
| TypoScript | `_setup.typoscript` | `_setup.typoscript` |
138+
139+
### File Organization
140+
141+
Store code snippets in the same directory as the RST file that uses them, or in a shared location:
142+
143+
```
144+
Documentation/
145+
├── Usage/
146+
│ ├── Index.rst
147+
│ ├── ApiEndpointExample.rst
148+
│ ├── _ApiEndpoint.php ← DTO class
149+
│ ├── _ApiClientService.php ← Service class
150+
│ └── _tca-apiendpoint.php ← TCA definition
151+
├── Developer/
152+
│ ├── Adr/
153+
│ │ ├── ADR-009-ExtensionConfig.rst
154+
│ │ ├── _TranslationService.php
155+
│ │ └── _DirectUsage.php
156+
```
121157

122158
### Basic Syntax
123159

124160
```rst
125-
.. literalinclude:: _codesnippets/MyClass.php
161+
.. literalinclude:: _MyClass.php
126162
:language: php
127-
:caption: Classes/MyClass.php
163+
:caption: EXT:my_ext/Classes/Service/MyClass.php
128164
```
129165

130166
### Options
131167

132168
| Option | Purpose |
133169
|--------|---------|
134-
| `:language:` | Syntax highlighting language |
135-
| `:caption:` | Display caption |
136-
| `:lines:` | Include only specific lines |
170+
| `:language:` | Syntax highlighting language (required) |
171+
| `:caption:` | Display caption showing target file path |
172+
| `:lines:` | Include only specific lines (e.g., `15-30`) |
137173
| `:linenos:` | Show line numbers |
138-
| `:emphasize-lines:` | Highlight lines |
139-
| `:start-after:` | Start after matching text |
140-
| `:end-before:` | End before matching text |
174+
| `:emphasize-lines:` | Highlight specific lines |
175+
| `:start-after:` | Start after matching text marker |
176+
| `:end-before:` | End before matching text marker |
177+
178+
### Example: Complete Service Class
179+
180+
**Source file:**
181+
```php
182+
// Documentation/Usage/_TranslationService.php
183+
<?php
184+
185+
declare(strict_types=1);
186+
187+
namespace Acme\AcmeTranslate\Service;
188+
189+
use Netresearch\NrVault\Http\SecretPlacement;
190+
use Netresearch\NrVault\Service\VaultServiceInterface;
191+
192+
final class TranslationService
193+
{
194+
public function __construct(
195+
private readonly VaultServiceInterface $vault,
196+
) {}
197+
198+
public function translate(string $text): string
199+
{
200+
return $this->vault->http()
201+
->withAuthentication($this->apiKey, SecretPlacement::Bearer)
202+
->sendRequest($request);
203+
}
204+
}
205+
```
206+
207+
**RST usage:**
208+
```rst
209+
.. literalinclude:: _TranslationService.php
210+
:language: php
211+
:caption: EXT:acme_translate/Classes/Service/TranslationService.php
212+
:linenos:
213+
```
141214

142-
### Example with Line Selection
215+
### Example: Extract Specific Lines
143216

144217
```rst
145-
.. literalinclude:: _codesnippets/CompleteClass.php
218+
.. literalinclude:: _TranslationService.php
146219
:language: php
147-
:caption: Relevant excerpt
148-
:lines: 15-30
149-
:emphasize-lines: 5,10
220+
:caption: Key method implementation
221+
:lines: 17-22
222+
:emphasize-lines: 3-4
150223
```
151224

152-
### File Organization
225+
### Example: Using Text Markers
153226

227+
For more maintainable line selection, use text markers:
228+
229+
**Source file with markers:**
230+
```php
231+
// ... class definition ...
232+
233+
// START: translate-method
234+
public function translate(string $text): string
235+
{
236+
return $this->vault->http()
237+
->withAuthentication($this->apiKey, SecretPlacement::Bearer)
238+
->sendRequest($request);
239+
}
240+
// END: translate-method
154241
```
155-
Documentation/
156-
├── Section/
157-
│ ├── Index.rst
158-
│ └── _codesnippets/
159-
│ ├── MyClass.php
160-
│ └── config.yaml
242+
243+
**RST usage:**
244+
```rst
245+
.. literalinclude:: _TranslationService.php
246+
:language: php
247+
:caption: Translation method
248+
:start-after: // START: translate-method
249+
:end-before: // END: translate-method
161250
```
162251

163252
## Inline Code Roles
@@ -344,17 +433,20 @@ The `PROJECT:` prefix loads files from the extension directory.
344433

345434
## Decision Guide
346435

347-
### Use `code-block` When
436+
### Use `literalinclude` When (Preferred)
348437

349-
- Showing example code inline in documentation
350-
- Code is short (< 30 lines)
351-
- Code is illustrative, not production-ready
438+
- **Complete code examples** (classes, services, configuration)
439+
- Code is **5+ lines** or represents a complete unit
440+
- Code should be **syntactically valid** and testable
441+
- Same code might be **referenced multiple times**
442+
- You want **IDE support** for the source files
352443

353-
### Use `literalinclude` When
444+
### Use `code-block` When
354445

355-
- Code is long (> 30 lines)
356-
- Code should be executable/testable
357-
- Same code is referenced multiple times
446+
- Very short snippets (< 5 lines)
447+
- Pseudocode or conceptual examples
448+
- Code with intentional placeholders like `<your-value>`
449+
- Quick syntax demonstrations
358450

359451
### Use `confval` When
360452

@@ -376,15 +468,15 @@ The `PROJECT:` prefix loads files from the extension directory.
376468

377469
## Pre-Commit Checklist
378470

379-
1.Code blocks have `:caption:` with file path
380-
2.Correct language identifier used
381-
3.Syntax is valid (highlighting works)
382-
4.Placeholders use angle brackets
383-
5.Long code uses `literalinclude`
384-
6.Configuration uses `confval` directive
385-
7.Inline code uses appropriate roles
386-
8.UI elements use `:guilabel:`
387-
9.File paths use `:file:` or `:path:`
471+
1.**Complete code examples use `literalinclude`** with `_filename.ext` source files
472+
2.Source files have underscore prefix (`_MyClass.php`)
473+
3.All code blocks/includes have `:caption:` with target file path
474+
4.Correct `:language:` identifier used
475+
5.Code syntax is valid (highlighting works)
476+
6.Placeholders use angle brackets `<your-value>`
477+
7.Configuration uses `confval` directive
478+
8.Inline code uses appropriate roles (`:php:`, `:file:`, etc.)
479+
9.UI elements use `:guilabel:`
388480

389481
## References
390482

0 commit comments

Comments
 (0)