2019-03-17 16:58:39 +00:00
|
|
|
package gopher
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-04-28 19:46:47 +00:00
|
|
|
"strings"
|
2019-03-17 16:58:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
//------------------------------------------------\\
|
|
|
|
// + + + T Y P E S + + + \\
|
|
|
|
//--------------------------------------------------\\
|
|
|
|
|
2019-03-21 05:29:09 +00:00
|
|
|
// View is a struct representing a gopher page. It contains
|
2019-03-17 16:58:39 +00:00
|
|
|
// the page content as a string slice, a list of link URLs
|
|
|
|
// as string slices, and the Url struct representing the page.
|
|
|
|
type View struct {
|
2019-04-28 19:46:47 +00:00
|
|
|
Content []string
|
|
|
|
Links []string
|
|
|
|
Address Url
|
2019-03-17 16:58:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------\\
|
|
|
|
// + + + R E C E I V E R S + + + \\
|
|
|
|
//--------------------------------------------------\\
|
|
|
|
|
2019-04-28 19:46:47 +00:00
|
|
|
// ParseMap is called by a view struct to parse a gophermap.
|
2019-03-21 05:29:09 +00:00
|
|
|
// It checks if the view is for a gophermap. If not,it does
|
2019-03-17 16:58:39 +00:00
|
|
|
// nothing. If so, it parses the gophermap into comment lines
|
|
|
|
// and link lines. For link lines it adds a link to the links
|
|
|
|
// slice and changes the content value to just the printable
|
|
|
|
// string plus a gophertype indicator and a link number that
|
|
|
|
// relates to the link position in the links slice. This
|
|
|
|
// receiver does not return anything.
|
|
|
|
func (v *View) ParseMap() {
|
|
|
|
if v.Address.Gophertype == "1" || v.Address.Gophertype == "7" {
|
|
|
|
for i, e := range v.Content {
|
|
|
|
e = strings.Trim(e, "\r\n")
|
2019-04-17 03:38:00 +00:00
|
|
|
if e == "." {
|
|
|
|
v.Content[i] = " "
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-04-28 19:46:47 +00:00
|
|
|
line := strings.Split(e, "\t")
|
2019-03-17 16:58:39 +00:00
|
|
|
var title string
|
|
|
|
if len(line[0]) > 1 {
|
|
|
|
title = line[0][1:]
|
|
|
|
} else {
|
|
|
|
title = ""
|
|
|
|
}
|
2019-08-29 23:51:36 +00:00
|
|
|
if len(line) > 1 && len(line[0]) > 0 && string(line[0][0]) == "i" {
|
2019-03-17 16:58:39 +00:00
|
|
|
v.Content[i] = " " + string(title)
|
|
|
|
} else if len(line) >= 4 {
|
|
|
|
fulllink := fmt.Sprintf("%s:%s/%s%s", line[2], line[3], string(line[0][0]), line[1])
|
|
|
|
v.Links = append(v.Links, fulllink)
|
2019-05-06 22:33:18 +00:00
|
|
|
linktext := fmt.Sprintf("(%s) %2d %s", getType(string(line[0][0])), len(v.Links), title)
|
2019-03-17 16:58:39 +00:00
|
|
|
v.Content[i] = linktext
|
2019-04-17 03:38:00 +00:00
|
|
|
}
|
2019-03-17 16:58:39 +00:00
|
|
|
}
|
2019-04-28 19:46:47 +00:00
|
|
|
}
|
2019-03-17 16:58:39 +00:00
|
|
|
}
|
|
|
|
|
2019-03-21 05:29:09 +00:00
|
|
|
// Display is called on a view struct to print the contents of the view.
|
2019-03-17 16:58:39 +00:00
|
|
|
// This receiver does not return anything.
|
|
|
|
func (v View) Display() {
|
|
|
|
fmt.Println()
|
|
|
|
for _, el := range v.Content {
|
|
|
|
fmt.Println(el)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------\\
|
|
|
|
// + + + F U N C T I O N S + + + \\
|
|
|
|
//--------------------------------------------------\\
|
|
|
|
|
2019-03-21 05:29:09 +00:00
|
|
|
// MakeView creates and returns a new View struct from
|
|
|
|
// a Url and a string splice of content. This is used to
|
|
|
|
// initialize a View with a Url struct, links, and content.
|
|
|
|
// It takes a Url struct and a content []string and returns
|
2019-03-17 16:58:39 +00:00
|
|
|
// a View (NOT a pointer to a View).
|
|
|
|
func MakeView(url Url, content []string) View {
|
|
|
|
v := View{content, make([]string, 0), url}
|
|
|
|
v.ParseMap()
|
|
|
|
return v
|
|
|
|
}
|