[PATCH] Add preview+cancel buttons to edit Add preview and cancel functionality when editing and creating pages. Signed-off-by: Peter Korsgaard --- src/wiki.c | 92 +++++++++++++++++++++++++++++++++++---------------------- src/wikitext.h | 11 +++++- 2 files changed, 66 insertions(+), 37 deletions(-) Index: didiwiki/src/wiki.c =================================================================== --- didiwiki.orig/src/wiki.c +++ didiwiki/src/wiki.c @@ -591,13 +591,27 @@ } void -wiki_show_edit_page(HttpResponse *res, char *wikitext, char *page) +wiki_show_edit_page(HttpResponse *res, char *wikitext, char *page, + int preview) { + wiki_show_header(res, page, FALSE); if (wikitext == NULL) wikitext = ""; http_response_printf(res, EDITFORM, page, wikitext); - + + if (preview) + { + char *html_clean_wikitext; + + http_response_printf(res, EDITPREVIEW); + http_response_printf_alloc_buffer(res, strlen(wikitext)*2); + + html_clean_wikitext = util_htmlize(wikitext, strlen(wikitext)); + + wiki_print_data_as_html(res, html_clean_wikitext); + } + wiki_show_footer(res); http_response_send(res); @@ -957,7 +971,7 @@ HttpResponse *res = http_response_new(req); char *page = http_request_get_path_info(req); char *command = http_request_get_query_string(req); - char *wikitext = ""; + char *wikitext = NULL; util_dehttpize(page); /* remove any encoding on the requested page name. */ @@ -1040,40 +1054,48 @@ } else { - /* TODO: dont blindly write wikitext data to disk */ - if ( (wikitext = http_request_param_get(req, "wikitext")) != NULL) - { - file_write(page, wikitext); - } - - if (access(page, R_OK) == 0) /* page exists */ - { - wikitext = file_read(page); - - if (!strcmp(command, "edit")) - { - /* print edit page */ - wiki_show_edit_page(res, wikitext, page); - } - else - { - wiki_show_page(res, wikitext, page); - } - } - else - { - if (!strcmp(command, "create")) - { - wiki_show_edit_page(res, NULL, page); - } + if (access(page, R_OK) == 0) /* page exists */ + { + wikitext = file_read(page); + } + + if (!strcmp(command, "edit") || !strcmp(command, "create")) + { + char *newtext = http_request_param_get(req, "wikitext"); + + if (http_request_param_get(req, "save") && newtext) + { + file_write(page, newtext); + wiki_redirect(res, page); + } + else if (http_request_param_get(req, "cancel")) + { + wiki_redirect(res, page); + } + + if (http_request_param_get(req, "preview")) + { + wiki_show_edit_page(res, newtext, page, TRUE); + } + else + { + wiki_show_edit_page(res, wikitext, page, FALSE); + } + } else - { - char buf[1024]; - snprintf(buf, 1024, "%s?create", page); - wiki_redirect(res, buf); - } + { + if (wikitext) + { + wiki_show_page(res, wikitext, page); + } + else + { + char buf[1024]; + snprintf(buf, sizeof(buf), "%s?create", page); + wiki_redirect(res, buf); + } + } } - } } Index: didiwiki/src/wikitext.h =================================================================== --- didiwiki.orig/src/wikitext.h +++ didiwiki/src/wikitext.h @@ -53,9 +53,11 @@ #define EDITFORM \ \ -"
\n" \ +"\n" \ "\n" \ -"

\n" \ +"

" \ +"" \ +"

\n" \ "
\n" \ "\n" +#define EDITPREVIEW \ + \ +"
Preview:
\n" + + #define HOMETEXT "" \ \ "==Welcome to !DidiWiki\n" \