מהדר שפת א

קוד שפת א

אפשרויות הטמעה

תוצאה

פקודות מוגדרות

*א1* — הצג
*א2* — הטמע רגיל
*א3* — חיפוש והטמעה מיוחדת
`; /* תרגום */ function compileCode() { hideOptions(); pendingOptions = []; const source = document.getElementById("code").value; try { const lines = source .split(/\r?\n/) .map(line => line.trim()) .filter(line => line !== ""); if (lines.length === 0) { throw new Error("לא הוזן קוד"); } program = []; for (let i = 0; i < lines.length; i++) { program.push({ original: lines[i], html: null, type: "pending", line: i }); } for (const item of program) { translateLine(item); } rebuildResult(); } catch (error) { showError(error.message); } } /* תרגום שורה */ function translateLine(item) { const command = item.original; /* א1 */ if (command.startsWith("*א1*")) { const value = command.substring(4).trim(); if (!value) { throw new Error("חסר תוכן אחרי *א1*"); } item.html = displayValue(value); item.type = "display"; return; } /* א2 */ if (command.startsWith("*א2*")) { const value = command.substring(4).trim(); if (!value) { throw new Error("חסר תוכן אחרי *א2*"); } item.html = embedValue(value); item.type = "embed"; return; } /* א3 */ if (command.startsWith("*א3*")) { const query = command.substring(4).trim(); if (!query) { throw new Error( "חסר טקסט חיפוש אחרי *א3*" ); } const options = searchForEmbedOptions(query); if (options.length === 0) { item.html = ` `; item.type = "search"; return; } item.html = null; item.type = "search"; pendingOptions.push({ item: item, options: options }); return; } throw new Error( "פקודה לא תקינה: " + command ); } /* א1 — הצגה */ function displayValue(value) { const lower = value .toLowerCase() .split("?")[0] .split("#")[0]; /* תמונה */ if ( lower.endsWith(".jpg") || lower.endsWith(".jpeg") || lower.endsWith(".png") || lower.endsWith(".gif") || lower.endsWith(".webp") || lower.endsWith(".svg") ) { return ` `; } /* וידאו */ if ( lower.endsWith(".mp4") || lower.endsWith(".webm") || lower.endsWith(".ogg") || lower.endsWith(".mov") ) { return ` `; } /* אודיו */ if ( lower.endsWith(".mp3") || lower.endsWith(".wav") || lower.endsWith(".m4a") || lower.endsWith(".aac") || lower.endsWith(".flac") ) { return ` `; } /* קישור */ if ( lower.startsWith("http://") || lower.startsWith("https://") ) { return ` ${escapeHtml(value)} `; } /* טקסט */ return `
${escapeHtml(value)}
`; } /* א2 — הטמעה */ function embedValue(value) { const lower = value.toLowerCase(); /* YouTube */ if ( lower.includes("youtube.com/") || lower.includes("youtu.be/") ) { const videoId = getYouTubeId(value); if (videoId) { return ` `; } } /* Vimeo */ if ( lower.includes("vimeo.com/") ) { const id = getVimeoId(value); if (id) { return ` `; } } const mediaURL = lower .split("?")[0] .split("#")[0]; /* תמונה */ if ( mediaURL.endsWith(".jpg") || mediaURL.endsWith(".jpeg") || mediaURL.endsWith(".png") || mediaURL.endsWith(".gif") || mediaURL.endsWith(".webp") || mediaURL.endsWith(".svg") ) { return ` `; } /* וידאו */ if ( mediaURL.endsWith(".mp4") || mediaURL.endsWith(".webm") || mediaURL.endsWith(".ogg") || mediaURL.endsWith(".mov") ) { return ` `; } /* אודיו */ if ( mediaURL.endsWith(".mp3") || mediaURL.endsWith(".wav") || mediaURL.endsWith(".m4a") || mediaURL.endsWith(".aac") || mediaURL.endsWith(".flac") ) { return ` `; } /* iframe */ if ( lower.startsWith("http://") || lower.startsWith("https://") ) { return ` `; } throw new Error( "לא ניתן לזהות את התוכן להטמעה: " + value ); } /* א3 */ function searchForEmbedOptions(query) { const results = []; if ( query.startsWith("http://") || query.startsWith("https://") ) { const lower = query.toLowerCase(); /* YouTube */ if ( lower.includes("youtube.com") || lower.includes("youtu.be") ) { const id = getYouTubeId(query); if (id) { results.push({ title: "YouTube", description: "סרטון YouTube שניתן להטמעה", type: "YouTube", html: ` ` }); } } /* Vimeo */ if ( lower.includes("vimeo.com") ) { const id = getVimeoId(query); if (id) { results.push({ title: "Vimeo", description: "סרטון Vimeo שניתן להטמעה", type: "Vimeo", html: ` ` }); } } /* Instagram */ if ( lower.includes("instagram.com") ) { results.push({ title: "Instagram", description: "נמצאה כתובת Instagram. ההטמעה תלויה בהרשאות התוכן.", type: "Instagram", html: `
` }); } /* TikTok */ if ( lower.includes("tiktok.com") ) { results.push({ title: "TikTok", description: "נמצאה כתובת TikTok. ההטמעה תלויה בהרשאות התוכן.", type: "TikTok", html: `
` }); } /* iframe */ results.push({ title: "הטמעה ישירה", description: "ניסיון להציג את הכתובת בתוך iframe.", type: "iframe", html: ` ` }); return results; } const encoded = encodeURIComponent(query); results.push({ title: "חיפוש YouTube", description: "חיפוש התוכן ב-YouTube", type: "חיפוש", url: "https://www.youtube.com/results?search_query=" + encoded, html: null }); results.push({ title: "חיפוש Google", description: "חיפוש כללי למציאת מקור מתאים להטמעה", type: "חיפוש", url: "https://www.google.com/search?q=" + encoded, html: null }); return results; } /* הצגת אפשרויות */ function showOptions() { const box = document.getElementById("options"); const content = document.getElementById("optionsContent"); content.innerHTML = ""; if ( pendingOptions.length === 0 ) { box.style.display = "none"; return; } for ( const pending of pendingOptions ) { const heading = document.createElement("div"); heading.innerHTML = ` ${escapeHtml(pending.item.original)} `; content.appendChild(heading); pending.options.forEach( (option, optionIndex) => { const div = document.createElement("div"); div.className = "option"; div.innerHTML = `
${escapeHtml(option.title)}
${escapeHtml(option.type)}
${escapeHtml(option.description)}
`; const button = document.createElement("button"); button.type = "button"; if (option.html) { button.textContent = "בחר והטמע"; button.addEventListener( "click", () => { selectEmbedding( pending.item.line, optionIndex ); } ); } else { button.textContent = "פתח חיפוש"; button.addEventListener( "click", () => { window.open( option.url, "_blank", "noopener,noreferrer" ); } ); } div.appendChild(button); content.appendChild(div); } ); } box.style.display = "block"; } /* בחירת הטמעה */ function selectEmbedding( lineIndex, optionIndex ) { const item = program[lineIndex]; const pending = pendingOptions.find( p => p.item.line === lineIndex ); if (!pending) { return; } const option = pending.options[optionIndex]; if (!option) { return; } item.html = option.html; item.type = "embed-selected"; pendingOptions = pendingOptions.filter( p => p.item.line !== lineIndex ); rebuildResult(); } /* בניית HTML */ function rebuildResult() { let body = ""; for ( const item of program ) { if ( item.html !== null ) { body += item.html + "\n"; } else { body += ` `; } } const fullHTML = HTML_HEAD + body + HTML_FOOT; /* חשוב: התוצאה מוצגת כטקסט בתוך החלון. היא לא נכתבת כ-HTML לדף עצמו. */ document .getElementById("result") .textContent = fullHTML; showOptions(); } /* שגיאה */ function showError(message) { document .getElementById("result") .innerHTML = `
פקודה לא תקינה

