Fossil

Check-in [18abb60d42]
Login

Check-in [18abb60d42]

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Found a workaround for the th1 puts escaping, but am still wondering whether it's a viable long-term solution.
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | pikchr-th
Files: files | file ages | folders
SHA3-256: 18abb60d42333922b1286f4235eaecb90de3fadb59d326c3f77f3d8096dfb10e
User & Date: stephan 2020-09-13 18:26:35.370
Context
2020-09-13
18:27
Merged in trunk for latest pikchr. ... (check-in: 810b8272fb user: stephan tags: pikchr-th)
18:26
Found a workaround for the th1 puts escaping, but am still wondering whether it's a viable long-term solution. ... (check-in: 18abb60d42 user: stephan tags: pikchr-th)
16:35
Initial work on a pikchr CLI command which integrates TH1 for programmatic pikchr generation. DO NOT MERGE: the required changes to TH1 need more testing to ensure they do not introduce fallout in the skin output. Also, output redirection is still very imcomplete and TH1 error reporting does not yet do the right thing. ... (check-in: 8a4304eae2 user: stephan tags: pikchr-th)
Changes
Unified Diff Ignore Whitespace Patch
Changes to src/pikchrshow.c.
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207

208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228


229

230
231
232
233
234

235




236
237


238
239
240
241
242
243
244
245
246
247
248
249
250
251

252
253


254
255
256
257
258
259
260
261
  }
  builtin_emit_fossil_js_apis("page.pikchrshow", 0);
  builtin_fulfill_js_requests();
  style_footer();
}

static void pikchr_th_init(u32 fThInit){
  Th_FossilInit(fThInit);
}

/*
** COMMAND: pikchr
**
** Usage: %fossil pikchr [options] ?INFILE? ?OUTFILE?
**
** Options:
**
**    -div      On success, adds a DIV wrapper around the
**              resulting SVG output which limits its max-width.
**
**    -th       Process the input using TH1 before passing it to pikchr.
**
**    -th-novar Disable $var and $<var> TH1 processing. Only applies
**              with the -th flag.



**
**    -th-trace Trace TH1 execution (for debugging purposes)
**
** TH1 Caveats: the built-in TH1 commands make some assumptions about
** HTML escaping and output which do not apply via this
** command. e.g. some commands will output directly to stdout, rather
** than the output buffer this command requires. Improvements in that
** regard are under consideration/construction.
*/
void pikchr_cmd(void){
  Blob bIn = empty_blob;
  Blob bOut = empty_blob;
  const char * zInfile = "-";
  const char * zOutfile = "-";
  const int fWithDiv = find_option("div",0,0)!=0;
  const int fTh1 = find_option("th",0,0)!=0;
  const int fNoVar = find_option("th-novar",0,0)!=0;
  int isErr = 0;
  u32 fThInit = TH_INIT_DEFAULT;


  Th_InitTraceLog()/*processes -th-trace flag*/;
  verify_all_options();
  if(g.argc>4){
    usage("?INFILE? ?OUTFILE?");
  }
  if(g.argc>2){
    zInfile = g.argv[2];
  }
  if(g.argc>3){
    zOutfile = g.argv[3];
  }
  blob_read_from_file(&bIn, zInfile, ExtFILE);
  if(fTh1){
    Blob out = empty_blob;
    Blob * oldOut;
    db_find_and_open_repository(OPEN_ANY_SCHEMA | OPEN_OK_NOT_FOUND, 0)
      /* ^^^ needed for certain functions to work */;
    oldOut = Th_SetOutputBlob(&out);
    pikchr_th_init(fThInit);
    isErr = Th_RenderToBlob(blob_str(&bIn), &out,


                            fNoVar ? TH_R2B_NO_VARS : 0);

    blob_reset(&bIn);
    bIn = out;
    Th_SetOutputBlob(oldOut);
    /*fossil_print("th'd:\n%b\n", &bIn);*/
  }

  if(!isErr){




    int w = 0, h = 0;
    const char * zContent = blob_str(&bIn);


    char *zOut = pikchr(zContent, "pikchr", 0, &w, &h);
    if( w>0 && h>0 ){
      if(fWithDiv){
        blob_appendf(&bOut,"<div style='max-width:%dpx;'>\n", w);
      }
      blob_append(&bOut, zOut, -1);
      if(fWithDiv){
        blob_append(&bOut,"</div>\n", 7);
      }
    }else{
      isErr = 1;
      blob_append(&bOut, zOut, -1);
    }
    fossil_free(zOut);

  }
  if(isErr){


    fossil_fatal("ERROR: %b", &bOut);
  }else{
    blob_write_to_file(&bOut, zOutfile);
  }
  Th_PrintTraceLog();
  blob_reset(&bIn);
  blob_reset(&bOut);
}







