Ejemplo n.º 1
0
func (p *Page) Menus() PageMenus {
	p.pageMenusInit.Do(func() {
		p.pageMenus = PageMenus{}

		if ms, ok := p.Params["menu"]; ok {
			link, _ := p.RelPermalink()

			me := MenuEntry{Name: p.LinkTitle(), Weight: p.Weight, URL: link}

			// Could be the name of the menu to attach it to
			mname, err := cast.ToStringE(ms)

			if err == nil {
				me.Menu = mname
				p.pageMenus[mname] = &me
				return
			}

			// Could be a slice of strings
			mnames, err := cast.ToStringSliceE(ms)

			if err == nil {
				for _, mname := range mnames {
					me.Menu = mname
					p.pageMenus[mname] = &me
				}
				return
			}

			// Could be a structured menu entry
			menus, err := cast.ToStringMapE(ms)

			if err != nil {
				jww.ERROR.Printf("unable to process menus for %q\n", p.Title)
			}

			for name, menu := range menus {
				menuEntry := MenuEntry{Name: p.LinkTitle(), URL: link, Weight: p.Weight, Menu: name}
				if menu != nil {
					jww.DEBUG.Printf("found menu: %q, in %q\n", name, p.Title)
					ime, err := cast.ToStringMapE(menu)
					if err != nil {
						jww.ERROR.Printf("unable to process menus for %q: %s", p.Title, err)
					}

					menuEntry.marshallMap(ime)
				}
				p.pageMenus[name] = &menuEntry

			}
		}
	})

	return p.pageMenus
}
Ejemplo n.º 2
0
Archivo: page.go Proyecto: vinchu/hugo
func (page *Page) Menus() PageMenus {
	ret := PageMenus{}

	if ms, ok := page.Params["menu"]; ok {
		link, _ := page.Permalink()

		me := MenuEntry{Name: page.LinkTitle(), Weight: page.Weight, Url: link}

		// Could be the name of the menu to attach it to
		mname, err := cast.ToStringE(ms)

		if err == nil {
			me.Menu = mname
			ret[mname] = &me
			return ret
		}

		// Could be an slice of strings
		mnames, err := cast.ToStringSliceE(ms)

		if err == nil {
			for _, mname := range mnames {
				me.Menu = mname
				ret[mname] = &me
				return ret
			}
		}

		// Could be a structured menu entry
		menus, err := cast.ToStringMapE(ms)

		if err != nil {
			jww.ERROR.Printf("unable to process menus for %q\n", page.Title)
		}

		for name, menu := range menus {
			menuEntry := MenuEntry{Name: page.LinkTitle(), Url: link, Weight: page.Weight, Menu: name}
			jww.DEBUG.Printf("found menu: %q, in %q\n", name, page.Title)

			ime, err := cast.ToStringMapE(menu)
			if err != nil {
				jww.ERROR.Printf("unable to process menus for %q\n", page.Title)
			}

			menuEntry.MarshallMap(ime)
			ret[name] = &menuEntry
		}
		return ret
	}

	return nil
}
Ejemplo n.º 3
0
func (p Page) contentRead(filename string) (*Content, error) {
	file, err := os.Open(filename)
	if err != nil {
		return nil, err
	}
	defer file.Close()

	parser, err := parser.ReadFrom(file)
	if err != nil {
		return nil, err
	}

	rawdata, err := parser.Metadata()
	if err != nil {
		return nil, err
	}

	metadata, err := cast.ToStringMapE(rawdata)
	if err != nil {
		return nil, err
	}

	return &Content{
		Path:     filename,
		Metadata: metadata,
		Body:     string(parser.Content()),
	}, nil
}
Ejemplo n.º 4
0
Archivo: site.go Proyecto: johnsto/hugo
func (s *Site) getMenusFromConfig() Menus {

	ret := Menus{}

	if menus := viper.GetStringMap("menu"); menus != nil {
		for name, menu := range menus {
			m, err := cast.ToSliceE(menu)
			if err != nil {
				jww.ERROR.Printf("unable to process menus in site config\n")
				jww.ERROR.Println(err)
			} else {
				for _, entry := range m {
					jww.DEBUG.Printf("found menu: %q, in site config\n", name)

					menuEntry := MenuEntry{Menu: name}
					ime, err := cast.ToStringMapE(entry)
					if err != nil {
						jww.ERROR.Printf("unable to process menus in site config\n")
						jww.ERROR.Println(err)
					}

					menuEntry.MarshallMap(ime)
					if ret[name] == nil {
						ret[name] = &Menu{}
					}
					*ret[name] = ret[name].Add(&menuEntry)
				}
			}
		}
		return ret
	}
	return ret
}
Ejemplo n.º 5
0
func convertJekyllContent(m interface{}, content string) string {
	metadata, _ := cast.ToStringMapE(m)

	lines := strings.Split(content, "\n")
	var resultLines []string
	for _, line := range lines {
		resultLines = append(resultLines, strings.Trim(line, "\r\n"))
	}

	content = strings.Join(resultLines, "\n")

	excerptSep := "<!--more-->"
	if value, ok := metadata["excerpt_separator"]; ok {
		if str, strOk := value.(string); strOk {
			content = strings.Replace(content, strings.TrimSpace(str), excerptSep, -1)
		}
	}

	replaceList := []struct {
		re      *regexp.Regexp
		replace string
	}{
		{regexp.MustCompile("<!-- more -->"), "<!--more-->"},
		{regexp.MustCompile(`\{%\s*raw\s*%\}\s*(.*?)\s*\{%\s*endraw\s*%\}`), "$1"},
		{regexp.MustCompile(`{%\s*highlight\s*(.*?)\s*%}`), "{{< highlight $1 >}}"},
		{regexp.MustCompile(`{%\s*endhighlight\s*%}`), "{{< / highlight >}}"},
	}

	for _, replace := range replaceList {
		content = replace.re.ReplaceAllString(content, replace.replace)
	}

	return content
}
Ejemplo n.º 6
0
func toSortedLanguages(l map[string]interface{}) (helpers.Languages, error) {
	langs := make(helpers.Languages, len(l))
	i := 0

	for lang, langConf := range l {
		langsMap, err := cast.ToStringMapE(langConf)

		if err != nil {
			return nil, fmt.Errorf("Language config is not a map: %T", langConf)
		}

		language := helpers.NewLanguage(lang)

		for loki, v := range langsMap {
			switch loki {
			case "title":
				language.Title = cast.ToString(v)
			case "languagename":
				language.LanguageName = cast.ToString(v)
			case "weight":
				language.Weight = cast.ToInt(v)
			}

			// Put all into the Params map
			language.SetParam(loki, v)
		}

		langs[i] = language
		i++
	}

	sort.Sort(langs)

	return langs, nil
}
Ejemplo n.º 7
0
func mapify(i interface{}) (map[string]interface{}, bool) {
	v, err := cast.ToStringMapE(i)
	if err != nil {
		return v, false
	} else {
		return v, true
	}
}
Ejemplo n.º 8
0
// Recursively coerces all maps to a stringmap. Because that's how we want it.
func ToStringMapRecursive(src map[string]interface{}) {
	for key, val := range src {
		coerced, err := cast.ToStringMapE(val)
		if err == nil {
			src[key] = coerced
			ToStringMapRecursive(coerced)
		}
	}
}
Ejemplo n.º 9
0
func convertJekyllMetaData(m interface{}, postName string, postDate time.Time, draft bool) (interface{}, error) {
	url := postDate.Format("/2006/01/02/") + postName + "/"

	metadata, err := cast.ToStringMapE(m)
	if err != nil {
		return nil, err
	}

	if draft {
		metadata["draft"] = true
	}

	for key, value := range metadata {
		lowerKey := strings.ToLower(key)

		switch lowerKey {
		case "layout":
			delete(metadata, key)
		case "permalink":
			if str, ok := value.(string); ok {
				url = str
			}
			delete(metadata, key)
		case "category":
			if str, ok := value.(string); ok {
				metadata["categories"] = []string{str}
			}
			delete(metadata, key)
		case "excerpt_separator":
			if key != lowerKey {
				delete(metadata, key)
				metadata[lowerKey] = value
			}
		case "date":
			if str, ok := value.(string); ok {
				re := regexp.MustCompile(`(\d+):(\d+):(\d+)`)
				r := re.FindAllStringSubmatch(str, -1)
				if len(r) > 0 {
					hour, _ := strconv.Atoi(r[0][1])
					minute, _ := strconv.Atoi(r[0][2])
					second, _ := strconv.Atoi(r[0][3])
					postDate = time.Date(postDate.Year(), postDate.Month(), postDate.Day(), hour, minute, second, 0, time.UTC)
				}
			}
			delete(metadata, key)
		}

	}

	metadata["url"] = url
	metadata["date"] = postDate.Format(time.RFC3339)

	return metadata, nil
}
Ejemplo n.º 10
0
// createMetadata generates Metadata for a new page based upon the metadata
// found in an archetype.
func createMetadata(archetype parser.Page, name string) (map[string]interface{}, error) {
	archMetadata, err := archetype.Metadata()
	if err != nil {
		return nil, err
	}

	metadata, err := cast.ToStringMapE(archMetadata)
	if err != nil {
		return nil, err
	}

	for k := range metadata {
		switch strings.ToLower(k) {
		case "date":
			metadata[k] = time.Now()
		case "title":
			metadata[k] = helpers.MakeTitle(helpers.Filename(name))
		}
	}

	caseimatch := func(m map[string]interface{}, key string) bool {
		for k := range m {
			if strings.ToLower(k) == strings.ToLower(key) {
				return true
			}
		}
		return false
	}

	if metadata == nil {
		metadata = make(map[string]interface{})
	}

	if !caseimatch(metadata, "date") {
		metadata["date"] = time.Now()
	}

	if !caseimatch(metadata, "title") {
		metadata["title"] = helpers.MakeTitle(helpers.Filename(name))
	}

	if x := parser.FormatSanitize(viper.GetString("metaDataFormat")); x == "json" || x == "yaml" || x == "toml" {
		metadata["date"] = time.Now().Format(time.RFC3339)
	}

	return metadata, nil
}
Ejemplo n.º 11
0
Archivo: site.go Proyecto: jaden/hugo
func (s *Site) getMenusFromConfig() Menus {

	ret := Menus{}

	if menus := viper.GetStringMap("menu"); menus != nil {
		for name, menu := range menus {
			m, err := cast.ToSliceE(menu)
			if err != nil {
				jww.ERROR.Printf("unable to process menus in site config\n")
				jww.ERROR.Println(err)
			} else {
				for _, entry := range m {
					jww.DEBUG.Printf("found menu: %q, in site config\n", name)

					menuEntry := MenuEntry{Menu: name}
					ime, err := cast.ToStringMapE(entry)
					if err != nil {
						jww.ERROR.Printf("unable to process menus in site config\n")
						jww.ERROR.Println(err)
					}

					menuEntry.MarshallMap(ime)

					if strings.HasPrefix(menuEntry.Url, "/") {
						// make it match the nodes
						menuEntryUrl := menuEntry.Url
						menuEntryUrl = helpers.UrlizeAndPrep(menuEntryUrl)
						if !s.Info.canonifyUrls {
							menuEntryUrl = helpers.AddContextRoot(string(s.Info.BaseUrl), menuEntryUrl)
						}
						menuEntry.Url = menuEntryUrl
					}

					if ret[name] == nil {
						ret[name] = &Menu{}
					}
					*ret[name] = ret[name].Add(&menuEntry)
				}
			}
		}
		return ret
	}
	return ret
}
Ejemplo n.º 12
0
func (c *FlatMapConfig) Flatten(src interface{}) (map[string]interface{}, error) {
	var err error

	result := make(map[string]interface{})

	if reflect.TypeOf(src).Kind() == reflect.Map {
		var s map[string]interface{}
		s, err = cast.ToStringMapE(src)
		if err != nil {
			return result, err
		}

		for k, raw := range s {
			err = c.flatten(result, k, reflect.ValueOf(raw))
			if err != nil {
				break
			}
		}

	} else if reflect.TypeOf(src).Kind() == reflect.Slice {
		var s []interface{}
		s, err = cast.ToSliceE(src)
		if err != nil {
			return result, err
		}

		for i, raw := range s {
			err = c.flatten(result, c.sliceKeyTemplate(i), reflect.ValueOf(raw))
			if err != nil {
				break
			}
		}
	} else {
		err = fmt.Errorf("Unknown type for interface")
	}

	return result, err
}
Ejemplo n.º 13
0
func getMetadata(path string) (map[string]interface{}, string, error) {
	contentBytes, err := ioutil.ReadFile(path)
	if err != nil {
		return nil, "", err
	}

	psr, err := parser.ReadFrom(bytes.NewReader(contentBytes))
	if err != nil {
		return nil, "", err
	}

	m, err := psr.Metadata()
	if err != nil {
		return nil, "", err
	}

	metadata, err := cast.ToStringMapE(m)
	if err != nil {
		return nil, "", err
	}

	return metadata, string(psr.Content()), nil
}
Ejemplo n.º 14
0
Archivo: site.go Proyecto: jempe/hugo
func (s *Site) getMenusFromConfig() Menus {

	ret := Menus{}

	if menus := viper.GetStringMap("menu"); menus != nil {
		for name, menu := range menus {
			m, err := cast.ToSliceE(menu)
			if err != nil {
				jww.ERROR.Printf("unable to process menus in site config\n")
				jww.ERROR.Println(err)
			} else {
				for _, entry := range m {
					jww.DEBUG.Printf("found menu: %q, in site config\n", name)

					menuEntry := MenuEntry{Menu: name}
					ime, err := cast.ToStringMapE(entry)
					if err != nil {
						jww.ERROR.Printf("unable to process menus in site config\n")
						jww.ERROR.Println(err)
					}

					menuEntry.MarshallMap(ime)
					if strings.HasPrefix(menuEntry.Url, "/") {
						// make it absolute so it matches the nodes
						menuEntry.Url = s.permalinkStr(menuEntry.Url)
					}
					if ret[name] == nil {
						ret[name] = &Menu{}
					}
					*ret[name] = ret[name].Add(&menuEntry)
				}
			}
		}
		return ret
	}
	return ret
}
Ejemplo n.º 15
0
func NewContent(kind, name string) (err error) {
	jww.INFO.Println("attempting to create ", name, "of", kind)

	location := FindArchetype(kind)

	var by []byte

	if location != "" {
		by, err = ioutil.ReadFile(location)
		if err != nil {
			jww.ERROR.Println(err)
		}
	}
	if location == "" || err != nil {
		by = []byte("+++\n title = \"title\"\n draft = true \n+++\n")
	}

	psr, err := parser.ReadFrom(bytes.NewReader(by))
	if err != nil {
		return err
	}
	metadata, err := psr.Metadata()
	if err != nil {
		return err
	}
	newmetadata, err := cast.ToStringMapE(metadata)
	if err != nil {
		return err
	}

	for k, _ := range newmetadata {
		switch strings.ToLower(k) {
		case "date":
			newmetadata[k] = time.Now()
		case "title":
			newmetadata[k] = helpers.MakeTitle(helpers.Filename(name))
		}
	}

	caseimatch := func(m map[string]interface{}, key string) bool {
		for k, _ := range m {
			if strings.ToLower(k) == strings.ToLower(key) {
				return true
			}
		}
		return false
	}

	if !caseimatch(newmetadata, "date") {
		newmetadata["date"] = time.Now()
	}

	if !caseimatch(newmetadata, "title") {
		newmetadata["title"] = helpers.MakeTitle(helpers.Filename(name))
	}

	page, err := hugolib.NewPage(name)
	if err != nil {
		return err
	}

	if x := viper.GetString("MetaDataFormat"); x == "json" || x == "yaml" || x == "toml" {
		newmetadata["date"] = time.Now().Format(time.RFC3339)
	}

	page.Dir = viper.GetString("sourceDir")
	page.SetSourceMetaData(newmetadata, parser.FormatToLeadRune(viper.GetString("MetaDataFormat")))

	if err = page.SafeSaveSourceAs(path.Join(viper.GetString("contentDir"), name)); err != nil {
		return
	}
	jww.FEEDBACK.Println(helpers.AbsPathify(path.Join(viper.GetString("contentDir"), name)), "created")

	return nil
}