Fossil SCM

Add a utility methods to translate mimetype --flag values consistently and determine mimetype from file extension. Have wiki_filter_mimetypes() accept both short- and long-form names, plus the special case 'plaintext' to account for an inconstency. Have (forum new --content X) determine the mimetype from X's name if --markup is not provided or fail if it cannot.

stephan 2026-11-19 16:31 UTC trunk
Commit dc3dbcdca88b8111a560c215b58cd4f1bb374f2143258deda6426cfd6bafc838
2 files changed +15 -7 +41 -12
+15 -7
--- src/forum.c
+++ src/forum.c
@@ -3471,19 +3471,27 @@
34713471
fossil_fatal("the --title option is required");
34723472
}
34733473
if( zFile==0 ){
34743474
fossil_fatal("the --content option is required");
34753475
}
3476
- if( zMarkup==0 || fossil_strcmp(zMarkup,"markdown")==0 ){
3477
- zMimetype = "text/x-markdown";
3478
- }else if( fossil_strcmp(zMarkup,"wiki")==0 ){
3479
- zMimetype = 0;
3480
- }else if( fossil_strcmp(zMarkup,"plaintext")==0 ){
3481
- zMimetype = "text/plain";
3482
- }else{
3476
+ if( zMarkup==0 ){
3477
+ zMimetype = wiki_guess_filename_mimetype(zFile, 0);
3478
+ if( !zMimetype ){
3479
+ fossil_fatal("Either use --markup <type> or a --content "
3480
+ "filename extension of (.md, .txt, .wiki) "
3481
+ "to specify the mime type.");
3482
+ zMimetype = DEFAULT_FORUM_MIMETYPE;
3483
+ }
3484
+ }else if( !wiki_is_valid_mimetype(zMarkup) ){
34833485
fossil_fatal("unknown --markup \"%s\". Should be one of: "
34843486
"markdown, wiki, plaintext", zMarkup);
3487
+ }else{
3488
+ zMimetype = wiki_filter_mimetypes(zMarkup)/*normalize short names*/;
3489
+ assert( zMimetype );
3490
+ }
3491
+ if( 0==fossil_strcmp(zMimetype, "text/x-fossil-wiki") ){
3492
+ zMimetype = 0 /* avoid setting N-card */;
34853493
}
34863494
if( fossil_strcmp(zFile,"-")==0 ){
34873495
blob_read_from_channel(&content, stdin, -1);
34883496
}else{
34893497
blob_read_from_file(&content, zFile, ExtFILE);
34903498
--- src/forum.c
+++ src/forum.c
@@ -3471,19 +3471,27 @@
3471 fossil_fatal("the --title option is required");
3472 }
3473 if( zFile==0 ){
3474 fossil_fatal("the --content option is required");
3475 }
3476 if( zMarkup==0 || fossil_strcmp(zMarkup,"markdown")==0 ){
3477 zMimetype = "text/x-markdown";
3478 }else if( fossil_strcmp(zMarkup,"wiki")==0 ){
3479 zMimetype = 0;
3480 }else if( fossil_strcmp(zMarkup,"plaintext")==0 ){
3481 zMimetype = "text/plain";
3482 }else{
 
 
3483 fossil_fatal("unknown --markup \"%s\". Should be one of: "
3484 "markdown, wiki, plaintext", zMarkup);
 
 
 
 
 
 
3485 }
3486 if( fossil_strcmp(zFile,"-")==0 ){
3487 blob_read_from_channel(&content, stdin, -1);
3488 }else{
3489 blob_read_from_file(&content, zFile, ExtFILE);
3490
--- src/forum.c
+++ src/forum.c
@@ -3471,19 +3471,27 @@
3471 fossil_fatal("the --title option is required");
3472 }
3473 if( zFile==0 ){
3474 fossil_fatal("the --content option is required");
3475 }
3476 if( zMarkup==0 ){
3477 zMimetype = wiki_guess_filename_mimetype(zFile, 0);
3478 if( !zMimetype ){
3479 fossil_fatal("Either use --markup <type> or a --content "
3480 "filename extension of (.md, .txt, .wiki) "
3481 "to specify the mime type.");
3482 zMimetype = DEFAULT_FORUM_MIMETYPE;
3483 }
3484 }else if( !wiki_is_valid_mimetype(zMarkup) ){
3485 fossil_fatal("unknown --markup \"%s\". Should be one of: "
3486 "markdown, wiki, plaintext", zMarkup);
3487 }else{
3488 zMimetype = wiki_filter_mimetypes(zMarkup)/*normalize short names*/;
3489 assert( zMimetype );
3490 }
3491 if( 0==fossil_strcmp(zMimetype, "text/x-fossil-wiki") ){
3492 zMimetype = 0 /* avoid setting N-card */;
3493 }
3494 if( fossil_strcmp(zFile,"-")==0 ){
3495 blob_read_from_channel(&content, stdin, -1);
3496 }else{
3497 blob_read_from_file(&content, zFile, ExtFILE);
3498
+41 -12
--- src/wiki.c
+++ src/wiki.c
@@ -168,27 +168,56 @@
168168
"text/x-markdown", "Markdown", "markdown",
169169
"text/plain", "Plain Text", "plain"
170170
};
171171
172172
/*
173
-** Only allow certain mimetypes through.
174
-** All others become "text/x-fossil-wiki"
173
+** If zMimetype is one of the supported wiki content mimetypes or a
174
+** shorthand form of it, returns the full mimetype string, else
175
+** returns 0.
176
+*/
177
+const char * wiki_is_valid_mimetype(const char *zMimetype){
178
+ int i;
179
+ for(i=0; i<count(azStyles); i+=3){
180
+ if( fossil_strcmp(zMimetype,azStyles[i+2])==0
181
+ || fossil_strcmp(zMimetype,azStyles[i])==0 ){
182
+ return azStyles[i];
183
+ }
184
+ }
185
+ if( fossil_strcmp(zMimetype, "text/x-markdown")==0
186
+ || fossil_strcmp(zMimetype, "text/plain")==0 ){
187
+ return zMimetype;
188
+ }
189
+ if( fossil_strcmp(zMimetype, "plaintext")==0 ){
190
+ /* Inconsistent "plain" vs "plaintext" in some commands/API. */
191
+ return "text/plain";
192
+ }
193
+ return 0;
194
+}
195
+
196
+/*
197
+** Only allow certain mimetypes through. All others become
198
+** "text/x-fossil-wiki". See wiki_is_valid_mimetype().
175199
*/
176200
const char *wiki_filter_mimetypes(const char *zMimetype){
177201
if( zMimetype!=0 ){
178
- int i;
179
- for(i=0; i<count(azStyles); i+=3){
180
- if( fossil_strcmp(zMimetype,azStyles[i+2])==0 ){
181
- return azStyles[i];
182
- }
183
- }
184
- if( fossil_strcmp(zMimetype, "text/x-markdown")==0
185
- || fossil_strcmp(zMimetype, "text/plain")==0 ){
186
- return zMimetype;
202
+ zMimetype = wiki_is_valid_mimetype(zMimetype);
203
+ }
204
+ return zMimetype ? zMimetype : "text/x-fossil-wiki";
205
+}
206
+
207
+char const *wiki_guess_filename_mimetype(const char *zFilename,
208
+ const char *zDefault){
209
+ if( zFilename ){
210
+ if( 0==sqlite3_strglob("*.md", zFilename) ){
211
+ return "text/x-markdown";
212
+ }else if( 0==sqlite3_strglob("*.txt", zFilename) ){
213
+ return "text/plain";
214
+ }else if( 0==sqlite3_strglob("*.wiki", zFilename) ){
215
+ return "text/x-fossil-wiki";
187216
}
188217
}
189
- return "text/x-fossil-wiki";
218
+ return 0;
190219
}
191220
192221
/*
193222
** Render wiki text according to its mimetype.
194223
**
195224
--- src/wiki.c
+++ src/wiki.c
@@ -168,27 +168,56 @@
168 "text/x-markdown", "Markdown", "markdown",
169 "text/plain", "Plain Text", "plain"
170 };
171
172 /*
173 ** Only allow certain mimetypes through.
174 ** All others become "text/x-fossil-wiki"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
175 */
176 const char *wiki_filter_mimetypes(const char *zMimetype){
177 if( zMimetype!=0 ){
178 int i;
179 for(i=0; i<count(azStyles); i+=3){
180 if( fossil_strcmp(zMimetype,azStyles[i+2])==0 ){
181 return azStyles[i];
182 }
183 }
184 if( fossil_strcmp(zMimetype, "text/x-markdown")==0
185 || fossil_strcmp(zMimetype, "text/plain")==0 ){
186 return zMimetype;
 
 
 
 
 
187 }
188 }
189 return "text/x-fossil-wiki";
190 }
191
192 /*
193 ** Render wiki text according to its mimetype.
194 **
195
--- src/wiki.c
+++ src/wiki.c
@@ -168,27 +168,56 @@
168 "text/x-markdown", "Markdown", "markdown",
169 "text/plain", "Plain Text", "plain"
170 };
171
172 /*
173 ** If zMimetype is one of the supported wiki content mimetypes or a
174 ** shorthand form of it, returns the full mimetype string, else
175 ** returns 0.
176 */
177 const char * wiki_is_valid_mimetype(const char *zMimetype){
178 int i;
179 for(i=0; i<count(azStyles); i+=3){
180 if( fossil_strcmp(zMimetype,azStyles[i+2])==0
181 || fossil_strcmp(zMimetype,azStyles[i])==0 ){
182 return azStyles[i];
183 }
184 }
185 if( fossil_strcmp(zMimetype, "text/x-markdown")==0
186 || fossil_strcmp(zMimetype, "text/plain")==0 ){
187 return zMimetype;
188 }
189 if( fossil_strcmp(zMimetype, "plaintext")==0 ){
190 /* Inconsistent "plain" vs "plaintext" in some commands/API. */
191 return "text/plain";
192 }
193 return 0;
194 }
195
196 /*
197 ** Only allow certain mimetypes through. All others become
198 ** "text/x-fossil-wiki". See wiki_is_valid_mimetype().
199 */
200 const char *wiki_filter_mimetypes(const char *zMimetype){
201 if( zMimetype!=0 ){
202 zMimetype = wiki_is_valid_mimetype(zMimetype);
203 }
204 return zMimetype ? zMimetype : "text/x-fossil-wiki";
205 }
206
207 char const *wiki_guess_filename_mimetype(const char *zFilename,
208 const char *zDefault){
209 if( zFilename ){
210 if( 0==sqlite3_strglob("*.md", zFilename) ){
211 return "text/x-markdown";
212 }else if( 0==sqlite3_strglob("*.txt", zFilename) ){
213 return "text/plain";
214 }else if( 0==sqlite3_strglob("*.wiki", zFilename) ){
215 return "text/x-fossil-wiki";
216 }
217 }
218 return 0;
219 }
220
221 /*
222 ** Render wiki text according to its mimetype.
223 **
224

Keyboard Shortcuts

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