Skip to content

Commit d2b5862

Browse files
DOC-5804 updated spec for new special comment
1 parent 3b730f7 commit d2b5862

File tree

3 files changed

+79
-10
lines changed

3 files changed

+79
-10
lines changed

build/tcedocs/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ The `TEST_MARKER` dictionary maps programming languages to test framework annota
111111

112112
Each code example uses special comments, such as `HIDE_START` and `REMOVE_START`, to control how the examples are displayed. The following list gives an explanation:
113113

114-
- `EXAMPLE id`: Defines the identifier of the source code example file, where `id` is any common string (for example, `cmds_string`). IDs should only contain ASCII alphanumeric characters, underline characters (`_`), or hyphen characters (`-`). Do not use multibyte characters.
114+
- `EXAMPLE: id`: Defines the identifier of the source code example file, where `id` is any common string (for example, `cmds_string`). IDs should only contain ASCII alphanumeric characters, underline characters (`_`), or hyphen characters (`-`). Do not use multibyte characters.
115+
- `BINDER_ID id`: Defines the [BinderHub](https://binderhub.readthedocs.io/en/latest/) commit hash for the example. This is used to generate a link to a BinderHub instance that will run the example.
115116
- `HIDE_START`: Starts a code block that should be *hidden* when showing the example. This code block will only become visible if **unhide** (the eye button) is clicked.
116117
- `HIDE_END`: Marks the end a hidden code block.
117118
- `REMOVE_START`: Starts a code block that should be entirely removed when the example is processed by the build code. This is useful for removing lines of code that do not contribute to the example but are needed to embed the code into a proper test case or framework. Good examples of such code blocks are imports of external libraries or test assertions.

build/tcedocs/SPECIFICATION.md

Lines changed: 76 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -299,24 +299,84 @@ This allows the same language to appear multiple times in the tab interface with
299299
**Purpose**: Parse and process individual example files
300300

301301
**Special Comment Markers**:
302-
- `EXAMPLE: {id}`: Defines the example identifier
302+
- `EXAMPLE: {id}`: Defines the example identifier (required, must be first line)
303+
- `BINDER_ID {hash}`: Defines the BinderHub commit hash for interactive notebook link (optional)
303304
- `HIDE_START` / `HIDE_END`: Code blocks hidden by default (revealed with eye button)
304305
- `REMOVE_START` / `REMOVE_END`: Code blocks completely removed from display
305306
- `STEP_START {name}` / `STEP_END`: Named code blocks for step-by-step examples
306307

308+
**BINDER_ID Marker**:
309+
310+
The `BINDER_ID` marker provides a commit hash for [BinderHub](https://binderhub.readthedocs.io/en/latest/) integration, allowing users to run examples in an interactive Jupyter notebook environment.
311+
312+
**Syntax**:
313+
```python
314+
# EXAMPLE: example_id
315+
# BINDER_ID 6bbed3da294e8de5a8c2ad99abf883731a50d4dd
316+
```
317+
318+
**Requirements**:
319+
- Must appear after the `EXAMPLE:` marker (typically on line 2)
320+
- Must use the language's comment prefix (e.g., `#` for Python, `//` for JavaScript)
321+
- The hash value is a Git commit SHA from the binder-launchers repository
322+
- Only one `BINDER_ID` per example file
323+
- Optional - not all examples need BinderHub integration
324+
325+
**Usage**:
326+
The hash is used to construct a BinderHub URL like:
327+
```
328+
https://redis.io/binder/v2/gh/redis/binder-launchers/{hash}?urlpath=%2Fdoc%2Ftree%2Fdemo.ipynb
329+
```
330+
331+
This allows documentation to include "Try this in Jupyter" links that launch interactive notebook environments with the example pre-loaded.
332+
307333
**Processing Algorithm**:
308334
1. Read file line by line
309335
2. Detect special comment markers (using language-specific comment prefix)
310-
3. Track hidden/highlighted/removed ranges
311-
4. Extract named steps
312-
5. Filter out test markers and removed blocks
313-
6. Generate metadata (highlight ranges, hidden ranges, named steps)
314-
7. Write processed content back to file (in-place modification)
336+
3. Extract example ID from `EXAMPLE:` marker (line 1)
337+
4. Extract BinderHub hash from `BINDER_ID` marker if present (typically line 2)
338+
5. Track hidden/highlighted/removed ranges
339+
6. Extract named steps with `STEP_START`/`STEP_END`
340+
7. Filter out test markers and removed blocks
341+
8. Generate metadata (highlight ranges, hidden ranges, named steps, binder ID)
342+
9. Write processed content back to file (in-place modification)
343+
344+
**BINDER_ID Extraction Details**:
345+
346+
The parser should implement the following logic in `build/components/example.py`:
347+
348+
1. **Detection**: After reading the `EXAMPLE:` line, check subsequent lines for `BINDER_ID` marker
349+
- Look for the pattern: `{comment_prefix} BINDER_ID {hash}`
350+
- Example for Python: `# BINDER_ID 6bbed3da294e8de5a8c2ad99abf883731a50d4dd`
351+
- Example for JavaScript: `// BINDER_ID 6bbed3da294e8de5a8c2ad99abf883731a50d4dd`
352+
353+
2. **Extraction**: Parse the hash value
354+
- Strip the comment prefix and `BINDER_ID` keyword
355+
- Trim whitespace from the remaining string
356+
- The result should be a 40-character Git commit SHA (hexadecimal)
357+
- Example regex pattern: `{comment_prefix}\s*BINDER_ID\s+([a-f0-9]{40})`
358+
359+
3. **Validation** (optional but recommended):
360+
- Verify the hash is exactly 40 characters
361+
- Verify it contains only hexadecimal characters (0-9, a-f)
362+
- Log a warning if validation fails but continue processing
363+
364+
4. **Storage**: Add to metadata
365+
- Store in the language-specific metadata object as `binderId`
366+
- Type: string
367+
- Only include the field if `BINDER_ID` marker was found
368+
- Do not set to null or empty string if not found - omit the field entirely
369+
370+
5. **Line Processing**: The `BINDER_ID` line should be treated like `EXAMPLE:`
371+
- It should NOT appear in the processed output file
372+
- It should NOT affect line number calculations for highlight/hidden ranges
373+
- Remove it during processing (similar to how `EXAMPLE:` line is handled)
315374

316375
**Output Metadata** (stored in `examples.json`):
317376
- `highlight`: Line ranges to highlight (e.g., `["1-10", "15-20"]`)
318377
- `hidden`: Line ranges initially hidden (e.g., `["5-8"]`)
319378
- `named_steps`: Map of step names to line ranges (e.g., `{"connect": "1-5"}`)
379+
- `binderId`: BinderHub commit hash (optional, e.g., `"6bbed3da294e8de5a8c2ad99abf883731a50d4dd"`)
320380

321381
> **Note**: For language-specific configuration (comment prefixes, test markers), see [Appendix: Language Mappings](#language-mappings).
322382
@@ -1182,7 +1242,8 @@ In Markdown files:
11821242
"named_steps": {
11831243
"step_name": "1-5"
11841244
},
1185-
"sourceUrl": "https://github.com/..."
1245+
"sourceUrl": "https://github.com/...",
1246+
"binderId": "6bbed3da294e8de5a8c2ad99abf883731a50d4dd"
11861247
}
11871248
}
11881249
}
@@ -1196,12 +1257,20 @@ In Markdown files:
11961257
- `hidden`: Line ranges initially hidden (revealed with eye button)
11971258
- `named_steps`: Map of step names to line ranges
11981259
- `sourceUrl`: GitHub link to original source (null for local examples)
1260+
- `binderId`: **Optional** - BinderHub commit hash for interactive notebook link (string, only present if `BINDER_ID` marker exists in source file)
1261+
1262+
**Metadata Hierarchy**:
1263+
- The `binderId` field is stored **per-language**, not per-example-set
1264+
- This allows different languages to have different BinderHub configurations
1265+
- Example: Python might have a BinderHub link, while Node.js doesn't
1266+
- If `BINDER_ID` marker is not present in the source file, the `binderId` field should be omitted entirely (not set to null or empty string)
11991267

12001268
### Special Comment Reference
12011269

12021270
| Marker | Purpose | Example | Notes |
12031271
|--------|---------|---------|-------|
12041272
| `EXAMPLE: id` | Define example ID | `# EXAMPLE: home_vecsets` | Must be first line |
1273+
| `BINDER_ID hash` | Define BinderHub commit hash | `# BINDER_ID 6bbed3da294e8de5a8c2ad99abf883731a50d4dd` | Optional, typically line 2. Used to generate interactive notebook links. Hash is a Git commit SHA from binder-launchers repo. |
12051274
| `HIDE_START` | Start hidden block | `# HIDE_START` | Code hidden by default |
12061275
| `HIDE_END` | End hidden block | `# HIDE_END` | Must close HIDE_START |
12071276
| `REMOVE_START` | Start removed block | `# REMOVE_START` | Code completely removed |

local_examples/client-specific/redis-py/landing.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# EXAMPLE: landing
2-
# REMOVE_START
2+
# BINDER_ID 6bbed3da294e8de5a8c2ad99abf883731a50d4dd
33
import redis
4-
# REMOVE_END
54

65
# STEP_START connect
76
r = redis.Redis(host='localhost', port=6379, decode_responses=True)

0 commit comments

Comments
 (0)