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

Add support for user-supplied fragments config #34

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
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
22 changes: 21 additions & 1 deletion bin/dasht-query-line
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@
# If undefined, its value is assumed to be `$XDG_DATA_HOME/dasht/docsets/`
# or, if `XDG_DATA_HOME` is undefined, `$HOME/.local/share/dasht/docsets/`.
#
# `DASHT_FRAGMENTS_CONF`
# Defines the filesystem location where custom URI fragment definition for
# your [Dash] docsets are stored. These file has the format
# *DOCSET* `=` *FRAGMENT*
# where *DOCSET* and *FRAGMENT* are separated by a space followed by a
# `=` followed by another space character. The *FRAGMENT* starts with a
# `#` character. If the environment variable is undefined, its value is
# assumed to be `$XDG_CONFIG_HOME/dasht/fragments.conf` or, if
# `XDG_CONFIG_HOME` is undefined, `$HOME/.config/dasht/fragments.conf`.
#
# ## EXIT STATUS
#
# 44
Expand All @@ -87,19 +97,21 @@
# Distributed under the terms of the ISC license (see the LICENSE file).

: ${DASHT_DOCSETS_DIR:=${XDG_DATA_HOME:-$HOME/.local/share}/dasht/docsets}
: ${DASHT_FRAGMENTS_CONF:=${XDG_CONFIG_HOME:-$HOME/.config}/dasht/fragments.conf}

pattern=$(echo " $1 " | sed -e 's/[%_]/\\&/g' -e 's/[[:space:]]\{1,\}/%/g')
test $# -gt 0 && shift # shift off PATTERN so argv contains solely DOCSETs

status=44 # (default) exit with a nonzero status when no results are found
trap 'status=0' USR1 # override default exit status when results are found
fragments_conf=$(test -s "$DASHT_FRAGMENTS_CONF" && cat "$DASHT_FRAGMENTS_CONF")

dasht-docsets "$@" | while read -r docset; do
database="$DASHT_DOCSETS_DIR/$docset".docset/Contents/Resources/docSet.dsidx
file_url="file://$(dirname "$database")/Documents/"

dasht-query-exec "$pattern" "$database" -line |
awk -v docset="$docset" -v file_url="$file_url" '
awk -v docset="$docset" -v file_url="$file_url" -v fragments_conf="$fragments_conf" '
BEGIN {
# default URI fragments that move user
# past navigation links at top of page
Expand Down Expand Up @@ -241,6 +253,14 @@ dasht-docsets "$@" | while read -r docset; do
fragment_by_docset["jQuery"] = "#content"
fragment_by_docset["jQuery_Mobile"] = "#content"
fragment_by_docset["jQuery_UI"] = "#content"

if (fragments_conf != "") {
while (getline < fragments_conf)
Copy link
Owner

Choose a reason for hiding this comment

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

We might avoid reading the fragments file here if it contents are passed in through -v option for awk.

Copy link
Author

Choose a reason for hiding this comment

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

Unfortunately, my awk skills are really bad, so I have no idea how to archieve that. Sorry :(

{
split($0,field," = ");
fragment_by_docset[field[1]] = field[2];
}
}
}

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