Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update dasht-query-line and dasht-query-html to work with tsv #56

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 22 additions & 18 deletions bin/dasht-query-html
Original file line number Diff line number Diff line change
Expand Up @@ -113,42 +113,46 @@ trap 'exit 44' USR1 # exit with a nonzero status when no results found
if (pattern == "") pattern = "^." # grouped by leading character
}
NR == 1 { print "<table>" }
$2 == "=" { result[$1] = substr($0, index($0, $2) + length($2) + 1) }
$1 == "from" { result["from"] = wordbreak_cached(result["from"], "<wbr>") }
$1 == "name" {
{

# $1, $2, $3, $4 :: name, docset, type, url
smackesey marked this conversation as resolved.
Show resolved Hide resolved

# mark search terms with STX and ETX bytes which are ignored by escape()
if (pattern) {
gsub(pattern, "\002&\003", result["name"])
gsub(pattern, "\002&\003", $1)
}

# mark word-wrappable points with VT bytes which are ignored by escape()
result["name"] = wordbreak(result["name"], "\v", "\002\003")
$1 = wordbreak($1, "\v", "\002\003")

# escape XML entities in search result to make them visible in browsers
result["name"] = escape(result["name"])
$1 = escape($1)

# insert word-break opportunity <wbr> tags at points marked by VT bytes
gsub("\v", "<wbr>", result["name"])
gsub("\v", "<wbr>", $1)

# highlight search terms in search result using the STX and ETX markers
if (pattern) {
gsub("\002", "<b><i><u>", result["name"])
gsub("\003", "</u></i></b>", result["name"])
gsub("\002", "<b><i><u>", $1)
gsub("\003", "</u></i></b>", $1)
}
}
$1 == "url" { print \
"<tr>"\
"<td><a href=\"" result["url"] "\">" result["name"] "</a></td>"\
"<td valign=\"bottom\" align=\"right\">" result["from"] "</td>"\
"<td valign=\"bottom\">" tolower(result["type"]) "</td>"\
"</tr>"

$2 = wordbreak_cached($2, "<wbr>") # docset field

print \
"<tr>"\
"<td><a href=\"" $4 "\">" $1 "</a></td>"\
"<td valign=\"bottom\" align=\"right\">" $2 "</td>"\
"<td valign=\"bottom\">" tolower($3) "</td>"\
"</tr>"

}
END {
if (NR > 0) {
print "</table>"
if (NR == 4) {
if (NR == 1) {
# there was only one search result, so automatically visit its url
print "<meta http-equiv=\"refresh\" content=\"0;url=" result["url"] "\">"
print "<meta http-equiv=\"refresh\" content=\"0;url=" $4 "\">"
}
}
}
Expand Down
37 changes: 20 additions & 17 deletions bin/dasht-query-line
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#
# ## NAME
#
# dasht-query-line - searches [Dash] docsets and emits groups of lines
# dasht-query-line - searches [Dash] docsets and emits results as tsv
smackesey marked this conversation as resolved.
Show resolved Hide resolved
#
# ## SYNOPSIS
#
Expand All @@ -28,8 +28,8 @@
#
# Searches for *PATTERN* in all installed [Dash] docsets, optionally searching
# only in those whose names match *DOCSET*s, by calling dasht-query-exec(1)
# and emits the results in groups of lines, as described in "Results" below.
# However, if no results were found, this program exits with a nonzero status.
# and emits the results as TSV. However, if no results were found, this program
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please spell out "TSV" in prose: "as TSV" -> "in Tab Separated Vector (TSV) format"

# exits with a nonzero status.
#
# ### Searching
#
Expand All @@ -42,26 +42,24 @@
#
# ### Results
#
# Each search result is printed to stdout as a group of four lines of text:
# Each search result is printed to stdout as a tab-separated line with fields:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fields are separated, not the line; so "as a line with 4 tab-separated fields".

#
# `name` `=` *VALUE*
# `name`
# Name of the token that matched the *PATTERN*.
#
# `type` `=` *VALUE*
# `type`
# Type of the token, as defined in the docset.
#
# `from` `=` *VALUE*
# `from`
# Name of the docset this result was found in.
#
# `url` `=` *VALUE*
# `url`
# URL of the API documentation for this result.
#
# For example, here is a search result for "c - x" from the "bash" docset:
# For example, here is a search result for "c - x" from the "bash" docset, with
# tab characters represented by "<TAB>":
#
# name = undo (C-_ or C-x C-u)
# type = Function
# from = Bash
# 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
# 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
#
# ## ENVIRONMENT
#
Expand Down Expand Up @@ -245,9 +243,11 @@ dasht-docsets "$@" | while read -r docset; do

{ $1 = $1 } # strip whitespace from key

$2 == "=" {
result[$1] = substr($0, index($0, $2) + length($2) + 1)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would change the index($0, $2) call to length($1) + 1 for robustness (no problem if $1 contains =).

}

$1 == "url" { were_any_results_found=1
# indicate the source of this result
print "from = " docset

# strip embedded XML from result URL
gsub("<.*>", "", $3)
Expand All @@ -259,9 +259,12 @@ dasht-docsets "$@" | while read -r docset; do

# resolve URL to filesystem location
$3 = file_url $3
}

/./ # reject any empty lines from input
# print TSV line
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would drop this (redundant) comment.

printf "%s\t%s\t%s\t%s\n", result["name"], docset, \
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add parentheses to the printf() call and list each argument on its own line.

result["type"], $3

}

END { exit !were_any_results_found }
' && kill -s USR1 $$ || : # notify this script if any results were found
Expand Down
24 changes: 11 additions & 13 deletions man/man1/dasht-query-line.1
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.TH DASHT\-QUERY\-LINE 1 2020\-05\-16 2.4.0
.SH NAME
.PP
dasht\-query\-line \- searches Dash \[la]https://kapeli.com/dash\[ra] docsets and emits groups of lines
dasht\-query\-line \- searches Dash \[la]https://kapeli.com/dash\[ra] docsets and emits groups of lines as tsv
.SH SYNOPSIS
.PP
\fB\fCdasht\-query\-line\fR [\fIPATTERN\fP] [\fIDOCSET\fP]...
Expand All @@ -24,8 +24,8 @@ Searches for \fIPATTERN\fP in all installed Dash \[la]https://kapeli.com/dash\[r
only in those whose names match \fIDOCSET\fPs, by calling
.BR dasht-query-exec (1)

and emits the results in groups of lines, as described in "Results" below.
However, if no results were found, this program exits with a nonzero status.
and emits the results as TSV. However, if no results were found, this program
exits with a nonzero status.
.SS Searching
.PP
Whitespace characters in \fIPATTERN\fP are treated as wildcards, whereas the
Expand All @@ -36,28 +36,26 @@ can match anywhere: beginning, middle, or end. As a result, if \fIPATTERN\fP is
undefined, it becomes a whitespace wildcard and thereby matches everything.
.SS Results
.PP
Each search result is printed to stdout as a group of four lines of text:
Each search result is printed to stdout as a tab\-separated line with fields:
.TP
\fB\fCname\fR \fB\fC=\fR \fIVALUE\fP
\fB\fCname\fR
Name of the token that matched the \fIPATTERN\fP\&.
.TP
\fB\fCtype\fR \fB\fC=\fR \fIVALUE\fP
\fB\fCtype\fR
Type of the token, as defined in the docset.
.TP
\fB\fCfrom\fR \fB\fC=\fR \fIVALUE\fP
\fB\fCfrom\fR
Name of the docset this result was found in.
.TP
\fB\fCurl\fR \fB\fC=\fR \fIVALUE\fP
\fB\fCurl\fR
URL of the API documentation for this result.
.PP
For example, here is a search result for "c \- x" from the "bash" docset:
For example, here is a search result for "c \- x" from the "bash" docset, with
tab characters represented by "<TAB>":
.PP
.RS
.nf
name = undo (C\-_ or C\-x C\-u)
type = Function
from = Bash
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
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
.fi
.RE
.SH ENVIRONMENT
Expand Down