|
















>
>
>
















|

|
>















<

|
<
|
|
>
>
|
>
|
|
<
<
|
>

>
>
>
>
|
|
>
>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
>


>
>
|







165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226

227
228

229
230
231
232
233
234
235
236


237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
  }
  builtin_emit_fossil_js_apis("page.pikchrshow", 0);
  builtin_fulfill_js_requests();
  style_footer();
}

static void pikchr_th_init(u32 fThInit){
  Th_FossilInit(fThInit & TH_INIT_MASK);
}

/*
** COMMAND: pikchr
**
** Usage: %fossil pikchr [options] ?INFILE? ?OUTFILE?
**
** Options:
**
**    -div      On success, adds a DIV wrapper around the
**              resulting SVG output which limits its max-width.
**
**    -th       Process the input using TH1 before passing it to pikchr.
**
**    -th-novar Disable $var and $<var> TH1 processing. Only applies
**              with the -th flag.
**
**    -th-nopic When using -th, output the post-TH1'd script
**              instead of the pikchr-rendered output.
**
**    -th-trace Trace TH1 execution (for debugging purposes)
**
** TH1 Caveats: the built-in TH1 commands make some assumptions about
** HTML escaping and output which do not apply via this
** command. e.g. some commands will output directly to stdout, rather
** than the output buffer this command requires. Improvements in that
** regard are under consideration/construction.
*/
void pikchr_cmd(void){
  Blob bIn = empty_blob;
  Blob bOut = empty_blob;
  const char * zInfile = "-";
  const char * zOutfile = "-";
  const int fWithDiv = find_option("div",0,0)!=0;
  const int fTh1 = find_option("th",0,0)!=0;
  const int fNoPic = find_option("th-nopic",0,0)!=0;
  int isErr = 0;
  u32 fThFlags = TH_INIT_DEFAULT | TH_INIT_NO_ESC
    | (find_option("th-novar",0,0)!=0 ? TH_R2B_NO_VARS : 0);

  Th_InitTraceLog()/*processes -th-trace flag*/;
  verify_all_options();
  if(g.argc>4){
    usage("?INFILE? ?OUTFILE?");
  }
  if(g.argc>2){
    zInfile = g.argv[2];
  }
  if(g.argc>3){
    zOutfile = g.argv[3];
  }
  blob_read_from_file(&bIn, zInfile, ExtFILE);
  if(fTh1){
    Blob out = empty_blob;

    db_find_and_open_repository(OPEN_ANY_SCHEMA | OPEN_OK_NOT_FOUND, 0)
      /* ^^^ needed for certain TH1 functions to work */;

    pikchr_th_init(fThFlags);
    isErr = Th_RenderToBlob(blob_str(&bIn), &out, fThFlags) ? 1 : 0;
    if(isErr){
      blob_reset(&bOut);
      bOut = out;
    }else{
      blob_reset(&bIn);
      bIn = out;


    }
  }
  if(!isErr){
    if(fTh1 && fNoPic){
      bOut = bIn;
      bIn = empty_blob;
    }else{
      int w = 0, h = 0;
      const char * zContent = blob_str(&bIn);
      char *zOut;

      zOut = pikchr(zContent, "pikchr", 0, &w, &h);
      if( w>0 && h>0 ){
        if(fWithDiv){
          blob_appendf(&bOut,"<div style='max-width:%dpx;'>\n", w);
        }
        blob_append(&bOut, zOut, -1);
        if(fWithDiv){
          blob_append(&bOut,"</div>\n", 7);
        }
      }else{
        isErr = 2;
        blob_append(&bOut, zOut, -1);
      }
      fossil_free(zOut);
    }
  }
  if(isErr){
    /*fossil_print("ERROR: raw input:\n%b\n", &bIn);*/
    fossil_fatal("%s ERROR: %b", 1==isErr ? "TH1" : "pikchr",
                 &bOut);
  }else{
    blob_write_to_file(&bOut, zOutfile);
  }
  Th_PrintTraceLog();
  blob_reset(&bIn);
  blob_reset(&bOut);
}
Changes to src/th_main.c.
29
30
31
32
33
34
35

