GithubHelp home page GithubHelp logo

hsx's Introduction

HSX

HSX (Hypertext S-expression) is a simple yet powerful HTML5 generation library for Common Lisp, forked from flute, originally created by Bo Yao.

Introduction

HSX allows you to generate HTML using S-expressions, providing a more Lisp-friendly way to create web content. By using the hsx macro, you can define HTML elements and their attributes in a concise and readable manner.

Getting Started

Basic Usage

Use the hsx macro to create HTML elements. Attributes are specified using a property list following the element name, and child elements are nested directly within.

(hsx
  (div :id "example" :class "container"
    (h1 "Welcome to HSX")
    (p "This is an example paragraph.")))

This generates:

<div id="example" class="container">
  <h1>Welcome to HSX</h1>
  <p>This is an example paragraph.</p>
</div>

Examples

Dynamic Content

HSX allows embedding Common Lisp code directly within your HTML structure, making it easy to generate dynamic content.

(hsx
  (div
    (p :id (format nil "id-~a" (random 100)))
    (ul
      (loop :for i :from 1 :to 5 :collect (li (format nil "Item ~a" i))))
    (if (> (random 10) 5)
        (p "Condition met!")
        (p "Condition not met!"))))

This might generate:

<div>
  <p id="id-42"></p>
  <ul>
    <li>Item 1</li>
    <li>Item 2</li>
    <li>Item 3</li>
    <li>Item 4</li>
    <li>Item 5</li>
  </ul>
  <p>Condition not met!</p>
</div>

Using Fragments

To group multiple elements without adding an extra wrapper, use the fragment <>.

(hsx
  (<>
    (h1 "Grouped Elements")
    (p "First paragraph.")
    (p "Second paragraph.")))

This generates:

<h1>Grouped Elements</h1>
<p>First paragraph.</p>
<p>Second paragraph.</p>

Creating Components

You can define reusable components with the defcomp macro. Components are functions that can take keyword arguments and properties.

(defcomp card (&key title children)
  (hsx
    (div :class "card"
      (h1 title)
      children)))

Or using a property list:

(defcomp card (&rest props)
  (hsx
    (div :class "card"
      (h1 (getf props :title))
      (getf props :children))))

Usage example:

(hsx
  (card :title "Card Title"
    (p "This is a card component.")))

Generates:

<div class="card">
  <h1>Card Title</h1>
  <p>This is a card component.</p>
</div>

Rendering HTML

To render HSX to an HTML string, use the render-to-string function.

(render-to-string
  (hsx
    (div :class "content"
      (h1 "Rendered to String")
      (p "This HTML is generated as a string."))))

License

This project is licensed under the MIT License.

© 2024 skyizwhite

© 2018 Bo Yao

Feel free to contribute to the project and report any issues or feature requests on the GitHub repository.

hsx's People

Contributors

skyizwhite avatar jonatack avatar

Stargazers

Matt D avatar Dmytro avatar Matteo Landi avatar Alexander Artemenko avatar Rintaro Itokawa avatar Juan M. Bello-Rivas avatar  avatar  avatar Danny O'Brien avatar hikettei avatar nodrygo avatar Paolo Amoroso avatar Colin Woodbury avatar  avatar Raph avatar Satoshi Imai avatar cxxxr avatar

Watchers

 avatar

hsx's Issues

Rewrite test

  • Rewrite to package-inferred-system
  • Split test packages
  • Add GitHub Actions for automated unit testing

Wrong format

(pi:define-element layout ()
  (pi:h
    (html
      (head
        (title "skyizwhite.dev")
        (script :src "/static/htmx.min.js")
        (link :href "/static/main.css" :rel "stylesheet")
        (link :href "/static/tailwind.css" :rel "stylesheet"))
      (body :class "h-[100svh]"
        pi:children))))

(pi:define-element page ()
  (pi:h
    (cmp:layout
      (section :class "h-full flex justify-center items-center"
        (p :class "text-primary text-4xl"
          "Hello World!")))))

(page)

this generates:

<!DOCTYPE html>
<html>
  <head>
    <title>skyizwhite.dev</title>
    <script src="/static/htmx.min.js"></script>
    <link href="/static/main.css" rel="stylesheet">
    <link href="/static/tailwind.css" rel="stylesheet">
  </head>
  <body class="h-[100svh]">
    <section class="h-full flex justify-center items-center">
      <p class="text-primary text-4xl">Hello World!</p></section>
  </body>
</html>

expected:

<!DOCTYPE html>
<html>
  <head>
    <title>skyizwhite.dev</title>
    <script src="/static/htmx.min.js"></script>
    <link href="/static/main.css" rel="stylesheet">
    <link href="/static/tailwind.css" rel="stylesheet">
  </head>
  <body class="h-[100svh]">
    <section class="h-full flex justify-center items-center">
      <p class="text-primary text-4xl">Hello World!</p>
    </section>
  </body>
</html>

Remove local-nicknames

Since local-nicknames is a relatively new feature of asdf, piccolo cannot be built unless asdf is up-to-date.

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.