本文整理汇总了Golang中github.com/spf13/hugo/parser.ReadFrom函数的典型用法代码示例。如果您正苦于以下问题:Golang ReadFrom函数的具体用法?Golang ReadFrom怎么用?Golang ReadFrom使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ReadFrom函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestUndraftContent
func TestUndraftContent(t *testing.T) {
tests := []struct {
fm string
expectedErr string
}{
{jsonFM, "not a Draft: nothing was done"},
{jsonDraftFM, ""},
{tomlFM, "not a Draft: nothing was done"},
{tomlDraftFM, ""},
{yamlFM, "not a Draft: nothing was done"},
{yamlDraftFM, ""},
}
for _, test := range tests {
r := bytes.NewReader([]byte(test.fm))
p, _ := parser.ReadFrom(r)
res, err := undraftContent(p)
if test.expectedErr != "" {
if err == nil {
t.Error("Expected error, got none")
continue
}
if err.Error() != test.expectedErr {
t.Errorf("Expected %q, got %q", test.expectedErr, err)
continue
}
} else {
r = bytes.NewReader(res.Bytes())
p, _ = parser.ReadFrom(r)
meta, err := p.Metadata()
if err != nil {
t.Errorf("unexpected error %q", err)
continue
}
for k, v := range meta.(map[string]interface{}) {
if k == "draft" {
if v.(bool) {
t.Errorf("Expected %q to be \"false\", got \"true\"", k)
continue
}
}
if k == "date" {
if !strings.HasPrefix(v.(string), time.Now().Format("2006-01-02")) {
t.Errorf("Expected %v to start with %v", v.(string), time.Now().Format("2006-01-02"))
}
}
}
}
}
}
示例2: parse
func (p *Page) parse(reader io.Reader) error {
psr, err := parser.ReadFrom(reader)
if err != nil {
return err
}
p.renderable = psr.IsRenderable()
p.frontmatter = psr.FrontMatter()
p.rawContent = psr.Content()
p.lang = p.Source.File.Lang()
meta, err := psr.Metadata()
if meta != nil {
if err != nil {
jww.ERROR.Printf("Error parsing page meta data for %s", p.File.Path())
jww.ERROR.Println(err)
return err
}
if err = p.update(meta); err != nil {
return err
}
}
return nil
}
示例3: parse
func (page *Page) parse(reader io.Reader) error {
psr, err := parser.ReadFrom(reader)
if err != nil {
return err
}
page.renderable = psr.IsRenderable()
page.frontmatter = psr.FrontMatter()
meta, err := psr.Metadata()
if meta != nil {
if err != nil {
jww.ERROR.Printf("Error parsing page meta data for %s", page.FileName)
jww.ERROR.Println(err)
return err
}
if err = page.update(meta); err != nil {
return err
}
}
page.rawContent = psr.Content()
page.setSummary()
return nil
}
示例4: parse
func (page *Page) parse(reader io.Reader) error {
p, err := parser.ReadFrom(reader)
if err != nil {
return err
}
page.renderable = p.IsRenderable()
front := p.FrontMatter()
if len(front) != 0 {
fm := page.detectFrontMatter(rune(front[0]))
meta, err := fm.parse(front)
if err != nil {
return err
}
if err = page.update(meta); err != nil {
return err
}
}
page.rawContent = p.Content()
page.setSummary()
return nil
}
示例5: parse
func (page *Page) parse(reader io.Reader) error {
p, err := parser.ReadFrom(reader)
if err != nil {
return err
}
page.renderable = p.IsRenderable()
front := p.FrontMatter()
if len(front) != 0 {
fm := page.detectFrontMatter(rune(front[0]))
meta, err := fm.parse(front)
if err != nil {
return err
}
if err = page.update(meta); err != nil {
return err
}
}
switch page.guessMarkupType() {
case "md", "markdown", "mdown":
page.convertMarkdown(bytes.NewReader(p.Content()))
case "rst":
page.convertRestructuredText(bytes.NewReader(p.Content()))
case "html":
fallthrough
default:
page.Content = template.HTML(p.Content())
}
return nil
}
示例6: contentRead
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
}
示例7: NewContent
// NewContent creates a new content file in the content directory based upon the
// given kind, which is used to lookup an archetype.
func NewContent(fs afero.Fs, kind, name string) (err error) {
jww.INFO.Println("attempting to create ", name, "of", kind)
location := FindArchetype(fs, kind)
var by []byte
if location != "" {
by, err = afero.ReadFile(fs, 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 := createMetadata(psr, name)
if err != nil {
jww.ERROR.Printf("Error processing archetype file %s: %s\n", location, err)
return err
}
page, err := hugolib.NewPage(name)
if err != nil {
return err
}
if err = page.SetSourceMetaData(metadata, parser.FormatToLeadRune(viper.GetString("metaDataFormat"))); err != nil {
return
}
page.SetSourceContent(psr.Content())
if err = page.SafeSaveSourceAs(filepath.Join(viper.GetString("contentDir"), name)); err != nil {
return
}
jww.FEEDBACK.Println(helpers.AbsPathify(filepath.Join(viper.GetString("contentDir"), name)), "created")
editor := viper.GetString("newContentEditor")
if editor != "" {
jww.FEEDBACK.Printf("Editing %s with %q ...\n", name, editor)
cmd := exec.Command(editor, helpers.AbsPathify(path.Join(viper.GetString("contentDir"), name)))
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
return nil
}
示例8: convertJekyllPost
func convertJekyllPost(path, relPath, targetDir string, draft bool) error {
jww.TRACE.Println("Converting", path)
filename := filepath.Base(path)
postDate, postName, err := parseJekyllFilename(filename)
if err != nil {
jww.WARN.Printf("Failed to parse filename '%s': %s. Skipping.", filename, err)
return nil
}
jww.TRACE.Println(filename, postDate, postName)
targetFile := filepath.Join(targetDir, relPath)
targetParentDir := filepath.Dir(targetFile)
os.MkdirAll(targetParentDir, 0777)
contentBytes, err := ioutil.ReadFile(path)
if err != nil {
jww.ERROR.Println("Read file error:", path)
return err
}
psr, err := parser.ReadFrom(bytes.NewReader(contentBytes))
if err != nil {
jww.ERROR.Println("Parse file error:", path)
return err
}
metadata, err := psr.Metadata()
if err != nil {
jww.ERROR.Println("Processing file error:", path)
return err
}
newmetadata, err := convertJekyllMetaData(metadata, postName, postDate, draft)
if err != nil {
jww.ERROR.Println("Convert metadata error:", path)
return err
}
jww.TRACE.Println(newmetadata)
content := convertJekyllContent(newmetadata, string(psr.Content()))
page, err := hugolib.NewPage(filename)
if err != nil {
jww.ERROR.Println("New page error", filename)
return err
}
page.SetDir(targetParentDir)
page.SetSourceContent([]byte(content))
page.SetSourceMetaData(newmetadata, parser.FormatToLeadRune("yaml"))
page.SaveSourceAs(targetFile)
jww.TRACE.Println("Target file:", targetFile)
return nil
}
示例9: convertContents
func convertContents(mark rune) (err error) {
InitializeConfig()
site := &hugolib.Site{}
if err := site.Initialise(); err != nil {
return err
}
if site.Source == nil {
panic(fmt.Sprintf("site.Source not set"))
}
if len(site.Source.Files()) < 1 {
return fmt.Errorf("No source files found")
}
jww.FEEDBACK.Println("processing", len(site.Source.Files()), "content files")
for _, file := range site.Source.Files() {
jww.INFO.Println("Attempting to convert", file.LogicalName)
page, err := hugolib.NewPage(file.LogicalName)
if err != nil {
return err
}
psr, err := parser.ReadFrom(file.Contents)
if err != nil {
jww.ERROR.Println("Error processing file:", path.Join(file.Dir, file.LogicalName))
return err
}
metadata, err := psr.Metadata()
if err != nil {
jww.ERROR.Println("Error processing file:", path.Join(file.Dir, file.LogicalName))
return err
}
page.Dir = file.Dir
page.SetSourceContent(psr.Content())
page.SetSourceMetaData(metadata, mark)
if OutputDir != "" {
page.SaveSourceAs(path.Join(OutputDir, page.FullFilePath()))
} else {
if Unsafe {
page.SaveSource()
} else {
jww.FEEDBACK.Println("Unsafe operation not allowed, use --unsafe or set a different output path")
}
}
}
return
}
示例10: Undraft
// Publish publishes the specified content by setting its draft status
// to false and setting its publish date to now. If the specified content is
// not a draft, it will log an error.
func Undraft(cmd *cobra.Command, args []string) {
InitializeConfig()
if len(args) < 1 {
cmd.Usage()
jww.FATAL.Fatalln("a piece of content needs to be specified")
}
location := args[0]
// open the file
f, err := os.Open(location)
if err != nil {
jww.ERROR.Print(err)
return
}
// get the page from file
p, err := parser.ReadFrom(f)
f.Close()
if err != nil {
jww.ERROR.Print(err)
return
}
w, err := undraftContent(p)
if err != nil {
jww.ERROR.Printf("an error occurred while undrafting %q: %s", location, err)
return
}
f, err = os.OpenFile(location, os.O_WRONLY|os.O_TRUNC, 0644)
if err != nil {
jww.ERROR.Printf("%q not be undrafted due to error opening file to save changes: %q\n", location, err)
return
}
defer f.Close()
_, err = w.WriteTo(f)
if err != nil {
jww.ERROR.Printf("%q not be undrafted due to save error: %q\n", location, err)
}
return
}
示例11: Undraft
// Undraft publishes the specified content by setting its draft status
// to false and setting its publish date to now. If the specified content is
// not a draft, it will log an error.
func Undraft(cmd *cobra.Command, args []string) error {
if err := InitializeConfig(); err != nil {
return err
}
if len(args) < 1 {
return newUserError("a piece of content needs to be specified")
}
location := args[0]
// open the file
f, err := hugofs.Source().Open(location)
if err != nil {
return err
}
// get the page from file
p, err := parser.ReadFrom(f)
f.Close()
if err != nil {
return err
}
w, err := undraftContent(p)
if err != nil {
return newSystemErrorF("an error occurred while undrafting %q: %s", location, err)
}
f, err = hugofs.Source().OpenFile(location, os.O_WRONLY|os.O_TRUNC, 0644)
if err != nil {
return newSystemErrorF("%q not be undrafted due to error opening file to save changes: %q\n", location, err)
}
defer f.Close()
_, err = w.WriteTo(f)
if err != nil {
return newSystemErrorF("%q not be undrafted due to save error: %q\n", location, err)
}
return nil
}
示例12: getMetadata
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
}
示例13: loadComicDetails
func (s *server) loadComicDetails() error {
s.comicDB = make(map[string]*Comic)
matches, err := filepath.Glob("./comics/*.md")
if err != nil {
return err
}
for _, match := range matches {
file, err := os.Open(match)
if err != nil {
return err
}
basename := filepath.Base(match)
slug := strings.TrimSuffix(basename, filepath.Ext(basename))
comic := &Comic{
Slug: slug,
}
page, err := parser.ReadFrom(file)
if err != nil {
return err
}
metaData, err := page.Metadata()
if err != nil {
return err
}
jsonStr, err := json.Marshal(metaData)
if err != nil {
return err
}
if err := json.Unmarshal(jsonStr, &comic); err != nil {
return err
}
comic.Description = string(blackfriday.MarkdownCommon(page.Content()))
s.comicDB[comic.Slug] = comic
}
return nil
}
示例14: NewContent
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
}
示例15: processFile
func processFile(filename string, in io.ReadSeeker, out io.Writer, stdin bool) error {
if in == nil {
f, err := os.Open(filename)
if err != nil {
return err
}
defer f.Close()
in = f
}
// slurp in the whole file for comparison later
src, err := ioutil.ReadAll(in)
if err != nil {
return err
}
in.Seek(0, 0)
// parse the file with hugo/parser to extract front matter
page, err := parser.ReadFrom(in)
if err != nil {
return err
}
md, err := markdown.Process(filename, page.Content(), nil)
if err != nil {
return err
}
// If we have front matter, insert a newline to separate the front matter
// from the markdown content.
sep := ""
if len(page.FrontMatter()) > 0 {
sep = "\n"
}
res := make([]byte, len(page.FrontMatter())+len(sep)+len(md))
copy(res, append(append(page.FrontMatter(), []byte(sep)...), md...))
if !bytes.Equal(src, res) {
// formatting has changed
if *list {
fmt.Fprintln(out, filename)
}
if *write {
err = ioutil.WriteFile(filename, res, 0)
if err != nil {
return err
}
}
if *doDiff {
data, err := diff(src, res)
if err != nil {
return fmt.Errorf("computing diff: %s", err)
}
fmt.Printf("diff %s mdfmt/%s\n", filename, filename)
out.Write(data)
}
}
if !*list && !*write && !*doDiff {
_, err = out.Write(res)
}
return err
}