-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgotolino.go
167 lines (138 loc) · 4.15 KB
/
gotolino.go
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package main
import (
"bufio"
"fmt"
"os"
"runtime"
"strings"
"path/filepath"
)
func check(e error) {
if e != nil {
panic(e)
}
}
func main() {
settingNotes, settingsPath := readSettings()
fmt.Println("Settings Notes", settingNotes, "settingsPath", settingsPath)
notes, err := os.Open(settingNotes)
check(err)
defer notes.Close()
filteredNotes := make(Notes, 0)
books := make([]string, 0)
scanner := bufio.NewScanner(notes)
currentlyCreatingNewNote := true
// tmpSavings
tmpAuthor := "tmp"
tmpBook := "tmp"
tmpNote := "tmp"
tmpMarked := "tmp"
tmpNoteType := "tmp"
tmpSiteInformation := "tmp"
noteCount := 0
fmt.Println("Starting to collect the notes 🏃🏼")
for scanner.Scan() {
switch checkNoteType(scanner.Text()) {
case "note":
tmpNote = getNote(scanner.Text())
tmpSiteInformation = getSiteInformation(scanner.Text())
tmpNoteType = "note"
case "marking":
tmpNoteType = "marking"
tmpSiteInformation = getSiteInformation(scanner.Text())
tmpMarked = getMarking(scanner.Text())
case "bookmark":
tmpNoteType = "bookmark"
tmpSiteInformation = getSiteInformation(scanner.Text())
case "delimeter":
noteCount += 1
currentlyCreatingNewNote = true
case "empty":
case "added":
if currentlyCreatingNewNote {
newNote := Note{
author: tmpAuthor,
book: tmpBook,
notetype: tmpNoteType,
marked: tmpMarked,
note: tmpNote,
siteInformation: tmpSiteInformation,
}
filteredNotes = append(filteredNotes, newNote)
currentlyCreatingNewNote = false
// Reset Tmp Fields
tmpNote = "tmp"
tmpAuthor = "tmp"
tmpBook = "tmp"
tmpAuthor = "tmp"
tmpMarked = "tmp"
tmpNoteType = "tmp"
tmpSiteInformation = "tmp"
}
case "other":
fmt.Println(scanner.Text())
titleOrMarking, authorOrMarkingFlag := getTitleAndAuthorOrMarking(scanner.Text())
if authorOrMarkingFlag != "marking" {
tmpBook = titleOrMarking
tmpAuthor = authorOrMarkingFlag
// Add Book to booklist
if(!contains(books, tmpBook)) {
books = append(books, tmpBook)
}
} else {
tmpMarked = titleOrMarking
}
}
}
sortedNotesByBook := getNotesSortedByBooks(books, filteredNotes)
for _, book := range sortedNotesByBook {
// create folder code
bookNamePath := settingsPath + "/" + book[0].author + " - " + book[0].book
fmt.Println("Book being looped:", bookNamePath)
if _, err := os.Stat(bookNamePath); os.IsNotExist(err) {
fmt.Println("Creating directory for:", bookNamePath)
err := os.Mkdir(bookNamePath, 0755)
check(err)
} else {
fmt.Println("Path already exists for: ", bookNamePath)
}
for _, note := range book {
noteFileName := note.siteInformation + ".md"
noteString := createNoteMd(note)
notePath := bookNamePath + "/" + noteFileName
if _, err := os.Stat(notePath); os.IsNotExist(err) {
file, err := os.Create(notePath)
defer file.Close()
check(err)
_, err2 := file.WriteString(noteString)
check(err2)
} else {
fmt.Println("Path already exists for: ", notePath)
}
}
fmt.Println("✅ Done creating notes in: ", settingsPath)
}
}
// readSettings reads the settings.txt in the current directory and returns the two locations needed for the programm
func readSettings() (string, string) {
currentScriptDirectory := GetCurrentScriptDirectory();
var notesTxtLocation, notesSavingPath string
settings, err := os.Open(currentScriptDirectory + "/settings.txt")
check(err)
defer settings.Close()
scanner := bufio.NewScanner(settings)
for scanner.Scan() {
if strings.Contains(scanner.Text(), "notesTxtLocation") {
notesTxtLocation = getStringSeperated(scanner.Text(), "=")
} else if strings.Contains(scanner.Text(), "notesSavingPath") {
notesSavingPath = getStringSeperated(scanner.Text(), "=")
}
}
return notesTxtLocation, notesSavingPath
}
// GetCurrentScriptDirectory returns the directory of the currently running go script file.
func GetCurrentScriptDirectory() string {
// NOTE: Replace the 1 with a 0 if you use this code directly, instead of wrapping it in a function.
_, scriptPath, _, _ := runtime.Caller(1)
return filepath.Join(scriptPath, "../")
}