36
37
38
39
40
41
42
43
*/
#define TH_INIT_NONE        ((u32)0x00000000) /* No flags. */
#define TH_INIT_NEED_CONFIG ((u32)0x00000001) /* Open configuration first? */
#define TH_INIT_FORCE_TCL   ((u32)0x00000002) /* Force Tcl to be enabled? */
#define TH_INIT_FORCE_RESET ((u32)0x00000004) /* Force TH1 commands re-added? */
#define TH_INIT_FORCE_SETUP ((u32)0x00000008) /* Force eval of setup script? */
#define TH_INIT_NO_REPO     ((u32)0x00000010) /* Skip opening repository. */

#define TH_INIT_MASK        ((u32)0x0000001F) /* All possible init flags. */

/*
** Useful and/or "well-known" combinations of flag values.
*/
#define TH_INIT_DEFAULT     (TH_INIT_NONE)      /* Default flags. */
#define TH_INIT_HOOK        (TH_INIT_NEED_CONFIG | TH_INIT_FORCE_SETUP)
#define TH_INIT_FORBID_MASK (TH_INIT_FORCE_TCL) /* Illegal from a script. */







>
|







29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
*/
#define TH_INIT_NONE        ((u32)0x00000000) /* No flags. */
#define TH_INIT_NEED_CONFIG ((u32)0x00000001) /* Open configuration first? */
#define TH_INIT_FORCE_TCL   ((u32)0x00000002) /* Force Tcl to be enabled? */
#define TH_INIT_FORCE_RESET ((u32)0x00000004) /* Force TH1 commands re-added? */
#define TH_INIT_FORCE_SETUP ((u32)0x00000008) /* Force eval of setup script? */
#define TH_INIT_NO_REPO     ((u32)0x00000010) /* Skip opening repository. */
#define TH_INIT_NO_ESC      ((u32)0x00000020) /* Do not html-escape certain output. */
#define TH_INIT_MASK        ((u32)0x0000003F) /* All possible init flags. */

/*
** Useful and/or "well-known" combinations of flag values.
*/
#define TH_INIT_DEFAULT     (TH_INIT_NONE)      /* Default flags. */
#define TH_INIT_HOOK        (TH_INIT_NEED_CONFIG | TH_INIT_FORCE_SETUP)
#define TH_INIT_FORBID_MASK (TH_INIT_FORCE_TCL) /* Illegal from a script. */
469
470
471
472
473
474
475


476
477
478
479
480
481
482
483
484
485
486
static int putsCmd(
  Th_Interp *interp,
  void *pConvert,
  int argc,
  const char **argv,
  int *argl
){


  if( argc!=2 ){
    return Th_WrongNumArgs(interp, "puts STRING");
  }
  sendText(0,(char*)argv[1], argl[1], *(unsigned int*)pConvert);
  return TH_OK;
}

