\documentclass{ltxdoc}
\usepackage{tikz,geometry,minted,fontspec,luacode}
\usepackage[ngerman]{babel}
\usetikzlibrary{positioning}
\geometry{a4paper,margin=2cm}
\setmainfont[
	Renderer=HarfBuzz,
	ItalicFont=Recursive,
	BoldFont=Recursive,
	BoldItalicFont=Recursive,
	Scale=0.9,
	UprightFeatures={
		RawFeature={+ss01,+ss02,+ss08,+case},
		RawFeature={+axis={MONO=0.0,CASL=0.2,slnt=0,CRSV=0,wght=400}}
	},
	BoldFeatures={RawFeature={+axis={MONO=0.0,CASL=0.2,slnt=0,CRSV=0,wght=700}}},
	BoldItalicFeatures={RawFeature={+axis={MONO=0.0,CASL=0.2,slnt=-15,CRSV=1,wght=700}}},
	ItalicFeatures={RawFeature={+axis={MONO=0.0,CASL=0.2,slnt=-15,CRSV=1,wght=400}}}
	]{Recursive}
\setmonofont[
	Renderer=HarfBuzz,
	ItalicFont=Recursive,
	BoldFont=Recursive,
	BoldItalicFont=Recursive,
	Scale=0.9,
	UprightFeatures={
		RawFeature={+ss01,+ss02,+ss08,+case},
		RawFeature={+axis={MONO=1.0,CASL=0.2,slnt=0,CRSV=0,wght=400}}
	},
	BoldFeatures={RawFeature={+axis={MONO=1.0,CASL=0.2,slnt=0,CRSV=0,wght=700}}},
	BoldItalicFeatures={RawFeature={+axis={MONO=1.0,CASL=0.2,slnt=-15,CRSV=1,wght=700}}},
	ItalicFeatures={RawFeature={+axis={MONO=1.0,CASL=0.2,slnt=-15,CRSV=1,wght=400}}}
	]{Recursive}
\newfontfamily\titlefont[
	Renderer=HarfBuzz,
	ItalicFont=Recursive,
	BoldFont=Recursive,
	BoldItalicFont=Recursive,
	Scale=0.9,
	UprightFeatures={
		RawFeature={+ss01,+ss02,+ss08,+case},
		RawFeature={+axis={MONO=0.0,CASL=0.4,slnt=0,CRSV=1,wght=500}}
	},
	BoldFeatures={RawFeature={+axis={MONO=0.0,CASL=0.4,slnt=0,CRSV=1,wght=800}}},
	BoldItalicFeatures={RawFeature={+axis={MONO=0.0,CASL=0.4,slnt=-15,CRSV=1,wght=800}}},
	ItalicFeatures={RawFeature={+axis={MONO=0.0,CASL=0.4,slnt=-15,CRSV=1,wght=500}}}
	]{Recursive}

\directlua{ os.execute "mkdir -p cccbform-examples" }
\directlua{ os.execute "cp cccbform.cls cccbform-examples/" }
\directlua{ os.execute "cp logo.pdf cccbform-examples/" }

\begin{luacode*}
HEADER = [[
\documentclass[shortexample]{cccbform}
\begin{document}
]]
FOOTER = [[
\end{document}
]]
\end{luacode*}

