|
| 1 | +# Java Hypertext Preprocessor (JHP) |
| 2 | + |
| 3 | +[](https://www.oracle.com/java/) |
| 4 | +[](https://maven.apache.org/) |
| 5 | +[](LICENSE) |
| 6 | + |
| 7 | +A powerful, flexible template engine for Java applications. Inspired by PHP and modern template engines like Jinja2 and Blade, JHP provides an elegant syntax for embedding dynamic content in your HTML templates. |
| 8 | + |
| 9 | +## Features |
| 10 | + |
| 11 | +- **Intuitive Syntax** - Clean, readable template syntax with `{{ }}` for output and `{% %}` for logic |
| 12 | +- **HTML Escaping** - Automatic HTML escaping by default to prevent XSS attacks |
| 13 | +- **Control Structures** - Full support for if/else, for, foreach, and while loops |
| 14 | +- **Template Includes** - Modular templates with include directives |
| 15 | +- **Built-in Functions** - Common string and utility functions out of the box |
| 16 | +- **Extensible** - Easy to add custom functions |
| 17 | +- **Thread-Safe** - Built for concurrent environments |
| 18 | +- **Template Caching** - Automatic AST caching for improved performance |
| 19 | +- **Flexible Error Handling** - Multiple modes for handling runtime issues |
| 20 | + |
| 21 | +## Quick Start |
| 22 | + |
| 23 | +### Installation |
| 24 | + |
| 25 | +Add to your `pom.xml`: |
| 26 | + |
| 27 | +```xml |
| 28 | +<dependency> |
| 29 | + <groupId>com.hindbiswas.jhp</groupId> |
| 30 | + <artifactId>jhp</artifactId> |
| 31 | + <version>1.0-SNAPSHOT</version> |
| 32 | +</dependency> |
| 33 | +``` |
| 34 | + |
| 35 | +### Basic Usage |
| 36 | + |
| 37 | +```java |
| 38 | +import com.hindbiswas.jhp.*; |
| 39 | +import com.hindbiswas.jhp.engine.*; |
| 40 | + |
| 41 | +// Create engine |
| 42 | +Settings settings = Settings.builder() |
| 43 | + .base("/path/to/templates") |
| 44 | + .build(); |
| 45 | +FunctionLibrary lib = new FunctionLibrary(); |
| 46 | +JhpEngine engine = new JhpEngine(settings, lib); |
| 47 | + |
| 48 | +// Prepare context |
| 49 | +Context ctx = new Context(); |
| 50 | +ctx.add("name", "Alice"); |
| 51 | +ctx.add("age", 25); |
| 52 | + |
| 53 | +// Render template |
| 54 | +String output = engine.render("welcome.jhp", ctx); |
| 55 | +``` |
| 56 | + |
| 57 | +**Template (welcome.jhp):** |
| 58 | +```html |
| 59 | +<h1>Hello {{ name }}!</h1> |
| 60 | +{% if (age >= 18) %} |
| 61 | + <p>You are an adult.</p> |
| 62 | +{% else %} |
| 63 | + <p>You are a minor.</p> |
| 64 | +{% endif %} |
| 65 | +``` |
| 66 | + |
| 67 | +## Template Syntax |
| 68 | + |
| 69 | +### Output Variables |
| 70 | + |
| 71 | +```html |
| 72 | +{{ variable }} <!-- HTML-escaped output --> |
| 73 | +{{{ rawHtml }}} <!-- Raw output (not escaped) --> |
| 74 | +``` |
| 75 | + |
| 76 | +### Control Structures |
| 77 | + |
| 78 | +```html |
| 79 | +<!-- If Statement --> |
| 80 | +{% if (condition) %} |
| 81 | + Content |
| 82 | +{% elseif (otherCondition) %} |
| 83 | + Other content |
| 84 | +{% else %} |
| 85 | + Default content |
| 86 | +{% endif %} |
| 87 | + |
| 88 | +<!-- For Loop --> |
| 89 | +{% for (i = 0; i < 10; i++) %} |
| 90 | + <p>Item {{ i }}</p> |
| 91 | +{% endfor %} |
| 92 | + |
| 93 | +<!-- Foreach Loop --> |
| 94 | +{% foreach (item in items) %} |
| 95 | + <li>{{ item }}</li> |
| 96 | +{% endforeach %} |
| 97 | + |
| 98 | +<!-- Include Template --> |
| 99 | +{% include 'header.jhp' %} |
| 100 | +``` |
| 101 | + |
| 102 | +### Functions |
| 103 | + |
| 104 | +```html |
| 105 | +{{ touppercase(name) }} |
| 106 | +{{ tolowercase(email) }} |
| 107 | +{{ trim(input) }} |
| 108 | +{{ len(items) }} |
| 109 | +{{ now() }} |
| 110 | +``` |
| 111 | + |
| 112 | +## Documentation |
| 113 | + |
| 114 | +Comprehensive documentation is available in the [`docs/`](docs/) directory: |
| 115 | + |
| 116 | +- [Installation Guide](docs/installation.md) |
| 117 | +- [Getting Started](docs/getting-started.md) |
| 118 | +- [Template Syntax](docs/template-syntax.md) |
| 119 | +- [Control Structures](docs/control-structures.md) |
| 120 | +- [Functions](docs/functions.md) |
| 121 | +- [Context & Variables](docs/context-variables.md) |
| 122 | +- [Configuration](docs/configuration.md) |
| 123 | +- [Advanced Usage](docs/advanced-usage.md) |
| 124 | +- [API Reference](docs/api-reference.md) |
| 125 | +- [Examples](docs/examples.md) |
| 126 | + |
| 127 | +## Building from Source |
| 128 | + |
| 129 | +```bash |
| 130 | +# Clone the repository |
| 131 | +git clone https://github.com/hind-sagar-biswas/java-hypertext-preprocessor.git |
| 132 | +cd java-hypertext-preprocessor |
| 133 | + |
| 134 | +# Build with Maven |
| 135 | +mvn clean install |
| 136 | + |
| 137 | +# Run tests |
| 138 | +mvn test |
| 139 | +``` |
| 140 | + |
| 141 | +## Requirements |
| 142 | + |
| 143 | +- Java 25 or higher (Java 24 supported) |
| 144 | +- Maven 3.6+ |
| 145 | +- ANTLR 4.13.2 (automatically managed) |
| 146 | + |
| 147 | +## Testing |
| 148 | + |
| 149 | +The project includes comprehensive unit tests using JUnit Jupiter: |
| 150 | + |
| 151 | +```bash |
| 152 | +mvn test |
| 153 | +``` |
| 154 | + |
| 155 | +**Test Coverage:** |
| 156 | +- 88 unit tests |
| 157 | +- Context and variable management |
| 158 | +- Settings and configuration |
| 159 | +- Function library (built-in and custom) |
| 160 | +- Template rendering and integration |
| 161 | +- AST node structures |
| 162 | + |
| 163 | +## Examples |
| 164 | + |
| 165 | +### Web Application |
| 166 | + |
| 167 | +```java |
| 168 | +@GetMapping("/profile/{username}") |
| 169 | +public ResponseEntity<String> profile(@PathVariable String username) { |
| 170 | + User user = userService.findByUsername(username); |
| 171 | + |
| 172 | + Context ctx = new Context(); |
| 173 | + ctx.add("user", user.toMap()); |
| 174 | + |
| 175 | + String html = engine.render("profile.jhp", ctx); |
| 176 | + return ResponseEntity.ok().contentType(MediaType.TEXT_HTML).body(html); |
| 177 | +} |
| 178 | +``` |
| 179 | + |
| 180 | +### Email Templates |
| 181 | + |
| 182 | +```java |
| 183 | +public void sendWelcomeEmail(User user) throws Exception { |
| 184 | + Context ctx = new Context(); |
| 185 | + ctx.add("userName", user.getName()); |
| 186 | + ctx.add("activationLink", generateActivationLink(user)); |
| 187 | + |
| 188 | + String html = engine.render("welcome-email.jhp", ctx); |
| 189 | + emailClient.send(user.getEmail(), "Welcome!", html); |
| 190 | +} |
| 191 | +``` |
| 192 | + |
| 193 | +### Custom Functions |
| 194 | + |
| 195 | +```java |
| 196 | +FunctionLibrary lib = new FunctionLibrary(); |
| 197 | + |
| 198 | +lib.register("formatprice", (args, scopes) -> { |
| 199 | + Number price = (Number) args.get(0); |
| 200 | + return String.format("$%.2f", price.doubleValue()); |
| 201 | +}); |
| 202 | + |
| 203 | +JhpEngine engine = new JhpEngine(settings, lib); |
| 204 | +``` |
| 205 | + |
| 206 | +**Template:** |
| 207 | +```html |
| 208 | +<span class="price">{{ formatprice(product.price) }}</span> |
| 209 | +``` |
| 210 | + |
| 211 | +## Configuration |
| 212 | + |
| 213 | +```java |
| 214 | +Settings settings = Settings.builder() |
| 215 | + .base("/var/www/templates") // Base directory |
| 216 | + .escape(true) // HTML escaping (default: true) |
| 217 | + .maxIncludeDepth(15) // Max include depth (default: 15) |
| 218 | + .issueHandleMode(IssueHandleMode.COMMENT) // Error handling mode |
| 219 | + .build(); |
| 220 | +``` |
| 221 | + |
| 222 | +### Issue Handling Modes |
| 223 | + |
| 224 | +- **COMMENT** - Render issues as HTML comments (default) |
| 225 | +- **THROW** - Throw exceptions for issues |
| 226 | +- **DEBUG** - Render detailed debug information |
| 227 | +- **IGNORE** - Silently ignore issues |
| 228 | + |
| 229 | +## Contributing |
| 230 | + |
| 231 | +Contributions are welcome! Please feel free to submit pull requests or open issues. |
| 232 | + |
| 233 | +1. Fork the repository |
| 234 | +2. Create your feature branch (`git checkout -b feature/amazing-feature`) |
| 235 | +3. Commit your changes (`git commit -m 'Add some amazing feature'`) |
| 236 | +4. Push to the branch (`git push origin feature/amazing-feature`) |
| 237 | +5. Open a Pull Request |
| 238 | + |
| 239 | +## License |
| 240 | + |
| 241 | +This project is open source. Check the repository for license details. |
| 242 | + |
| 243 | +## Links |
| 244 | + |
| 245 | +- **Documentation**: [docs/](docs/) |
| 246 | +- **GitHub**: [java-hypertext-preprocessor](https://github.com/hind-sagar-biswas/java-hypertext-preprocessor/) |
| 247 | +- **Issues**: [Issue Tracker](https://github.com/hind-sagar-biswas/java-hypertext-preprocessor/issues) |
| 248 | + |
| 249 | +## Acknowledgments |
| 250 | + |
| 251 | +Inspired by: |
| 252 | +- PHP's template syntax |
| 253 | +- Jinja2 (Python) |
| 254 | +- Blade (Laravel) |
| 255 | +- Twig (Symfony) |
| 256 | + |
| 257 | +--- |
| 258 | + |
| 259 | +Made with ❤️ by [Hind Sagar Biswas](https://github.com/hind-sagar-biswas) |
0 commit comments