/*
** TH1 command: redirect URL ?withMethod?
**
** Issues an HTTP redirect to the specified URL and then exits the process.







>
>



|







470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
static int putsCmd(
  Th_Interp *interp,
  void *pConvert,
  int argc,
  const char **argv,
  int *argl
){
  unsigned int doEscape = (TH_INIT_NO_ESC & g.th1Flags)
    ? 0U : *(unsigned int*)pConvert;
  if( argc!=2 ){
    return Th_WrongNumArgs(interp, "puts STRING");
  }
  sendText(0,(char*)argv[1], argl[1], doEscape);
  return TH_OK;
}

/*
** TH1 command: redirect URL ?withMethod?
**
** Issues an HTTP redirect to the specified URL and then exits the process.
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
void Th_FossilInit(u32 flags){
  int wasInit = 0;
  int needConfig = flags & TH_INIT_NEED_CONFIG;
  int forceReset = flags & TH_INIT_FORCE_RESET;
  int forceTcl = flags & TH_INIT_FORCE_TCL;
  int forceSetup = flags & TH_INIT_FORCE_SETUP;
  int noRepo = flags & TH_INIT_NO_REPO;
  static unsigned int aFlags[] = { 0, 1, WIKI_LINKSONLY };
  static int anonFlag = LOGIN_ANON;
  static int zeroInt = 0;
  static struct _Command {
    const char *zName;
    Th_CommandProc xProc;
    void *pContext;
  } aCommand[] = {







|







2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
void Th_FossilInit(u32 flags){
  int wasInit = 0;
  int needConfig = flags & TH_INIT_NEED_CONFIG;
  int forceReset = flags & TH_INIT_FORCE_RESET;
  int forceTcl = flags & TH_INIT_FORCE_TCL;
  int forceSetup = flags & TH_INIT_FORCE_SETUP;
  int noRepo = flags & TH_INIT_NO_REPO;
  static unsigned int aFlags[] = {0, 1, WIKI_LINKSONLY };
  static int anonFlag = LOGIN_ANON;
  static int zeroInt = 0;
  static struct _Command {
    const char *zName;
    Th_CommandProc xProc;
    void *pContext;
  } aCommand[] = {
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
** may be passed as the 3rd argument, or 0 for default options.
*/
int Th_RenderToBlob(const char *z, Blob * pOut, u32 mFlags){
  int i = 0;
  int n;
  int rc = TH_OK;
  char *zResult;
  Blob * const origOut = pThOut;

  assert(0==(TH_R2B_MASK & TH_INIT_MASK) && "init/r2b mask conflict");
  pThOut = pOut;
  Th_FossilInit(mFlags & TH_INIT_MASK);
  while( z[i] ){
    if( !(TH_R2B_NO_VARS & mFlags)
        && z[i]=='$' && (n = validVarName(&z[i+1]))>0 ){
      const char *zVar;
      int nVar;
      int encode = 1;







|


<







2655
2656
2657
2658
2659
2660
2661
2662
2663
2664

2665
2666
2667
2668
2669
2670
2671
** may be passed as the 3rd argument, or 0 for default options.
*/
int Th_RenderToBlob(const char *z, Blob * pOut, u32 mFlags){
  int i = 0;
  int n;
  int rc = TH_OK;
  char *zResult;
  Blob * const origOut = Th_SetOutputBlob(pOut);

  assert(0==(TH_R2B_MASK & TH_INIT_MASK) && "init/r2b mask conflict");

  Th_FossilInit(mFlags & TH_INIT_MASK);
  while( z[i] ){
    if( !(TH_R2B_NO_VARS & mFlags)
        && z[i]=='$' && (n = validVarName(&z[i+1]))>0 ){
      const char *zVar;
      int nVar;
      int encode = 1;
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
  }
  if( rc==TH_ERROR ){
    zResult = (char*)Th_GetResult(g.interp, &n);
    sendError(pOut,zResult, n, 1);
  }else{
    sendText(pOut,z, i, 0);
  }
  pThOut = origOut;
  return rc;
}

/*
** The z[] input contains text mixed with TH1 scripts.
** The TH1 scripts are contained within <th1>...</th1>.
** TH1 variables are $aaa or $<aaa>.  The first form of







|







2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
  }
  if( rc==TH_ERROR ){
    zResult = (char*)Th_GetResult(g.interp, &n);
    sendError(pOut,zResult, n, 1);
  }else{
    sendText(pOut,z, i, 0);
  }
  Th_SetOutputBlob(origOut);
  return rc;
}

/*
** The z[] input contains text mixed with TH1 scripts.
** The TH1 scripts are contained within <th1>...</th1>.
** TH1 variables are $aaa or $<aaa>.  The first form of