RiN Compiler
Compiler for parsing custom HTML
Installation
npm i rin-compiler
Features
- Simple and easy to use
- Use tags inside comments (HTML/CSS/JS)
- Components
- Custom Widgets
- Functional Widgets to run custom JavaScript when compiling
Usage
Basic compilation
This will automatically compile the files and write it to the disk.
import RiN from "rin-compiler"
// This will compile all the files in the "src" directory and output the files for the "dist" directory
RiN(path.resolve(__dirname, "src"), "default", "all", {
outFolder: path.resolve(__dirname, "dist")
});
Advanced compiling
For advanced compiling with more possibilities, consider using the Compiler
interface.
import { readFile } from "node:fs" import { resolve } from "node:path" import { Compiler, CompiledPageInfo } from "rin-compiler" let srcDir: string = resolve(__dirname, "src"), appView: string = "./views/App.html", options = { title: "My App", minify: true, CacheMaxAge: 30000, appWidgets: { /* Anything defined in appWidgets can be accessed in HTML viawidgets. As example, this can be accessed via widget. */ CommonName: "My App" }, functionalWidgets: { /* Anything defined in functionalWidgets can be executed in HTML like value You can access the value via the first parameter of the function you provide. The returned value will be rendered */ Capitalize: (value: string, App: CompiledPageInfo) => value[0].toUpperCase() + value.slice(1, value.length) } } const compiler = new Compiler(srcDir, appView, options) let renderedPage: CompiledPageInfo = await compiler.compile((await readFile(resolve(__dirname, "src/index.html"))).toString())
Tags
Tag | Description |
---|---|
|
Access App Widgets (
|
|
Execute Functional Widgets. (
|
|
Define App Widgets (
|
|
Embed the text content of a file using Files Widget (
|
<[component-name]/> |
Components (
|
<[component-name].[attribute-name]/> |
Get the attributes passed to the Component (
|
⚠ ATTENTION⚠
All these tags must be used inside comments.
Widgets
App Widgets
App Widgets are like a variable, you can define them in pages and access them anywhere in HTML.
There are some App Widgets available by default:
Widget | Description |
---|---|
|
HTML Content of the page. This should be rendered inside the app view in order to be a full page. |
|
Title of the page |
|
HTML Tags to import the CSS files (
tags) |
|
HTML Tags to import the JS files ( tags) |
App widgets are always definable.
- To define a app widget, use the
tag inside a page (This tag only works inside a page). - To access a app widget, use the
tag inside the app view.
Example:
index.html
views/App.html
It's also possible to define app widgets by the options passed to the RiN Compiler. But the problem with this is that widgets defined in the compiler options are same for all pages.
Example:
let options = {
appWidgets: {
PageID: "home"
}
}
RiN(srcDir, appView, files, options);
Functional Widgets
Functional Widgets are used to run JavaScript when compiling to get a different output. These can be used anywhere in HTML.
There are some Functional Widgets available by default:
Functional Widget | Description |
---|---|
|
Evaluate JavaScript when compiling |
Functional Widgets are always definable.
- To define a functional widget, pass a function
(value: string, App: RiN.CompiledPageInfo) => string
in the compiler options. - To execute a functional widget, use the
tag anywhere in the HTML.
Example:
let options = {
functionalWidgets: {
/**
* Capitalize a sentence
* @param {string} value String inside the functional widget tags.
* @param {CompiledPageInfo} App Information about the compiling page
* @example this is the value
*/
Capitalize: (value, App) => value[0].toUpperCase() + value.slice(1, value.length)
}
}
RiN(srcDir, appView, files, options);
index.html
Files Widget
Files Widget is used to embed a file containing text into a HTML file. This widget can be used anywhere in HTML. This tag is replaced by the text of the file.
Use
tag for this widget. Note that the path is relative to the srcDir
passed to the compiler.
Example:
<style>/* */style>
Components
Components are reusable bits of HTML code.
- To define a component,
- Create a directory named
components
in the source directory. - Create a HTML file and provide a name for it. The filename is the name of the component.
- Create a directory named
- To use a component, use the
<[component-name]/>
tag anywhere in the HTML. It's also possible to pass attributes to the component and access them in the component file via the
tag.
Example:
components/Popup.html
<div class="popup"> <h1>h1> <p>p> div>