// quicknote
// Copyright (C) 2023 M R Lemon
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package main
import (
"bufio"
"flag"
"fmt"
"io"
"net/http"
"os"
"os/user"
"path/filepath"
"regexp"
"strings"
"github.com/atotto/clipboard"
)
var commentVar string
var mainContent string
func init() {
const (
defaultComment = ""
usage = "add a comment to the quicknote"
)
flag.StringVar(&commentVar, "c", defaultComment, usage+" (shorthand)")
flag.StringVar(&commentVar, "comment", defaultComment, usage)
flag.Parse()
mainContent = flag.Arg(0)
}
func main() {
var url, markdownFile string
urlFromClipboard, err := clipboard.ReadAll()
if err != nil {
fmt.Printf("Error getting URL from clipboard: %v\n", err)
os.Exit(1)
}
url = strings.TrimSpace(urlFromClipboard)
usr, err := user.Current()
if err != nil {
fmt.Printf("Error getting user home directory: %v\n", err)
os.Exit(1)
}
markdownFile = filepath.Join(usr.HomeDir, "Documents", "Notes", "quicknote.md")
resp, err := http.Get(url)
if err != nil {
fmt.Printf("Error fetching URL: %v\n", err)
os.Exit(1)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Printf("Error reading response body: %v\n", err)
os.Exit(1)
}
htmlContent := string(body)
title, err := extractTitle(htmlContent)
if err != nil {
fmt.Printf("Error: %v\n", err)
title = promptForTitle()
}
markdownLink := fmt.Sprintf("- [%s](%s)\n", title, url)
if commentVar != "" {
markdownLink = fmt.Sprintf("- %s: [%s](%s)\n", strings.TrimSpace(commentVar), title, url)
}
err = appendToFile(markdownFile, markdownLink)
if err != nil {
fmt.Printf("Error appending to file: %v\n", err)
os.Exit(1)
}
}
func extractTitle(html string) (string, error) {
re := regexp.MustCompile(`(?i)<\s*title\s*>(.*?)<\s*/\s*title\s*>`)
match := re.FindStringSubmatch(html)
if len(match) > 0 {
title := strings.TrimSpace(match[1])
if title != "" {
return title, nil
}
return "", fmt.Errorf("title tag is empty")
}
return "", fmt.Errorf("title tag is not present")
}
func promptForTitle() string {
reader := bufio.NewReader(os.Stdin)
fmt.Print("Please enter a title for the URL: ")
title, _ := reader.ReadString('\n')
return strings.TrimSpace(title)
}
// appendToFile writes content to the file filename.
func appendToFile(filename, content string) error {
f, err := os.OpenFile(filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer f.Close()
if _, err := f.WriteString(content); err != nil {
return err
}
return nil
}