From db800338e05a0d04de57d70876c4e015e469dd33 Mon Sep 17 00:00:00 2001 From: Juan Eugenio Abadie Date: Sat, 9 Nov 2019 20:09:01 -0300 Subject: [PATCH 1/2] Support snippets from the local filesystem --- snip.sh | 36 +++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/snip.sh b/snip.sh index 71f3166..389fad0 100755 --- a/snip.sh +++ b/snip.sh @@ -28,6 +28,24 @@ __snip__get_setting() { } +__snip__fix_home_directory() { + local param + param=${1:?Missing param} + if [[ $param =~ ^~/ ]]; then + echo ${param/\~/$HOME} + else + echo $param + fi +} + + +__snip__is_local_snippet() { + local snippet + snippet="${1:?Missing snippet as param}" + [[ $snippet =~ ^(\.|~|/) ]] +} + + __snip__get_snippet_url() { local snippet snippet="${1:?Missing snippet as param}" @@ -114,14 +132,18 @@ __snip__replace_snips() { for ((i=0; i < n_snippets; ++i)); do local snippet="${snippets[$i]}" - # get full url of the snippet - local snippet_url - snippet_url=$(__snip__get_snippet_url "$snippet") || return 1 - - # download snippet if necessary local snippet_file - snippet_file=$cache_dir/$(__snip__create_hash "$snippet_url") - __snip__download_snippet "$snippet_url" "$snippet_file" $force || return 1 + if __snip__is_local_snippet "$snippet"; then + snippet_file=$(__snip__fix_home_directory "$snippet") + else + # get full url of the snippet + local snippet_url + snippet_url=$(__snip__get_snippet_url "$snippet") || return 1 + + # download snippet if necessary + snippet_file=$cache_dir/$(__snip__create_hash "$snippet_url") + __snip__download_snippet "$snippet_url" "$snippet_file" $force || return 1 + fi # replace snips recursively local recursive_snippet_file From 5ac4666a304793022690b362c4a76b3a023d7c58 Mon Sep 17 00:00:00 2001 From: Juan Eugenio Abadie Date: Sat, 9 Nov 2019 23:58:51 -0300 Subject: [PATCH 2/2] Update readme --- readme.md | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/readme.md b/readme.md index 586f198..ab5b44c 100644 --- a/readme.md +++ b/readme.md @@ -1,6 +1,6 @@ # Snip -Add code snippets to your code directly from the web. +Add code snippets to your code directly from the web (or your local filesystem). ## Installation @@ -34,7 +34,7 @@ snip() { ## Usage -- Add `snip("")` (a.k.a. *the snip line*) anywhere in your code (usually as a comment) and the retrieved content will be placed after that line. +- Add `snip("")` (a.k.a. *the snip line*) anywhere in your code (usually as a comment) and the retrieved content will be placed after that line. - Prefix any command with `snip` (eg: `snip bash script.sh`) and the *snip lines* (if any) will be replaced with the content retrieved from the url provided. > Adding your *snip line* as a comment avoids your linter to complain about syntax (it works the same). @@ -102,6 +102,20 @@ $ snip g++ examples/main.cpp && ./a.out > Hello World ``` +If you set `base_url` in your settings, you can also shorten the reference to the snippet like this: + +```cpp +//snip("snippet.hpp") // snip will download $base_url/snippet.hpp +``` + +And if you want to reference a snippet in your file system, just provide the path to the file: + +```cpp +//snip("/home/you/snippet.hpp") // full path +//snip("./snippet.hpp") // relative path +//snip("~/snippet.hpp") // you can even reference your home path with ~ +``` + ### Bash ```bash