Exemple #1
0
func LoadPkgNameList(filePath string) {
	PackageNameList = make(map[string]string)

	// If file does not exist, simply ignore.
	if !com.IsFile(filePath) {
		return
	}

	data, err := com.ReadFile(filePath)
	if err != nil {
		log.Error("Package name list", "Fail to read file")
		log.Fatal("", err.Error())
	}

	pkgs := strings.Split(string(data), "\n")
	for i, line := range pkgs {
		infos := strings.Split(line, "=")
		if len(infos) != 2 {
			// Last item might be empty line.
			if i == len(pkgs)-1 {
				continue
			}
			log.Error("", "Fail to parse package name: "+line)
			log.Fatal("", "Invalid package name information")
		}
		PackageNameList[strings.TrimSpace(infos[0])] =
			strings.TrimSpace(infos[1])
	}
}
Exemple #2
0
func getFile(filePath string) *archive {
	df := &archive{}
	p, err := com.ReadFile(filePath + ".md")
	if err != nil {
		beego.Error("models.getFile -> ", err)
		return nil
	}

	s := string(p)
	// Parse author and date.
	if i := strings.Index(s, "---\n"); i > -1 {
		if j := strings.Index(s[i+4:], "---\n"); j > -1 {
			infos := strings.Split(s[i+4:j+i+4], "\n")
			for k := range infos {
				z := strings.Index(infos[k], ":")
				if z == -1 {
					continue
				}

				switch k {
				case 0: // Author.
					df.Author = strings.TrimSpace(infos[k][z+1:])
				case 1: // Date.
					df.Date = strings.TrimSpace(infos[k][z+1:])
				}
			}
			s = s[j+i+8:]
			for {
				if !strings.HasPrefix(s, "\n") {
					break
				}
				s = s[1:]
			}
		}
	}

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

	df.Content = markdown(df.Content)

	// add "click to read more" feature
	more := []byte("\n<p>[more]</p>\n")
	if i := bytes.Index(df.Content, more); i != -1 {
		df.Excerpt = df.Content[:i]
		for k := 0; k < len(more); k++ {
			df.Content[i+k] = ' '
		}
	}

	return df
}
Exemple #3
0
// checkImports checks package denpendencies.
func CheckImports(absPath, importPath string, nod *Node) (importPkgs []string, err error) {
	dir, err := os.Open(absPath)
	if err != nil {
		return nil, err
	}
	defer dir.Close()

	// Get file info slice.
	fis, err := dir.Readdir(0)
	if err != nil {
		return nil, err
	}

	files := make([]*source, 0, 10)
	for _, fi := range fis {
		// Only handle files.
		if strings.HasSuffix(fi.Name(), ".go") {
			data, err := com.ReadFile(absPath + fi.Name())
			if err != nil {
				return nil, err
			}

			files = append(files, &source{
				name: fi.Name(),
				data: data,
			})
		}
	}

	// Check if has Go source files.
	if len(files) > 0 {
		w := &walker{ImportPath: importPath}
		importPkgs, err = w.build(files, nod)
		if err != nil {
			return nil, err
		}
	}

	return importPkgs, err
}
Exemple #4
0
func ExampleReadFile() {
	b, err := com.ReadFile("SaveFile.txt")
	fmt.Println(string(b), err)
}