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.
Commit
dc3dbcdca88b8111a560c215b58cd4f1bb374f2143258deda6426cfd6bafc838
Parent
80d48144157f55b…
2 files changed
+15
-7
+41
-12
+15
-7
| --- src/forum.c | ||
| +++ src/forum.c | ||
| @@ -3471,19 +3471,27 @@ | ||
| 3471 | 3471 | fossil_fatal("the --title option is required"); |
| 3472 | 3472 | } |
| 3473 | 3473 | if( zFile==0 ){ |
| 3474 | 3474 | fossil_fatal("the --content option is required"); |
| 3475 | 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{ | |
| 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) ){ | |
| 3483 | 3485 | fossil_fatal("unknown --markup \"%s\". Should be one of: " |
| 3484 | 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 */; | |
| 3485 | 3493 | } |
| 3486 | 3494 | if( fossil_strcmp(zFile,"-")==0 ){ |
| 3487 | 3495 | blob_read_from_channel(&content, stdin, -1); |
| 3488 | 3496 | }else{ |
| 3489 | 3497 | blob_read_from_file(&content, zFile, ExtFILE); |
| 3490 | 3498 |
| --- 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 @@ | ||
| 168 | 168 | "text/x-markdown", "Markdown", "markdown", |
| 169 | 169 | "text/plain", "Plain Text", "plain" |
| 170 | 170 | }; |
| 171 | 171 | |
| 172 | 172 | /* |
| 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(). | |
| 175 | 199 | */ |
| 176 | 200 | const char *wiki_filter_mimetypes(const char *zMimetype){ |
| 177 | 201 | 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"; | |
| 187 | 216 | } |
| 188 | 217 | } |
| 189 | - return "text/x-fossil-wiki"; | |
| 218 | + return 0; | |
| 190 | 219 | } |
| 191 | 220 | |
| 192 | 221 | /* |
| 193 | 222 | ** Render wiki text according to its mimetype. |
| 194 | 223 | ** |
| 195 | 224 |
| --- 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 |