o
    Vðh$'  ã                   @   s\  d dl Z d dlZd dlZd dlZd dlZd dlZd dlmZ d dlmZ d dl	m
Z
mZ d dlmZ e j e j e¡¡Zg d¢Zdedefd	d
„Zdedefdd„Zdededede
dedB f
dd„Zdedede
fdd„Zdedefdd„Zedkr¬dZejejeej d e j !ed¡Z"e j !e"d¡Z#e j !ed¡Z$e %de› d ¡ ee#e$ƒ dS dS )!é    N)Údefaultdict)Údatetime)ÚEnvironmentÚFileSystemLoader)Úannotation_data)zai 2027zperceptual origin of mathzallergy, hives, eoeÚ	ts_stringÚreturnc                 C   s`   | sdS z|   d¡r| dd… d } t | ¡}| d¡W S  ty/   t d| › ¡ |  Y S w )zNFormats an ISO 8601 timestamp string (with optional Z) into a readable format.Ú ÚZNéÿÿÿÿz+00:00z%b '%yzCould not parse timestamp: )Úendswithr   ÚfromisoformatÚstrftimeÚ
ValueErrorÚloggingÚwarning)r   Zdt_obj© r   ú8/Users/srajan/code/music-website/annotation_generator.pyÚformat_timestamp   s   

þr   Útextc                 C   sD   |   ¡ } t dd| ¡} t dd| ¡} t dd| ¡} |  d¡} | p!dS )z'Converts text into a URL-friendly slug.z\s+Ú-z[^a-z0-9\-]r	   z-+Zuntitled)ÚlowerÚreÚsubÚstrip)r   r   r   r   Úslugify'   s   
r   Ú
group_nameÚannotationsÚ
output_dirÚenvc                 C   sì  z|  d¡}W n ty" } zt d|› ¡ W Y d}~dS d}~ww t ¡ }ttƒ}|D ]T}t| 	dd¡ƒ|d< | 	d¡rL| 
|d ¡|d< | ¡  nd|d< | 	d	¡rl|d	  	d
¡rl| 
|d	 d
 ¡|d< | ¡  nd	|v rtd|d< | 	dd¡}	||	  |¡ q-|D ]}	||	 jdd„ d q„|j| |d}
dt| ƒ› d}tj ||¡}z-t|ddd}| |
¡ W d  ƒ n1 s¿w   Y  t d| › d|› ¡ || dœW S  tyõ } zt d|› d| › d|› ¡ W Y d}~dS d}~ww )zBRenders the HTML for a single annotation group using the template.z(templates/annotation_group_template.htmlz.Error loading annotation_group_template.html: NÚ	timestampr	   Ztimestamp_strÚ
annotationZannotation_htmlZ	selectionZselectedTextZselectedText_htmlÚurlzNo URL Providedc                 S   s   |   dd¡S )Nr    r	   )Úget©Úxr   r   r   Ú<lambda>W   s    z.render_annotation_group_html.<locals>.<lambda>©Úkey)r   Úgrouped_annotationsZannotation_z.htmlÚwúutf-8©Úencodingz'Successfully generated HTML for group 'z' -> )Úfilenamer   zError writing HTML to z for group 'z': )Úget_templateÚ	Exceptionr   ÚerrorÚmarkdownÚMarkdownr   Úlistr   r#   ÚconvertÚresetÚappendÚsortÚrenderr   ÚosÚpathÚjoinÚopenÚwriteÚdebug)r   r   r   r   ÚtemplateÚeÚ	md_parserZgrouped_by_urlÚannr"   Úhtml_contentr.   Úoutput_pathÚfr   r   r   Úrender_annotation_group_html0   sV   €þ


ýÿþ€þrG   Ú
pages_infoc              
   C   s  t  d¡ tj tdd¡}d}tj |¡raz.t|ddd}| ¡ }W d  ƒ n1 s,w   Y  t	 
