Repository navigation
Expand file tree
/
Copy pathcreate_file.bash
More file actions
executable file
·115 lines (94 loc) · 2.3 KB
/
Copy pathcreate_file.bash
File metadata and controls
executable file
·115 lines (94 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/env bash
# Create new note/save web page
# Arguments:
# $1 - name of tag
# $2 - option flag(-r)
#
# If $1 argument is empty - show menu
# If $1 is not exist tag - show menu
# If $2 is "-r" - repeat this command
cwd=$(dirname $0)
quite="exit"
page="page"
tags=$(cat $cwd/tags)
PS3="Select tag: "
function save_to_history
{
local first_line=$(head -n1 $1)
local history_file="$cwd/.history"
echo "$first_line $1" >> $history_file
}
function editor_open
{
vim -S $cwd/.create-nvimrc +start $1
}
function from_web_page
{
echo "Write url and associated name(not required)"
read url name
local is_url=$(echo "$url" | grep "^http")
if [[ -z $is_url ]]; then
echo "Write valid url that start on http or https"
exit
fi
local target_directory="$cwd/content/_pages"
local target_file="$target_directory"
if [[ -n $name ]]; then
local target_file="$target_directory/$name"
fi
wget --directory-prefix=$target_file \
--adjust-extension \
"$url"
exit
}
function from_tag
{
local dir=$(echo "$cwd/content/$1")
local list=$(ls $dir)
if [[ -n $list ]]; then
local file_max_name=$(echo "$list" | sort -n | tail -1)
local max=$(basename -s .md $file_max_name)
local new_filename=$(($max + 1))
local to_new_file=$(echo "$dir/$new_filename.md")
editor_open $to_new_file
else
local to_new_file=$(echo "$dir/1.md")
editor_open $to_new_file
fi
if [[ -e $to_new_file ]]; then
echo "New note added"
echo "$to_new_file"
save_to_history "$to_new_file"
fi
}
if [[ -n $1 ]]; then
if [[ $1 = $page ]]; then
from_web_page
fi
target_directory_tag=$(echo "$cwd/content/$1")
if [[ -d $target_directory_tag ]]; then
from_tag "$1"
if [[ $2 = '-r' ]]; then
echo "Continue? [y/n]"
read answer
if [[ $answer = "y" ]]; then
note create $1 -r
fi
fi
exit
else
echo "Tag not found, select from list"
fi
fi
select tag in $quite $page $tags
do
if [[ $tag = $quite ]]; then
exit
fi
if [[ $tag = $page ]]; then
from_web_page
fi
if [[ -n $tag ]]; then
from_tag "$tag"
fi
done