diff options
-rw-r--r-- | quicknote.go | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/quicknote.go b/quicknote.go index 71fc019..ba55054 100644 --- a/quicknote.go +++ b/quicknote.go @@ -34,6 +34,32 @@ import ( var commentVar string var mainContent string +/* +The init() function is a special function in Go that is automatically executed +before the main() function. It is typically used for initialization tasks, such +as parsing command-line flags or setting up global variables. + +In this specific code, the init() function performs the following tasks: + +1. Defines two constants: + - defaultComment: An empty string, which will be used as the default value + for the comment flag. + - usage: A string that describes the purpose of the comment flag. + +2. Registers two command-line flags using the flag package: + - -c or --comment: A string flag that allows the user to provide a comment. + The flag is bound to the commentVar variable, and the default value is + defaultComment. The usage string is used to describe the flag. + +3. Calls flag.Parse() to parse the command-line arguments. + +4. Assigns the first non-flag command-line argument to the mainContent variable. + +This function is likely part of a larger program that deals with managing +quicknotes or similar text-based notes. The commentVar and mainContent variables +are likely used elsewhere in the program to handle the user-provided comment and +the main content of the note, respectively. +*/ func init() { const ( defaultComment = "" @@ -94,6 +120,22 @@ func main() { } } +/* +extractTitle extracts the title from an HTML string. + +Parameters: +- html (string): The HTML string to extract the title from. + +Returns: +- string: The extracted title. +- error: An error if the title tag is not present or empty. + +The function uses a regular expression to find the <title> tag in the HTML string. +If the tag is found, it trims any leading or trailing whitespace from the title text +and returns it. If the title is an empty string, it returns an error indicating that +the title tag is empty. If the title tag is not found, it returns an error indicating +that the title tag is not present. +*/ func extractTitle(html string) (string, error) { re := regexp.MustCompile(`(?i)<\s*title\s*>(.*?)<\s*/\s*title\s*>`) match := re.FindStringSubmatch(html) @@ -107,6 +149,10 @@ func extractTitle(html string) (string, error) { return "", fmt.Errorf("title tag is not present") } +// promptForTitle prompts the user to enter a title for a URL and returns the trimmed input string. +// It reads from standard input (os.Stdin) using a buffered reader (bufio.NewReader). +// The user's input is read until a newline character ('\n') is encountered. +// The leading and trailing whitespace characters are removed from the input string using strings.TrimSpace. func promptForTitle() string { reader := bufio.NewReader(os.Stdin) fmt.Print("Please enter a title for the URL: ") |