מהדר שפת א

קוד שפת א

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

תוצאה

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

*א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++) { const line = lines[i]; program.push({ original: line, 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*" ); } /* בשלב הזה אין לנו API. לכן מייצרים תוצאות חיפוש אפשריות לפי השירותים שניתן לזהות. */ 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(); /* תמונה */ 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/watch") || lower.includes("youtu.be/") ) { const videoId = getYouTubeId(value); if (videoId) { return ` `; } } /* Vimeo */ if (lower.includes("vimeo.com/")) { const id = getVimeoId(value); if (id) { return ` `; } } /* קבצי תמונה */ 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 ` `; } /* ברירת מחדל: iframe */ if ( lower.startsWith("http://") || lower.startsWith("https://") ) { return ` `; } throw new Error( "לא ניתן לזהות את התוכן להטמעה: " + value ); } /* א3 חיפוש אפשרויות */ function searchForEmbedOptions(query) { const results = []; /* אם המשתמש נתן URL */ 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: `
` }); } /* כתובת כללית */ results.push({ title: "הטמעה ישירה", description: "ניסיון להציג את הכתובת בתוך iframe.", type: "iframe", html: ` ` }); return results; } /* חיפוש טקסטואלי ללא API: מציג אפשרויות חיפוש חיצוניות. המשתמש יכול לפתוח את החיפוש ולבחור מקור מתאים. */ 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 title = pending.item.original; const heading = document.createElement("div"); heading.innerHTML = ` ${escapeHtml(title)} `; content.appendChild( heading ); for ( const option of pending.options ) { const div = document.createElement("div"); div.className = "option"; const button = option.html ? ` : `; div.innerHTML = `
${escapeHtml(option.title)}
${escapeHtml(option.type)}
${escapeHtml(option.description)}
${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; } /* הכנסת ה-HTML למקום שבו הייתה א3 */ item.html = option.html; item.type = "embed-selected"; /* הסרת ההמתנה */ pendingOptions = pendingOptions.filter( p => p.item.line !== lineIndex ); /* בניית התוצאה מחדש */ rebuildResult(); /* עדכון חלון האפשרויות */ showOptions(); } /* בניית מסמך HTML מלא */ function rebuildResult() { let body = ""; for ( const item of program ) { if (item.html !== null) { body += item.html + "\n"; } else { /* *א3* שעדיין ממתינה נשמרת במקום שלה. */ body += ` `; } } const fullHTML = HTML_HEAD + body + HTML_FOOT; document .getElementById("result") .textContent = fullHTML; showOptions(); } /* הצגת שגיאה */ function showError(message) { document .getElementById("result") .innerHTML = `
פקודה לא תקינה

${escapeHtml(message)}
`; } /* העתקת HTML */ 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); if ( parsed.hostname.includes( "youtu.be" ) ) { return parsed.pathname .replace("/", ""); } return parsed.searchParams .get("v"); } 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); }