A type-safe, composable DSL for building HTML pages in OCaml.
- Type-safe: Build HTML using OCaml's type system
- Composable: Combine elements easily with functional composition
- Safe: Automatic HTML escaping to prevent XSS attacks
- Complete: Support for all common HTML elements and attributes
- Template system: Create reusable templates for dynamic content
- Well-tested: Comprehensive test suite covering all features
opam install cathmtlOr from source:
git clone https://github.com/claeusdev/cathtml.git
cd cathtml
dune build
dune installdune builddune runtestdune exec cathmtlThis project uses ocamlformat for consistent code style. Format code with:
dune build @fmt --auto-promoteOr using ocamlformat directly:
ocamlformat --inplace lib/*.ml bin/*.ml test/*.mlopen Cathmtl.Html
let page = html [] [
head [] [
title [] [text "My Page"]
];
body [] [
h1 [] [text "Hello, World!"];
p [] [text "This is a paragraph."]
]
]
let html_string = to_string pagelet div_with_class = div [class_name "container"; id "main"] [
p [] [text "Content here"]
]let image = img [attr "src" "photo.jpg"; attr "alt" "Photo"] []let user_card = template (fun (name, email) ->
div [class_name "card"] [
h2 [] [text name];
p [] [text email]
]
)
let card_html = apply_template user_card ("Alice", "[email protected]")text s- Create text contentelement tag attrs children- Create an HTML element (low-level)create_element tag attrs children- Helper to create an element with tag name and attributesto_string html/render html- Convert HTML content to string representation
All common HTML elements are available:
- Document structure:
html,head,body,title,meta - Headings:
h1,h2,h3,h4,h5,h6 - Text elements:
p,div,span,strong,em,code,pre - Lists:
ul,ol,li - Links and media:
a,img - Forms:
form,input,textarea,button,label,select,option - Tables:
table,thead,tbody,tr,th,td - Semantic elements:
header,nav,main,section,article,aside,footer
class_name name- Create a class attributeid name- Create an id attributestyle css- Create a style attributedata key value- Create a data attributeattr name value- Create a custom attribute
self_closing tag attrs- Create a self-closing element (likeimg,input, etc.)comment text- Create an HTML comment nodedoctype type_str- Create a DOCTYPE declaration
is_empty html- Check if content is emptyattributes html- Get attributes from an element (returnsSome attrsorNone)children html- Get children from an element (returnsSome childrenorNone)escape_html s- Escape HTML special characters in text contentescape_attr s- Escape special characters in attribute values
See bin/main.ml for more comprehensive examples including:
- Blog post templates
- Contact forms
- Data tables
open Cathmtl.Html
let page = html [] [
head [] [
title [] [text "My Page"];
meta [attr "charset" "UTF-8"] []
];
body [] [
header [] [
h1 [] [text "Welcome"];
nav [] [
a [attr "href" "/"] [text "Home"];
text " | ";
a [attr "href" "/about"] [text "About"]
]
];
main [] [
section [] [
h2 [] [text "Features"];
ul [] [
li [] [text "Type-safe HTML generation"];
li [] [text "Automatic escaping"];
li [] [text "Composable API"]
]
]
];
footer [] [
p [] [text "© 2024"]
]
]
]
let () = print_endline (to_string page)cathmtl/
├── lib/ # Library source code
│ └── html.ml # Main HTML DSL implementation
├── bin/ # Executable examples
│ └── main.ml # Demo application
├── test/ # Test suite
│ └── test_cathmtl.ml
├── dune-project # Dune project configuration
├── .ocamlformat # Code formatting configuration
└── README.md # This file
The project includes a comprehensive test suite covering:
- Text and element rendering
- Nested structures
- Attributes and escaping
- Self-closing elements
- DOCTYPE and comments
- Template system
- Complex page structures
Run the test suite:
dune runtestContributions are welcome! Please ensure:
- Code is formatted with
ocamlformat - Tests pass (
dune runtest) - New features include tests
See LICENSE file for details.
Nana Adjei Manu ([email protected])
- GitHub: https://github.com/claeusdev/cathtml
- Documentation: https://github.com/claeusdev/cathtml