\begin{luacode*}
	function makeVerb( envname )
		local buf
		local function bufread( chunk )
			if chunk:find( "\\end{"..envname.."}" ) then  return nil  end
			buf[#buf+1] = chunk
			return ""
		end
		local function luaverbstart( )
			buf = { }
			luatexbase.add_to_callback( "process_input_buffer", bufread, "bufread" )
		end
		local function luaverbend( )
			luatexbase.remove_from_callback( "process_input_buffer", "bufread" )
			buf[#buf+1] = ""
			buf = table.concat( buf, "\n" )
			return buf
		end
		return luaverbstart, luaverbend
	end

	function CMD(name)  return "\\"..name  end
\end{luacode*}

\directlua{ ShortExampleStart, ShortExampleEnd = makeVerb "ShortExample" }

\NewDocumentEnvironment{ShortExample}{m m}{
	\directlua{ShortExampleStart()}}{\directlua{
		CONTENT = ShortExampleEnd( )
		local f = io.open( "cccbform-examples/#1.tex", "w" )
		f:write( HEADER, CONTENT, FOOTER )
		f:close( )
		os.execute( "cd cccbform-examples ; latexmk -pdflua #1.tex" )
		local n, f = 0, io.open( "cccbform-examples/#1.tex" )
		for l in f:lines( ) do  n = n + 1  end
		tex.print( CMD"def"..CMD"lc".."{" .. (n-1) .. "}" )
	}

    \begin{tikzpicture}[example frame/.style={draw=gray!50, thick, inner sep=1ex}]
        \node[draw=gray!30, fill=gray!5, inner sep=1ex, rounded corners, minimum width=\textwidth] (example container) {};
        \node[example frame, anchor=north west, minimum height=7cm] (code) at (example container.north west) {
            \begin{minipage}[t]{0.45\textwidth}
                \inputminted[firstline=3,lastline=\lc]{tex}{cccbform-examples/#1.tex}
            \end{minipage}
        };
        \node[example frame, anchor=north west, right=2mm of code, minimum height=7cm] (output) {
            \begin{minipage}[t]{0.45\textwidth}
                \includegraphics[width=\linewidth]{cccbform-examples/#1.pdf}
            \end{minipage}
        };
        \node[anchor=north west, font=\small\itshape] at (example container.north west |- code.north west) {Example: #2};
    \end{tikzpicture}

%	\begin{tikzpicture}
%		\node[anchor=north west, inner sep=1ex, draw] (code) {
%			\begin{minipage}[t]{0.45\textwidth}
%				\inputminted[firstline=3,lastline=\lc]{tex}{cccbform-examples/#1.tex}
%			\end{minipage}
%		};
%		\node[anchor=north west, right=2mm of code, draw] (output) {
%			\begin{minipage}[t]{0.45\textwidth}
%				\includegraphics[width=\linewidth]{cccbform-examples/#1.pdf}
%			\end{minipage}
%		};
%	\end{tikzpicture}
}

\NewDocumentEnvironment{StandaloneExample}{m}{
	\VerbatimEnvironment%
	\begin{VerbatimOut}{cccbform-examples/#1.tex}}%
	{\end{VerbatimOut}%
	\directlua{ os.execute "cd cccbform-examples ; latexmk -pdflua #1.tex" }
	\directlua{
		local n, f = 0, io.open( "cccbform-examples/#1.tex" )
		for l in f:lines( ) do  n = n + 1  end
		tex.print( CMD"def"..CMD"lc".."{" .. (n-1) .. "}" )
	}
	\begin{tikzpicture}
		\node[anchor=north west, inner sep=1ex, draw] (code) {
			\begin{minipage}[t]{0.45\textwidth}
				\inputminted[firstline=3,lastline=\lc]{tex}{cccbform-examples/#1.tex}
			\end{minipage}
		};
		\node[anchor=north west, right=2mm of code, draw] (output) {
			\begin{minipage}[t]{0.45\textwidth}
				\includegraphics[width=\linewidth]{cccbform-examples/#1.pdf}
			\end{minipage}
		};
	\end{tikzpicture}
}

\title{\texttt{cccbform} -- Formulare für den CCCB}
\author{Frank Nord}
\begin{document}
\maketitle
\tableofcontents

\section{Übersicht}

\begin{ShortExample}{text}{Text}
\Text{Test}
\end{ShortExample}

\begin{ShortExample}{textfield}{normales Textfeld}
\TextField{abc}
\end{ShortExample}

\begin{ShortExample}{shorttextfield}{kurzes Textfeld}
\ShortTextField{abc}
\end{ShortExample}

\end{document}
