
    h-                        d dl Z d dlmZ d dlZd dlZd dlmZ d dlmZ d dl	 d dl
mZ  e         e e j                  d            Z e j                  d	d
      Z e j                  dd      ZdZdZd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z d Z!d Z"d  Z#d! Z$d" Z%d# Z&d$ Z'd%e(d&e(fd'Z)d(e(d)e(d&e(fd*Z*y)+    N)OpenAI)HTML)HtmlFormatter)*)load_dotenvOPENAI_API_KEY)api_keyOPENAI_MODELzgpt-4o-miniOUTPUT_FOLDERz./result
imscc_tempwiki_contentc                     t        d      }|j                  |       }t        |      }|j                  d      D cg c]$  }|j	                         s|j	                  d      & c}S c c}w )N	subtopicstopic
z- )
get_promptformat
clientCallsplitstrip)r   prompt_datapromptresponselines        ;/var/www/html/content_weaver/dependencies/content_weaver.pygenerate_subtopicsr      sW    [)Ke,F&!H)1)=NDJJtNNNs   A(A(c                 R    t        d      }|j                  |       }t        |      S )Ncourse_objectivesr   r   r   r   r   r   r   s      r   generate_course_objectivesr"      s+    01Ke,Ff    c                 R    t        d      }|j                  |       }t        |      S )Nlearning_outcomesr   r    r!   s      r   generate_course_outcomesr&      s+    01Ke,Ffr#   c                 R    t        d      }|j                  |       }t        |      S )Ncourse_outliner   r    r!   s      r   generate_course_outliner)   %   s+    -.Ke,Ffr#   c                 R    t        d      }|j                  |       }t        |      S )Noutliner   r    r!   s      r   establish_course_outliner,   +   *    Y'Ke,Ffr#   c                     d}|D ]F  }t        d      }|j                  | |      }t        |      }|d| d| dz  }|t        | |      z  }H |S )N high_level_outliner   modulez### r   

)r   r   r   generate_module_questions)r   modulesr+   r2   r   r   r   s          r    generate_detailed_course_outliner6   1   sr    G < !56##%#?f%T&H:T22,UF;;< Nr#   c                 p    i }|D ].  }t        d      }|j                  | |      }t        |      ||<   0 |S )Ncourse_concepts)r   subr    )r   r   explanationsr9   r   r   s         r   explain_conceptsr;   =   sM    L / !23##%S#9&v.S/
 r#   c                 X    t        d      }|j                  | |      }t        |      }|S )z9Generate multiple-choice questions for a specific module.module_questionsr1   r    )r   r2   r   r   r   s        r   r4   r4   F   s3    
 /0KeF;F&!HOr#   c                 n    i }| D ]-  }t        d      }|j                  |      }t        |      ||<   / |S )Ncourse_exercises)r9   r    )r   	questionsr9   r   r   s        r   generate_questionsrA   P   sJ    I , !34###,#F+	#,
 r#   c                 R    t        d      }|j                  |       }t        |      S )Nsummaryr   r    r!   s      r   generate_summaryrD   Y   r-   r#   c                 r    t        d      }|j                  | dj                  |            }t        |      S )Nchaining_summary, r   r   r   r   joinr   r   r   r   r   s       r   generate_chaining_summaryrL   ^   s6    /0Ketyy7KLFfr#   c                 r    t        d      }|j                  | dj                  |            }t        |      S )Nchaining_course_objectivesrG   rH   rI   rK   s       r   #generate_chaining_course_objectivesrO   c   s6    9:Ketyy7KLFfr#   c                 r    t        d      }|j                  | dj                  |            }t        |      S )Nchaining_course_outcomesrG   rH   rI   rK   s       r   !generate_chaining_course_outcomesrR   h   s6    78Ketyy7KLFfr#   c                 x    d}t        d      }|j                  | dj                  |      |      }t        |      S )Nz5-10chaining_course_outlinerG   )r   r   no_of_modulesrI   )r   r   rU   r   r   s        r    generate_chaining_course_outlinerV   m   s>    M67Ketyy7K[hiFfr#   c                     t         j                  j                  j                  t        d| dg      }|j
                  d   j                  j                  j                         S )Nuser)rolecontent)modelmessagesr   )	clientchatcompletionscreater
   choicesmessagerZ   r   )r   r   s     r   r   r   s   sW    {{&&--!f56 . H A&&..4466r#   c                 
   | j                         j                  d      }g }|D ]\  }d|v s|j                  dd      }t        |      dkD  s)|j                  |d   j                  d      d   j                                ^ |S )Nr   z.    :r   )r   r   lenappend)r(   linestitlesr   partss        r   extract_module_titlesrk   z   s      "((.EF >4<JJtQ'E5zA~eAhnnS1!4::<=	>
 Mr#   c                 :   t        |       }t        d       t        | |      }t        d       t        |      }t        d       d|  d}t	        | |      }|d| dz  }t        d       t        | |      }|d| dz  }t        d	       t        | |      }|d
| dz  }t        d       t        | |      }|d| dz  }t        d       t        |      }	t        | |	      }
|d|
 z  }t        d       |D ]"  }|d| dz  }|||    dz  }|d||    dz  }$ |S )Nz## Course subtopics generatedz ## Course explanations generatedz## Course questions generatedz
