#include #include #include #include #include #include #include "tlmpdoc.h" #include "internal.h" struct _F_sample_private{ vector tb; int width; bool puttable; // Use a table or only a pre statement string tableopt; int nbtab; // Tabs to insert in front of each line }; static void sample_add (vector &tb, const char *s) { char buf[strlen(s)*4+1]; char *pt = buf; while (*s != '\0'){ if (*s == '<'){ *pt++ = '&'; *pt++ = 'l'; *pt++ = 't'; *pt++ = ';'; }else if (*s == '>'){ *pt++ = '&'; *pt++ = 'g'; *pt++ = 't'; *pt++ = ';'; }else{ *pt++ = *s; } s++; } *pt = '\0'; tb.push_back (string(buf)); } void _F_sample::htmlout (PARAM_STRING s) { sample_add (priv->tb,s.ptr); } void _F_sample::printf (const char *ctl, ...) { va_list list; va_start (list,ctl); char tmp[1000]; tmp[0] = 0xff; // We remember this line is already indented, no // space to remove, see below vsnprintf (tmp+1,sizeof(tmp)-2,ctl,list); va_end (list); sample_add (priv->tb,tmp); } /* Include a file as a sample text */ void _F_sample::include (const char *file, const char *prefix) { glocal _F_sample *x = this; glocal const char *prefix = prefix; (file,true); char buf[1000]; snprintf (buf,sizeof(buf)-1,"%s%s\n",glocal.prefix,line); glocal.x->htmlout(buf); return 0; } void _F_sample::include (const char *file) { include (file,""); } /* Set the width (percent) of the table box */ void _F_sample::setwidth (int width) { priv->width = width; } /* Select the presentation option of the table. If opt is NULL, the table is not used. Only a pre statement */ void _F_sample::settableopt (const char *opt) { if (opt == NULL){ priv->puttable = false; }else{ priv->puttable = true; priv->tableopt = opt; } } /* Select the number of tabs to insert in front of each lines. */ void _F_sample::setnbtab (int nbtab) { priv->nbtab = nbtab; } /* */ static int sample_minspaces(const vector &tb) { int n = tb.size(); int minspaces=1000; for (int i=0; i ' '){ if (spaces < minspaces) minspaces = spaces; } } } return minspaces; } static void sample_format ( const vector &tb, int minspaces, int nbtab) { char tabs[nbtab+1]; memset (tabs,'\t',nbtab+1); tabs[nbtab] = '\0'; int n = tb.size(); for (int i=0; i 0) fputs (tabs,stdout); if (s[0] == 0xff){ s++; }else{ int spaces = 0; while (spaces < minspaces && isspace(*s) && *s != '\n'){ spaces++; s++; } } if (strncmp(s,"##BASE##",8)!=0) htmlout (s); } } void sample (_F_sample &c, const char *title, int width) { if (!tlmpdoc_domap){ if (tlmpdoc_content != 0){ tlmpdoc_table_entry (title,NODE_SAMPLE); }else if (tlmpdoc_is_data() || tlmpdoc_vdepth <= tlmpdoc_niveau){ _F_sample_private priv; c.priv = &priv; priv.width = width; priv.puttable = true; priv.tableopt = "border=1"; priv.nbtab = 0; c.body(); // We remove the space in front of the lines int minspaces = sample_minspaces (priv.tb); htmlprintf ("
\n"); if (priv.puttable){ htmlprintf ("
\n"
					,priv.width,priv.tableopt.c_str());
			}else{
				htmlprintf ("
\n");
			}
			sample_format (priv.tb,minspaces,priv.nbtab);
			if (priv.puttable){
				htmlprintf ("
\n"); htmlprintf ("
%s
\n",title); }else{ htmlprintf ("\n"); } htmlprintf ("
\n"); } } } void sample (_F_sample &c, const char *title) { sample (c,title,60); } struct _F_pre_private{ vector tb; }; void _F_pre::htmlout (PARAM_STRING s) { priv->tb.emplace_back (s.ptr); } void _F_pre::printf (const char *ctl, ...) { va_list list; va_start (list,ctl); char tmp[1000]; tmp[0] = 0xff; // We remember this line is already indented, no // space to remove, see below vsnprintf (tmp+1,sizeof(tmp)-2,ctl,list); va_end (list); priv->tb.emplace_back (tmp); } void pre (_F_pre &c) { if (!tlmpdoc_domap && tlmpdoc_content == 0 && (tlmpdoc_is_data() || tlmpdoc_vdepth <= tlmpdoc_niveau)){ _F_pre_private priv; c.priv = &priv; c.body(); // We remove the space in front of the lines int minspaces = sample_minspaces (priv.tb); htmlprintf ("
\n"); htmlprintf ("
\n");
		sample_format (priv.tb,minspaces,1);
		htmlprintf ("
\n"); htmlprintf ("
\n"); } }