providers: Add import/include provider
This commit is contained in:
		
							parent
							
								
									e6415a250b
								
							
						
					
					
						commit
						17bb6a383b
					
				@ -4,6 +4,7 @@ import "git.tebibyte.media/sashakoshka/step"
 | 
			
		||||
import fpos       "git.tebibyte.media/sashakoshka/step/providers/os"
 | 
			
		||||
import fphttp     "git.tebibyte.media/sashakoshka/step/providers/http"
 | 
			
		||||
import fpsprig    "git.tebibyte.media/sashakoshka/step/providers/sprig"
 | 
			
		||||
import fpimport   "git.tebibyte.media/sashakoshka/step/providers/import"
 | 
			
		||||
import fpmarkdown "git.tebibyte.media/sashakoshka/step/providers/markdown"
 | 
			
		||||
 | 
			
		||||
// All returns a slice of all providers defined in sub-packages.
 | 
			
		||||
@ -12,6 +13,7 @@ func All () []step.FuncProvider {
 | 
			
		||||
		new(fpos.Provider),
 | 
			
		||||
		new(fphttp.Provider),
 | 
			
		||||
		new(fpsprig.Provider),
 | 
			
		||||
		new(fpimport.Provider),
 | 
			
		||||
		fpmarkdown.Default(),
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										46
									
								
								providers/import/import.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										46
									
								
								providers/import/import.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,46 @@
 | 
			
		||||
package impor
 | 
			
		||||
 | 
			
		||||
import "strings"
 | 
			
		||||
import "html/template"
 | 
			
		||||
import "git.tebibyte.media/sashakoshka/step"
 | 
			
		||||
 | 
			
		||||
var _ step.FuncProviderFor = new(Provider)
 | 
			
		||||
 | 
			
		||||
// Provider provides import functions.
 | 
			
		||||
type Provider struct {
 | 
			
		||||
	
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// FuncMap fulfills the step.FuncProvider interface.
 | 
			
		||||
func (this *Provider) FuncMap () template.FuncMap {
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// FuncMapFor fulfills the step.FuncProviderFor interface.
 | 
			
		||||
func (this *Provider) FuncMapFor (document *step.Document) template.FuncMap {
 | 
			
		||||
	stat := &state {
 | 
			
		||||
		document: document,
 | 
			
		||||
	}
 | 
			
		||||
	return template.FuncMap {
 | 
			
		||||
		"include":     stat.funcInclude,
 | 
			
		||||
		"includeHTML": stat.funcIncludeHTML,
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type state struct {
 | 
			
		||||
	document *step.Document
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (this *state) funcInclude (name string, data any) (string, error) {
 | 
			
		||||
	document, err := this.document.Environment().Parse(name)
 | 
			
		||||
	if err != nil { return "", err }
 | 
			
		||||
	builder := strings.Builder { }
 | 
			
		||||
	err = document.Execute(&builder, step.ExecutionData { Data: data })
 | 
			
		||||
	if err != nil { return "", err }
 | 
			
		||||
	return builder.String(), nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (this *state) funcIncludeHTML (name string, data any) (template.HTML, error) {
 | 
			
		||||
	result, err := this.funcInclude(name, data)
 | 
			
		||||
	return template.HTML(result), err
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user