Skip to content

Commit

Permalink
Merge pull request #14 from whoan/support-local-snippets
Browse files Browse the repository at this point in the history
Support local snippets
  • Loading branch information
whoan authored Nov 10, 2019
2 parents 9fb6720 + 5ac4666 commit fae76b6
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 9 deletions.
18 changes: 16 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -34,7 +34,7 @@ snip() {

## Usage

- Add `snip("<url|path>")` (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("<url|local_path>")` (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).
Expand Down Expand Up @@ -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
Expand Down
36 changes: 29 additions & 7 deletions snip.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit fae76b6

Please sign in to comment.