|
| 1 | +/** |
| 2 | + * Handles inline editable commands in documentation. |
| 3 | + * Replaces placeholders in code blocks with inline input fields. |
| 4 | + */ |
| 5 | +document.addEventListener('DOMContentLoaded', () => { |
| 6 | + const codeBlocks = document.querySelectorAll('div.highlight-sh pre, div.highlight-bash pre, div.highlight-default pre'); |
| 7 | + |
| 8 | + codeBlocks.forEach(block => { |
| 9 | + |
| 10 | + const originalHTML = block.innerHTML; |
| 11 | + |
| 12 | + const placeholders = [ |
| 13 | + "<your virtual env name>", |
| 14 | + "<model name>", |
| 15 | + "<tokenizer path>", |
| 16 | + "<Hugging Face access token>", |
| 17 | + "<output directory to store run logs>", |
| 18 | + "<name for this run>", |
| 19 | + "<number of fine-tuning steps to run>", |
| 20 | + "<batch size per device>", |
| 21 | + "<Hugging Face dataset name>", |
| 22 | + "<data split for train>", |
| 23 | + "<data columns to train on>", |
| 24 | + "<gcs path for MaxText checkpoint>", |
| 25 | + "<Google Cloud Project ID>", |
| 26 | + "<Name of GKE Cluster>", |
| 27 | + "<GKE Cluster Zone>", |
| 28 | + "<Name of Workload>", |
| 29 | + "<TPU Type>", |
| 30 | + "<GCS Path for Output/Logs>", |
| 31 | + "<Fine-Tuning Steps>", |
| 32 | + "<Hugging Face Access Token>", |
| 33 | + "<Model Name>", |
| 34 | + "<Model Tokenizer>", |
| 35 | + "<Hugging Face Dataset Name>", |
| 36 | + "<Data Split for Train>", |
| 37 | + "<Data Columns to Train on>", |
| 38 | + "<cluster name>", |
| 39 | + "<GCP project ID>", |
| 40 | + "<zone name>", |
| 41 | + "<path/to/gcr.io>", |
| 42 | + "<number of slices>", |
| 43 | + "<Flag to use zarr3>", |
| 44 | + "<Flag to use ocdbt>", |
| 45 | + "<Hugging Face Model>", |
| 46 | + "<MaxText Model>", |
| 47 | + "<Tokenizer>", |
| 48 | + "<Name for this run>", |
| 49 | + "<Docker Image Name>" |
| 50 | + ]; |
| 51 | + |
| 52 | + let newHTML = originalHTML; |
| 53 | + |
| 54 | + placeholders.forEach(placeholder => { |
| 55 | + // 1. create robust regex for this placeholder |
| 56 | + // escape chars |
| 57 | + const escapeRegex = (string) => string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); |
| 58 | + |
| 59 | + const htmlEscapedKey = placeholder |
| 60 | + .replace(/&/g, '&') |
| 61 | + .replace(/</g, '<') |
| 62 | + .replace(/>/g, '>'); |
| 63 | + |
| 64 | + let pattern = ''; |
| 65 | + for (let i = 0; i < htmlEscapedKey.length; i++) { |
| 66 | + const char = htmlEscapedKey[i]; |
| 67 | + pattern += escapeRegex(char) + '(?:<[^>]+>)*'; |
| 68 | + } |
| 69 | + |
| 70 | + const regex = new RegExp(pattern, 'g'); |
| 71 | + |
| 72 | + // Replace with an input element |
| 73 | + // We use the original placeholder text as placeholder for the input |
| 74 | + const inputHTML = `<input class="inline-input" placeholder="${placeholder}" style="width: ${placeholder.length + 2}ch;" />`; |
| 75 | + |
| 76 | + newHTML = newHTML.replace(regex, inputHTML); |
| 77 | + }); |
| 78 | + |
| 79 | + if (newHTML !== originalHTML) { |
| 80 | + block.innerHTML = newHTML; |
| 81 | + } |
| 82 | + }); |
| 83 | + |
| 84 | + // Add event listeners to newly created inputs to auto-resize |
| 85 | + document.querySelectorAll('.inline-input').forEach(input => { |
| 86 | + input.addEventListener('input', function () { |
| 87 | + this.style.width = Math.max(this.value.length, this.placeholder.length) + 2 + 'ch'; |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + /** |
| 92 | + * Intercept copy button clicks to include user input values. |
| 93 | + * Runs in capture phase to precede sphinx-copybutton's listener. |
| 94 | + */ |
| 95 | + document.addEventListener('click', (event) => { |
| 96 | + // Check if the clicked element is a copy button or inside one |
| 97 | + const button = event.target.closest('.copybtn'); |
| 98 | + if (!button) return; |
| 99 | + |
| 100 | + // Find the associated code block |
| 101 | + // Sphinx-copybutton places the button inside .highlight usually |
| 102 | + const highlightDiv = button.closest('.highlight'); |
| 103 | + if (!highlightDiv) return; |
| 104 | + |
| 105 | + const inputs = highlightDiv.querySelectorAll('input.inline-input'); |
| 106 | + if (inputs.length === 0) return; |
| 107 | + |
| 108 | + const swaps = []; |
| 109 | + inputs.forEach(input => { |
| 110 | + // Create a temporary span with the input's current value |
| 111 | + const span = document.createElement('span'); |
| 112 | + // If value is empty, fallback to placeholder to match original text behavior |
| 113 | + const val = input.value; |
| 114 | + span.textContent = val ? val : input.placeholder; |
| 115 | + |
| 116 | + // Mimic input appearance slightly if needed, but plain text is what we want copied |
| 117 | + span.style.color = val ? 'inherit' : 'gray'; |
| 118 | + |
| 119 | + input.replaceWith(span); |
| 120 | + swaps.push({ input, span }); |
| 121 | + }); |
| 122 | + |
| 123 | + // Revert immediately after the current event loop |
| 124 | + setTimeout(() => { |
| 125 | + swaps.forEach(({ input, span }) => { |
| 126 | + span.replaceWith(input); |
| 127 | + }); |
| 128 | + }, 0); |
| 129 | + }, true); |
| 130 | +}); |
0 commit comments