מהדר שפת א

קוד שפת א

תוצאת HTML

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

תצוגה מקדימה

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

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

*א1* — הצג תוכן
*א2* — חפש תוכן
`; /* ================================================== קומפילציה ================================================== */ 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 ); } } /* ================================================== פירוק תוכנית ================================================== */ function parseProgram(source) { const lines = source.split(/\r?\n/); const result = []; let i = 0; while (i < lines.length) { const line = lines[i].trim(); /* ---------------------------------------------- שורה ריקה ---------------------------------------------- */ if (!line) { i++; continue; } /* ---------------------------------------------- פקודה ---------------------------------------------- */ let command = null; if (line.startsWith("*א1*")) { command = "א1"; } else if (line.startsWith("*א2*")) { command = "א2"; } else { throw new Error( "פקודה לא תקינה בשורה " + (i + 1) + ": " + line ); } /* ---------------------------------------------- תוכן באותה שורה ---------------------------------------------- */ let value = line.substring(4).trim(); i++; if (value) { result.push({ command: command, value: value, html: null }); continue; } /* ---------------------------------------------- תוכן בשורות הבאות ---------------------------------------------- */ const contentLines = []; while (i < lines.length) { const nextLine = lines[i]; const trimmed = nextLine.trim(); if ( trimmed.startsWith("*א1*") || trimmed.startsWith("*א2*") ) { break; } contentLines.push( nextLine ); i++; } /* ---------------------------------------------- הסרת רווחים ריקים ---------------------------------------------- */ while ( contentLines.length > 0 && contentLines[0].trim() === "" ) { contentLines.shift(); } while ( contentLines.length > 0 && contentLines[ contentLines.length - 1 ].trim() === "" ) { contentLines.pop(); } value = contentLines.join("\n").trim(); if (!value) { throw new Error( "חסר תוכן אחרי *" + command + "*" ); } result.push({ command: command, value: value, html: null }); } if (result.length === 0) { throw new Error( "לא נמצאו פקודות בשפת א" ); } return result; } /* ================================================== תרגום ================================================== */ function translateProgram() { for (const item of program) { if (item.command === "א1") { item.html = displayValue(item.value); } else if (item.command === "א2") { item.html = searchAndEmbed(item.value); } } } /* ================================================== א1 ================================================== */ function displayValue(value) { const trimmed = value.trim(); 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 `
`; } } /* ---------------------------------------------- תמונה ---------------------------------------------- */ if ( /\.(jpg|jpeg|png|gif|webp|svg)(\?.*)?$/i.test(url) ) { return `
`; } /* ---------------------------------------------- וידאו ---------------------------------------------- */ if ( /\.(mp4|webm|ogg|mov)(\?.*)?$/i.test(url) ) { return `
`; } /* ---------------------------------------------- אודיו ---------------------------------------------- */ if ( /\.(mp3|wav|m4a|aac|flac)(\?.*)?$/i.test(url) ) { return `
`; } /* ---------------------------------------------- URL רגיל ---------------------------------------------- */ return `
${escapeHtml(url)}
`; } /* ================================================== א2 ================================================== */ function searchAndEmbed(query) { const encoded = encodeURIComponent(query); return `

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

חיפוש YouTube

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

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

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

פתח חיפוש
חיפוש Bing

פתיחת תוצאות Bing

פתח חיפוש
`; } /* ================================================== בניית התוצאה ================================================== */ function rebuildResult() { let body = ""; let preview = ""; for (const item of program) { body += item.html + "\n\n"; preview += item.html + "\n"; } const fullHTML = HTML_HEAD + body + HTML_FOOT; /* תוצאת HTML חשוב: textContent ולא innerHTML כדי שה-HTML יוצג כטקסט. */ const resultElement = document.getElementById( "result" ); resultElement.textContent = fullHTML; /* תצוגה מקדימה */ const previewElement = document.getElementById( "preview" ); previewElement.innerHTML = preview; } /* ================================================== שגיאה ================================================== */ function showError(message) { const result = document.getElementById( "result" ); result.innerHTML = ""; const errorBox = document.createElement( "div" ); errorBox.className = "error"; errorBox.textContent = "שגיאה במהדר\n\n" + message; result.appendChild( errorBox ); document .getElementById("preview") .innerHTML = `
${escapeHtml(message)}
`; } /* ================================================== העתקה ================================================== */ function copyResult() { const result = document.getElementById( "result" ); const text = result.textContent; if (!text.trim()) { return; } if ( navigator.clipboard && navigator.clipboard.writeText ) { navigator.clipboard .writeText(text) .then(function () { showCopyMessage(); }) .catch(function () { 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" ); showCopyMessage(); } catch (error) { alert( "ההעתקה נכשלה" ); } textarea.remove(); } /* ================================================== הודעת העתקה ================================================== */ function showCopyMessage() { const button = document.querySelector( ".secondary" ); const originalText = button.textContent; button.textContent = "הועתק!"; button.style.background = "#16803c"; setTimeout(function () { button.textContent = originalText; button.style.background = ""; }, 1500); } /* ================================================== YouTube ID ================================================== */ function getYouTubeId(url) { try { const parsed = new URL(url); /* youtu.be */ if ( parsed.hostname .toLowerCase() .includes("youtu.be") ) { return parsed.pathname .replace(/^\/+/, "") .split("/")[0]; } /* youtube.com */ if ( parsed.hostname .toLowerCase() .includes("youtube.com") ) { /* סרטון רגיל */ const v = parsed.searchParams.get( "v" ); if (v) { return v; } /* Shorts */ const shorts = parsed.pathname.match( /\/shorts\/([^/?]+)/ ); if (shorts) { return shorts[1]; } /* Embed */ const embed = parsed.pathname.match( /\/embed\/([^/?]+)/ ); if (embed) { return embed[1]; } } } catch (error) { 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); }