Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Umut Isik committed Jan 29, 2020
0 parents commit 25876e4
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 0 deletions.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module hacker-laws-cli

go 1.13
74 changes: 74 additions & 0 deletions lib/parser/parser.go
Original file line number Diff line number Diff line change
@@ -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
}
61 changes: 61 additions & 0 deletions lib/repo/repo.go
Original file line number Diff line number Diff line change
@@ -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)
}
35 changes: 35 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -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()
}

0 comments on commit 25876e4

Please sign in to comment.