Skip to content

Commit 722dc99

Browse files
committed
support space between # and | for yaml options
1 parent 98eb454 commit 722dc99

File tree

3 files changed

+24
-18
lines changed

3 files changed

+24
-18
lines changed

src/core/handlers/base.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import {
2828
} from "../lib/mapped-text.ts";
2929
import {
3030
addLanguageComment,
31-
optionCommentPrefixFromLanguage,
31+
optionCommentPatternFromLanguage,
3232
} from "../lib/partition-cell-options.ts";
3333
import { ConcreteSchema } from "../lib/yaml-schema/types.ts";
3434
import {
@@ -516,10 +516,11 @@ export const baseHandler: LanguageHandler = {
516516
// split content into front matter vs input
517517
const contentLines = mappedLines(cell.sourceWithYaml!, true);
518518
const frontMatterLines: MappedString[] = [];
519-
const comment: string = optionCommentPrefixFromLanguage(this.languageName);
519+
const commentPattern = optionCommentPatternFromLanguage(this.languageName);
520520
let inputIndex = 0;
521521
for (const contentLine of contentLines) {
522-
if (contentLine.value.startsWith(comment)) {
522+
const commentMatch = contentLine.value.match(commentPattern);
523+
if (commentMatch) {
523524
if (contentLine.value.indexOf("echo: fenced") === -1) {
524525
frontMatterLines.push(contentLine);
525526
}

src/core/lib/partition-cell-options.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,17 @@ export function partitionCellOptions(
4141
source: string[],
4242
) {
4343
const commentChars = langCommentChars(language);
44-
const optionPrefix = optionCommentPrefix(commentChars[0]);
44+
const optionPattern = optionCommentPattern(commentChars[0]);
4545
const optionSuffix = commentChars[1] || "";
4646

4747
// find the yaml lines
4848
const optionsSource: string[] = [];
4949
const yamlLines: string[] = [];
5050
for (const line of source) {
51-
if (line.startsWith(optionPrefix)) {
51+
const optionMatch = line.match(optionPattern);
52+
if (optionMatch) {
5253
if (!optionSuffix || line.trimRight().endsWith(optionSuffix)) {
53-
let yamlOption = line.substring(optionPrefix.length);
54+
let yamlOption = line.substring(optionMatch[0].length);
5455
if (optionSuffix) {
5556
yamlOption = yamlOption.trimRight();
5657
yamlOption = yamlOption.substring(
@@ -150,7 +151,7 @@ export function partitionCellOptionsText(
150151
source: MappedString,
151152
) {
152153
const commentChars = langCommentChars(language);
153-
const optionPrefix = optionCommentPrefix(commentChars[0]);
154+
const optionPattern = optionCommentPattern(commentChars[0]);
154155
const optionSuffix = commentChars[1] || "";
155156

156157
// find the yaml lines
@@ -159,22 +160,24 @@ export function partitionCellOptionsText(
159160

160161
let endOfYaml = 0;
161162
for (const line of rangedLines(source.value, true)) {
162-
if (line.substring.startsWith(optionPrefix)) {
163+
const optionMatch = line.substring.match(optionPattern);
164+
if (optionMatch) {
163165
if (!optionSuffix || line.substring.trimRight().endsWith(optionSuffix)) {
164-
let yamlOption = line.substring.substring(optionPrefix.length);
166+
let yamlOption = line.substring.substring(optionMatch[0].length);
165167
if (optionSuffix) {
166168
yamlOption = yamlOption.trimRight();
167169
yamlOption = yamlOption.substring(
168170
0,
169171
yamlOption.length - optionSuffix.length,
170172
);
171173
}
172-
endOfYaml = line.range.start + optionPrefix.length + yamlOption.length -
174+
endOfYaml = line.range.start + optionMatch[0].length +
175+
yamlOption.length -
173176
optionSuffix.length;
174177
const rangedYamlOption = {
175178
substring: yamlOption,
176179
range: {
177-
start: line.range.start + optionPrefix.length,
180+
start: line.range.start + optionMatch[0].length,
178181
end: endOfYaml,
179182
},
180183
};
@@ -252,8 +255,8 @@ export function langCommentChars(lang: string): string[] {
252255
return chars;
253256
}
254257
}
255-
export function optionCommentPrefix(comment: string) {
256-
return comment + "| ";
258+
export function optionCommentPattern(comment: string) {
259+
return new RegExp("^" + comment + "\\s*\\| ");
257260
}
258261

259262
// FIXME this is an awkward spot for this particular entry point
@@ -264,8 +267,8 @@ export function addLanguageComment(
264267
kLangCommentChars[language] = comment;
265268
}
266269

267-
export function optionCommentPrefixFromLanguage(language: string) {
268-
return optionCommentPrefix(langCommentChars(language)[0]);
270+
export function optionCommentPatternFromLanguage(language: string) {
271+
return optionCommentPattern(langCommentChars(language)[0]);
269272
}
270273

271274
export const kLangCommentChars: Record<string, string | [string, string]> = {

src/resources/jupyter/notebook.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# pyright: reportMissingImports=false
22

33
import os
4+
import re
45
import atexit
56
import glob
67
import sys
@@ -464,15 +465,16 @@ def nb_cell_yaml_options(lang, cell):
464465
def nb_cell_yaml_lines(lang, source):
465466
# determine language comment chars
466467
comment_chars = nb_language_comment_chars(lang)
467-
option_prefix = comment_chars[0] + "| "
468+
option_pattern = "^" + comment_chars[0] + "\\s*\\| "
468469
option_suffix = comment_chars[1] if len(comment_chars) > 1 else None
469470

470471
# go through the lines until we've found all of the yaml
471472
yaml_lines = []
472473
for line in source.splitlines():
473-
if line.startswith(option_prefix):
474+
option_match = re.match(option_pattern, line)
475+
if option_match:
474476
if (not option_suffix) or line.rstrip().endswith(option_suffix):
475-
yaml_option = line[len(option_prefix):]
477+
yaml_option = line[len(option_match.group()):]
476478
if (option_suffix):
477479
yaml_option = yaml_option.rstrip()[:-len(option_suffix)]
478480
yaml_lines.append(yaml_option)

0 commit comments

Comments
 (0)