o
    40h.                     @   s   d dl mZ d dlmZ d dlmZ ddlmZmZ e Z	dd Z
dd	 Zd
d Zd dlmZ d dlZdd Zd dlmZ deedddfddZdS )    )
Translator)slugify)BeautifulSoup   )GENERAL_META_DESCRIPTIONGENERAL_META_KEYWORDSc                 C   s,   z
t | d}|jW S  ty   |  Y S w )NEnglish)
translator	translateresult	Exception)texttranslation r   ;/var/www/html/mimamsha/mimansha/apps/mimansha_main/utils.pytranslate_to_english   s   r   c                 C   s"   t | d}dd |dD }|S )z
    Extracts all superscript text enclosed in <sup> tags from an HTML string.
    
    Args:
        html_text (str): The HTML content to parse
        
    Returns:
        list: A list of all superscript texts found in order of appearance
    html.parserc                 S   s   g | ]}|  qS r   )get_text).0supr   r   r   
<listcomp>   s    z(extract_superscripts.<locals>.<listcomp>r   )r   find_all)	html_textsoupsuperscriptsr   r   r   extract_superscripts   s   

r   c                 C   s   ddl }ddlm} |jdd| |jd} |jdd| |jd} dD ]}|jd	| d
d| |jd} |jd| dd| |jd} q |jdd| |jd} |dd| } || } |dd| } |dd| } ddd |  D } |  } | S )a   
    Convert HTML content to plain text by removing HTML tags and decoding HTML entities.
    
    Args:
        html_content (str): HTML content to convert
        
    Returns:
        str: Plain text without HTML tags and with decoded HTML entities
    r   N)unescapez<!DOCTYPE.*?> flagsz
<!--.*?-->)divph1h2h3h4h5h6librtrz<\s*z[^>]*>
z<\s*/\s*z\s*>z<\s*br\s*/?>z<[^>]*>z + z\n{3,}z

c                 s   s    | ]}|  V  qd S )N)strip)r   liner   r   r   	<genexpr>F   s    z%html_to_plain_text.<locals>.<genexpr>)	rehtmlr   subDOTALL
IGNORECASEjoin
splitlinesr-   )html_contentr0   r   tagr   r   r   html_to_plain_text!   s   
r9   Nc                 C   s  t d| t j}|s| ddS |d }i }t d|t jt jB D ]}|d}|d }||t|< q#|s^d}t ||t jt jB }|D ]}|d}|d }||t|< qI|s|d}	|	D ]}
t 	d	|
}|r|d}|d }||t|< qgt j
dd| t jd
}t|d}|jddD ]'}|jr|jjdv rqt 
ddd t|}|t|krt|d}|| qt|}d}t| D ]\}}|d| d| d| d| d	7 }q|d7 }||dS )ad  
    Processes HTML content with numbered references in [1] format and creates a footnotes section.
    Formats reference numbers as superscript with specific styling.
    
    Args:
        html_content (str): HTML string containing references like [1] and footnotes in the format:
            [footnotes]
            [1] Footnote text...
            [2] Another footnote...
            [/footnotes]
        
    Returns:
        dict: {
            'processed_content': HTML with linked superscript footnotes,
            'footnotes_template': Formatted footnotes section with back-to-text links
        }
    z!\[footnotes\](.*?)\[\/footnotes\]r   )processed_contentfootnotes_templater   z)\[(\d+)\]\s*(.*?)(?=\n\[\d+\]|\Z|\n\s*\n)   z\[(\d+)\](.*?)(?=\n\[\d+\]|\Z)r+   z\[(\d+)\](.*)r   r   T)string)scriptstylez	\[(\d+)\]c                 S   s*   d|  d d|  d d|  d dS )Nz<sup><a href="#footnote-r   z
