Giter VIP home page Giter VIP logo

rin's Introduction

RiN Compiler ๐ŸŒบโœจ

RiN

Compiler for compiling 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 via <App.[widget-name]/> widgets.
            As example, this can be accessed via <App.CommonName/> widget. */
            CommonName: "My App"
        },
        functionalWidgets: {
            /* Anything defined in functionalWidgets can be executed in HTML like
                <App.[functional-widget-name]>value<App.[functional-widget-name]/>
            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
<App.[widget-name]/> Access App Widgets (โญ๐Ÿ“ƒ๐Ÿ”„)
<App.[functional-widget-name]/> Execute Functional Widgets. (โญ๐Ÿ“ƒ๐Ÿ”„)
<Page.[widget-name]/> Define App Widgets (๐Ÿ“ƒ)
<File:[path-to-the-file]/> Embed the text content of a file using Files Widget (โญ๐Ÿ“ƒ๐Ÿ”„)
<[component-name]/> Components (โญ๐Ÿ“ƒ)
<[component-name].[attribute-name]/> Get the attributes passed to the Component (๐Ÿ”„)

โญ Can be used in app view
๐Ÿ“ƒ Can be used in pages
๐Ÿ”„ Can be used in components

โš  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
<App.Page/> HTML Content of the page. This should be rendered inside the app view in order to be a full page.
<App.Title/> Title of the page
<App.Imports.cssTags/> HTML Tags to import the CSS files (<link> tags)
<App.Imports.jsTags/> HTML Tags to import the JS files (<script> tags)

App widgets are always definable.

  • To define a app widget, use the <Page.[widget-name]> tag inside a page (This tag only works inside a page).
  • To access a app widget, use the <App.[widget-name]/> tag inside the app view.

Example:

index.html

<!-- โœจ Using this tag, we can define a custom widget. In this example we are defining the custom widget PageID, to a value of "home". This can be later accessed in the app view โœจ -->
<!-- <Page.PageID>home<Page.PageID/> -->

views/App.html

<!-- โœจ Using this tag, we can access the custom widget defined in the index.html โœจ -->
<!-- โœจ This will be replaced to "home" โœจ -->
<!-- <App.PageID/> -->

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
<App.Run> 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 <App.[functional-widget-name]> 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 <App.Capitalize>this is the value</App.Capitalize>
         */
        Capitalize: (value, App) => value[0].toUpperCase() + value.slice(1, value.length)
    }
}

RiN(srcDir, appView, files, options);

index.html

<!-- โœจ This would be replaced as "This is the value" since our Capitalize functional widget capitalizes the sentence โœจ -->
<!-- <App.Capitalize>this is the value</App.Capitalize> -->

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 <File:[path-to-the-file]/> tag for this widget. Note that the path is relative to the srcDir passed to the compiler.

Example:

<!-- โœจ These type of widgets are important because loading external css files are much slower than internal css. โœจ -->
<style>/* <File:./css/main.css/> */</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.
  • 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 <Component.[attribute]/> tag.

Example:

components/Popup.html

<!-- You can also define styles for the component in this file with the <style> tag as usual -->

<div class="popup">
    <!-- โœจ Access the attributes passed to the component โœจ -->
    <h1><!-- <Component.heading/> --></h1>
    <p><!-- <Component.description/> --></p>

    <!-- โœจ Component.Run is a functional widget which is only available inside component files. It gives the information of the component via the Component object. โœจ -->
    <!-- <Component.Run>Component.buttons?.split(",")?.map(text=>`<button>${text.trim()}</button>`).join("")</Component.Run> -->
</div>

index.html

<!-- โœจ Since we named that file as Popup.html, we can call it like this. โœจ -->
<!-- <Popup heading="Hello" description="RiN by RahalDevs" buttons="CLOSE, OK"/> -->

rin's People

Contributors

chalanan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.