Example #1
0
func parsePost(path string) (*Post, error) {
	log.Info("Parse:", path)

	f, err := os.OpenFile(path, os.O_RDONLY, 0)
	if err != nil {
		return nil, err
	}
	defer f.Close()
	p := &Post{
		Name: strings.TrimSuffix(filepath.Base(path), filepath.Ext(path)),
	}

	scanner := bufio.NewScanner(f)
	for scanner.Scan() {
		s := scanner.Text()

		if strings.HasPrefix(s, "+++") {
			break
		}

		sp := strings.SplitN(s, ":", 2)
		if len(sp) != 2 {
			return nil, fmt.Errorf("invalid header: %s", s)
		}

		k, v := strings.TrimSpace(sp[0]), strings.TrimSpace(sp[1])
		switch k {
		case "title":
			p.Title = v
		case "description":
			p.Description = v
		case "time":
			p.Time, err = time.Parse("2006/01/02 15:04", v)
			if err != nil {
				return nil, err
			}
		case "category":
			p.Category = v
		case "html":
			p.HTML = v
		default:
			return nil, fmt.Errorf("invalid header: %s", s)
		}
	}

	if p.HTML != "" {
		b, _ := ioutil.ReadFile(p.HTML)
		p.Content = string(b)
	} else {
		var content bytes.Buffer
		for scanner.Scan() {
			content.Write(scanner.Bytes())
			content.WriteString("\n")
		}

		p.Content = string(blackfriday.MarkdownCommon(content.Bytes()))
	}

	return p, nil
}
Example #2
0
func serv() {
	os.Mkdir("log", 0644)
	log.SetStatFilePath("log")

	http.Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(http.Dir("assets"))))
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		if b, ok := pages[r.RequestURI]; ok {
			w.Write(b.Bytes())
		} else {
			http.NotFound(w, r)
		}
		log.Info("RemoteAddr: ", r.RemoteAddr, " ; LocalAddr: ", getAddr(), getMac(), " ; Router: ", r.RequestURI, " ; UA: ", r.UserAgent())
		go checkLog("mainlog.log")
		today := time.Now().Format("2006-01-02")
		MainLogfile := "log/" + today + "mainlog.log"
		go writeLogToFile([]string{"RemoteAddr: " + r.RemoteAddr + " ; LocalAddr: " + getAddr() + getMac() + " ; Router: " + r.RequestURI + " ; From: " + r.Referer() + " ; UA: " + r.UserAgent()}, MainLogfile)
	})
	log.Info("Working at port:2333")
	http.ListenAndServe(":2333", nil)
}
Example #3
0
func parseCategories() {
	log.Info("Parse Categories")

	f, err := os.OpenFile("conf/category.conf", os.O_RDONLY, 0777)
	log.FatalOnError(err)
	defer f.Close()

	scanner := bufio.NewScanner(f)
	for scanner.Scan() {
		sp := strings.SplitN(scanner.Text(), ":", 3)
		c := &Category{}
		if len(sp) > 0 {
			c.Name = sp[0]
		}
		if len(sp) > 1 {
			c.Title = sp[1]
		}
		if len(sp) > 2 {
			c.Description = sp[2]
		}

		categories = append(categories, c)
	}
}
Example #4
-1
func build() {
	var err error
	t, err = template.ParseGlob("templates/*.t")
	if err != nil {
		log.Fatal(err)
		return
	}
	parseCategories()

	filepath.Walk("posts", func(path string, info os.FileInfo, err error) error {
		if !info.IsDir() && filepath.Ext(path) == ".md" {
			p, err := parsePost(path)
			if err != nil {
				log.Error(err)
				return nil
			}
			var b bytes.Buffer

			if p.HTML != "" {
				b = *bytes.NewBufferString(p.Content)
			} else {
				err = t.ExecuteTemplate(&b, "post.t", p)
				if err != nil {
					log.Error(err)
					return nil
				}
			}

			pages["/"+p.Name] = b
			addPostToCategory(p)
			addPostToFeed(p)
		}
		return nil
	})

	log.Info("create home page")

	var b bytes.Buffer
	err = t.ExecuteTemplate(&b, "index.t", categories)
	log.FatalOnError(err)
	pages["/"] = b

	log.Info("create feeds")
	feed := feed()

	atom, err := feed.ToAtom()
	log.FatalOnError(err)
	pages["/feed"] = *bytes.NewBufferString(atom)

	rss, err := feed.ToRss()
	log.FatalOnError(err)
	pages["/rss"] = *bytes.NewBufferString(rss)
}