Fossil SCM

Added th1 (enable_htmlify bool) command so that pikchr th1 code in th1Docs has a way of toggling that g.th1Flag off and on.

stephan 2020-09-13 23:38 pikchr-th
Commit 6f7f73b6af4697d445cc16f2cbcb529e16f8f9ea52862a4d8d66fdcb3c49eb50
+17 -13
--- src/pikchrshow.c
+++ src/pikchrshow.c
@@ -166,39 +166,43 @@
166166
builtin_emit_fossil_js_apis("page.pikchrshow", 0);
167167
builtin_fulfill_js_requests();
168168
style_footer();
169169
}
170170
171
-static void pikchr_th_init(u32 fThInit){
172
- Th_FossilInit(fThInit & TH_INIT_MASK);
173
-}
174
-
175171
/*
176172
** COMMAND: pikchr
177173
**
178174
** Usage: %fossil pikchr [options] ?INFILE? ?OUTFILE?
175
+**
176
+** Accepts a pikchr script as input and outputs the rendered
177
+** script as an SVG graphic.
179178
**
180179
** Options:
181180
**
182181
** -div On success, adds a DIV wrapper around the
183182
** resulting SVG output which limits its max-width.
184183
**
185184
** -th Process the input using TH1 before passing it to pikchr.
186185
**
187186
** -th-novar Disable $var and $<var> TH1 processing. Only applies
188
-** with the -th flag.
187
+** with the -th flag. Use this if the pikchr script uses
188
+** '$' for its own purposes.
189189
**
190190
** -th-nosvg When using -th, output the post-TH1'd script
191191
** instead of the pikchr-rendered output.
192192
**
193193
** -th-trace Trace TH1 execution (for debugging purposes)
194194
**
195
-** TH1 Caveats: the built-in TH1 commands make some assumptions about
196
-** HTML escaping and output which do not apply via this
197
-** command. e.g. some commands will output directly to stdout, rather
198
-** than the output buffer this command requires. Improvements in that
199
-** regard are under consideration/construction.
195
+** TH1 Caveats:
196
+**
197
+** If the -th flag is used, this command must open a fossil database
198
+** for certain functionality to work. It will run TH1 without opening
199
+** a db if one cannot be found in the current checkout or with the -R
200
+** REPO flag, but any TH1 commands which require a db will then fail.
201
+**
202
+** Many of the fossil-installed TH1 functions do not make any sense
203
+** for pikchr scripts
200204
*/
201205
void pikchr_cmd(void){
202206
Blob bIn = empty_blob;
203207
Blob bOut = empty_blob;
204208
const char * zInfile = "-";
@@ -205,11 +209,11 @@
205209
const char * zOutfile = "-";
206210
const int fWithDiv = find_option("div",0,0)!=0;
207211
const int fTh1 = find_option("th",0,0)!=0;
208212
const int fNosvg = find_option("th-nosvg",0,0)!=0;
209213
int isErr = 0;
210
- u32 fThFlags = TH_INIT_DEFAULT | TH_INIT_NO_ENCODE
214
+ u32 fThFlags = TH_INIT_NO_ENCODE
211215
| (find_option("th-novar",0,0)!=0 ? TH_R2B_NO_VARS : 0);
212216
213217
Th_InitTraceLog()/*processes -th-trace flag*/;
214218
verify_all_options();
215219
if(g.argc>4){
@@ -224,12 +228,12 @@
224228
blob_read_from_file(&bIn, zInfile, ExtFILE);
225229
if(fTh1){
226230
Blob out = empty_blob;
227231
db_find_and_open_repository(OPEN_ANY_SCHEMA | OPEN_OK_NOT_FOUND, 0)
228232
/* ^^^ needed for certain TH1 functions to work */;
229
- pikchr_th_init(fThFlags);
230
- isErr = Th_RenderToBlob(blob_str(&bIn), &out, fThFlags) ? 1 : 0;
233
+ isErr = Th_RenderToBlob(blob_str(&bIn), &out, fThFlags)
234
+ ? 1 : 0;
231235
if(isErr){
232236
blob_reset(&bOut);
233237
bOut = out;
234238
}else{
235239
blob_reset(&bIn);
236240
--- src/pikchrshow.c
+++ src/pikchrshow.c
@@ -166,39 +166,43 @@
166 builtin_emit_fossil_js_apis("page.pikchrshow", 0);
167 builtin_fulfill_js_requests();
168 style_footer();
169 }
170
171 static void pikchr_th_init(u32 fThInit){
172 Th_FossilInit(fThInit & TH_INIT_MASK);
173 }
174
175 /*
176 ** COMMAND: pikchr
177 **
178 ** Usage: %fossil pikchr [options] ?INFILE? ?OUTFILE?
 
 
 
179 **
180 ** Options:
181 **
182 ** -div On success, adds a DIV wrapper around the
183 ** resulting SVG output which limits its max-width.
184 **
185 ** -th Process the input using TH1 before passing it to pikchr.
186 **
187 ** -th-novar Disable $var and $<var> TH1 processing. Only applies
188 ** with the -th flag.
 
189 **
190 ** -th-nosvg When using -th, output the post-TH1'd script
191 ** instead of the pikchr-rendered output.
192 **
193 ** -th-trace Trace TH1 execution (for debugging purposes)
194 **
195 ** TH1 Caveats: the built-in TH1 commands make some assumptions about
196 ** HTML escaping and output which do not apply via this
197 ** command. e.g. some commands will output directly to stdout, rather
198 ** than the output buffer this command requires. Improvements in that
199 ** regard are under consideration/construction.
 
 
 
 
200 */
201 void pikchr_cmd(void){
202 Blob bIn = empty_blob;
203 Blob bOut = empty_blob;
204 const char * zInfile = "-";
@@ -205,11 +209,11 @@
205 const char * zOutfile = "-";
206 const int fWithDiv = find_option("div",0,0)!=0;
207 const int fTh1 = find_option("th",0,0)!=0;
208 const int fNosvg = find_option("th-nosvg",0,0)!=0;
209 int isErr = 0;
210 u32 fThFlags = TH_INIT_DEFAULT | TH_INIT_NO_ENCODE
211 | (find_option("th-novar",0,0)!=0 ? TH_R2B_NO_VARS : 0);
212
213 Th_InitTraceLog()/*processes -th-trace flag*/;
214 verify_all_options();
215 if(g.argc>4){
@@ -224,12 +228,12 @@
224 blob_read_from_file(&bIn, zInfile, ExtFILE);
225 if(fTh1){
226 Blob out = empty_blob;
227 db_find_and_open_repository(OPEN_ANY_SCHEMA | OPEN_OK_NOT_FOUND, 0)
228 /* ^^^ needed for certain TH1 functions to work */;
229 pikchr_th_init(fThFlags);
230 isErr = Th_RenderToBlob(blob_str(&bIn), &out, fThFlags) ? 1 : 0;
231 if(isErr){
232 blob_reset(&bOut);
233 bOut = out;
234 }else{
235 blob_reset(&bIn);
236
--- src/pikchrshow.c
+++ src/pikchrshow.c
@@ -166,39 +166,43 @@
166 builtin_emit_fossil_js_apis("page.pikchrshow", 0);
167 builtin_fulfill_js_requests();
168 style_footer();
169 }
170
 
 
 
 
171 /*
172 ** COMMAND: pikchr
173 **
174 ** Usage: %fossil pikchr [options] ?INFILE? ?OUTFILE?
175 **
176 ** Accepts a pikchr script as input and outputs the rendered
177 ** script as an SVG graphic.
178 **
179 ** Options:
180 **
181 ** -div On success, adds a DIV wrapper around the
182 ** resulting SVG output which limits its max-width.
183 **
184 ** -th Process the input using TH1 before passing it to pikchr.
185 **
186 ** -th-novar Disable $var and $<var> TH1 processing. Only applies
187 ** with the -th flag. Use this if the pikchr script uses
188 ** '$' for its own purposes.
189 **
190 ** -th-nosvg When using -th, output the post-TH1'd script
191 ** instead of the pikchr-rendered output.
192 **
193 ** -th-trace Trace TH1 execution (for debugging purposes)
194 **
195 ** TH1 Caveats:
196 **
197 ** If the -th flag is used, this command must open a fossil database
198 ** for certain functionality to work. It will run TH1 without opening
199 ** a db if one cannot be found in the current checkout or with the -R
200 ** REPO flag, but any TH1 commands which require a db will then fail.
201 **
202 ** Many of the fossil-installed TH1 functions do not make any sense
203 ** for pikchr scripts
204 */
205 void pikchr_cmd(void){
206 Blob bIn = empty_blob;
207 Blob bOut = empty_blob;
208 const char * zInfile = "-";
@@ -205,11 +209,11 @@
209 const char * zOutfile = "-";
210 const int fWithDiv = find_option("div",0,0)!=0;
211 const int fTh1 = find_option("th",0,0)!=0;
212 const int fNosvg = find_option("th-nosvg",0,0)!=0;
213 int isErr = 0;
214 u32 fThFlags = TH_INIT_NO_ENCODE
215 | (find_option("th-novar",0,0)!=0 ? TH_R2B_NO_VARS : 0);
216
217 Th_InitTraceLog()/*processes -th-trace flag*/;
218 verify_all_options();
219 if(g.argc>4){
@@ -224,12 +228,12 @@
228 blob_read_from_file(&bIn, zInfile, ExtFILE);
229 if(fTh1){
230 Blob out = empty_blob;
231 db_find_and_open_repository(OPEN_ANY_SCHEMA | OPEN_OK_NOT_FOUND, 0)
232 /* ^^^ needed for certain TH1 functions to work */;
233 isErr = Th_RenderToBlob(blob_str(&bIn), &out, fThFlags)
234 ? 1 : 0;
235 if(isErr){
236 blob_reset(&bOut);
237 bOut = out;
238 }else{
239 blob_reset(&bIn);
240
+39 -4
--- src/th_main.c
+++ src/th_main.c
@@ -289,10 +289,41 @@
289289
if( g.thTrace ){
290290
Th_Trace("enable_output {%.*s} -> %d<br />\n", argl[1],argv[1],enableOutput);
291291
}
292292
return rc;
293293
}
294
+
295
+/*
296
+** TH1 command: enable_htmlify BOOLEAN
297
+**
298
+** Enable or disable the HTML escaping done by all output which
299
+** originates from TH1 (via sendText()).
300
+*/
301
+static int enableHtmlifyCmd(
302
+ Th_Interp *interp,
303
+ void *p,
304
+ int argc,
305
+ const char **argv,
306
+ int *argl
307
+){
308
+ int rc, buul;
309
+ if( argc<2 || argc>3 ){
310
+ return Th_WrongNumArgs(interp,
311
+ "enable_htmlify [TRACE_LABEL] BOOLEAN");
312
+ }
313
+ rc = Th_ToInt(interp, argv[argc-1], argl[argc-1], &buul);
314
+ if( g.thTrace ){
315
+ Th_Trace("enable_htmlify {%.*s} -> %d<br />\n",
316
+ argl[1],argv[1],buul);
317
+ }
318
+ if(buul){
319
+ g.th1Flags &= ~TH_INIT_NO_ENCODE;
320
+ }else{
321
+ g.th1Flags |= TH_INIT_NO_ENCODE;
322
+ }
323
+ return rc;
324
+}
294325
295326
/*
296327
** Returns a name for a TH1 return code.
297328
*/
298329
const char *Th_ReturnCodeName(int rc, int nullIfOk){
@@ -2200,10 +2231,11 @@
22002231
{"copybtn", copybtnCmd, 0},
22012232
{"date", dateCmd, 0},
22022233
{"decorate", wikiCmd, (void*)&aFlags[2]},
22032234
{"defHeader", defHeaderCmd, 0},
22042235
{"dir", dirCmd, 0},
2236
+ {"enable_htmlify",enableHtmlifyCmd, 0},
22052237
{"enable_output", enableOutputCmd, 0},
22062238
{"encode64", encode64Cmd, 0},
22072239
{"getParameter", getParameterCmd, 0},
22082240
{"glob_match", globMatchCmd, 0},
22092241
{"globalState", globalStateCmd, 0},
@@ -2682,19 +2714,22 @@
26822714
#if INTERFACE
26832715
/*
26842716
** Flags for use with Th_RenderToBlob. These must not overlap with
26852717
** TH_INIT_MASK.
26862718
*/
2687
-#define TH_R2B_MASK ((u32)0xff0000)
2688
-#define TH_R2B_NO_VARS ((u32)0x010000) /* Disables eval of $vars and $<vars> */
2719
+#define TH_R2B_MASK ((u32)0x0f000)
2720
+#define TH_R2B_NO_VARS ((u32)0x01000) /* Disables eval of $vars and $<vars> */
26892721
#endif
26902722
26912723
/*
26922724
** If pOut is NULL, this works identically to Th_Render(), else it
26932725
** works just like that function but appends any TH1-generated output
26942726
** to the given blob. A bitmask of TH_R2B_xxx and/or TH_INIT_xxx flags
2695
-** may be passed as the 3rd argument, or 0 for default options.
2727
+** may be passed as the 3rd argument, or 0 for default options. Note
2728
+** that this function necessarily calls Th_FossilInit(), which may
2729
+** unset flags used on previous calls unless mFlags is explicitly
2730
+** passed in.
26962731
*/
26972732
int Th_RenderToBlob(const char *z, Blob * pOut, u32 mFlags){
26982733
int i = 0;
26992734
int n;
27002735
int rc = TH_OK;
@@ -2702,11 +2737,11 @@
27022737
Blob * const origOut = Th_SetOutputBlob(pOut);
27032738
27042739
assert(0==(TH_R2B_MASK & TH_INIT_MASK) && "init/r2b mask conflict");
27052740
Th_FossilInit(mFlags & TH_INIT_MASK);
27062741
while( z[i] ){
2707
- if( !(TH_R2B_NO_VARS & mFlags)
2742
+ if( 0!=(TH_R2B_NO_VARS & mFlags)
27082743
&& z[i]=='$' && (n = validVarName(&z[i+1]))>0 ){
27092744
const char *zVar;
27102745
int nVar;
27112746
int encode = 1;
27122747
sendText(pOut,z, i, 0);
27132748
--- src/th_main.c
+++ src/th_main.c
@@ -289,10 +289,41 @@
289 if( g.thTrace ){
290 Th_Trace("enable_output {%.*s} -> %d<br />\n", argl[1],argv[1],enableOutput);
291 }
292 return rc;
293 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
294
295 /*
296 ** Returns a name for a TH1 return code.
297 */
298 const char *Th_ReturnCodeName(int rc, int nullIfOk){
@@ -2200,10 +2231,11 @@
2200 {"copybtn", copybtnCmd, 0},
2201 {"date", dateCmd, 0},
2202 {"decorate", wikiCmd, (void*)&aFlags[2]},
2203 {"defHeader", defHeaderCmd, 0},
2204 {"dir", dirCmd, 0},
 
2205 {"enable_output", enableOutputCmd, 0},
2206 {"encode64", encode64Cmd, 0},
2207 {"getParameter", getParameterCmd, 0},
2208 {"glob_match", globMatchCmd, 0},
2209 {"globalState", globalStateCmd, 0},
@@ -2682,19 +2714,22 @@
2682 #if INTERFACE
2683 /*
2684 ** Flags for use with Th_RenderToBlob. These must not overlap with
2685 ** TH_INIT_MASK.
2686 */
2687 #define TH_R2B_MASK ((u32)0xff0000)
2688 #define TH_R2B_NO_VARS ((u32)0x010000) /* Disables eval of $vars and $<vars> */
2689 #endif
2690
2691 /*
2692 ** If pOut is NULL, this works identically to Th_Render(), else it
2693 ** works just like that function but appends any TH1-generated output
2694 ** to the given blob. A bitmask of TH_R2B_xxx and/or TH_INIT_xxx flags
2695 ** may be passed as the 3rd argument, or 0 for default options.
 
 
 
2696 */
2697 int Th_RenderToBlob(const char *z, Blob * pOut, u32 mFlags){
2698 int i = 0;
2699 int n;
2700 int rc = TH_OK;
@@ -2702,11 +2737,11 @@
2702 Blob * const origOut = Th_SetOutputBlob(pOut);
2703
2704 assert(0==(TH_R2B_MASK & TH_INIT_MASK) && "init/r2b mask conflict");
2705 Th_FossilInit(mFlags & TH_INIT_MASK);
2706 while( z[i] ){
2707 if( !(TH_R2B_NO_VARS & mFlags)
2708 && z[i]=='$' && (n = validVarName(&z[i+1]))>0 ){
2709 const char *zVar;
2710 int nVar;
2711 int encode = 1;
2712 sendText(pOut,z, i, 0);
2713
--- src/th_main.c
+++ src/th_main.c
@@ -289,10 +289,41 @@
289 if( g.thTrace ){
290 Th_Trace("enable_output {%.*s} -> %d<br />\n", argl[1],argv[1],enableOutput);
291 }
292 return rc;
293 }
294
295 /*
296 ** TH1 command: enable_htmlify BOOLEAN
297 **
298 ** Enable or disable the HTML escaping done by all output which
299 ** originates from TH1 (via sendText()).
300 */
301 static int enableHtmlifyCmd(
302 Th_Interp *interp,
303 void *p,
304 int argc,
305 const char **argv,
306 int *argl
307 ){
308 int rc, buul;
309 if( argc<2 || argc>3 ){
310 return Th_WrongNumArgs(interp,
311 "enable_htmlify [TRACE_LABEL] BOOLEAN");
312 }
313 rc = Th_ToInt(interp, argv[argc-1], argl[argc-1], &buul);
314 if( g.thTrace ){
315 Th_Trace("enable_htmlify {%.*s} -> %d<br />\n",
316 argl[1],argv[1],buul);
317 }
318 if(buul){
319 g.th1Flags &= ~TH_INIT_NO_ENCODE;
320 }else{
321 g.th1Flags |= TH_INIT_NO_ENCODE;
322 }
323 return rc;
324 }
325
326 /*
327 ** Returns a name for a TH1 return code.
328 */
329 const char *Th_ReturnCodeName(int rc, int nullIfOk){
@@ -2200,10 +2231,11 @@
2231 {"copybtn", copybtnCmd, 0},
2232 {"date", dateCmd, 0},
2233 {"decorate", wikiCmd, (void*)&aFlags[2]},
2234 {"defHeader", defHeaderCmd, 0},
2235 {"dir", dirCmd, 0},
2236 {"enable_htmlify",enableHtmlifyCmd, 0},
2237 {"enable_output", enableOutputCmd, 0},
2238 {"encode64", encode64Cmd, 0},
2239 {"getParameter", getParameterCmd, 0},
2240 {"glob_match", globMatchCmd, 0},
2241 {"globalState", globalStateCmd, 0},
@@ -2682,19 +2714,22 @@
2714 #if INTERFACE
2715 /*
2716 ** Flags for use with Th_RenderToBlob. These must not overlap with
2717 ** TH_INIT_MASK.
2718 */
2719 #define TH_R2B_MASK ((u32)0x0f000)
2720 #define TH_R2B_NO_VARS ((u32)0x01000) /* Disables eval of $vars and $<vars> */
2721 #endif
2722
2723 /*
2724 ** If pOut is NULL, this works identically to Th_Render(), else it
2725 ** works just like that function but appends any TH1-generated output
2726 ** to the given blob. A bitmask of TH_R2B_xxx and/or TH_INIT_xxx flags
2727 ** may be passed as the 3rd argument, or 0 for default options. Note
2728 ** that this function necessarily calls Th_FossilInit(), which may
2729 ** unset flags used on previous calls unless mFlags is explicitly
2730 ** passed in.
2731 */
2732 int Th_RenderToBlob(const char *z, Blob * pOut, u32 mFlags){
2733 int i = 0;
2734 int n;
2735 int rc = TH_OK;
@@ -2702,11 +2737,11 @@
2737 Blob * const origOut = Th_SetOutputBlob(pOut);
2738
2739 assert(0==(TH_R2B_MASK & TH_INIT_MASK) && "init/r2b mask conflict");
2740 Th_FossilInit(mFlags & TH_INIT_MASK);
2741 while( z[i] ){
2742 if( 0!=(TH_R2B_NO_VARS & mFlags)
2743 && z[i]=='$' && (n = validVarName(&z[i+1]))>0 ){
2744 const char *zVar;
2745 int nVar;
2746 int encode = 1;
2747 sendText(pOut,z, i, 0);
2748
+15
--- www/th1.md
+++ www/th1.md
@@ -176,10 +176,11 @@
176176
* [copybtn](#copybtn)
177177
* [date](#date)
178178
* [decorate](#decorate)
179179
* [defHeader](#defHeader)
180180
* [dir](#dir)
181
+ * [enable\_htmlify](#enable_htmlify)
181182
* [enable\_output](#enable_output)
182183
* [encode64](#encode64)
183184
* [getParameter](#getParameter)
184185
* [glob\_match](#glob_match)
185186
* [globalState](#globalState)
@@ -349,10 +350,24 @@
349350
the files matching the pattern GLOB within CHECKIN will be returned.
350351
If DETAILS is non-zero, the result will be a list-of-lists, with each
351352
element containing at least three elements: the file name, the file
352353
size (in bytes), and the file last modification time (relative to the
353354
time zone configured for the repository).
355
+
356
+<a id="enable_htmlify"></a>TH1 enable\_htmlify Command
357
+------------------------------------------------------
358
+
359
+ * enable\_htmlify ?TRACE-LABEL? BOOLEAN
360
+
361
+By default, certain output from `puts` and similar commands is escaped
362
+for HTML. That feature can be toggled off by passing `0` to this
363
+command and re-enabled by passing any non-0 value. The optional
364
+`TRACE-LABEL` argument causes the TH1 tracing output (if enabled) to
365
+add a marker when this command is invoked, and includes that label and
366
+the boolean argument's value in the trace. If tracing is disabled,
367
+that argument has no effect.
368
+
354369
355370
<a id="enable_output"></a>TH1 enable\_output Command
356371
------------------------------------------------------
357372
358373
* enable\_output BOOLEAN
359374
--- www/th1.md
+++ www/th1.md
@@ -176,10 +176,11 @@
176 * [copybtn](#copybtn)
177 * [date](#date)
178 * [decorate](#decorate)
179 * [defHeader](#defHeader)
180 * [dir](#dir)
 
181 * [enable\_output](#enable_output)
182 * [encode64](#encode64)
183 * [getParameter](#getParameter)
184 * [glob\_match](#glob_match)
185 * [globalState](#globalState)
@@ -349,10 +350,24 @@
349 the files matching the pattern GLOB within CHECKIN will be returned.
350 If DETAILS is non-zero, the result will be a list-of-lists, with each
351 element containing at least three elements: the file name, the file
352 size (in bytes), and the file last modification time (relative to the
353 time zone configured for the repository).
 
 
 
 
 
 
 
 
 
 
 
 
 
 
354
355 <a id="enable_output"></a>TH1 enable\_output Command
356 ------------------------------------------------------
357
358 * enable\_output BOOLEAN
359
--- www/th1.md
+++ www/th1.md
@@ -176,10 +176,11 @@
176 * [copybtn](#copybtn)
177 * [date](#date)
178 * [decorate](#decorate)
179 * [defHeader](#defHeader)
180 * [dir](#dir)
181 * [enable\_htmlify](#enable_htmlify)
182 * [enable\_output](#enable_output)
183 * [encode64](#encode64)
184 * [getParameter](#getParameter)
185 * [glob\_match](#glob_match)
186 * [globalState](#globalState)
@@ -349,10 +350,24 @@
350 the files matching the pattern GLOB within CHECKIN will be returned.
351 If DETAILS is non-zero, the result will be a list-of-lists, with each
352 element containing at least three elements: the file name, the file
353 size (in bytes), and the file last modification time (relative to the
354 time zone configured for the repository).
355
356 <a id="enable_htmlify"></a>TH1 enable\_htmlify Command
357 ------------------------------------------------------
358
359 * enable\_htmlify ?TRACE-LABEL? BOOLEAN
360
361 By default, certain output from `puts` and similar commands is escaped
362 for HTML. That feature can be toggled off by passing `0` to this
363 command and re-enabled by passing any non-0 value. The optional
364 `TRACE-LABEL` argument causes the TH1 tracing output (if enabled) to
365 add a marker when this command is invoked, and includes that label and
366 the boolean argument's value in the trace. If tracing is disabled,
367 that argument has no effect.
368
369
370 <a id="enable_output"></a>TH1 enable\_output Command
371 ------------------------------------------------------
372
373 * enable\_output BOOLEAN
374

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button