מהדר שפת א

קוד שפת א

תוצאת 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); } } /* ================================================== פירוק התוכנית ================================================== א1 וא2 עובדים בדיוק באותו אופן. אפשר: *א1*שלום *א1* שלום *א1* שלום *א2*שלום *א2* שלום *א2* שלום וגם תוכן של כמה שורות. ================================================== */ function parseProgram(source) { const lines = source.split(/\r?\n/); const result = []; let i = 0; while (i < lines.length) { let line = lines[i].trim(); /* ---------------------------------------------- מתעלמים משורות ריקות בין פקודות ---------------------------------------------- */ if (!line) { i++; continue; } /* ============================================== בדיקת סוג הפקודה ============================================== */ let command = null; if (line.startsWith("*א1*")) { command = "א1"; } else if (line.startsWith("*א2*")) { command = "א2"; } /* ---------------------------------------------- אם זו פקודה ---------------------------------------------- */ if (command) { /* לוקחים את כל מה שאחרי הפקודה. */ let value = line.slice(4).trim(); i++; /* -------------------------------------------- אם יש תוכן באותה שורה -------------------------------------------- */ if (value) { result.push({ command: command, value: value, html: null, type: command === "א1" ? "display" : "search" }); continue; } /* -------------------------------------------- אם אין תוכן באותה שורה, לוקחים את השורות הבאות עד שמגיעה פקודה חדשה. -------------------------------------------- */ const contentLines = []; while (i < lines.length) { const nextLine = lines[i]; const trimmedNext = nextLine.trim(); /* אם הגיעה פקודה חדשה, עוצרים. */ if ( trimmedNext.startsWith("*א1*") || trimmedNext.startsWith("*א2*") ) { break; } /* מוסיפים את השורה. */ contentLines.push(nextLine); i++; } /* -------------------------------------------- ניקוי שורות ריקות בתחילת התוכן ובסופו -------------------------------------------- */ while ( contentLines.length && !contentLines[0].trim() ) { contentLines.shift(); } while ( contentLines.length && !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, type: command === "א1" ? "display" : "search" }); 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 === "א2") { 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)}
`; } /* ================================================== א2 חיפוש ================================================== */ 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") ) { /* סרטון רגיל */ 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 { 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); }