מהדר שפת א

קוד שפת א

תוצאת HTML

עדיין לא נוצר קוד.

תצוגה מקדימה

התוצאה תוצג כאן.

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

*א1* — הצג מקור או תוכן
*א3* — חפש והצג את כל התוצאות
`; /* ================================================== תרגום ================================================== */ function compileCode() { const source = document.getElementById("code").value; try { if (!source.trim()) { throw new Error( "לא הוזן קוד" ); } program = parseProgram(source); translateProgram(); rebuildResult(); } catch (error) { showError(error.message); } } /* ================================================== פירוק התוכנית ================================================== הפקודה א1 יכולה להיות: *א1*שלום או: *א1* שלום או: *א1* שלום ================================================== */ function parseProgram(source) { const lines = source .split(/\r?\n/) .map(line => line.trim()) .filter(line => line !== ""); const result = []; let i = 0; while (i < lines.length) { const line = lines[i]; /* ---------------------------------------------- א1 ---------------------------------------------- */ if (line.startsWith("*א1*")) { let value = line.substring(4).trim(); /* אם אין תוכן באותה שורה, נבדוק את השורה הבאה. */ if (!value && i + 1 < lines.length) { const next = lines[i + 1]; /* אם השורה הבאה אינה פקודה, היא התוכן של א1. */ if ( !next.startsWith("*א1*") && !next.startsWith("*א3*") ) { value = next.trim(); i++; } } if (!value) { throw new Error( "חסר תוכן אחרי *א1*" ); } result.push({ command: "א1", value: value, html: null, type: "display" }); i++; continue; } /* ---------------------------------------------- א3 ---------------------------------------------- */ if (line.startsWith("*א3*")) { const query = line.substring(4).trim(); if (!query) { throw new Error( "חסר טקסט חיפוש אחרי *א3*" ); } result.push({ command: "א3", value: query, html: null, type: "search" }); i++; continue; } /* ---------------------------------------------- טקסט ללא פקודה ---------------------------------------------- */ throw new Error( "פקודה לא תקינה: " + line ); } return result; } /* ================================================== תרגום התוכנית ================================================== */ function translateProgram() { for (const item of program) { if (item.command === "א1") { item.html = displayValue(item.value); } else if (item.command === "א3") { item.html = searchAndEmbed(item.value); } } } /* ================================================== א1 הצגה ================================================== */ function displayValue(value) { const trimmed = value.trim(); /* ---------------------------------------------- URL ---------------------------------------------- */ if ( /^https?:\/\//i.test(trimmed) ) { return displayUrl(trimmed); } /* ---------------------------------------------- טקסט ---------------------------------------------- */ return `
${escapeHtml(trimmed)}
`; } /* ================================================== הצגת URL ================================================== */ function displayUrl(url) { const lower = url.toLowerCase(); /* ---------------------------------------------- YouTube ---------------------------------------------- */ if ( lower.includes("youtube.com") || lower.includes("youtu.be") ) { const id = getYouTubeId(url); if (id) { return `
`; } } /* ---------------------------------------------- Vimeo ---------------------------------------------- */ if ( lower.includes("vimeo.com/") ) { const id = getVimeoId(url); if (id) { return `
`; } } /* ---------------------------------------------- ניסיון כתמונה ---------------------------------------------- חשוב: לא מסתמכים על סיומת. הדפדפן עצמו ינסה לטעון את הכתובת כתמונה. אם היא באמת תמונה היא תוצג. ---------------------------------------------- */ return `
${escapeHtml(url)}
`; } /* ================================================== א3 חיפוש והטמעת כל התוצאות ================================================== הערה: בדפדפן בלבד אי אפשר לבצע חיפוש כללי ב-Google/YouTube ולקבל את כל התוצאות באופן חופשי. לכן כאן אנחנו מייצרים חיפוש דרך מנועי החיפוש. למערכת אמיתית צריך API / שרת. ================================================== */ function searchAndEmbed(query) { const encoded = encodeURIComponent(query); /* במקום לטעון תוצאות חיפוש ישירות בתוך האתר, נציג מקורות חיפוש זמינים. */ return `

תוצאות חיפוש עבור: ${escapeHtml(query)}

חיפוש YouTube

פתיחת תוצאות YouTube

פתח חיפוש
חיפוש Google

פתיחת תוצאות Google

פתח חיפוש
`; } /* ================================================== בניית HTML ================================================== */ function rebuildResult() { let body = ""; let preview = ""; for (const item of program) { body += item.html + "\n"; preview += item.html + "\n"; } const fullHTML = HTML_HEAD + body + HTML_FOOT; /* ---------------------------------------------- חלון HTML ---------------------------------------------- */ document .getElementById("result") .textContent = fullHTML; /* ---------------------------------------------- חלון תצוגה מקדימה ---------------------------------------------- */ document .getElementById("preview") .innerHTML = preview; } /* ================================================== שגיאה ================================================== */ function showError(message) { document .getElementById("result") .innerHTML = `
פקודה לא תקינה

${escapeHtml(message)}
`; document .getElementById("preview") .innerHTML = `
${escapeHtml(message)}
`; } /* ================================================== העתקה ================================================== */ function copyResult() { const text = document .getElementById("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"; 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(/^\/+/, "") .split("/")[0]; } if ( parsed.hostname.includes("youtube.com") ) { /* סרטון רגיל: youtube.com/watch?v=... */ const v = parsed.searchParams.get("v"); if (v) { return v; } /* Shorts: youtube.com/shorts/... */ const shorts = parsed.pathname.match( /\/shorts\/([^/?]+)/ ); if (shorts) { return shorts[1]; } /* Embed: youtube.com/embed/... */ const embed = parsed.pathname.match( /\/embed\/([^/?]+)/ ); if (embed) { return embed[1]; } } } catch { return null; } return null; } /* ================================================== Vimeo ID ================================================== */ function getVimeoId(url) { const match = url.match( /vimeo\.com\/(?:video\/)?(\d+)/ ); return match ? match[1] : null; } /* ================================================== HTML Escape ================================================== */ function escapeHtml(text) { return String(text) .replace(/&/g, "&") .replace(//g, ">") .replace(/"/g, """) .replace(/'/g, "'"); } /* ================================================== Attribute Escape ================================================== */ function escapeAttribute(text) { return escapeHtml(text); }