psql: add asciidoc output format
authorBruce Momjian
Tue, 31 Mar 2015 15:33:25 +0000 (11:33 -0400)
committerBruce Momjian
Tue, 31 Mar 2015 15:33:25 +0000 (11:33 -0400)
Patch by Szymon Guz, adjustments by me

Testing by Michael Paquier, Pavel Stehule

doc/src/sgml/ref/psql-ref.sgml
src/bin/psql/command.c
src/bin/psql/help.c
src/bin/psql/print.c
src/bin/psql/print.h
src/bin/psql/tab-complete.c
src/test/regress/expected/psql.out
src/test/regress/sql/psql.sql

index a33e460bca08dc00d0e34140924545baf3faaf0c..1f29615f833863f3dec9080caa1dfcc982cc98c6 100644 (file)
@@ -2090,7 +2090,7 @@ lo_import 152801
           
           Sets the output format to one of unaligned,
           alignedwrapped,
-          html,
+          html, asciidoc,
           latex (uses tabular),
           latex-longtable, or
           troff-ms.
@@ -2119,7 +2119,7 @@ lo_import 152801
           
 
           
-          The html, latex,
+          The html, asciidoc, latex,
           latex-longtable, and troff-ms
           formats put out tables that are intended to
           be included in documents using the respective mark-up
index e64c033bf8c474e48ef4e71f3a186f7529cb2710..916f1c6301ebaeed30faf26af08e9dcce90351e0 100644 (file)
@@ -2249,6 +2249,9 @@ _align2string(enum printFormat in)
        case PRINT_HTML:
            return "html";
            break;
+       case PRINT_ASCIIDOC:
+           return "asciidoc";
+           break;
        case PRINT_LATEX:
            return "latex";
            break;
@@ -2325,6 +2328,8 @@ do_pset(const char *param, const char *value, printQueryOpt *popt, bool quiet)
            popt->topt.format = PRINT_WRAPPED;
        else if (pg_strncasecmp("html", value, vallen) == 0)
            popt->topt.format = PRINT_HTML;
+       else if (pg_strncasecmp("asciidoc", value, vallen) == 0)
+           popt->topt.format = PRINT_ASCIIDOC;
        else if (pg_strncasecmp("latex", value, vallen) == 0)
            popt->topt.format = PRINT_LATEX;
        else if (pg_strncasecmp("latex-longtable", value, vallen) == 0)
@@ -2333,7 +2338,7 @@ do_pset(const char *param, const char *value, printQueryOpt *popt, bool quiet)
            popt->topt.format = PRINT_TROFF_MS;
        else
        {
-           psql_error("\\pset: allowed formats are unaligned, aligned, wrapped, html, latex, troff-ms\n");
+           psql_error("\\pset: allowed formats are unaligned, aligned, wrapped, html, asciidoc, latex, troff-ms\n");
            return false;
        }
 
index 2da444b6d594b41b0a050cfbf271af3822f0d9f7..f58f5e52f3303d42fa91ffa7c25b9d4e458cb99f 100644 (file)
@@ -351,7 +351,7 @@ helpVariables(unsigned short int pager)
    fprintf(output, _("  expanded (or x)    toggle expanded output\n"));
    fprintf(output, _("  fieldsep           field separator for unaligned output (default '|')\n"));
    fprintf(output, _("  fieldsep_zero      set field separator in unaligned mode to zero\n"));
-   fprintf(output, _("  format             set output format [unaligned, aligned, wrapped, html, latex, ..]\n"));
+   fprintf(output, _("  format             set output format [unaligned, aligned, wrapped, html, asciidoc, ...]\n"));
    fprintf(output, _("  footer             enable or disable display of the table footer [on, off]\n"));
    fprintf(output, _("  linestyle          set the border line drawing style [ascii, old-ascii, unicode]\n"));
    fprintf(output, _("  null               set the string to be printed in place of a null value\n"));
