% 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.6.9	Amalioration de la gestion des style := intégration directe dans la tcbox !
%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=0.5\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=CouleurVertForet!25,boxrule=1.25pt,
		colframe=green,boxsep=1.25pt,
		sharp corners=downhill,
		arc=12pt,
		top=2pt,bottom=1pt,left=6pt,right=6pt
		},
	fonttitle=\color{CouleurVertForet}\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}
			\draw[draw=none,fill=lightgray!25] (interior.south west) rectangle ([xshift=1.3em]interior.north west) ;
		\end{tcbclipinterior}%
		}
	}
}

%==Création de la boîte (essai v2 ?)
\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{cadre/.style={}}
			\tcbset{titre/.style={}}
			\tcbset{stylebase/.style={stylepiton,colframe=green,colback=CouleurVertForet!5,width=\CODPITlargeur,fontupper=\CODPITfonte,fontlower=\CODPITfonte,\CODPITalign,leftupper=0.75em}}
			\ifboolKV[envpiton]{Filigrane}%si filigrane
				{\tcbset{filigrane/.style={watermark text={\faPython},watermark opacity=0.175,watermark zoom=0.50}}}%
				{\tcbset{filigrane/.style={}}}%
		}
		{}%
	\IfEq{\CODPITstyle}{Classique}%style Classique
		{%
			\ifboolKV[envpiton]{Lignes}%si lignes=true
				{\tcbset{stylebase/.style={thonnystylelineos,leftupper=0.75em,width=\CODPITlargeur,colframe=darkgray!50!black,fontupper=\CODPITfonte,fontlower=\CODPITfonte,\CODPITalign}}}%
				{\tcbset{stylebase/.style={thonnystyle,leftupper=0.4em,width=\CODPITlargeur,colframe=darkgray!50!black,fontupper=\CODPITfonte,fontlower=\CODPITfonte,\CODPITalign}}}%
			%\tcbset{width=\CODPITlargeur,colframe=darkgray!50!black,fontupper=\CODPITfonte,fontlower=\CODPITfonte,\CODPITalign}
			\ifboolKV[envpiton]{Filigrane}%si filigrane
				{\tcbset{filigrane/.style={watermark text={\faPython},watermark opacity=0.25,watermark zoom=0.50}}}%
				{\tcbset{filigrane/.style={}}}%
			\ifboolKV[envpiton]{BarreTitre}%
				{%
					\ifboolKV[envpiton]{Cadre}
						{\tcbset{cadre/.style={}}}%
						{\tcbset{cadre/.style={boxrule=0pt,frame hidden}}}%
					\tcbset{titre/.style={lefttitle=0.4em,title={\CODPITtitre},fonttitle=\bfseries\footnotesize\sffamily,colbacktitle=darkgray!50!black}}
				}%
				{%
					\tcbset{titre/.style={notitle}}
					\ifboolKV[envpiton]{Cadre}
						{\tcbset{cadre/.style={}}}
						{\tcbset{cadre/.style={boxrule=0pt,frame hidden}}}
				}
		}
		{}%
	%on crée la boîte
	\begin{tcolorbox}[stylebase,filigrane,cadre,titre]
}%
{%
	\end{tcolorbox}%
}
\fi


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

\tcbset{consolepyluatexlogo/.style={%avec logo python
	consolepylua,%
	overlay={%
		\draw ([yshift=0.5pt]frame.south) node[CouleurVertForet!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[CouleurVertForet!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[CouleurVertForet!50!black,fill=white,font=\scriptsize\ttfamily] {Fin de la Console Python} ;
		\draw ([yshift=-0.5pt]frame.north) node[CouleurVertForet!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}
		{%
			\begin{tcolorbox}[consolepyluatexlogo,width=\ConsPyluaLarg,\ConsPyluaAlign,#3]
		}%
		{%
			\begin{tcolorbox}[consolepyluatex,width=\ConsPyluaLarg,\ConsPyluaAlign,#3]
		}%
%		{\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