本文整理汇总了Golang中github.com/spf13/hugo/hugofs.Source函数的典型用法代码示例。如果您正苦于以下问题:Golang Source函数的具体用法?Golang Source怎么用?Golang Source使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Source函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: captureFiles
func (f *Filesystem) captureFiles() {
walker := func(filePath string, fi os.FileInfo, err error) error {
if err != nil {
return nil
}
b, err := f.shouldRead(filePath, fi)
if err != nil {
return err
}
if b {
rd, err := NewLazyFileReader(hugofs.Source(), filePath)
if err != nil {
return err
}
f.add(filePath, rd)
}
return err
}
err := helpers.SymbolicWalk(hugofs.Source(), f.Base, walker)
if err != nil {
jww.ERROR.Println(err)
}
}
示例2: captureFiles
func (f *Filesystem) captureFiles() {
walker := func(filePath string, fi os.FileInfo, err error) error {
if err != nil {
return nil
}
b, err := f.shouldRead(filePath, fi)
if err != nil {
return err
}
if b {
rd, err := NewLazyFileReader(hugofs.Source(), filePath)
if err != nil {
return err
}
f.add(filePath, rd)
}
return err
}
err := helpers.SymbolicWalk(hugofs.Source(), f.Base, walker)
if err != nil {
jww.ERROR.Println(err)
if err == helpers.WalkRootTooShortError {
panic("The root path is too short. If this is a test, make sure to init the content paths.")
}
}
}
示例3: AddTemplateFile
func (t *GoHTMLTemplate) AddTemplateFile(name, baseTemplatePath, path string) error {
t.checkState()
// get the suffix and switch on that
ext := filepath.Ext(path)
switch ext {
case ".amber":
templateName := strings.TrimSuffix(name, filepath.Ext(name)) + ".html"
compiler := amber.New()
b, err := afero.ReadFile(hugofs.Source(), path)
if err != nil {
return err
}
// Parse the input data
if err := compiler.ParseData(b, path); err != nil {
return err
}
if _, err := compiler.CompileWithTemplate(t.New(templateName)); err != nil {
return err
}
case ".ace":
var innerContent, baseContent []byte
innerContent, err := afero.ReadFile(hugofs.Source(), path)
if err != nil {
return err
}
if baseTemplatePath != "" {
baseContent, err = afero.ReadFile(hugofs.Source(), baseTemplatePath)
if err != nil {
return err
}
}
return t.AddAceTemplate(name, baseTemplatePath, path, baseContent, innerContent)
default:
if baseTemplatePath != "" {
return t.AddTemplateFileWithMaster(name, path, baseTemplatePath)
}
b, err := afero.ReadFile(hugofs.Source(), path)
if err != nil {
return err
}
jww.DEBUG.Printf("Add template file from path %s", path)
return t.AddTemplate(name, string(b))
}
return nil
}
示例4: TestDoNewSite_error_force_config_inside_exists
func TestDoNewSite_error_force_config_inside_exists(t *testing.T) {
basepath := filepath.Join(os.TempDir(), "blog")
configPath := filepath.Join(basepath, "config.toml")
hugofs.InitMemFs()
hugofs.Source().MkdirAll(basepath, 777)
hugofs.Source().Create(configPath)
err := doNewSite(basepath, true)
assert.NotNil(t, err)
}
示例5: resGetResource
// resGetResource loads the content of a local or remote file
func resGetResource(url string) ([]byte, error) {
if url == "" {
return nil, nil
}
if strings.Contains(url, "://") {
return resGetRemote(url, hugofs.Source(), http.DefaultClient)
}
return resGetLocal(url, hugofs.Source())
}
示例6: TestDoNewSite_error_base_exists
func TestDoNewSite_error_base_exists(t *testing.T) {
basepath := filepath.Join(os.TempDir(), "blog")
hugofs.InitMemFs()
hugofs.Source().MkdirAll(basepath, 777)
hugofs.Source().Create(filepath.Join(basepath, "foo"))
// Since the directory already exists and isn't empty, expect an error
err := doNewSite(basepath, false)
assert.NotNil(t, err)
}
示例7: getDirList
// getDirList provides NewWatcher() with a list of directories to watch for changes.
func getDirList() []string {
var a []string
dataDir := helpers.AbsPathify(viper.GetString("DataDir"))
layoutDir := helpers.AbsPathify(viper.GetString("LayoutDir"))
walker := func(path string, fi os.FileInfo, err error) error {
if err != nil {
if path == dataDir && os.IsNotExist(err) {
jww.WARN.Println("Skip DataDir:", err)
return nil
}
if path == layoutDir && os.IsNotExist(err) {
jww.WARN.Println("Skip LayoutDir:", err)
return nil
}
jww.ERROR.Println("Walker: ", err)
return nil
}
if fi.Mode()&os.ModeSymlink == os.ModeSymlink {
link, err := filepath.EvalSymlinks(path)
if err != nil {
jww.ERROR.Printf("Cannot read symbolic link '%s', error was: %s", path, err)
return nil
}
linkfi, err := os.Stat(link)
if err != nil {
jww.ERROR.Printf("Cannot stat '%s', error was: %s", link, err)
return nil
}
if !linkfi.Mode().IsRegular() {
jww.ERROR.Printf("Symbolic links for directories not supported, skipping '%s'", path)
}
return nil
}
if fi.IsDir() {
if fi.Name() == ".git" ||
fi.Name() == "node_modules" || fi.Name() == "bower_components" {
return filepath.SkipDir
}
a = append(a, path)
}
return nil
}
helpers.SymbolicWalk(hugofs.Source(), dataDir, walker)
helpers.SymbolicWalk(hugofs.Source(), helpers.AbsPathify(viper.GetString("ContentDir")), walker)
helpers.SymbolicWalk(hugofs.Source(), helpers.AbsPathify(viper.GetString("LayoutDir")), walker)
helpers.SymbolicWalk(hugofs.Source(), helpers.AbsPathify(viper.GetString("StaticDir")), walker)
if helpers.ThemeSet() {
helpers.SymbolicWalk(hugofs.Source(), helpers.AbsPathify(viper.GetString("themesDir")+"/"+viper.GetString("theme")), walker)
}
return a
}
示例8: testCommonResetState
func testCommonResetState() {
hugofs.InitMemFs()
viper.Reset()
viper.SetFs(hugofs.Source())
loadDefaultSettings()
// Default is false, but true is easier to use as default in tests
viper.Set("DefaultContentLanguageInSubdir", true)
if err := hugofs.Source().Mkdir("content", 0755); err != nil {
panic("Content folder creation failed.")
}
}
示例9: doNewSite
func doNewSite(basepath string, force bool) error {
dirs := []string{
filepath.Join(basepath, "layouts"),
filepath.Join(basepath, "content"),
filepath.Join(basepath, "archetypes"),
filepath.Join(basepath, "static"),
filepath.Join(basepath, "data"),
filepath.Join(basepath, "themes"),
}
if exists, _ := helpers.Exists(basepath, hugofs.Source()); exists {
if isDir, _ := helpers.IsDir(basepath, hugofs.Source()); !isDir {
return errors.New(basepath + " already exists but not a directory")
}
isEmpty, _ := helpers.IsEmpty(basepath, hugofs.Source())
switch {
case !isEmpty && !force:
return errors.New(basepath + " already exists and is not empty")
case !isEmpty && force:
all := append(dirs, filepath.Join(basepath, "config."+configFormat))
for _, path := range all {
if exists, _ := helpers.Exists(path, hugofs.Source()); exists {
return errors.New(path + " already exists")
}
}
}
}
for _, dir := range dirs {
hugofs.Source().MkdirAll(dir, 0777)
}
createConfig(basepath, configFormat)
jww.FEEDBACK.Printf("Congratulations! Your new Hugo site is created in %q.\n\n", basepath)
jww.FEEDBACK.Println(`Just a few more steps and you're ready to go:
1. Download a theme into the same-named folder. Choose a theme from https://themes.gohugo.io or
create your own with the "hugo new theme <THEMENAME>" command
2. Perhaps you want to add some content. You can add single files with "hugo new <SECTIONNAME>/<FILENAME>.<FORMAT>"
3. Start the built-in live server via "hugo server"
For more information read the documentation at https://gohugo.io.`)
return nil
}
示例10: AddTemplateFileWithMaster
func (t *GoHTMLTemplate) AddTemplateFileWithMaster(name, overlayFilename, masterFilename string) error {
// There is currently no known way to associate a cloned template with an existing one.
// This funky master/overlay design will hopefully improve in a future version of Go.
//
// Simplicity is hard.
//
// Until then we'll have to live with this hackery.
//
// See https://github.com/golang/go/issues/14285
//
// So, to do minimum amount of changes to get this to work:
//
// 1. Lookup or Parse the master
// 2. Parse and store the overlay in a separate map
masterTpl := t.Lookup(masterFilename)
if masterTpl == nil {
b, err := afero.ReadFile(hugofs.Source(), masterFilename)
if err != nil {
return err
}
masterTpl, err = t.New(masterFilename).Parse(string(b))
if err != nil {
// TODO(bep) Add a method that does this
t.errors = append(t.errors, &templateErr{name: name, err: err})
return err
}
}
b, err := afero.ReadFile(hugofs.Source(), overlayFilename)
if err != nil {
return err
}
overlayTpl, err := template.Must(masterTpl.Clone()).Parse(string(b))
if err != nil {
t.errors = append(t.errors, &templateErr{name: name, err: err})
} else {
// The extra lookup is a workaround, see
// * https://github.com/golang/go/issues/16101
// * https://github.com/spf13/hugo/issues/2549
t.overlays[name] = overlayTpl.Lookup(overlayTpl.Name())
}
return err
}
示例11: NewContent
// NewContent adds new content to a Hugo site.
func NewContent(cmd *cobra.Command, args []string) error {
if err := InitializeConfig(); err != nil {
return err
}
if flagChanged(cmd.Flags(), "format") {
viper.Set("MetaDataFormat", configFormat)
}
if flagChanged(cmd.Flags(), "editor") {
viper.Set("NewContentEditor", contentEditor)
}
if len(args) < 1 {
return newUserError("path needs to be provided")
}
createpath := args[0]
var kind string
createpath, kind = newContentPathSection(createpath)
if contentType != "" {
kind = contentType
}
return create.NewContent(hugofs.Source(), kind, createpath)
}
示例12: createThemeMD
func createThemeMD(inpath string) (err error) {
by := []byte(`# theme.toml template for a Hugo theme
# See https://github.com/spf13/hugoThemes#themetoml for an example
name = "` + strings.Title(helpers.MakeTitle(filepath.Base(inpath))) + `"
license = "MIT"
licenselink = "https://github.com/yourname/yourtheme/blob/master/LICENSE.md"
description = ""
homepage = "http://siteforthistheme.com/"
tags = ["", ""]
features = ["", ""]
min_version = 0.15
[author]
name = ""
homepage = ""
# If porting an existing theme
[original]
name = ""
homepage = ""
repo = ""
`)
err = helpers.WriteToDisk(filepath.Join(inpath, "theme.toml"), bytes.NewReader(by), hugofs.Source())
if err != nil {
return
}
return nil
}
示例13: getCSV
// getCSV expects a data separator and one or n-parts of a URL to a resource which
// can either be a local or a remote one.
// The data separator can be a comma, semi-colon, pipe, etc, but only one character.
// If you provide multiple parts for the URL they will be joined together to the final URL.
// GetCSV returns nil or a slice slice to use in a short code.
func getCSV(sep string, urlParts ...string) [][]string {
var d [][]string
url := strings.Join(urlParts, "")
var clearCacheSleep = func(i int, u string) {
jww.ERROR.Printf("Retry #%d for %s and sleeping for %s", i, url, resSleep)
time.Sleep(resSleep)
resDeleteCache(url, hugofs.Source())
}
for i := 0; i <= resRetries; i++ {
c, err := resGetResource(url)
if err == nil && false == bytes.Contains(c, []byte(sep)) {
err = errors.New("Cannot find separator " + sep + " in CSV.")
}
if err != nil {
jww.ERROR.Printf("Failed to read csv resource %s with error message %s", url, err)
clearCacheSleep(i, url)
continue
}
if d, err = parseCSV(c, sep); err != nil {
jww.ERROR.Printf("Failed to parse csv file %s with error message %s", url, err)
clearCacheSleep(i, url)
continue
}
break
}
return d
}
示例14: saveSource
func (p *Page) saveSource(by []byte, inpath string, safe bool) (err error) {
if !filepath.IsAbs(inpath) {
inpath = helpers.AbsPathify(inpath)
}
jww.INFO.Println("creating", inpath)
if safe {
err = helpers.SafeWriteToDisk(inpath, bytes.NewReader(by), hugofs.Source())
} else {
err = helpers.WriteToDisk(inpath, bytes.NewReader(by), hugofs.Source())
}
if err != nil {
return
}
return nil
}
示例15: loadJekyllConfig
func loadJekyllConfig(jekyllRoot string) map[string]interface{} {
fs := hugofs.Source()
path := filepath.Join(jekyllRoot, "_config.yml")
exists, err := helpers.Exists(path, fs)
if err != nil || !exists {
jww.WARN.Println("_config.yaml not found: Is the specified Jekyll root correct?")
return nil
}
f, err := fs.Open(path)
if err != nil {
return nil
}
defer f.Close()
b, err := ioutil.ReadAll(f)
if err != nil {
return nil
}
c, err := parser.HandleYAMLMetaData(b)
if err != nil {
return nil
}
return c.(map[string]interface{})
}