מהדר שפת א

קוד שפת א

תוצאת HTML

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

תצוגה מקדימה

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

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

*א1* — הצג תוכן
*א2* — חפש תוכן
'; /* ================================================ תרגום ================================================ */ function compileCode() { const source = document.getElementById('code').value; try { if (!source.trim()) { throw new Error('לא הוזן קוד'); } const program = parseProgram(source); const result = translateProgram(program); document.getElementById('result').textContent = result.fullHTML; document.getElementById('preview').innerHTML = result.preview; } 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.indexOf('*א1*') === 0) { command = 'א1'; } else if (line.indexOf('*א2*') === 0) { command = 'א2'; } else { throw new Error( 'פקודה לא תקינה בשורה ' + (i + 1) + ': ' + line ); } let value = line.substring(4).trim(); i++; /* תוכן באותה שורה */ if (value) { result.push({ command: command, value: value }); continue; } /* תוכן בשורות הבאות */ const contentLines = []; while (i < lines.length) { const next = lines[i]; const trimmed = next.trim(); if ( trimmed.indexOf('*א1*') === 0 || trimmed.indexOf('*א2*') === 0 ) { break; } contentLines.push(next); 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 }); } if (result.length === 0) { throw new Error( 'לא נמצאו פקודות בשפת א' ); } return result; } /* ================================================ תרגום התוכנית ================================================ */ function translateProgram(program) { let body = ''; let preview = ''; for (let i = 0; i < program.length; i++) { const item = program[i]; let html = ''; if (item.command === 'א1') { html = displayValue(item.value); } else if (item.command === 'א2') { html = searchValue(item.value); } body += html + '\n'; preview += html + '\n'; } return { fullHTML: HTML_HEAD + body + HTML_FOOT, preview: preview }; } /* ================================================ א1 — הצגת תוכן ================================================ */ function displayValue(value) { const text = value.trim(); if (/^https?:\/\//i.test(text)) { return displayURL(text); } return ( '
' + escapeHTML(text) + '
' ); } /* ================================================ URL ================================================ */ function displayURL(url) { const lower = url.toLowerCase(); /* YouTube */ if ( lower.indexOf('youtube.com') !== -1 || lower.indexOf('youtu.be') !== -1 ) { const id = getYouTubeID(url); if (id) { return ( '
' + '' + '
' ); } } /* Vimeo */ if ( lower.indexOf('vimeo.com/') !== -1 ) { 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 ( '
' + '' + '
' ); } /* קישור רגיל */ return ( '
' + '' + escapeHTML(url) + '' + '
' ); } /* ================================================ א2 — חיפוש ================================================ */ function searchValue(query) { const encoded = encodeURIComponent(query); return ( '
' + '

' + 'תוצאות חיפוש עבור: ' + escapeHTML(query) + '

' + '
' + 'חיפוש YouTube' + '

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

' + '' + 'פתח חיפוש' + '' + '
' + '
' + 'חיפוש Google' + '

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

' + '' + 'פתח חיפוש' + '' + '
' + '
' + 'חיפוש Bing' + '

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

' + '' + 'פתח חיפוש' + '' + '
' + '
' ); } /* ================================================ YouTube ID ================================================ */ function getYouTubeID(url) { try { const parsed = new URL(url); const hostname = parsed.hostname.toLowerCase(); if ( hostname.indexOf('youtu.be') !== -1 ) { return parsed.pathname .replace(/^\/+/, '') .split('/')[0]; } if ( hostname.indexOf('youtube.com') !== -1 ) { const video = parsed.searchParams.get('v'); if (video) { return video; } const shorts = parsed.pathname.match( /\/shorts\/([^/?]+)/ ); if (shorts) { return shorts[1]; } 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+)/i ); return match ? match[1] : null; } /* ================================================ Escape HTML ================================================ */ function escapeHTML(text) { return String(text) .replace(/&/g, '&') .replace(//g, '>') .replace(/"/g, '"') .replace(/'/g, '''); } /* ================================================ Escape Attribute ================================================ */ function escapeAttribute(text) { return escapeHTML(text); } /* ================================================ הצגת שגיאה ================================================ */ function showError(message) { const result = document.getElementById('result'); result.textContent = ''; const error = document.createElement('div'); error.className = 'error'; error.textContent = 'שגיאה במהדר\n\n' + message; result.appendChild( error ); document.getElementById( 'preview' ).innerHTML = '
' + escapeHTML(message) + '
'; } /* ================================================ העתקת HTML ================================================ */ function copyResult() { const text = document.getElementById( '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 area = document.createElement('textarea'); area.value = text; area.style.position = 'fixed'; area.style.left = '-9999px'; area.style.top = '0'; document.body.appendChild( area ); area.focus(); area.select(); try { document.execCommand( 'copy' ); showCopyMessage(); } catch (error) { alert( 'ההעתקה נכשלה' ); } area.remove(); } /* ================================================ הודעת העתקה ================================================ */ function showCopyMessage() { const button = document.querySelector( '.secondary' ); const oldText = button.textContent; button.textContent = 'הועתק!'; button.style.background = '#16803c'; setTimeout(function() { button.textContent = oldText; button.style.background = ''; }, 1500); }