main go file added
This commit is contained in:
commit
c9807a96e8
|
@ -0,0 +1,100 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"fmt"
|
||||
"bufio"
|
||||
)
|
||||
|
||||
type hlink struct {
|
||||
url string
|
||||
text string
|
||||
}
|
||||
|
||||
type Page struct {
|
||||
Body string
|
||||
Links []hlink
|
||||
}
|
||||
|
||||
type Game struct {
|
||||
Title string
|
||||
Pages []Page
|
||||
}
|
||||
|
||||
var title string = "eow"
|
||||
var head string = `<!DOCTYPE HTML>
|
||||
<html>
|
||||
<head>
|
||||
<title>` + title + `</title>
|
||||
<meta charset="utf-8">
|
||||
<link rel="stylesheet" href="style.css"/>
|
||||
</head>
|
||||
<body>
|
||||
`
|
||||
var tail string = "\n" + `</body>
|
||||
</html>`
|
||||
|
||||
|
||||
func text2link(text string, url string) string {
|
||||
return " <a href=\"" + url + "\">" + text + "</a>"
|
||||
}
|
||||
|
||||
func writeHTML(title string, body string, links...hlink) {
|
||||
f, _ := os.Create(title + ".html")
|
||||
f.WriteString(head)
|
||||
f.WriteString(" <p>" + body + "</p>")
|
||||
f.WriteString("\n <br>\n")
|
||||
for _,link := range(links){
|
||||
f.WriteString(text2link(link.text,link.url))
|
||||
}
|
||||
f.WriteString(tail)
|
||||
}
|
||||
|
||||
func cutFirstWord(w string) string {
|
||||
var c int
|
||||
for c = 0; c < len(w); c++ {
|
||||
if (string(w[c]) == " ") {
|
||||
break
|
||||
}
|
||||
}
|
||||
return w[c+1:]
|
||||
}
|
||||
|
||||
func fparse(fname string) Game{
|
||||
file, _ := os.Open(fname)
|
||||
defer file.Close()
|
||||
scanner := bufio.NewScanner(file)
|
||||
|
||||
game := Game{}
|
||||
|
||||
var pcount int = 0
|
||||
scanner.Scan()
|
||||
game.Title = scanner.Text()
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
switch {
|
||||
case len(line) == 0:
|
||||
pcount++
|
||||
case string(line[0]) == ":":
|
||||
game.Pages = append(game.Pages, Page{Body:cutFirstWord(line)})
|
||||
fmt.Println(cutFirstWord(line))
|
||||
case string(line[0]) == "-":
|
||||
game.Pages[pcount]:Links = append
|
||||
//fmt.Println("New link")
|
||||
}
|
||||
|
||||
//fmt.Println(firstchar)
|
||||
//game.Pages = append(game.Pages()
|
||||
}
|
||||
return game
|
||||
}
|
||||
|
||||
func main() {
|
||||
game := fparse("model")
|
||||
fmt.Println(game)
|
||||
/*
|
||||
l1 := hlink{url:"01.html",text:"First"}
|
||||
l2 := hlink{url:"02.html",text:"Second"}
|
||||
writeHTML("check", "Yolo World", l1, l2)*/
|
||||
}
|
||||
|
Loading…
Reference in New Issue