<?php

/**
 * Simple Markdown Parser
 * Supports basic markdown syntax:
 * # Headers
 * **bold** *italic* `code`
 * - lists
 * [links](url)
 * > quotes
 */

class SimpleMarkdown
{
    /**
     * Parse markdown text to HTML
     */
    public static function parse($text)
    {
        // Convert special characters to HTML entities first
        $text = htmlspecialchars($text, ENT_QUOTES, 'UTF-8');
        
        // Headers (h1-h6)
        $text = preg_replace('/^#{1,6}\s+(.+)$/m', '<h3>$1</h3>', $text);
        
        // Bold text
        $text = preg_replace('/\*\*(.+?)\*\*/', '<strong>$1</strong>', $text);
        
        // Italic text
        $text = preg_replace('/\*(.+?)\*/', '<em>$1</em>', $text);
        
        // Inline code
        $text = preg_replace('/`(.+?)`/', '<code>$1</code>', $text);
        
        // Code blocks
        $text = preg_replace('/```(.+?)```/s', '<pre><code>$1</code></pre>', $text);
        
        // Links
        $text = preg_replace('/\[(.+?)\]\((.+?)\)/', '<a href="$2" target="_blank">$1</a>', $text);
        
        // Blockquotes
        $text = preg_replace('/^>\s+(.+)$/m', '<blockquote>$1</blockquote>', $text);
        
        // Unordered lists
        $text = preg_replace('/^\-\s+(.+)$/m', '<li>$1</li>', $text);
        $text = preg_replace('/(<li>.*<\/li>)/s', '<ul>$1</ul>', $text);
        
        // Line breaks
        $text = nl2br($text);
        
        // Clean up multiple consecutive line breaks
        $text = preg_replace('/(<br\s*\/?>\s*){3,}/', '<br><br>', $text);
        
        return $text;
    }
    
    /**
     * Convert HTML back to markdown (simplified)
     * This is a basic implementation - may not handle all cases perfectly
     */
    public static function toMarkdown($html)
    {
        // Basic HTML to markdown conversion
        $markdown = $html;
        
        // Headers
        $markdown = preg_replace('/<h[1-6]>(.+?)<\/h[1-6]>/i', '# $1', $markdown);
        
        // Bold
        $markdown = preg_replace('/<strong>(.+?)<\/strong>/i', '**$1**', $markdown);
        
        // Italic
        $markdown = preg_replace('/<em>(.+?)<\/em>/i', '*$1*', $markdown);
        
        // Code
        $markdown = preg_replace('/<code>(.+?)<\/code>/i', '`$1`', $markdown);
        $markdown = preg_replace('/<pre><code>(.+?)<\/code><\/pre>/is', "```$1```", $markdown);
        
        // Links
        $markdown = preg_replace('/<a href="(.+?)"(?:\s+target="_blank")?>(.+?)<\/a>/i', '[$2]($1)', $markdown);
        
        // Blockquotes
        $markdown = preg_replace('/<blockquote>(.+?)<\/blockquote>/i', '> $1', $markdown);
        
        // Lists (simplified)
        $markdown = preg_replace('/<ul>(.+?)<\/ul>/is', '$1', $markdown);
        $markdown = preg_replace('/<li>(.+?)<\/li>/i', '- $1', $markdown);
        
        // Line breaks
        $markdown = preg_replace('/<br\s*\/?>/i', "\n", $markdown);
        
        // Decode HTML entities
        $markdown = htmlspecialchars_decode($markdown, ENT_QUOTES);
        
        // Clean up
        $markdown = preg_replace('/\n{3,}/', "\n\n", $markdown);
        $markdown = trim($markdown);
        
        return $markdown;
    }
}
?>