lektor-website/content/docs/api/environment/add-build-program/contents.lr

54 lines
1.4 KiB
Markdown

title: add_build_program
---
type: method
---
signature: cls, program
---
summary: Registers a build program for a source object.
---
body:
This is very experimental API and used to register build programs for
custom source objects. This can be used to implement virtual items. This
works in combination with [generator :ref](../generator/) and
[urlresolver :ref](../urlresolver/) and is responsible for generating
artifacts out of source objects.
## Example
```python
from lektor.sourceobj import VirtualSourceObject
from lektor.build_programs import BuildProgram
class Source(VirtualSourceObject):
@property
def source_content(self):
with open(self.parent.source_filename) as f:
return f.read().decode('utf-8')
@property
def url_path(self):
return self.parent.url_path + 'source.txt'
class SourceBuildProgram(BuildProgram):
def produce_artifacts(self):
self.declare_artifact(
self.source.url_path,
sources=list(self.source.iter_source_filenames()))
def build_artifact(self, artifact):
artifact.render_template_into('view_source.html',
this=self.source)
env.add_build_program(Source, SourceBuildProgram)
```
And here the example `view_source.html` template:
```html+jinja
<h2>Source for {{ this.parent.path }}</h2>
<pre>{{ this.source_content }}</pre>
```