¡ }| |¡}t  d	|› ¡ W n% ty` } zt  d
|› d|› ¡ W Y d}~nd}~ww t  |› d¡ g }	| D ])}
|
 dd¡}|
 d¡}|r|	 |d|› d|› ddœ¡ qmt  d|› d¡ qm|	jdd„ d zP| d¡}| d¡}g }|	D ]}| |j|d¡ q®|jd||d}tj |d¡}t|ddd}| |¡ W d  ƒ n1 sàw   Y  t  d |› ¡ W dS  ty } zt  d!|› ¡ W Y d}~dS d}~ww )"z:Generates the index.html file for the annotations section.z#Generating annotation index page...Úmdzannotation_index.mdz4<p>Annotation section description not available.</p>Úrr+   r,   Nz Successfully read and converted zError processing ú: z& not found. Using default description.r   zUntitled Groupr.   z/annotations/zAnnotations related to Ú.)Únamer"   Údescriptionz Skipping index entry for group 'z' due to missing filename.c                 S   s   | d S )NrM   r   r$   r   r   r   r&   ”   s    z0generate_annotation_index_html.<locals>.<lambda>r'   z!templates/list_page_template.htmlz*templates/list_templates/section_item.html)ÚsectionzWU - Annotations Index)Ú
page_titleÚsite_description_htmlÚlist_items_htmlz
index.htmlr*   z0Successfully generated annotation index.html -> z(Error generating annotation index.html: )r   Úinfor:   r;   r<   ÚCURRENT_DIRÚexistsr=   Úreadr2   r3   r5   r0   r1   r   r#   r7   r8   r/   r9   r>   )rH   r   r   Zindex_md_pathrQ   rF   Úindex_md_contentrB   rA   Úsections_dataÚpager   r.   r@   Úitem_templaterR   Úsection_inforD   rE   r   r   r   Úgenerate_annotation_index_htmlo   s`   

ÿ
"€ÿ


ý

ýÿ€ÿr\   Útemplates_dirc              
   C   s‚  t  dttƒ› d¡ t  d| › ¡ t  d|› ¡ tj| dd z
tt|ƒdd}W n tyJ } zt  	d|› d	|› ¡ W Y d
}~d
S d
}~ww t
tƒ}d}tD ]}| d¡}|rk|tv rk||  |¡ |d7 }qSt  d|› dt|ƒ› d¡ |sƒt  d¡ d
S t  d¡ g }| ¡ D ]\}	}
t|	|
| |d}|r¢| |¡ qŽt  	d|	› d¡ qŽ|rµt|| |ƒ nt  d¡ t  d¡ d
S )z0Generates the annotation section of the website.z(Starting annotation site generation for z target groups...zOutput directory set to: zTemplates directory set to: T)Úexist_ok)ÚloaderÚ
autoescapez+Failed to initialize Jinja2 environment at rK   Nr   Úgroupé   zFound z annotations belonging to z target groups.zFNo annotations found for the target groups. No HTML will be generated.z.--- Generating Annotation Group HTML Pages ---)r   r   r   r   z.Failed to generate HTML for annotation group 'ú'zYNo annotation group pages were generated successfully. Annotation index file not created.z,Annotation site generation process finished.)r   rS   ÚlenÚTARGET_GROUPSr:   Úmakedirsr   r   r0   r1   r   r4   r   r#   r7   r   ÚitemsrG   r\   )r   r]   Ú	jinja_envrA   r)   Zvalid_annotations_countr!   ra   Úgenerated_pages_infor   r   Ú	page_infor   r   r   Úgenerate_annotation_site°   sN   €þ
€

ü
rk   Ú__main__z(%(asctime)s - %(levelname)s: %(message)s)ÚlevelÚformatÚstreamÚoutputÚ	templateszRunning z as standalone script.)&r:   Úsysr   r   Újsonr2   Úcollectionsr   r   Újinja2r   r   Úimport_helpersr   r;   ÚdirnameÚabspathÚ__file__rT   re   Ústrr   r   r4   ÚdictrG   r\   rk   Ú__name__Ú
log_formatÚbasicConfigÚINFOÚstdoutr<   Zbase_output_dirZannotation_output_dirZscript_templates_dirrS   r   r   r   r   Ú<module>   s4    "	?A:õ