コード例 #1
0
ファイル: models.go プロジェクト: joubertredrat/website
func getFile(filePath string) *docFile {
	if strings.Contains(filePath, "images") ||
		len(strings.Split(filePath, "/")) <= 3 {
		return nil
	}

	df := &docFile{}
	p, err := loadFile(filePath + ".md")
	if err != nil {
		log.Error("Fail to load MD file: %v", err)
		return nil
	}

	// Parse and render.
	s := string(p)
	i := strings.Index(s, "\n")
	if i > -1 {
		// Has title.
		df.Title = strings.TrimSpace(
			strings.Replace(s[:i+1], "#", "", -1))
		if len(s) >= i+2 {
			df.Data = []byte(strings.TrimSpace(s[i+2:]))
		}
	} else {
		df.Data = p
	}

	df.Data = markdown(df.Data)
	return df
}
コード例 #2
0
ファイル: http.go プロジェクト: joubertredrat/website
func (t *transport) RoundTrip(req *http.Request) (*http.Response, error) {
	timer := time.AfterFunc(*requestTimeout, func() {
		t.t.CancelRequest(req)
		log.Error("Canceled request for %s", req.URL)
	})

	defer timer.Stop()

	return t.t.RoundTrip(req)
}
コード例 #3
0
ファイル: models.go プロジェクト: joubertredrat/website
func initDocMap(name string) {
	docTrees[name] = &docTree{}
	treeName := "conf/docTree_" + name + ".json"
	isConfExist := com.IsFile(treeName)
	if isConfExist {
		f, err := os.Open(treeName)
		if err != nil {
			log.Error("Fail to open '%s': %v", treeName, err)
			return
		}
		defer f.Close()

		d := json.NewDecoder(f)
		if err = d.Decode(docTrees[name]); err != nil {
			log.Error("Fail to decode '%s': %v", treeName, err)
			return
		}
	} else {
		// Generate 'docTree'.
		docTrees[name].Tree = append(docTrees[name].Tree, oldDocNode{Path: ""})
	}

	docLock.Lock()
	defer docLock.Unlock()

	docMap := make(map[string]*docFile)

	for _, l := range setting.Langs {
		os.MkdirAll(path.Join("docs", name, l), os.ModePerm)
		for _, v := range docTrees[name].Tree {
			var fullName string
			if isConfExist {
				fullName = v.Path
			} else {
				fullName = l + "/" + v.Path
			}

			docMap[fullName] = getFile(path.Join("docs", name, fullName))
		}
	}

	docMaps[name] = docMap
}
コード例 #4
0
ファイル: models.go プロジェクト: joubertredrat/website
func parseDocs(name string) {
	if docs[name] == nil {
		docs[name] = make(map[string]*DocRoot)
	}

	root, err := ParseDocs(path.Join("docs", name, "zh-CN"))
	if err != nil {
		log.Error("Fail to parse docs: %v", err)
	}

	if root != nil {
		docs[name]["zh-CN"] = root
	}

	root, err = ParseDocs(path.Join("docs", name, "en-US"))
	if err != nil {
		log.Error("Fail to parse docs: %v", err)
	}

	if root != nil {
		docs[name]["en-US"] = root
	}
}
コード例 #5
0
ファイル: models.go プロジェクト: joubertredrat/website
func InitModels() {
	for _, app := range setting.Apps {
		parseDocs(app.Name)
		initDocMap(app.Name)
	}

	if macaron.Env == macaron.DEV {
		return
	}

	c := cron.New()
	c.AddFunc("0 */5 * * * *", checkFileUpdates)
	c.Start()

	if needCheckUpdate() {
		checkFileUpdates()
		setting.Cfg.Section("app").Key("update_check_time").SetValue(com.ToStr(time.Now().Unix()))
		if err := setting.Cfg.SaveTo(setting.CUSTOM_PATH); err != nil {
			log.Error("Fail to save settings: %v", err)
		}
	}
}
コード例 #6
0
ファイル: models.go プロジェクト: joubertredrat/website
func checkFileUpdates() {
	log.Debug("Checking file updates")

	type tree struct {
		AppName, ApiUrl, RawUrl, TreeName, Prefix string
	}

	trees := make([]*tree, len(setting.Apps))
	for i, app := range setting.Apps {
		trees[i] = &tree{
			AppName:  app.Name,
			ApiUrl:   "https://api.github.com/repos/" + app.RepoName + "/git/trees/master?recursive=1&" + setting.Github,
			RawUrl:   "https://raw.github.com/" + app.RepoName + "/master/",
			TreeName: "conf/docTree_" + app.Name + ".json",
			Prefix:   "docs/" + app.Name + "/",
		}
	}

	for _, tree := range trees {
		var tmpTree struct {
			Tree []*oldDocNode
		}

		if err := com.HttpGetJSON(httpClient, tree.ApiUrl, &tmpTree); err != nil {
			log.Error("Fail to get trees: %v", err)
			return
		}

		var saveTree struct {
			Tree []*oldDocNode
		}
		saveTree.Tree = make([]*oldDocNode, 0, len(tmpTree.Tree))

		// Compare SHA.
		files := make([]com.RawFile, 0, len(tmpTree.Tree))
		for _, node := range tmpTree.Tree {
			// Skip non-md files and "README.md".
			if node.Type != "blob" || (!strings.HasSuffix(node.Path, ".md") &&
				!strings.Contains(node.Path, "images") &&
				!strings.HasSuffix(node.Path, ".json")) ||
				strings.HasPrefix(strings.ToLower(node.Path), "readme") {
				continue
			}

			name := strings.TrimSuffix(node.Path, ".md")

			if checkSHA(tree.AppName, name, node.Sha, tree.Prefix) {
				log.Info("Need to update: %s", name)
				files = append(files, &rawFile{
					name:   name,
					rawURL: tree.RawUrl + node.Path,
				})
			}

			saveTree.Tree = append(saveTree.Tree, &oldDocNode{
				Path: name,
				Sha:  node.Sha,
			})
			// For save purpose, reset name.
			node.Path = name
		}

		// Fetch files.
		if err := com.FetchFiles(httpClient, files, nil); err != nil {
			log.Error("Fail to fetch files: %v", err)
			return
		}

		// Update data.
		for _, f := range files {
			os.MkdirAll(path.Join(tree.Prefix, path.Dir(f.Name())), os.ModePerm)
			suf := ".md"
			if strings.Contains(f.Name(), "images") ||
				strings.HasSuffix(f.Name(), ".json") {
				suf = ""
			}
			fw, err := os.Create(tree.Prefix + f.Name() + suf)
			if err != nil {
				log.Error("Fail to open file: %v", err)
				continue
			}

			_, err = fw.Write(f.Data())
			fw.Close()
			if err != nil {
				log.Error("Fail to write data: %v", err)
				continue
			}
		}

		// Save documentation information.
		f, err := os.Create(tree.TreeName)
		if err != nil {
			log.Error("Fail to save data: %v", err)
			return
		}

		e := json.NewEncoder(f)
		err = e.Encode(&saveTree)
		if err != nil {
			log.Error("Fail to encode data: %v", err)
			return
		}
		f.Close()
	}

	log.Debug("Finish check file updates")
	for _, app := range setting.Apps {
		parseDocs(app.Name)
		initDocMap(app.Name)
	}
}