(notebook: PercentNotebook)
| 49 | * @returns The serialized percent format string |
| 50 | */ |
| 51 | export function serializePercentFormat(notebook: PercentNotebook): string { |
| 52 | const lines: string[] = [] |
| 53 | |
| 54 | for (const cell of notebook.cells) { |
| 55 | // Build the cell marker |
| 56 | let marker = '# %%' |
| 57 | |
| 58 | if (cell.cellType === 'markdown') { |
| 59 | marker += ' [markdown]' |
| 60 | } else if (cell.cellType === 'raw') { |
| 61 | marker += ' [raw]' |
| 62 | } |
| 63 | |
| 64 | if (cell.title) { |
| 65 | marker += ` ${cell.title}` |
| 66 | } |
| 67 | |
| 68 | if (cell.tags && cell.tags.length > 0) { |
| 69 | const tagsStr = cell.tags.map(t => JSON.stringify(t)).join(', ') |
| 70 | marker += ` tags=[${tagsStr}]` |
| 71 | } |
| 72 | |
| 73 | lines.push(marker) |
| 74 | |
| 75 | // Add cell content |
| 76 | if (cell.cellType === 'markdown') { |
| 77 | // Prefix each line with '# ' |
| 78 | const contentLines = cell.content.split('\n') |
| 79 | for (const contentLine of contentLines) { |
| 80 | if (contentLine === '') { |
| 81 | lines.push('#') |
| 82 | } else { |
| 83 | lines.push(`# ${contentLine}`) |
| 84 | } |
| 85 | } |
| 86 | } else { |
| 87 | lines.push(cell.content) |
| 88 | } |
| 89 | |
| 90 | // Add empty line between cells for readability |
| 91 | lines.push('') |
| 92 | } |
| 93 | |
| 94 | // Remove trailing empty line if present |
| 95 | while (lines.length > 0 && lines[lines.length - 1] === '') { |
| 96 | lines.pop() |
| 97 | } |
| 98 | |
| 99 | return `${lines.join('\n')}\n` |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Converts a Deepnote project file into separate percent format (.py) files. |
no outgoing calls
no test coverage detected