
\pdfoutput = 1
\pdfcompresslevel=0

\directlua0{ dofile ("mplib.lua") }
\directlua0{ dofile (kpse.find_file('mlib-pdf.lua')) }

% some things that are normally pre-defined in context

\newdimen\onebasepoint 
\onebasepoint = 1bp
\newbox\scratchbox
{\catcode`\% =12 \gdef\letterpercent{%}}
\directlua0{ tex.ctxcatcodes = 0 }

% the three macros output by the context converter:

\def\startMPLIBtoPDF#1#2#3#4% watch the transparency reset
  {\hbox\bgroup
   \xdef\MPllx{#1}%
   \xdef\MPlly{#2}%
   \xdef\MPurx{#3}%
   \xdef\MPury{#4}%
   \xdef\MPwidth {\the\dimexpr#3\onebasepoint-#1\onebasepoint\relax}%
   \xdef\MPheight{\the\dimexpr#4\onebasepoint-#2\onebasepoint\relax}%
   \setbox\scratchbox\vbox\bgroup
   \noindent % this is really needed in order to force tex into proper cm's
}

\let\MPLIBtoPDF\pdfliteral

\def\stopMPLIBtoPDF % watch the transparency reset
  {\egroup
   \setbox\scratchbox\hbox\bgroup
     \hskip-\MPllx\onebasepoint
     \raise-\MPlly\onebasepoint
     \box\scratchbox
   \egroup
   \setbox\scratchbox\vbox to \MPheight\bgroup
     \vfill
     \hsize\MPwidth
     \ht\scratchbox=0pt
     \dp\scratchbox=0pt
     \box\scratchbox
   \egroup
   \wd\scratchbox\MPwidth
   \ht\scratchbox\MPheight
   \box\scratchbox
   \egroup }

% our own command

\def\mp#1{\directlua0{ 
  if not mpx then mpx = metapost.load('plain') end
  local f = metapost.process(mpx,'#1')
  if f then    
    print (f.term)
    if f.fig then
      metapost.convert(f)
    end
  end
}}

\mp{beginfig(1); draw fullcircle scaled 20 withcolor (1,0,0); endfig;}

\bye