" id="ref-zS" class="footnote-ref text-red-950 font-semibold hover:underline superscript mx-1">z
</a></sup>)group)mr   r   r   <lambda>   s   * z#process_footnotes.<locals>.<lambda>z
    <div class="mt-16 border-t border-gray-200 pt-8" id="footnotes-section">
        <h3 class="text-2xl font-bold mb-6 text-gray-900">References</h3>
        <div class="space-y-4">
    z
            <div id="footnote-z" class="footnote-item py-3 border-b border-gray-100 last:border-0">
                <div class="flex items-start">
                    <span class="text-red-950 font-semibold min-w-8 mr-2">[zi]</span>
                    <div class="prose max-w-none flex-1 text-gray-700">
                        z
                    </div>
                </div>
                <div class="mt-2 text-right">
                    <a href="#ref-a  " class="inline-flex items-center text-sm text-red-800 hover:text-red-600 transition-colors">
                        <svg class="w-3 h-3 mr-1" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 10l7-7m0 0l7 7m-7-7v18"/>
                        </svg>

                    </a>
                </div>
            </div>
        a  
        </div>
    </div>
    
    <style>
    .footnote-ref {
        transition: all 0.2s ease;
    }
    
    .footnote-ref:hover {
        text-decoration-thickness: 1px;
    }
    
    .footnote-item {
        transition: background-color 0.3s ease;
    }
    
    .footnote-item.highlight {
        background-color: rgba(185, 28, 28, 0.05);
    }
      .superscript {
            font-size: 1rem;
            vertical-align: super;
            position: relative;
            color: #7f1d1d; /* red-950 equivalent */
            font-weight: 600; /* semibold */
            text-decoration: none;
            cursor: pointer;
            line-height: 0;
        }
        
        .superscript:hover {
            text-decoration: underline;
        }
    </style>
    
    <script>
    document.addEventListener('DOMContentLoaded', function() {
        // Smooth scroll to footnotes with offset
        document.querySelectorAll('a[href^="#footnote-"]').forEach(anchor => {
            anchor.addEventListener('click', function(e) {
                e.preventDefault();
                const targetId = this.getAttribute('href');
                const targetElement = document.querySelector(targetId);
                
                if (targetElement) {
                    // Scroll to the footnote with offset
                    const yOffset = -20;
                    const y = targetElement.getBoundingClientRect().top + window.pageYOffset + yOffset;
                    
                    window.scrollTo({
                        top: y,
                        behavior: 'smooth'
                    });
                    
                    // Highlight the footnote briefly
                    targetElement.classList.add('highlight');
                    setTimeout(() => {
                        targetElement.classList.remove('highlight');
                    }, 2000);
                }
            });
        });
        
        // Back to text links with 150px offset
        document.querySelectorAll('.footnote-item a[href^="#ref-"]').forEach(anchor => {
            anchor.addEventListener('click', function(e) {
                e.preventDefault();
                const href = this.getAttribute('href');
                const refElement = document.querySelector(href);
                
                if (refElement) {
                    const yOffset = -150; // Scroll 150px above the reference
                    const y = refElement.getBoundingClientRect().top + window.pageYOffset + yOffset;
                    
                    window.scrollTo({
                        top: y,
                        behavior: 'smooth'
                    });
                    
                    // Pulse effect on the reference
                    refElement.classList.add('pulse');
                    setTimeout(() => {
                        refElement.classList.remove('pulse');
                    }, 1000);
                }
            });
        });
        
        // Add pulse animation style
        const style = document.createElement('style');
        style.textContent = `
            @keyframes pulse {
                0% { transform: scale(1); opacity: 1; }
                50% { transform: scale(1.2); opacity: 0.8; }
                100% { transform: scale(1); opacity: 1; }
            }
            .pulse {
                animation: pulse 0.5s ease;
                display: inline-block;
            }
        `;
        document.head.appendChild(style);
    });
    </script>
    )r0   searchr3   r@   r-   finditer	MULTILINEintsplitmatchr2   r   r   parentnamestrreplace_withsorteditems)r7   footnotes_blockfootnotes_text	footnotesrH   numr   patternmatcheslinesr.   processed_htmlr   	text_nodenew_textnew_soupr:   r;   r   r   r   process_footnotesP   sr   






	lrZ   )settingsMIMAMSHAz/static/icons/MIMAMSHA.pngzhttps://www.mimamsha.comc                 K   sn   i d| d|d|ddddd|d	| d
|d|ddddd| d|d|dddddd|S )N
meta_titlemeta_descriptionmeta_keywordsauthorr\   og_typewebsitecanonical_urlog_titleog_descriptionog_image	site_nametwitter_cardsummary_large_imagetwitter_titletwitter_descriptiontwitter_imagetwitter_sitez	@mimamshatwitter_creatorrobotszindex, followr   )r]   r^   r_   rf   rl   rc   kwargsr   r   r   get_meta_contents-  sJ   
	
rq   )translatepyr   django.utils.textr   bs4r   	stopwordsr   r   r	   r   r   r9   r0   rZ   django.confr[   rq   r   r   r   r   <module>   s(    	, \