-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Umut Isik
committed
Jan 29, 2020
0 parents
commit 25876e4
Showing
4 changed files
with
173 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module hacker-laws-cli | ||
|
||
go 1.13 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |