Exemple #1
0
func ProcessExternal(page *gostatic.Page, args []string) error {
	if len(args) < 1 {
		return errors.New("'external' rule needs a command name")
	}
	cmdName := args[0]
	cmdArgs := args[1:]

	path, err := exec.LookPath(cmdName)
	if err != nil {
		path, err = exec.LookPath(filepath.Join(page.Site.Base, cmdName))
		if err != nil {
			return fmt.Errorf("command '%s' not found", cmdName)
		}
	}

	cmd := exec.Command(path, cmdArgs...)
	cmd.Stdin = strings.NewReader(page.Content())
	cmd.Dir = page.Site.Base
	var stderr bytes.Buffer
	cmd.Stderr = &stderr
	data, err := cmd.Output()
	if err != nil {
		return fmt.Errorf("'%s' failed: %s\n%s",
			strings.Join(args, " "), err, stderr.String())
	}

	page.SetContent(string(data))
	return nil
}
Exemple #2
0
func ProcessInnerTemplate(page *gostatic.Page, args []string) error {
	//todo catch
	defer func() {
		if err := recover(); err != nil {
			//return fmt.Sprintf("%s: %s", page.Source, err)
		}
	}()

	t, err := page.Site.Template.Clone()
	if err != nil {
		return err
	}
	t, err = t.New("ad-hoc").Parse(page.Content())
	if err != nil {
		return errors.New(fmt.Sprintf("Page %s: %s", page.Source, err))
	}

	var buffer bytes.Buffer
	err = t.ExecuteTemplate(&buffer, "ad-hoc", page)
	if err != nil {
		return errors.New(fmt.Sprintf("Page %s: %s", page.Source, err))
	}

	page.SetContent(buffer.String())
	return nil
}
Exemple #3
0
func ProcessExt(page *gostatic.Page, args []string) error {
	if len(args) < 1 {
		return errors.New(
			"'ext' rule requires an extension prefixed with dot")
	}
	newExt := args[0]

	ext := filepath.Ext(page.Path)
	if ext == "" {
		page.Path = page.Path + newExt
	} else {
		page.Path = page.Path[0:len(page.Path)-len(ext)] + newExt
	}
	return nil
}
Exemple #4
0
func ProcessTemplate(page *gostatic.Page, args []string) error {
	if len(args) < 1 {
		return errors.New("'template' rule needs an argument")
	}
	pagetype := args[0]
	//todo catch thiss
	defer func() {
		if err := recover(); err != nil {
			//return err //errors.New(fmt.Sprintf("%s: %s", page.Source, err))
		}
	}()

	var buffer bytes.Buffer
	err := page.Site.Template.ExecuteTemplate(&buffer, pagetype, page)
	if err != nil {
		return errors.New(fmt.Sprintf("%s: %s", page.Source, err))
	}

	page.SetContent(buffer.String())
	return nil
}
Exemple #5
0
// Runs some checks against the parsed page.
func checkPage(t *testing.T, pageText string, page *gostatic.Page) {
	if page.Content() != pageText {
		t.Errorf("expected '%s', got '%s'", pageText, page.Content())
	}

	if len(page.PageHeader.Other) != 2 {
		t.Errorf("expected length of 2, got '%d'", len(page.PageHeader.Other))
	}

	// Note: gostatic lib capitalizes the properties for some reason. So it
	// converted 'salve' to 'Salve' etc.
	salve := page.PageHeader.Other["Salve"]
	if salve != "hello" {
		t.Errorf("expected 'hello', got '%s'", salve)
	}

	munde := page.PageHeader.Other["Munde"]
	if munde != "world" {
		t.Errorf("expected 'world', got '%s'", munde)
	}
}
Exemple #6
0
func ProcessRelativize(page *gostatic.Page, args []string) error {
	repl := `$1="` + page.Rel("/") + `$2"`
	rv := RelRe.ReplaceAllStringFunc(page.Content(), func(inp string) string {
		if NonProtoRe.MatchString(inp) {
			return inp
		}
		return RelRe.ReplaceAllString(inp, repl)
	})
	page.SetContent(rv)
	return nil
}
func ProcessConfig(page *gostatic.Page, args []string) error {
	parts := gostatic.TrimSplitN(page.Content(), "\n----\n", 2)
	if len(parts) != 2 {
		// no configuration, well then...
		page.PageHeader = *gostatic.NewPageHeader()
		return nil
	}

	page.PageHeader = *gostatic.ParseHeader(parts[0])
	page.SetContent(parts[1])
	return nil
}
Exemple #8
0
func ProcessMarkdown(page *gostatic.Page, args []string) error {
	result := Markdown(page.Content())
	page.SetContent(result)
	return nil
}
Exemple #9
0
func ProcessDirectorify(page *gostatic.Page, args []string) error {
	if filepath.Base(page.Path) != "index.html" {
		page.Path = strings.Replace(page.Path, ".html", "/index.html", 1)
	}
	return nil
}
Exemple #10
0
func ProcessIgnore(page *gostatic.Page, args []string) error {
	page.SetState(gostatic.StateIgnored)
	return nil
}