% proflycee-tools-piton.tex
% Copyright 2023  Cédric Pierquet
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3
% of this license or (at your option) any later version.
% The latest version of this license is in
%   http://www.latex-project.org/lppl.txt
% and version 1.3 or later is part of all distributions of LaTeX
% version 2005/12/01 or later.

%2.5.9	Ajout d'une clé pour la couleur des nombres
%2.5.8	Modification des marges
%2.5.7	Style alternatif

\RequirePackage{iftex}

%%=============PYTHONPITON

%==Clés globales
\defKV[envpiton]{%
	Largeur=\def\CODPITlargeur{#1},%
	Alignement=\def\CODPITalign{#1},%
	TaillePolice=\def\CODPITfonte{#1},%
	Style=\def\CODPITstyle{#1},%
	Titre=\def\CODPITtitre{#1},%
	EspacementV=\def\CODPITespV{#1},%
	CouleurNombres=\def\CODPITcolnb{#1}
}

\setKVdefault[envpiton]{%
	Largeur=\linewidth,%
	Alignement=center,%
	Lignes=true,%
	Gobble=true,%
	TaillePolice=\footnotesize,%
	Filigrane=false,%
	Style=Moderne,%
	Cadre=true,%
	BarreTitre=true,%
	Titre={{\scriptsize\faCode} Code Python},%
	EspacementV=\baselineskip,%
	CouleurNombres=orange
}

%==Style Moderne
\tcbset{stylepiton/.style={%
	enhanced,boxrule=1.25pt,%
	sharp corners=downhill,arc=12pt,
	before skip=\CODPITespV,after skip=\CODPITespV,%
	top=\baselineskip,bottom=0mm,left=0.6em,right=5mm,%
	attach boxed title to top right={yshift=-\tcboxedtitleheight},
	boxed title style={
		size=small,colback=ForestGreen!25,boxrule=1.25pt,
		colframe=Green,boxsep=1.25pt,
		sharp corners=downhill,
		arc=12pt,
		top=2pt,bottom=1pt,left=6pt,right=6pt
		},
	fonttitle=\color{ForestGreen}\itshape\ttfamily\footnotesize,
	title={{\scriptsize\faPython}\:Code Python}
	}
}

%==Style Classique
\tcbset{thonnystyle/.style={%
	enhanced,boxrule=0.75pt,colframe=DarkGray!50!Black,%
	sharp corners,top=0mm,bottom=0mm,left=0.4em,right=5mm,%
	before skip=\CODPITespV,after skip=\CODPITespV,%
	colback=white
	}
}

\tcbset{thonnystylelineos/.style={%
	thonnystyle,%
	underlay={%
		\begin{tcbclipinterior}
			\filldraw[lightgray!25] (interior.south west) rectangle ([xshift=1.3em]interior.north west) ;
		\end{tcbclipinterior}%
		}
	}
}

%==Création de la boîte
\ifluatex
\RequirePackage{piton}
\NewPitonEnvironment{CodePiton}{ O{} m }%
{%
	\tcbset{reset}
	\useKVdefault[envpiton]%
	\setKV[envpiton]{#1}% on paramètres les nouvelles clés et on les simplifie
	%------les styles piton
	\SetPitonStyle{ Number = \color{\CODPITcolnb} }%
	\ifboolKV[envpiton]{Lignes}%si lignes=true
		{\PitonOptions{left-margin=0.75em,all-line-numbers}}%left-margin=auto,
		{}%
	\ifboolKV[envpiton]{Gobble}%si gobble=true
		{\PitonOptions{tabs-auto-gobble}}%
		{}%
	\PitonOptions{break-lines,indent-broken-lines}%,numbers-sep=0.65em}
	%------les styles tcbox
	\IfEq{\CODPITstyle}{Moderne}%style Moderne (défaut)
		{%
			\tcbset{stylepiton,colframe=Green,colback=ForestGreen!5,width=\CODPITlargeur,fontupper=\CODPITfonte,fontlower=\CODPITfonte,\CODPITalign,leftupper=0.75em}
			\ifboolKV[envpiton]{Filigrane}%si filigrane
				{\tcbset{watermark text={\faPython},watermark opacity=0.175,watermark zoom=0.50}}%
				{}%
		}
		{}%
	\IfEq{\CODPITstyle}{Classique}%style Classique
		{%
			\ifboolKV[envpiton]{Lignes}%si lignes=true
				{\tcbset{thonnystylelineos,leftupper=0.75em}}%
				{\tcbset{thonnystyle,leftupper=0.4em}}%
			\tcbset{width=\CODPITlargeur,colframe=DarkGray!50!Black,fontupper=\CODPITfonte,fontlower=\CODPITfonte,\CODPITalign}
			\ifboolKV[envpiton]{Filigrane}%si filigrane
				{\tcbset{watermark text={\faPython},watermark opacity=0.25,watermark zoom=0.50}}%
				{}%
			\ifboolKV[envpiton]{BarreTitre}%
				{%
					\ifboolKV[envpiton]{Cadre}
						{}
						{\tcbset{boxrule=0pt,frame hidden}}
					\tcbset{lefttitle=0.4em,title={\CODPITtitre},fonttitle=\bfseries\footnotesize\sffamily,colbacktitle=DarkGray!50!Black}
				}%
				{%
					\tcbset{notitle}
					\ifboolKV[envpiton]{Cadre}
						{}
						{\tcbset{boxrule=0pt,frame hidden}}
				}
		}
		{}%
	%on crée la boîte
	\begin{tcolorbox}[#2]
}%
{%
	\end{tcolorbox}%
}
\fi


%======CONSOLE PYLUATEX (package à charger manuellement !!)
\tcbset{consolepylua/.style={%base de la boîte
	enhanced,colback=white,colframe=ForestGreen,sharp corners,boxrule=1pt,%
	top=1.5mm,bottom=1.5mm,left=2mm,right=2mm,fontupper=\footnotesize,%
	before skip=\baselineskip,after skip=\baselineskip,%
	}
}

\tcbset{consolepyluatexlogo/.style={%avec logo python
	consolepylua,%
	overlay={%
		\draw ([yshift=0.5pt]frame.south) node[ForestGreen!50!black,fill=white,font=\scriptsize\ttfamily] {\scalebox{0.8}[0.8]{\faPython} Fin de la Console Python \scalebox{0.8}[0.8]{\faPython}} ;
		\draw ([yshift=-0.5pt]frame.north) node[ForestGreen!50!black,fill=white,font=\scriptsize\ttfamily] {\scalebox{0.8}[0.8]{\faPython} Début de la Console Python \scalebox{0.8}[0.8]{\faPython}} ;
		}
	}
}

\tcbset{consolepyluatex/.style={%sans logo python
	consolepylua,%
	overlay={%
		\draw ([yshift=0.5pt]frame.south) node[ForestGreen!50!black,fill=white,font=\scriptsize\ttfamily] {Fin de la Console Python} ;
		\draw ([yshift=-0.5pt]frame.north) node[ForestGreen!50!black,fill=white,font=\scriptsize\ttfamily] {Début de la Console Python} ;
		}
	}
}

\defKV[consolepyluatex]{%
	Largeur=\def\ConsPyluaLarg{#1},%
	Alignement=\def\ConsPyluaAlign{#1}
}
\setKVdefault[consolepyluatex]{%
	Logo=true,%
	Largeur=\linewidth,%
	Alignement=flush left
}

\NewDocumentEnvironment{ConsolePiton}{ O{} D<>{} m }%
%1=options piton
%2=clés
%3=options tcbox
{
	\restoreKV[consolepyluatex]% revenir au valeurs par défaut
	\setKV[consolepyluatex]{#2}% lit les arguments optionnels
	\PitonOptions{#1}
	\PyLTVerbatimEnv
	\ifboolKV[consolepyluatex]{Logo}
		{\tcbset{consolepyluatexlogo,width=\ConsPyluaLarg,\ConsPyluaAlign,#3}}
		{\tcbset{consolepyluatex,width=\ConsPyluaLarg,\ConsPyluaAlign,#3}}
	\begin{tcolorbox}
		\begin{pythonrepl}
}
{
	\directlua{
	tex.print("\\begin{Piton}")
	tex.print(pyluatex.get_last_output())
	tex.print("\\end{Piton}")
	tex.print("")
}
		\end{pythonrepl}
	\end{tcolorbox}
}

\endinput