# Course: r3   z## Summary
z## Course Summary generatedz## Course Objectives
z## Course Objectives generatedz## Course Outcomes
z## Course Outcomes generatedz## Course Outline
z## Course Outline generatedz## Module Details
z"## Course Module Details generatedz## z### Quiz Questions
)
r   printr;   rA   rL   rO   rR   rV   rk   r6   )r   r   r:   r@   mdrC   
objectivesoutcomesr+   module_titlesdetailed_modulesr9   s               r   build_markdownrs      s   "5)I	)+#E95L	,."9-I	)+eWD	!B (y9G L	
&&B	') 5UIFJ":,d
33B	*, 1	BH 
$
//B	(* /ui@Gy
--B	'))'2M7}M 01
22B	.0 :
C5o
c"#4((
$Ys^$4D99:
 Ir#   c                    g d}t        j                  | |      }t               j                  d      }|dz  }d| d| }t        j
                  j                  t              st	        j                  t        d       t        j
                  j                  t        |      }| d	}t        |d
d      5 }|j                  |       d d d        | d}t        |      j                  |       t        d| d|        y # 1 sw Y   ;xY w)Nzfenced-code-blockszcode-friendlytablesextras.codehilite
    body, h1, h2, h3, h4, h5, h6, pre, code {
        word-wrap: break-word;
        overflow-wrap: break-word;
        white-space: normal;
    }
    pre, code {
        white-space: pre-wrap;
    }
    <style>	</style>
Texist_ok.htmlwutf-8encodingz.pdf)stringu   ✅ Exported to z and )	markdown2markdownr   get_style_defsospathexistsr   makedirsrJ   openwriter   	write_pdfrm   )	markdown_contentoutput_baserx   html_contentstyle	full_html	html_filefpdf_files	            r   export_to_html_and_pdfr      s    >F%%&6vFL O**=9E	 	 	E %
<.9I77>>-(
MD1'',,}k:K -u%I	iw	/ 1		 d#H	$$X.	YKuXJ
78 s   7DDc                    t        |       }| j                  dd      }t        j                  j	                  t
              st        j                  t
        d       t        j                  j                  t
        |      }t        | ddd      5 }|j                  |       d d d        t        ||       |S # 1 sw Y   xY w)	N _Tr}   .mdr   r   r   )rs   replacer   r   r   r   r   rJ   r   r   r   )r   r   base_filenamer   r   s        r   generate_course_materialsr      s    %e,MM#s+M77>>-(
