Skip to content

Commit

Permalink
allow to override the standard ntriples fetch command (closes issue #8)
Browse files Browse the repository at this point in the history
also some refactoring
  • Loading branch information
seebi committed May 17, 2016
1 parent 957610e commit 6303635
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 39 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
* document password retrieval for webid keystore
* add a distinct option instead of new commands

## 0.8.0 (unpublished)

* allow to override the standard ntriples fetch command with alternatives such as any23's rover CLI

## 0.7.0 (May 2016)

* new command: turtleize - outputs an RDF file in turtle, using as much as possible prefix declarations
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -398,5 +398,7 @@ this is the place to setup personal configuration options such as
* WebID support
* syntax highlighting suppression
* setup of preferred accept headers
* setup of alternate ntriples fetch program such as any23's rover (see [this feature request](https://github.com/seebi/rdf.sh/issues/8) for background infos)

Please have a look at the [example rc file](https://github.com/seebi/rdf.sh/blob/master/example.rc).

12 changes: 9 additions & 3 deletions example.rc
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
# this file can be copied to you rdf.sh config directory
# this file can be copied to your rdf.sh configuration directory
# (e.g. to $HOME/.config/rdf.sh/rc) and is imported on each run

# which accept header should be used for requests
#RDFSH_ACCEPT_HEADER="text/turtle; q=1.0, application/x-turtle; q=0.9, text/n3; q=0.8, application/rdf+xml; q=0.5, text/plain; q=0.1"

# suppress highlightning? (true or false)
# suppress highlighting? (true or false)
#RDFSH_HIGHLIGHTING_SUPPRESS="true"

### add some additional curl options:
# allow webid authentification and ignores ssl server cert errors
# allow webid authentication and ignores ssl server cert errors
#RDFSH_CURLOPTIONS_ADDITONS="-E $HOME/.webid.pem --insecure"

# allow for overriding the default ntriples curl fetch command
# the command needs to ouput ntriples to stdout, stderr is suppressed
# the command takes one parameter (the URL of the resource)
RDFSH_HTTPGETNTRIPLES_COMMAND="any23 rover -t -f ntriples"

96 changes: 61 additions & 35 deletions rdf
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,6 @@ else
mimetypes="$RDFSH_ACCEPT_HEADER"
fi

# check for actively suppressing the pygmentize highlighting
if [ "$RDFSH_HIGHLIGHTING_SUPPRESS" == "true" ]
then
highlight=""
else
echo 'ttt'| pygmentize -l turtle 2>/dev/null >/dev/null
if [ "$?" == "0" ]; then
highlight="yes"
else
highlight=""
fi
fi

### mac workarounds
uname=`uname`
if [ "$uname" == "Darwin" ]
Expand All @@ -85,7 +72,50 @@ fi
# private functions
###

# takes an input command name and checks for availability with which
# outputs a given input turtle file
_outputTurtle ()
{
turtleFile=$1
if [ "$turtleFile" == "" ]
then
echo "_outputTurtle error: need an parameter"
exit 1
fi

# check for actively suppressing the pygmentize highlighting
if [ "$RDFSH_HIGHLIGHTING_SUPPRESS" == "true" ]
then
cat $turtleFile
else
# if hightlight is enable, try to pipe it through pygmentize, otherwise just cat it
echo 'ttt'| pygmentize -l turtle 2>/dev/null >/dev/null
if [ "$?" == "0" ]; then
cat $tmpfile.ttl | pygmentize -l turtle
else
cat $turtleFile
fi
fi
}

# fetches an URL as ntriples and outputs it to stdout
_httpGetNtriples ()
{
url=$1
if [ "$url" == "" ]
then
echo "_httpGetNtriples error: need an parameter"
exit 1
fi

if [ "$RDFSH_HTTPGETNTRIPLES_COMMAND" == "" ]
then
$curlcommand -H "Accept: $mimetypes" $url | rapper -q -i guess -o ntriples -I $url -
else
$RDFSH_HTTPGETNTRIPLES_COMMAND $url 2>/dev/null
fi
}

# takes an input command name and checks for availability
_checkTool ()
{
tool=$1
Expand Down Expand Up @@ -426,7 +456,7 @@ _getRelatedResources ()
uri=`_expandQName $resource`

tmpfile=`_getTempFile`
$thisexec get-ntriples $uri >$tmpfile.nt
_httpGetNtriples $uri >$tmpfile.nt
roqet -q -e "SELECT DISTINCT ?o {<$uri> ?property ?o. FILTER(isUri(?o))}" -D $tmpfile.nt 2>/dev/null | cut -d "<" -f 2 | cut -d ">" -f 1 | grep "^http"
rm $tmpfile $tmpfile.nt
}
Expand All @@ -448,7 +478,7 @@ _isPingbackEnabled ()
if [ "$pingbackServer" == "" ]
then
tmpfile=`_getTempFile`
$thisexec get-ntriples $uri >$tmpfile.nt
_httpGetNtriples $uri >$tmpfile.nt
pingbackServer=`roqet -q -e "SELECT ?o {<$uri> <http://purl.org/net/pingback/to> ?o}" -D $tmpfile.nt 2>/dev/null | head -1 | cut -d "<" -f 2 | cut -d ">" -f 1`
rm $tmpfile $tmpfile.nt
fi
Expand Down Expand Up @@ -505,7 +535,7 @@ do_edit ()
uri=`_expandQName $uri`
doEditTmpfile=`_getTempFile`

$thisexec get-ntriples $uri >${doEditTmpfile}
_httpGetNtriples $uri >${doEditTmpfile}
features=`_getFeatures ${doEditTmpfile}`
cat ${doEditTmpfile} | rapper -q ${features} -i ntriples -o turtle -I $uri - >"${doEditTmpfile}.ttl"
$EDITOR $doEditTmpfile.ttl
Expand Down Expand Up @@ -582,22 +612,17 @@ do_desc ()
fi
uri=`_expandQName $uri`
tmpfile=`_getTempFile`
$thisexec get-ntriples $uri >$tmpfile.nt
roqet -q -e "CONSTRUCT {<$uri> ?p ?o} WHERE {<$uri> ?p ?o}" -D $tmpfile.nt -r turtle | rapper -i turtle -o ntriples -q - "$uri" >$tmpfile.out

features=`_getFeatures $tmpfile.out`
_httpGetNtriples $uri >$tmpfile

rapper -q ${features} -i ntriples $tmpfile.out -o $output >$tmpfile.ttl
# fetches only triples with URI as subject (output is turtle)
roqet -q -e "CONSTRUCT {<$uri> ?p ?o} WHERE {<$uri> ?p ?o}" -D $tmpfile >$tmpfile.out

# if hightlight is enable, pipe it through pygmentize, otherwise just cat it
if [ "$highlight" == "yes" ]; then
cat $tmpfile.ttl | pygmentize -l turtle
else
cat $tmpfile.ttl
fi
# reformat and output turtle file
$thisexec turtleize $tmpfile.out >$tmpfile.ttl
_outputTurtle $tmpfile.ttl

# clean up
rm $tmpfile $tmpfile.out $tmpfile.nt $tmpfile.ttl
rm -f $tmpfile $tmpfile.out $tmpfile.ttl

# add history
_addToHistory $uri $historyfile
Expand All @@ -616,14 +641,12 @@ do_list ()
fi
uri=`_expandQName $uri`
tmpfile=`_getTempFile`
# turn history off for this internal call (dirty URIs)
export noHistory="true"
$thisexec get-ntriples $uri >$tmpfile.nt
_httpGetNtriples $uri >$tmpfile.nt
roqet -q -e "SELECT DISTINCT ?s WHERE {?s ?p ?o. FILTER isURI(?s) } " -D $tmpfile.nt 2>/dev/null | cut -d "<" -f 2 | cut -d ">" -f 1 | grep $uri
rm $tmpfile $tmpfile.nt
}

docu_get () { echo "curls rdf in xml or turtle to stdout (tries accept header)"; }
docu_get () { echo "fetches an URL as RDF to stdout (tries accept header)"; }
do_get ()
{
_checkTool curl
Expand All @@ -635,7 +658,10 @@ do_get ()
exit 1
fi
uri=`_expandQName $uri`
$curlcommand -H "Accept: $mimetypes" $uri
tmpfile=`_getTempFile`
_httpGetNtriples $uri >$tmpfile.nt
$thisexec turtleize $tmpfile.nt
rm $tmpfile $tmpfile.nt
_addToHistory $uri $historyfile
}

Expand All @@ -651,7 +677,7 @@ do_get-ntriples ()
exit 1
fi
uri=`_expandQName $uri`
$curlcommand -H "Accept: $mimetypes" $uri | rapper -q -i guess -o ntriples -I $uri -
_httpGetNtriples $uri
_addToHistory $uri $historyfile
}

Expand Down
2 changes: 1 addition & 1 deletion rdf.1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.TH rdf.sh 1 "05/2016" "version 0.7.0" "USER COMMANDS"
.TH rdf.sh 1 "05/2016" "version 0.8.0" "USER COMMANDS"
.SH NAME
rdf.sh \- A multi-tool shell script for doing Semantic Web jobs on the command line.
.SH SYNOPSIS
Expand Down

0 comments on commit 6303635

Please sign in to comment.