%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% pyjupyter.sty
% Python code environment styled for Jupyter-like listings
%
% Author : BARA O. Bright
% Date   : 2026/03/06
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{pyjupyter}[2026/03/06 Python Jupyter-style listings]


\PassOptionsToPackage{dvipsnames,svgnames,table}{xcolor}
\RequirePackage{xcolor}

\RequirePackage{listings}
\RequirePackage{tcolorbox}
\tcbuselibrary{listings,skins,breakable}

\IfFileExists{sourcecodepro.sty}{
    \RequirePackage{sourcecodepro}
}{
    \PackageWarning{pyjupyter}{Package 'sourcecodepro' not found. Using default monospaced font}
}

\lstset{
    inputencoding=utf8,
    extendedchars=true,
    literate=
    {é}{{\'e}}1 {è}{{\`e}}1 {ê}{{\^e}}1 {ë}{{\"e}}1
    {à}{{\`a}}1 {â}{{\^a}}1 {ä}{{\"a}}1 {ã}{{\~a}}1
    {ï}{{\"i}}1 {î}{{\^i}}1 {ì}{{\`i}}1 {í}{{\'i}}1
    {ö}{{\"o}}1 {ô}{{\^o}}1 {ò}{{\`o}}1 {ó}{{\'o}}1
    {ü}{{\"u}}1 {ù}{{\`u}}1 {û}{{\^u}}1
    {ç}{{\c c}}1 {Ç}{{\c C}}1
    {É}{{\'E}}1 {È}{{\`E}}1 {Ë}{{\"E}}1
}

\lstdefinestyle{python-jupyter}{
    language=Python,
    basicstyle=\ttfamily\small,
   alsoletter={+, -, *, /, =, <, >, !, \&, |,  **, \^, ~, 
<<, >>, +=, -=,*=, /=, //=, \%=, **=, \&=, |=, \^=, <<=, >>=},
    emph={+, -, *, /, =, <, >, !, \&, |,  **, \^, ~, 
<<, >>, +=, -=,*=, /=, //=, \%=, **=, \&=, |=, \^=, <<=, >>=}, 
    emphstyle=\color{violet}, 
    keywordstyle=\color{DarkGreen}\bfseries,
    keywords={class,def,return,if,else,for,while,in,raise,
        import,from,as,try,except,with,and,or,not,is,and,
        True,False,None,assert,break,continue,del,elif,
         finally,from,global,import,lambda,
         nonlocal,pass,yield},
    keywordstyle=[2]\color{DarkGreen},
    keywords=[2]{print,super,len,range,enumerate,zip,
        list,dict,set,int,str,float,bool}, 
    keywordstyle=[3]\color{violet},
    keywords=[3]{+, -, *, /, =, <, >, !, \&, |, :, **, \^, ~, 
    <<, >>, +=, -=,*=, /=, //=, \%=, **=, \&=, |=, \^=, <<=, >>=},
    commentstyle=\color{teal!80!black}\itshape,
    stringstyle=\color{red!70!black},    
    showstringspaces=false,
    escapeinside={@}{@},
    breaklines=true,
    tabsize=2,
    columns=flexible,
    keepspaces=true,
    upquote=true,
    morecomment=[s][\color{red!70!black}\itshape]{'''}{'''},
    morecomment=[s][\color{red!70!black}\itshape]{"""}{"""}
}

\tcbset{
    numbered/.style={
        left=10mm, 
        listing options={
            style=python-jupyter,
            numbers=left,
            numberstyle=\small\ttfamily\color{black!60},
            numbersep=8pt
        }
    }
}

\newtcblisting{jupyter}[1][]{
    enhanced,
    breakable,
    listing only,
    listing options={style=python-jupyter},    
    colback=gray!5,
    colframe=gray!30,
    arc=3pt,
    boxrule=1pt,
    boxsep=1mm,  
    #1
}

\endinput