index 9c12dbe049a58b311d79a452d5c0cc66e4110bdd..e97b5635c078cb43e4bc4c88412f18e7cec923e3 100644 (file)
@@ -1878,6 +1878,227 @@ print_html_vertical(const printTableContent *cont, FILE *fout)
 }
 
 
+/*************************/
+/* ASCIIDOC         */
+/*************************/
+
+static void
+asciidoc_escaped_print(const char *in, FILE *fout)
+{
+   const char *p;
+   for (p = in; *p; p++)
+   {
+       switch(*p)
+       {
+           case '|':
+               fputs("\\|", fout);
+               break;
+           default:
+               fputc(*p, fout);
+       }
+   }
+}
+
+static void
+print_asciidoc_text(const printTableContent *cont, FILE *fout)
+{
+   bool        opt_tuples_only = cont->opt->tuples_only;
+   unsigned short opt_border = cont->opt->border;
+   unsigned int i;
+   const char *const * ptr;
+
+   if (cancel_pressed)
+       return;
+
+   if (cont->opt->start_table)
+   {
+       /* print table in new paragraph - enforce preliminary new line */
+       fputs("\n", fout);
+
+       /* print title */
+       if (!opt_tuples_only && cont->title)
+       {
+           fputs(".", fout);
+           fputs(cont->title, fout);
+           fputs("\n", fout);
+       }
+
+       /* print table [] header definition */
+       fprintf(fout, "[%scols=\"", !opt_tuples_only ? "options=\"header\"," : "");
+       for(i = 0; i < cont->ncolumns; i++)
+       {
+           if (i != 0)
+               fputs(",", fout);
+           fprintf(fout, "%s", cont->aligns[(i) % cont->ncolumns] == 'r' ? ">l" : "
+       }
+       fputs("\"", fout);
+       switch (opt_border)
+       {
+           case 0:
+               fputs(",frame=\"none\",grid=\"none\"", fout);
+               break;
+           case 1:
+               fputs(",frame=\"none\"", fout);
+               break;
+           case 2:
+               fputs(",frame=\"all\",grid=\"all\"", fout);
+               break;
+       }
+       fputs("]\n", fout);
+       fputs("|====\n", fout);
+
+       /* print headers */
+       if (!opt_tuples_only)
+       {
+           for (ptr = cont->headers; *ptr; ptr++)
+           {
+               if (ptr != cont->headers)
+                   fputs(" ", fout);
+               fputs("^l|", fout);
+               asciidoc_escaped_print(*ptr, fout);
+           }
+           fputs("\n", fout);
+       }
+   }
+
+   /* print cells */
+   for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
+   {
+       if (i % cont->ncolumns == 0)
+       {
+           if (cancel_pressed)
+               break;
+       }
+
+       if (i % cont->ncolumns != 0)
+           fputs(" ", fout);
+       fputs("|", fout);
+
+       /* protect against needless spaces */
+       if ((*ptr)[strspn(*ptr, " \t")] == '\0')
+       {
+           if ((i + 1) % cont->ncolumns != 0)
+               fputs(" ", fout);
+       }
+       else
+           asciidoc_escaped_print(*ptr, fout);
+
+       if ((i + 1) % cont->ncolumns == 0)
+           fputs("\n", fout);
+   }
+
+   fputs("|====\n", fout);
+
+   if (cont->opt->stop_table)
+   {
+       printTableFooter *footers = footers_with_default(cont);
+
+       /* print footers */
+       if (!opt_tuples_only && footers != NULL && !cancel_pressed)
+       {
+           printTableFooter *f;
+
+           fputs("\n....\n", fout);
+           for (f = footers; f; f = f->next)
+           {
+               fputs(f->data, fout);
+               fputs("\n", fout);
+           }
+           fputs("....\n", fout);
+       }
+   }
+}
+
+static void
+print_asciidoc_vertical(const printTableContent *cont, FILE *fout)
+{
+   bool        opt_tuples_only = cont->opt->tuples_only;
+   unsigned short opt_border = cont->opt->border;
+   unsigned long record = cont->opt->prior_records + 1;
+   unsigned int i;
+   const char *const * ptr;
+
+   if (cancel_pressed)
+       return;
+
+   if (cont->opt->start_table)
+   {
+       /* print table in new paragraph - enforce preliminary new line */
+       fputs("\n", fout);
+
+       /* print title */
+       if (!opt_tuples_only && cont->title)
+       {
+           fputs(".", fout);
+           fputs(cont->title, fout);
+           fputs("\n", fout);
+       }
+
+       /* print table [] header definition */
+       fputs("[cols=\"h,l\"", fout);
+       switch (opt_border)
+       {
+           case 0:
+               fputs(",frame=\"none\",grid=\"none\"", fout);
+               break;
+           case 1:
+               fputs(",frame=\"none\"", fout);
+               break;
+           case 2:
+               fputs(",frame=\"all\",grid=\"all\"", fout);
+           break;
+       }
+       fputs("]\n", fout);
+       fputs("|====\n", fout);
+   }
+
+   /* print records */
+   for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
+   {
+       if (i % cont->ncolumns == 0)
+       {
+           if (cancel_pressed)
+               break;
+           if (!opt_tuples_only)
+               fprintf(fout,
+                       "2+^|Record %lu\n",
+                       record++);
+           else
+               fputs("2+|\n", fout);
+       }
+
+       fputs("
+       asciidoc_escaped_print(cont->headers[i % cont->ncolumns], fout);
+
+       fprintf(fout, " %s|", cont->aligns[i % cont->ncolumns] == 'r' ? ">l" : "
+       /* is string only whitespace? */
+       if ((*ptr)[strspn(*ptr, " \t")] == '\0')
+           fputs(" ", fout);
+       else
+           asciidoc_escaped_print(*ptr, fout);
+       fputs("\n", fout);
+   }
+
+   fputs("|====\n", fout);
+
+   if (cont->opt->stop_table)
+   {
+       /* print footers */
+       if (!opt_tuples_only && cont->footers != NULL && !cancel_pressed)
+       {
+           printTableFooter *f;
+
+           fputs("\n....\n", fout);
+           for (f = cont->footers; f; f = f->next)
+           {
+               fputs(f->data, fout);
+               fputs("\n", fout);
+           }
+           fputs("....\n", fout);
+       }
+   }
+}
+
 /*************************/
 /* LaTeX                */
 /*************************/
@@ -2872,6 +3093,12 @@ printTable(const printTableContent *cont, FILE *fout, FILE *flog)
            else
                print_html_text(cont, fout);
            break;
+       case PRINT_ASCIIDOC:
+           if (cont->opt->expanded == 1)
+               print_asciidoc_vertical(cont, fout);
+           else
+               print_asciidoc_text(cont, fout);
+           break;
        case PRINT_LATEX:
            if (cont->opt->expanded == 1)
                print_latex_vertical(cont, fout);
index 66cdaf32ae743c175b3bfda946ececc1b5975217..322db4d6effb78821f851855db7219f5f9439da7 100644 (file)
@@ -18,6 +18,7 @@ enum printFormat
    PRINT_ALIGNED,
    PRINT_WRAPPED,
    PRINT_HTML,
+   PRINT_ASCIIDOC,
    PRINT_LATEX,
    PRINT_LATEX_LONGTABLE,
    PRINT_TROFF_MS
index e39a07caf133700865ab09522b1beaaf5a12ea16..dfcb66ba6dea54e909f82dd6aad619c8b0eb41c2 100644 (file)
@@ -3790,8 +3790,8 @@ psql_completion(const char *text, int start, int end)
        if (strcmp(prev_wd, "format") == 0)
        {
            static const char *const my_list[] =
-           {"unaligned", "aligned", "wrapped", "html", "latex",
-           "troff-ms", NULL};
+           {"unaligned", "aligned", "wrapped", "html", "asciidoc",
+           "latex", "latex-longtable", "troff-ms", NULL};
 
            COMPLETE_WITH_LIST_CS(my_list);
        }
index a2a261595a6d0d70f05d4d7f1eef9d7ac32711a2..6ff9be1167bbc38c8a007de749c50cbdacfeaff9 100644 (file)
@@ -2126,3 +2126,209 @@ execute q;
 +------------------+-------------------+
 
 deallocate q;
+prepare q as select ' | = | lkjsafi\\/ /oeu rio)(!@&*#)*(!&@*) \ (&' as " | -- | 012345678 9abc def!*@#&!@(*&*~~_+-=\ \", '11' as "0123456789", 11 as int from generate_series(1,10) as n;
+\pset format asciidoc
+\pset expanded off
+\pset border 0
+execute q;
+
+[options="header",cols="l",frame="none",grid="none"]
+|====
+^l| \| -- \| 012345678 9abc def!*@#&!@(*&*~~_+-=\ \ ^l|0123456789 ^l|int
+| \| = \| lkjsafi\\/ /oeu rio)(!@&*#)*(!&@*) \ (& |11 |11
+| \| = \| lkjsafi\\/ /oeu rio)(!@&*#)*(!&@*) \ (& |11 |11
+| \| = \| lkjsafi\\/ /oeu rio)(!@&*#)*(!&@*) \ (& |11 |11
+| \| = \| lkjsafi\\/ /oeu rio)(!@&*#)*(!&@*) \ (& |11 |11
+| \| = \| lkjsafi\\/ /oeu rio)(!@&*#)*(!&@*) \ (& |11 |11
+| \| = \| lkjsafi\\/ /oeu rio)(!@&*#)*(!&@*) \ (& |11 |11
+| \| = \| lkjsafi\\/ /oeu rio)(!@&*#)*(!&@*) \ (& |11 |11
+| \| = \| lkjsafi\\/ /oeu rio)(!@&*#)*(!&@*) \ (& |11 |11
+| \| = \| lkjsafi\\/ /oeu rio)(!@&*#)*(!&@*) \ (& |11 |11
+| \| = \| lkjsafi\\/ /oeu rio)(!@&*#)*(!&@*) \ (& |11 |11
+|====
+
+....
+(10 rows)
+....
+\pset border 1
+execute q;
+
+[options="header",cols="l",frame="none"]
+|====
+^l| \| -- \| 012345678 9abc def!*@#&!@(*&*~~_+-=\ \ ^l|0123456789 ^l|int
+| \| = \| lkjsafi\\/ /oeu rio)(!@&*#)*(!&@*) \ (& |11 |11
+| \| = \| lkjsafi\\/ /oeu rio)(!@&*#)*(!&@*) \ (& |11 |11
+| \| = \| lkjsafi\\/ /oeu rio)(!@&*#)*(!&@*) \ (& |11 |11
+| \| = \| lkjsafi\\/ /oeu rio)(!@&*#)*(!&@*) \ (& |11 |11
+| \| = \| lkjsafi\\/ /oeu rio)(!@&*#)*(!&@*) \ (& |11 |11
+| \| = \| lkjsafi\\/ /oeu rio)(!@&*#)*(!&@*) \ (& |11 |11
+| \| = \| lkjsafi\\/ /oeu rio)(!@&*#)*(!&@*) \ (& |11 |11
+| \| = \| lkjsafi\\/ /oeu rio)(!@&*#)*(!&@*) \ (& |11 |11
+| \| = \| lkjsafi\\/ /oeu rio)(!@&*#)*(!&@*) \ (& |11 |11
+| \| = \| lkjsafi\\/ /oeu rio)(!@&*#)*(!&@*) \ (& |11 |11
+|====
+
+....
+(10 rows)
+....
+\pset border 2
+execute q;
+
+[options="header",cols="l",frame="all",grid="all"]
+|====
+^l| \| -- \| 012345678 9abc def!*@#&!@(*&*~~_+-=\ \ ^l|0123456789 ^l|int
+| \| = \| lkjsafi\\/ /oeu rio)(!@&*#)*(!&@*) \ (& |11 |11
+| \| = \| lkjsafi\\/ /oeu rio)(!@&*#)*(!&@*) \ (& |11 |11
+| \| = \| lkjsafi\\/ /oeu rio)(!@&*#)*(!&@*) \ (& |11 |11
+| \| = \| lkjsafi\\/ /oeu rio)(!@&*#)*(!&@*) \ (& |11 |11
+| \| = \| lkjsafi\\/ /oeu rio)(!@&*#)*(!&@*) \ (& |11 |11
+| \| = \| lkjsafi\\/ /oeu rio)(!@&*#)*(!&@*) \ (& |11 |11
+| \| = \| lkjsafi\\/ /oeu rio)(!@&*#)*(!&@*) \ (& |11 |11
+| \| = \| lkjsafi\\/ /oeu rio)(!@&*#)*(!&@*) \ (& |11 |11
+| \| = \| lkjsafi\\/ /oeu rio)(!@&*#)*(!&@*) \ (& |11 |11
+| \| = \| lkjsafi\\/ /oeu rio)(!@&*#)*(!&@*) \ (& |11 |11
+|====
+
+....
+(10 rows)
+....
+\pset expanded on
+\pset border 0
+execute q;
+
+[cols="h,l",frame="none",grid="none"]
+|====
+2+^|Record 1
+
+
+l|11
+2+^|Record 2
+
+
+l|11
+2+^|Record 3
+
+
+l|11
+2+^|Record 4
+
+
+l|11
+2+^|Record 5
+
+
+l|11
+2+^|Record 6
+
+
+l|11
+2+^|Record 7
+
+
+l|11
+2+^|Record 8
+
+
+l|11
+2+^|Record 9
+
+
+l|11
+2+^|Record 10
+
+
+l|11
+|====
+\pset border 1
+execute q;
+
+[cols="h,l",frame="none"]
+|====
+2+^|Record 1
+
+
+l|11
+2+^|Record 2
+
+
+l|11
+2+^|Record 3
+
+
+l|11
+2+^|Record 4
+
+
+l|11
+2+^|Record 5
+
+
+l|11
+2+^|Record 6
+
+
+l|11
+2+^|Record 7
+
+
+l|11
+2+^|Record 8
+
+
+l|11
+2+^|Record 9
+
+
+l|11
+2+^|Record 10
+
+
+l|11
+|====
+\pset border 2
+execute q;
+
+[cols="h,l",frame="all",grid="all"]
+|====
+2+^|Record 1
+
+
+l|11
+2+^|Record 2
+
+
+l|11
+2+^|Record 3
+
+
+l|11
+2+^|Record 4
+
+
+l|11
+2+^|Record 5
+
+
+l|11
+2+^|Record 6
+
+
+l|11
+2+^|Record 7
+
+
+l|11
+2+^|Record 8
+
+
+l|11
+2+^|Record 9
+
+
+l|11
+2+^|Record 10
+
+
+l|11
+|====
+deallocate q;
index 5ccc68f4ed37be712a650aa4fa71eb7c70a1bb4f..fa1df8b2d72daddfade0233e6d2b7eeab9e9b30d 100644 (file)
@@ -276,3 +276,28 @@ execute q;
 execute q;
 
 deallocate q;
+
+prepare q as select ' | = | lkjsafi\\/ /oeu rio)(!@&*#)*(!&@*) \ (&' as " | -- | 012345678 9abc def!*@#&!@(*&*~~_+-=\ \", '11' as "0123456789", 11 as int from generate_series(1,10) as n;
+
+\pset format asciidoc
+\pset expanded off
+\pset border 0
+execute q;
+
+\pset border 1
+execute q;
+
+\pset border 2
+execute q;
+
+\pset expanded on
+\pset border 0
+execute q;
+
+\pset border 1
+execute q;
+
+\pset border 2
+execute q;
+
+deallocate q;