MD1'',,}m<K	S!3	9 "Q	 !"+];" "s   B99Cc                    | j                   }t        |      }|| _        t        || j                        | _        t        || j                        | _        t        || j                        | _	        t        || j                        | _        |S )N)r   rs   content_descriptionrR   r   content_outcomesrO   content_objectivesrL   content_summaryrV   content_outline)course_datar   r   s      r   !generate_course_materials_from_dbr      s{    E%e,&6K##DUKLaLa#bK %HP[PePe%fK"";E;CXCX"YK"B5+J_J_"`Kr#   c           	      D   t        | j                  dd      j                  dd            }t        j                  j	                  t
        |      }t        j                  j	                  |t              }t        j                  t
        d       t        j                  |d       t        j                  j	                  t        | d      }t        j                  j	                  t        | d      }t        |       t        j                  j                  |      st        dd	
      t        | |       t        ||t        t        j                  j	                  |d             t        j                  |dt        j                         5 }t        j"                  t
              D ]`  \  }}}	|	D ]U  }
t        j                  j	                  ||
      }t        j                  j%                  ||      }|j'                  ||       W b 	 ddd       |S # 1 sw Y   |S xY w)z=
    Generate an IMSCC package from the article content.
    r   r   /Tr}   z.imsccr     z:Markdown file not found. Please generate the course first.status_codedetailzimsmanifest.xml)course_titlecontent_dirwiki_diroutput_filer   )startN)truncate_filenamer   r   r   rJ   working_dirr   r   r   rm   r   HTTPExceptionsave_sections_as_htmlcreate_manifestzipfileZipFileZIP_DEFLATEDwalkrelpathr   )r   article_titlearticle_dirwiki_content_diroutput_pathmarkdown_pathzfrootr   filesfile	full_patharcnames                r   generate_imsccr      s    &MM#s#++C5
M '',,{M:Kww||K=KKd+KK 40'',,}v.FGKGGLL=/0EFM	-77>>-(4pqq% 01 "$GGLL.?@	 
c7+?+?	@ -B ggk2 	-ND!U -GGLLt4	''//);/GG,-	-- - s   A=HHc                    t        |       }t        |      }|D ]  }|j                         }|s|d   j                  d      j                         }|j	                         j                  dd       d}t        ||      }t        j                  j                  ||      }	t        |	dd      5 }
|
j                  |       d	d	d	        t        d
t        |       d|        y	# 1 sw Y   xY w)za
    Reads a markdown file, splits it into sections, and saves each section as an HTML file.
    r   z# r   r   r   r   r   r   Nu   ✅ Exported z sections to: )get_course_contentsplit_markdown_sections
splitlinesr   lowerr   markdown_to_htmlr   r   rJ   r   r   rm   rf   )r   r   rZ   sectionssectionrh   
title_linefilenamehtmlr   outs              r   r   r     s    
 !'G&w/H ""$1X^^D)//1
 &&(00c:;5A
G4ggll#3X>+sW5 	IIdO	 	 
M#h-7G6H
IJ	 	s   .C%%C.	r   returnc                     | j                  dd      }t        j                  j                  t        | d      }t        j                  j                  |      st        dd      t        |      }|S )Nr   r   r   r   zCourse markdown file not found.r   )r   r   r   rJ   r   r   r   read_txt_file)r   r   course_pathrZ   s       r   r   r   /  sZ    MM#s+M'',,}s.CDK77>>+&4UVVK(GNr#   titlerZ   c                     g d}t        j                  ||      }t               j                  d      }|dz  }d| d| }|S )Nru   rw   ry   rz   r{   r|   )r   r   r   r   )r   rZ   rx   r   r   r   s         r   r   r   7  sV    >F%%gf=L O**=9E	 	 	E %
<.9Ir#   )+r   openair   r   r   
weasyprintr   pygments.formattersr   dependencies.helperdotenvr   getenvr]   r
   r   r   r   r   r"   r&   r)   r,   r6   r;   r4   rA   rD   rL   rO   rR   rV   r   rk   rs   r   r   r   r   r   strr   r    r#   r   <module>r      s   	     - !  				"23	4ryy7		/:6O	



7*X!9F	*XK0c c C # # r#   