Skip to content

aalmkainzi/htmc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

53 Commits
 
 
 
 
 
 
 
 

Repository files navigation

a library for creating HTML documents in C

#include "htm.c"

int main()
{
    char *doc =
    htmc(
        html(
            head(
                title("my html page")
            ),
            body(
                h1("BIG TITLE"),
                p("small text")
            )
        )
    );

    puts(doc);
    free(doc);
}

HTML tag attributes

attr(tag_name, ...list of attributes)
#include "htm.c"

int main()
{
    char *doc =
    htmc(
        html(
            head(
                title("my html page")
            ),
            body(
                attr(h1, "font-size: 16px;", "color: blue;")("BIG TITLE"),
                p("small text")
            )
        )
    );

    puts(doc);
    free(doc);
}

or

#include "htm.c"

int main()
{
    char *doc =
    htmc(
        html(
            head(
                title("my html page")
            ),
            body(
                attr(h1, htmc_strlit(font-size: 16px; color: blue;))("BIG TITLE"),
                p("small text")
            )
        )
    );
    
    puts(doc);
    free(doc);
}

Runtime Strings

#include "htm.c"

int main()
{
    char myname[16];
    fgets(myname, 16, stdin);
    
    char *doc =
    htmc(
        html(
            head(
                title(myname, "'s html page")
            ),
            body(
                h1("this is ", myname, "'s HTML page")
            )
        )
    );

    puts(doc);
    free(doc);
}

Formatted Strings

#include "htm.c"

int main()
{
    char myname[16];
    fgets(myname, 16, stdin);
    
    char *doc =
    htmc(
        html(
            head(
                title(
                    htmc_fmt("This is %s's web page", myname)
                )
            ),
            body(
                htmc_fmt("welcome to %s's web page", myname)
            )
        )
    );

    puts(doc);
    free(doc);
}

Embedding C code in the HTML

#include "htm.c"

int main()
{
    char *doc =
    htmc(
        html(
            head(),
            body(
                htmc_ccode(
                    int i;
                    for(i = 0 ; i < 10 ; i++)
                    {
                        htmc_yield(h1("BIG TEXT"));
                    }
                )
            )
        )
    );
    
    puts(doc);
    free(doc);
}

Namespacing

If the HTML tags pollute your namespace, you can choose to prefix them all with htmc_:

#define HTMC_PREFIX
#include "htm.c"

int main()
{    
    char *doc =
    htmc(
        htmc_html(
            htmc_head(
                htmc_title("my html page")
            ),
            htmc_body(
                htmc_h1("BIG TITLE"),
                htmc_p("small text")
            )
        )
    );

    puts(doc);
    free(doc);
}

This can be toggled on and off by re-including the header without defining HTMC_PREFIX

how to include:

in one translation unit do:

#include "htm.c"

and in all other TUs:

#include "htmc.h"

Or alternatively you can add htm.c to your build and only include htmc.h

Compiler support

This project uses statement expressions and __VA_OPT__, it was tested on Gcc, Clang and Tcc

About

dynamically generate HTML documents in C

Topics

Resources

License

Stars

Watchers

Forks

Languages