Skip to content

Commit 882c7f2

Browse files
committed
Merge pull request #56 from smackesey/tsv-output
Switch to Tab-Separated Values (TSV) format in dasht-query-line(1) and dasht-query-html(1) for easier line-by-line parsing and user scripting.
2 parents eb5e50a + c67cc7d commit 882c7f2

File tree

3 files changed

+53
-47
lines changed

3 files changed

+53
-47
lines changed

bin/dasht-query-html

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -111,44 +111,50 @@ trap 'exit 44' USR1 # exit with a nonzero status when no results found
111111
gsub("[[:space:]]+", ".*", pattern) # treat whitespace as wildcards
112112
pattern = ignorecase(pattern) # emulate IGNORECASE=1 for POSIX
113113
if (pattern == "") pattern = "^." # grouped by leading character
114+
FS = "\t" # fields are separated by tabs
114115
}
115-
NR == 1 { print "<table>" }
116-
$2 == "=" { result[$1] = substr($0, index($0, $2) + length($2) + 1) }
117-
$1 == "from" { result["from"] = wordbreak_cached(result["from"], "<wbr>") }
118-
$1 == "name" {
116+
NR == 1 {
117+
print "<table>"
118+
}
119+
{
120+
name = $1
121+
from = wordbreak_cached($2, "<wbr>")
122+
type = $3
123+
url = $4
124+
119125
# mark search terms with STX and ETX bytes which are ignored by escape()
120126
if (pattern) {
121-
gsub(pattern, "\002&\003", result["name"])
127+
gsub(pattern, "\002&\003", name)
122128
}
123129
124130
# mark word-wrappable points with VT bytes which are ignored by escape()
125-
result["name"] = wordbreak(result["name"], "\v", "\002\003")
131+
name = wordbreak(name, "\v", "\002\003")
126132
127133
# escape XML entities in search result to make them visible in browsers
128-
result["name"] = escape(result["name"])
134+
name = escape(name)
129135
130136
# insert word-break opportunity <wbr> tags at points marked by VT bytes
131-
gsub("\v", "<wbr>", result["name"])
137+
gsub("\v", "<wbr>", name)
132138
133139
# highlight search terms in search result using the STX and ETX markers
134140
if (pattern) {
135-
gsub("\002", "<b><i><u>", result["name"])
136-
gsub("\003", "</u></i></b>", result["name"])
141+
gsub("\002", "<b><i><u>", name)
142+
gsub("\003", "</u></i></b>", name)
137143
}
138-
}
139-
$1 == "url" { print \
140-
"<tr>"\
141-
"<td><a href=\"" result["url"] "\">" result["name"] "</a></td>"\
142-
"<td valign=\"bottom\" align=\"right\">" result["from"] "</td>"\
143-
"<td valign=\"bottom\">" tolower(result["type"]) "</td>"\
144-
"</tr>"
144+
145+
print \
146+
"<tr>"\
147+
"<td><a href=\"" url "\">" name "</a></td>"\
148+
"<td valign=\"bottom\" align=\"right\">" from "</td>"\
149+
"<td valign=\"bottom\">" tolower(type) "</td>"\
150+
"</tr>"
145151
}
146152
END {
147153
if (NR > 0) {
148154
print "</table>"
149-
if (NR == 4) {
155+
if (NR == 1) {
150156
# there was only one search result, so automatically visit its url
151-
print "<meta http-equiv=\"refresh\" content=\"0;url=" result["url"] "\">"
157+
print "<meta http-equiv=\"refresh\" content=\"0;url=" url "\">"
152158
}
153159
}
154160
}

bin/dasht-query-line

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#
55
# ## NAME
66
#
7-
# dasht-query-line - searches [Dash] docsets and emits groups of lines
7+
# dasht-query-line - searches [Dash] docsets and emits TSV table rows
88
#
99
# ## SYNOPSIS
1010
#
@@ -28,7 +28,7 @@
2828
#
2929
# Searches for *PATTERN* in all installed [Dash] docsets, optionally searching
3030
# only in those whose names match *DOCSET*s, by calling dasht-query-exec(1)
31-
# and emits the results in groups of lines, as described in "Results" below.
31+
# and emits the results, one per line, in Tab-Separated Values (TSV) format.
3232
# However, if no results were found, this program exits with a nonzero status.
3333
#
3434
# ### Searching
@@ -42,26 +42,24 @@
4242
#
4343
# ### Results
4444
#
45-
# Each search result is printed to stdout as a group of four lines of text:
45+
# Each search result is printed stdout as a line with 4 tab-separated fields:
4646
#
47-
# `name` `=` *VALUE*
47+
# `name`
4848
# Name of the token that matched the *PATTERN*.
4949
#
50-
# `type` `=` *VALUE*
50+
# `type`
5151
# Type of the token, as defined in the docset.
5252
#
53-
# `from` `=` *VALUE*
53+
# `from`
5454
# Name of the docset this result was found in.
5555
#
56-
# `url` `=` *VALUE*
56+
# `url`
5757
# URL of the API documentation for this result.
5858
#
59-
# For example, here is a search result for "c - x" from the "bash" docset:
59+
# For example, here is a search result for "c - x" from the "bash" docset,
60+
# with the tab separators represented by `<TAB>` for illustrative purposes:
6061
#
61-
# name = undo (C-_ or C-x C-u)
62-
# type = Function
63-
# from = Bash
64-
# url = file:///home/sunny/.local/share/dasht/docsets/Bash.docset/Contents/Resources/Documents/bash/Miscellaneous-Commands.html#//apple_ref/Function/undo%20%28C%2D%5F%20or%20C%2Dx%20C%2Du%29
62+
# undo (C-_ or C-x C-u)<TAB>Function<TAB>Bash<TAB>file:///home/sunny/.local/share/dasht/docsets/Bash.docset/Contents/Resources/Documents/bash/Miscellaneous-Commands.html#//apple_ref/Function/undo%20%28C%2D%5F%20or%20C%2Dx%20C%2Du%29
6563
#
6664
# ## ENVIRONMENT
6765
#
@@ -245,9 +243,12 @@ dasht-docsets "$@" | while read -r docset; do
245243
246244
{ $1 = $1 } # strip whitespace from key
247245
246+
$2 == "=" {
247+
# skip over the first 2 fields and grab the rest of the line
248+
result[$1] = substr($0, 1 + length($1) + 1 + length($2) + 1)
249+
}
250+
248251
$1 == "url" { were_any_results_found=1
249-
# indicate the source of this result
250-
print "from = " docset
251252
252253
# strip embedded XML from result URL
253254
gsub("<.*>", "", $3)
@@ -259,9 +260,10 @@ dasht-docsets "$@" | while read -r docset; do
259260
260261
# resolve URL to filesystem location
261262
$3 = file_url $3
262-
}
263263
264-
/./ # reject any empty lines from input
264+
printf("%s\t%s\t%s\t%s\n", result["name"], docset, result["type"], $3)
265+
266+
}
265267
266268
END { exit !were_any_results_found }
267269
' && kill -s USR1 $$ || : # notify this script if any results were found

man/man1/dasht-query-line.1

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
.TH DASHT\-QUERY\-LINE 1 2020\-05\-16 2.4.0
22
.SH NAME
33
.PP
4-
dasht\-query\-line \- searches Dash \[la]https://kapeli.com/dash\[ra] docsets and emits groups of lines
4+
dasht\-query\-line \- searches Dash \[la]https://kapeli.com/dash\[ra] docsets and emits TSV table rows
55
.SH SYNOPSIS
66
.PP
77
\fB\fCdasht\-query\-line\fR [\fIPATTERN\fP] [\fIDOCSET\fP]...
@@ -24,7 +24,7 @@ Searches for \fIPATTERN\fP in all installed Dash \[la]https://kapeli.com/dash\[r
2424
only in those whose names match \fIDOCSET\fPs, by calling
2525
.BR dasht-query-exec (1)
2626

27-
and emits the results in groups of lines, as described in "Results" below.
27+
and emits the results, one per line, in Tab\-Separated Values (TSV) format.
2828
However, if no results were found, this program exits with a nonzero status.
2929
.SS Searching
3030
.PP
@@ -36,28 +36,26 @@ can match anywhere: beginning, middle, or end. As a result, if \fIPATTERN\fP is
3636
undefined, it becomes a whitespace wildcard and thereby matches everything.
3737
.SS Results
3838
.PP
39-
Each search result is printed to stdout as a group of four lines of text:
39+
Each search result is printed stdout as a line with 4 tab\-separated fields:
4040
.TP
41-
\fB\fCname\fR \fB\fC=\fR \fIVALUE\fP
41+
\fB\fCname\fR
4242
Name of the token that matched the \fIPATTERN\fP\&.
4343
.TP
44-
\fB\fCtype\fR \fB\fC=\fR \fIVALUE\fP
44+
\fB\fCtype\fR
4545
Type of the token, as defined in the docset.
4646
.TP
47-
\fB\fCfrom\fR \fB\fC=\fR \fIVALUE\fP
47+
\fB\fCfrom\fR
4848
Name of the docset this result was found in.
4949
.TP
50-
\fB\fCurl\fR \fB\fC=\fR \fIVALUE\fP
50+
\fB\fCurl\fR
5151
URL of the API documentation for this result.
5252
.PP
53-
For example, here is a search result for "c \- x" from the "bash" docset:
53+
For example, here is a search result for "c \- x" from the "bash" docset,
54+
with the tab separators represented by \fB\fC<TAB>\fR for illustrative purposes:
5455
.PP
5556
.RS
5657
.nf
57-
name = undo (C\-_ or C\-x C\-u)
58-
type = Function
59-
from = Bash
60-
url = file:///home/sunny/.local/share/dasht/docsets/Bash.docset/Contents/Resources/Documents/bash/Miscellaneous\-Commands.html#//apple_ref/Function/undo%20%28C%2D%5F%20or%20C%2Dx%20C%2Du%29
58+
undo (C\-_ or C\-x C\-u)<TAB>Function<TAB>Bash<TAB>file:///home/sunny/.local/share/dasht/docsets/Bash.docset/Contents/Resources/Documents/bash/Miscellaneous\-Commands.html#//apple_ref/Function/undo%20%28C%2D%5F%20or%20C%2Dx%20C%2Du%29
6159
.fi
6260
.RE
6361
.SH ENVIRONMENT

0 commit comments

Comments
 (0)