${escapeHtml(message)}
`; } /* העתקה */ function copyResult() { const result = document.getElementById("result"); const text = result.textContent; if (!text) { return; } if ( navigator.clipboard && navigator.clipboard.writeText ) { navigator.clipboard .writeText(text) .catch(() => { fallbackCopy(text); }); } else { fallbackCopy(text); } } /* העתקה חלופית */ function fallbackCopy(text) { const textarea = document.createElement("textarea"); textarea.value = text; textarea.style.position = "fixed"; textarea.style.left = "-9999px"; textarea.style.top = "0"; document.body.appendChild( textarea ); textarea.focus(); textarea.select(); try { document.execCommand("copy"); } catch (error) { console.error( "ההעתקה נכשלה", error ); } textarea.remove(); } /* YouTube ID */ function getYouTubeId(url) { try { const parsed = new URL(url); const hostname = parsed.hostname.toLowerCase(); if ( hostname === "youtu.be" ) { return parsed.pathname .split("/") .filter(Boolean)[0] || null; } if ( hostname.includes("youtube.com") ) { const watchId = parsed.searchParams.get("v"); if (watchId) { return watchId; } const shortsMatch = parsed.pathname.match( /\/shorts\/([^/?]+)/ ); if (shortsMatch) { return shortsMatch[1]; } const embedMatch = parsed.pathname.match( /\/embed\/([^/?]+)/ ); if (embedMatch) { return embedMatch[1]; } } return null; } catch { return null; } } /* Vimeo ID */ function getVimeoId(url) { const match = url.match( /vimeo\.com\/(?:video\/)?(\d+)/ ); return match ? match[1] : null; } /* הסתרת אפשרויות */ function hideOptions() { document .getElementById("options") .style.display = "none"; } /* הגנה על HTML */ function escapeHtml(text) { return String(text) .replace(/&/g, "&") .replace(//g, ">") .replace(/"/g, """) .replace(/'/g, "'"); } function escapeAttribute(text) { return escapeHtml(text); }