diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..c6d5fab --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module hacker-laws-cli + +go 1.13 diff --git a/lib/parser/parser.go b/lib/parser/parser.go new file mode 100644 index 0000000..7bf774d --- /dev/null +++ b/lib/parser/parser.go @@ -0,0 +1,74 @@ +package parser + +import ( + //"fmt" + "strings" + + "hacker-laws-cli/lib/repo" +) + +func Parse(readme string, r *repo.Repo) { + var cat = 0 + + var parseStatus = false + var content = repo.NewContent("", "") + for _, line := range strings.Split(strings.TrimSuffix(readme, "\n"), "\n") { + //fmt.Println(line) + + if IsCategory(line) { + if !IsCategoryIgnored(line) { + //fmt.Println("Category:", line) + if (LineToTitle(line) == "Laws") { + cat = 0 + } else { + cat = 1 + } + + parseStatus = true + + continue + } else { + parseStatus = false + } + } + + if (parseStatus == true) { + if IsContent(line) { + //fmt.Println("Content:", line) + r.Categories[cat].Contents = append(r.Categories[cat].Contents, content) + content = repo.NewContent(LineToTitle(line), "") + continue + } + + content.Body = content.Body + line + "\n" + } + } +} + +func IsCategory(line string) bool { + return strings.HasPrefix(line, "## ") && !strings.HasPrefix(line, "### ") +} + + +func IsContent(line string) bool { + return strings.HasPrefix(line, "### ") +} + +func IsCategoryIgnored(line string) bool { + ignoreList := []string{"Reading List", "Contributing", "TODO", "Introduction"} + str := LineToTitle(line) + for _, s := range ignoreList { + if (s == str) { + return true + } + } + + return false +} + +func LineToTitle(line string) string { + line = strings.Replace(line, "#", "", -1) + line = strings.Trim(line, " ") + + return line +} diff --git a/lib/repo/repo.go b/lib/repo/repo.go new file mode 100644 index 0000000..6ff9e4a --- /dev/null +++ b/lib/repo/repo.go @@ -0,0 +1,61 @@ +package repo + +import ( + "fmt" + "math/rand" + "time" +) + +type Repo struct { + Categories []Category +} + +type Category struct { + Name string + Contents []Content +} + +type Content struct { + Title string + Slug string + Body string +} + +func New() Repo { + return Repo{ + Categories: []Category{ + { + Name: "Laws", + Contents: []Content{}, + }, + { + Name: "Principles", + Contents: []Content{}, + }, + }, + } +} + +func NewContent(title string, body string) Content { + return Content{ + Title: title, + Body: body, + } +} + +func (r *Repo) RandomContent() Content { + // Seed the random number generator using the current time (nanoseconds since epoch): + rand.Seed(time.Now().UnixNano()) + + // Hard to predict...but is it possible? I know the day, and hour, probably minute... + cat := rand.Intn(1000)% len(r.Categories) + + content := rand.Intn(1000)% len(r.Categories[cat].Contents) + + return r.Categories[cat].Contents[content] +} + +func (c *Content) Display() { + fmt.Println(c.Title) + fmt.Println(c.Body) +} \ No newline at end of file diff --git a/main.go b/main.go new file mode 100644 index 0000000..51e7b07 --- /dev/null +++ b/main.go @@ -0,0 +1,35 @@ +package main + +import ( + "fmt" + "net/http" + "io/ioutil" + + "hacker-laws-cli/lib/repo" + "hacker-laws-cli/lib/parser" +) + +func main() { + hackerLaws := repo.New() + + response, err := http.Get("https://raw.githubusercontent.com/dwmkerr/hacker-laws/master/README.md") + if err != nil { + fmt.Println(err) + } + defer response.Body.Close() + + responseData, err := ioutil.ReadAll(response.Body) + if err != nil { + fmt.Println(err) + } + + responseString := string(responseData) + + fmt.Println(hackerLaws) + + parser.Parse(responseString, &hackerLaws) + + randomContent := hackerLaws.RandomContent() + + randomContent.Display() +} \ No newline at end of file