Files
masonry/examples/custom-js-functions/templates/manifest.yaml

105 lines
3.0 KiB
YAML

name: "Blog Generator with Custom JS Functions"
description: "Demonstrates custom JavaScript template functions using Otto interpreter"
# Custom JavaScript functions that can be used in templates
functions:
# Convert a string to a URL-friendly slug
generateSlug: |
function main() {
var input = args[0] || "";
return input
.toLowerCase()
.replace(/[^a-z0-9]+/g, '-')
.replace(/^-+|-+$/g, '');
}
# Convert singular words to plural (simple English rules) - ES5 compatible
pluralize: |
function main() {
var word = args[0] || "";
if (word.slice(-1) === 'y') {
return word.slice(0, -1) + 'ies';
} else if (word.slice(-1) === 's' || word.slice(-2) === 'sh' || word.slice(-2) === 'ch' || word.slice(-1) === 'x' || word.slice(-1) === 'z') {
return word + 'es';
} else {
return word + 's';
}
}
# Generate validation rules based on field type
generateValidation: |
function main() {
var fieldType = args[0] || "";
var fieldName = args[1] || "field";
switch (fieldType) {
case 'string':
return 'if len(' + fieldName + ') == 0 { return errors.New("' + fieldName + ' is required") }';
case 'uuid':
return 'if _, err := uuid.Parse(' + fieldName + '); err != nil { return errors.New("invalid UUID format") }';
case 'boolean':
return '// Boolean validation not required';
case 'timestamp':
return 'if ' + fieldName + '.IsZero() { return errors.New("' + fieldName + ' is required") }';
default:
return '// No validation rules for type: ' + fieldType;
}
}
# Format a comment block
formatComment: |
function main() {
var text = args[0] || "";
var lines = text.split('\n');
var result = '// ' + lines.join('\n// ');
return result;
}
# Generate database field mapping
dbFieldMapping: |
function main() {
var fieldName = args[0] || "";
var fieldType = args[1] || "string";
var dbType;
switch (fieldType) {
case 'string':
dbType = 'VARCHAR(255)';
break;
case 'text':
dbType = 'TEXT';
break;
case 'uuid':
dbType = 'UUID';
break;
case 'boolean':
dbType = 'BOOLEAN';
break;
case 'timestamp':
dbType = 'TIMESTAMP';
break;
default:
dbType = 'VARCHAR(255)';
}
return '`db:"' + fieldName.toLowerCase() + '" json:"' + fieldName + '"`';
}
outputs:
# Generate Go models for each entity using custom functions
- path: "models/{{.Entity.Name | lower}}.go"
template: "model"
iterator: "entities"
item_context: "Entity"
# Generate controllers for each entity
- path: "controllers/{{.Entity.Name | lower}}_controller.go"
template: "controller"
iterator: "entities"
item_context: "Entity"
# Generate a single router file
- path: "router.go"
template: "router"
condition: "has_entities"