Skip to content

Commit

Permalink
Merge pull request #91 from Rooyca/master
Browse files Browse the repository at this point in the history
update: create notes from templates
  • Loading branch information
pimterry authored Sep 22, 2023
2 parents 47487d6 + 2ce8e7e commit aabe4e4
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 6 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,20 @@ There are also more complex options available. You can set any configuration pro

* `QUICKNOTE_FORMAT` changes the way that quicknotes are generated. The string formatted using the `date` command.
* `NOTES_EXT` changes the default extension that notes are saved with.
* `TEMPLATES_DIR` changes the directory in which templates are stored.
* `NOTES_DIRECTORY` changes the directory in which notes are stored.
* `EDITOR` can also be overriden here, for `notes` only.
* `POST_COMMAND` sets the command to run after any modification command (e.g. `open`, `mv`, ...) succeeds


## How do I use it?

### `notes new <note-name>`
### `notes new [-t <template-name>] <note-name>`

Opens your `$EDITOR` of choice for a new note, with the given name. The name can include slashes, if you want to put your note in a subfolder. Leave out the name if you want one to be generated for you (e.g. `quicknote-2016-12-21.md` - format configurable with `$QUICKNOTE_FORMAT`). If you want to place a quicknote in a subfolder, use a trailing slash: `notes new subfolder/`. Shorthand alias also available with `notes n`.

If you pass the `-t` flag to `notes new`, the note will be created from a template. The template is a file in your notes directory, with the same name as the template name you pass in. For example, if you have a template called `meeting-notes` in your notes directory, you can create a new note from that template with `notes new -t meeting-notes new-file-name`. This will open your `$EDITOR` with the contents of that template file, and you can edit it and save it as a new note.

If you do not supply an extension in `note-name`, it will be automatically appended with the default file extension (e.g. "newnote" will become "newnote.md"). However, if you include a one-to-four-letter file extension, notes will use that extension when creating the file (e.g. "newnote.tex" is created as "newnote.tex"; not "newnote.md", or "newnote.tex.md").

### `notes find <part-of-a-note-name>`
Expand Down
3 changes: 3 additions & 0 deletions config
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@
# Define the directory where notes are stored
# NOTES_DIRECTORY=~/notes

# Define the directory where templates are stored
# TEMPLATES_DIR=.templates

# Define command to run after modification command
# POST_COMMAND="/path/to/custom_script.sh"
40 changes: 38 additions & 2 deletions notes
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ notes_version="1.2.0"
# Default Date string before config
QUICKNOTE_FORMAT="quicknote-%Y-%m-%d"
NOTES_EXT="md"
TEMPLATES_DIR=".templates"
# Look for configuration file at ~/.config/notes/config and use it
if [ -f ~/.config/notes/config ]; then
. ~/.config/notes/config
Expand Down Expand Up @@ -117,9 +118,39 @@ generate_name() {
}

new_note() {
local template_path="" # Either empty (no template) or the path to the template we'll use
if [[ "$1" == "-t" ]]; then
shift

local template_name="$1"
if [[ $template_name == "" ]]; then
printf "Error: Template name is missing.\n"
exit 1
fi
shift

local template_folder="$notes_dir/$TEMPLATES_DIR"

if [[ ! -d "$template_folder" ]]; then
printf "Template folder not found: $template_folder\n"
exit 1
fi

if [[ ! "$template_name" == *.$NOTES_EXT ]]; then
template_name="$template_name.$NOTES_EXT"
fi

template_path="$template_folder/$template_name"
if [[ ! -e "$template_path" ]]; then
printf "Template not found\n"
printf "You can create it by running: notes new .templates/$template_name\n"
exit 1
fi
fi

local note_name="$*"
if [[ $note_name == "" ]]; then
note_name="$(generate_name)"
note_name="$(generate_name)"
fi

if echo "$note_name" | grep "/$" &> /dev/null; then
Expand All @@ -128,6 +159,11 @@ new_note() {

mkdir -p "$(dirname "$notes_dir/$note_name")"

if [[ "$template_path" != "" ]]; then
local note_path=$(get_full_note_path "$note_name")
cp "$template_path" "$note_path"
fi

open_note "$note_name"
}

Expand Down Expand Up @@ -284,7 +320,7 @@ usage() {
$name is a command line note taking tool.
Usage:
$name new|n <name> # Create a new note
$name new|n [-t <template>] <name> # Create a new note, if -t is given, use template
$name ls <pattern> # List notes by path
$name find|f [pattern] # Search notes by filename and path
$name grep|g <pattern> # Search notes by content
Expand Down
6 changes: 3 additions & 3 deletions notes.1
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ note. You can set $EDITOR in your shell to override it.
This file overrides any settings in your rc file.
.SH COMMANDS
.TP
.BR new ", " n " " \fR[\fINAME\fR]
Open your text editor with a new note, with given name.
.BR new ", " n " " "\fR[\fB\-t\fR] " \fR[\fBTEMPLATE\fR] \fR[\fINAME\fR]
Open your text editor with a new note, with the given name.
If no name is given, a quick note will be created with an auto-generated
name.
name. If the \-t option is provided, the specified TEMPLATE will be used for the new note.
.TP
.BR find ", " f " " \fR[\fIPATTERN\fR]
Searches for notes using the given pattern, returning all matches. If no
Expand Down
32 changes: 32 additions & 0 deletions test/test-new.bats
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,35 @@ notes="./notes"
assert_success
assert_exists "$NOTES_DIRECTORY/explicit-ext.zzz"
}

@test "Should create a new template note" {
run $notes new .templates/basic

assert_success
assert_exists "$NOTES_DIRECTORY/.templates/basic.md"
}

@test "Should create a new note with the given template" {
run $notes new .templates/basic
run $notes new -t basic note_with_template

assert_success
assert_exists "$NOTES_DIRECTORY/note_with_template.md"
}

@test "Should create new note from template within subfolders" {
run $notes new .templates/basic
run $notes new -t basic subfolder/note_with_template

assert_success
assert_exists "$NOTES_DIRECTORY/subfolder/note_with_template.md"
}

@test "Check that the template is used" {
run $notes append .templates/basic "This is a basic template"
run $notes new -t basic note_with_template
run $notes cat note_with_template

assert_success
assert_output "This is a basic template"
}

0 comments on commit aabe4e4

Please sign in to comment.