v2-0001-psql-Add-documentation-URL-to-help-output.patch

text/plain

Filename: v2-0001-psql-Add-documentation-URL-to-help-output.patch
Type: text/plain
Part: 0
Message: Re: psql show URL with help

Patch

Format: format-patch
Series: patch v2-0001
Subject: psql: Add documentation URL to \help output
File+
src/bin/psql/create_help.pl 8 2
src/bin/psql/help.c 9 2
From 8268421dd39f413e21ce3e935eee1d0b5e607db2 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter@eisentraut.org>
Date: Wed, 27 Feb 2019 09:09:21 +0100
Subject: [PATCH v2] psql: Add documentation URL to \help output

---
 src/bin/psql/create_help.pl | 10 ++++++++--
 src/bin/psql/help.c         | 11 +++++++++--
 2 files changed, 17 insertions(+), 4 deletions(-)

diff --git a/src/bin/psql/create_help.pl b/src/bin/psql/create_help.pl
index 314bb5d87c..e8dd90d671 100644
--- a/src/bin/psql/create_help.pl
+++ b/src/bin/psql/create_help.pl
@@ -64,6 +64,7 @@
 {
 	const char	   *cmd;		/* the command name */
 	const char	   *help;		/* the help associated with it */
+	const char	   *docbook_id;	/* DocBook XML id (for generating URL) */
 	void (*syntaxfunc)(PQExpBuffer);	/* function that prints the syntax associated with it */
 	int				nl_count;	/* number of newlines in syntax (for pager) */
 };
@@ -92,7 +93,7 @@
 
 foreach my $file (sort readdir DIR)
 {
-	my (@cmdnames, $cmddesc, $cmdsynopsis);
+	my ($cmdid, @cmdnames, $cmddesc, $cmdsynopsis);
 	$file =~ /\.sgml$/ or next;
 
 	open(my $fh, '<', "$docdir/$file") or next;
@@ -104,6 +105,9 @@
 	  m!<refmiscinfo>\s*SQL - Language Statements\s*</refmiscinfo>!i
 	  or next;
 
+	$filecontent =~ m!<refentry id="([a-z-]+)">!
+	  and $cmdid = $1;
+
 	# Collect multiple refnames
   LOOP:
 	{
@@ -116,7 +120,7 @@
 	$filecontent =~ m!<synopsis>\s*(.+?)\s*</synopsis>!is
 	  and $cmdsynopsis = $1;
 
-	if (@cmdnames && $cmddesc && $cmdsynopsis)
+	if (@cmdnames && $cmddesc && $cmdid && $cmdsynopsis)
 	{
 		s/\"/\\"/g foreach @cmdnames;
 
@@ -146,6 +150,7 @@
 		foreach my $cmdname (@cmdnames)
 		{
 			$entries{$cmdname} = {
+				cmdid       => $cmdid,
 				cmddesc     => $cmddesc,
 				cmdsynopsis => $cmdsynopsis,
 				params      => \@params,
@@ -188,6 +193,7 @@
 	$id =~ s/ /_/g;
 	print $cfile_handle "    { \"$_\",
       N_(\"$entries{$_}{cmddesc}\"),
+      \"$entries{$_}{cmdid}\",
       sql_help_$id,
       $entries{$_}{nl_count} },
 
diff --git a/src/bin/psql/help.c b/src/bin/psql/help.c
index 7c6fa2c590..6b781afc6c 100644
--- a/src/bin/psql/help.c
+++ b/src/bin/psql/help.c
@@ -621,16 +621,23 @@ helpSQL(const char *topic, unsigned short int pager)
 					strcmp(topic, "*") == 0)
 				{
 					PQExpBufferData buffer;
+					char	   *url;
 
 					initPQExpBuffer(&buffer);
 					QL_HELP[i].syntaxfunc(&buffer);
 					help_found = true;
+					url = psprintf("https://www.postgresql.org/docs/%s/%s.html",
+								   strstr(PG_VERSION, "devel") ? "devel" : PG_MAJORVERSION,
+								   QL_HELP[i].docbook_id);
 					fprintf(output, _("Command:     %s\n"
 									  "Description: %s\n"
-									  "Syntax:\n%s\n\n"),
+									  "Syntax:\n%s\n\n"
+									  "URL: %s\n\n"),
 							QL_HELP[i].cmd,
 							_(QL_HELP[i].help),
-							buffer.data);
+							buffer.data,
+							url);
+					free(url);
 					/* If we have an exact match, exit.  Fixes \h SELECT */
 					if (pg_strcasecmp(topic, QL_HELP[i].cmd) == 0)
 						break;

base-commit: bc09d5e4cc1813c9af60c4537fe7d70ed1baae11
-- 
2.20.1