מהדר שפת א

קוד שפת א

תוצאה

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

*א1* — הצג
`; /* ================================================== תרגום התוכנית ================================================== */ function compileCode() { const result = document.getElementById("result"); result.textContent = ""; 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; } /* ----------------------------------------------- פקודה לא מוכרת ----------------------------------------------- */ throw new Error( "פקודה לא תקינה: " + command ); } /* ================================================== א1 — הצגת תוכן ================================================== */ function displayValue(value) { const lower = value.toLowerCase(); /* ----------------------------------------------- תמונה ----------------------------------------------- */ if ( isImage(value) ) { return ` `; } /* ----------------------------------------------- וידאו ----------------------------------------------- */ if ( isVideo(value) ) { return ` `; } /* ----------------------------------------------- אודיו ----------------------------------------------- */ if ( isAudio(value) ) { return ` `; } /* ----------------------------------------------- קישור ----------------------------------------------- */ if ( lower.startsWith("http://") || lower.startsWith("https://") ) { return ` ${escapeHtml(value)} `; } /* ----------------------------------------------- טקסט רגיל ----------------------------------------------- */ return `
${escapeHtml(value)}
`; } /* ================================================== בדיקת תמונה ================================================== */ function isImage(value) { const clean = value .split("?")[0] .split("#")[0] .toLowerCase(); return ( clean.endsWith(".jpg") || clean.endsWith(".jpeg") || clean.endsWith(".png") || clean.endsWith(".gif") || clean.endsWith(".webp") || clean.endsWith(".svg") || clean.endsWith(".bmp") || clean.endsWith(".avif") ); } /* ================================================== בדיקת וידאו ================================================== */ function isVideo(value) { const clean = value .split("?")[0] .split("#")[0] .toLowerCase(); return ( clean.endsWith(".mp4") || clean.endsWith(".webm") || clean.endsWith(".ogg") || clean.endsWith(".ogv") || clean.endsWith(".mov") || clean.endsWith(".m4v") ); } /* ================================================== בדיקת אודיו ================================================== */ function isAudio(value) { const clean = value .split("?")[0] .split("#")[0] .toLowerCase(); return ( clean.endsWith(".mp3") || clean.endsWith(".wav") || clean.endsWith(".m4a") || clean.endsWith(".aac") || clean.endsWith(".flac") || clean.endsWith(".ogg") ); } /* ================================================== בניית HTML מלא ================================================== */ function rebuildResult() { let body = ""; for (const item of program) { if (item.html !== null) { body += item.html + "\n"; } } const fullHTML = HTML_HEAD + body + HTML_FOOT; /* חשוב: textContent מציג את הקוד כקוד ולא מפעיל אותו בתוך חלון התוצאה. */ document .getElementById("result") .textContent = fullHTML; } /* ================================================== הצגת שגיאה ================================================== */ 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(); } /* ================================================== הגנה על HTML ================================================== */ function escapeHtml(text) { return String(text) .replace(/&/g, "&") .replace(//g, ">") .replace(/"/g, """) .replace(/'/g, "'"); } function escapeAttribute(text) { return escapeHtml(text); }