Skip to content

Instantly share code, notes, and snippets.

@letoh
Last active October 5, 2015 23:38
Show Gist options
  • Select an option

  • Save letoh/2896412 to your computer and use it in GitHub Desktop.

Select an option

Save letoh/2896412 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include "html.h"
int main(int argc, char *argv[])
{
html(
head(
title("Site Title"),
script("http://test.com/test.js"),
css("css/test.css"),
js(
function foo()
{
alert("abc");
}
)),
body(
img("banner.jpg", id="banner"),
img("line.jpg"),
h1(raw(chapter 1)),
raw(
<form method="post" action="test.cgi">
<input type="submit" value="go">
</form>
)
));
return 0;
}
static void _body(void)
{
div(id="ctrlbar",
div(id="loginTrigger" align="right" style="margin: 5px 10px;",
span(onclick="popup('login');", raw(login)))
);
div(id="logo");
center(div(id="global", img("/img/image.jpg")));
div(id="login" class="container",
center(
div(id="msgbox" class="alert", _msg()),
form(post, "/index.do", "loginForm", onsubmit="return checkLoginInput(this)",
table(style="",
tr(td(label("User", for="user")), td(text("user"))),
tr(td(label("Password", for="pass")), td(pass("pass")))
), br,
hidden("checklogin", "1"),
submit(name="act" value="login" class="btn")
) /* form */
) /* center */
) /* div */ ;
}
int main(void)
{
const char *val;
cgi_init();
utils_cgi_start();
islogin = isAuth(LOGIN_USER);
if(islogin)
{
if((val = cgi_query("act")) && !strncmp(val, "logout", 7))
{
logout(LOGIN_USER);
utils_redirect("/index.do", 2,
"logout success!!<br />\ngo back to home in 2 seconds");
return 0;
}
else
{
utils_redirect("/demo.do", 0, "redirecting...");
return 0;
}
}
else if(NULL != (val = cgi_query("checklogin")) && 1 == atoi(val))
{
if(checkLogin(cgi_query("user"), cgi_query("pass")))
{
utils_redirect("/demo.do", 2,
"login success!!<br />\ni'll take you to the demo page!");
return 0;
}
else
{
loginFailed = 1;
}
}
html(head(_head()), body(_body()));
return 0;
}
#ifndef _HTML_H_
#define _HTML_H_
#define _t(tag) printf("<" tag ">\n")
#define xstr(s) #s
#define single(tag, ...) \
printf("<" xstr(tag) " " xstr(__VA_ARGS__) " />\n")
#define _vartag(t, a, ...) \
printf("<" xstr(t) " " a " >\n"); \
__VA_ARGS__; \
_t("/" xstr(t))
#define html(...) _vartag(html, attr(xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"), __VA_ARGS__)
#define head(...) _vartag(head, "", __VA_ARGS__)
#define body(...) _vartag(body, "", __VA_ARGS__)
#define title(s, ...) \
_t("title"); \
printf(s, ## __VA_ARGS__); \
_t("/title")
#define meta(...) single(meta, __VA_ARGS__)
#define css(f) \
printf("<link rel=\"stylesheet\" type=\"text/css\" href=\"%s\" />\n", f)
//#define script(f) \
// printf("<script type=\"text/javascript\" src=\"%s\" />\n", f)
#define script(f) \
printf("<script type=\"text/javascript\" src=\"%s\"></script>\n", f)
#define echo(r, ...) printf(r, ## __VA_ARGS__)
#define raw(r) echo(#r)
#define js(...) \
raw(<script type="text/javascript">); \
raw(__VA_ARGS__); \
raw(</script>)
#define attr xstr
#define h1(...) _vartag(h1, "", __VA_ARGS__)
#define h2(...) _vartag(h2, "", __VA_ARGS__)
#define h3(...) _vartag(h3, "", __VA_ARGS__)
#define h4(...) _vartag(h4, "", __VA_ARGS__)
#define img(f, ...) \
printf("<img src=\"%s\" %s />\n", f, # __VA_ARGS__) \
#define br printf("<br />\n")
#define div(a, ...) _vartag(div, attr(a), __VA_ARGS__)
#define span(a, ...) _vartag(span, attr(a), __VA_ARGS__)
#define center(...) _vartag(center, "", __VA_ARGS__)
/* table */
#define table(a, ...) _vartag(table, attr(a), ## __VA_ARGS__)
#define th(...) _vartag(th, "", ## __VA_ARGS__)
#define tr(...) _vartag(tr, "", ## __VA_ARGS__)
#define td(...) _vartag(td, "", ## __VA_ARGS__)
/** form
*
* form(GET, "/abc", "", ""
* [, <form input elements>]
* );
*
*/
#define form(m, s, n, a, ...) \
_vartag(form, attr(method=#m action=s name=n id=n a), ## __VA_ARGS__)
#define button(t, ...) _vartag(button, attr(__VA_ARGS__), echo(t))
#define label(t, ...) _vartag(label, attr(__VA_ARGS__), echo(t))
#define input(...) single(input, __VA_ARGS__)
#define submit(...) input(type="submit" __VA_ARGS__)
#define text(n, ...) input(type="text" name=n id=n __VA_ARGS__)
#define pass(n, ...) input(type="password" name=n id=n __VA_ARGS__)
#define img2(s, ...) input(type="image" src=s __VA_ARGS__)
#define hidden(n, v, ...) input(type="hidden" name=n id=n value=v __VA_ARGS__)
#define radio(l, g, v, ...) label(l), input(type="radio" name=g value=v __VA_ARGS__)
#define check(l, g, v, ...) label(l), input(type="checkbox" name=g value=v __VA_ARGS__)
#define hr(...) single(hr, __VA_ARGS__)
#define url(t, src, ...) _vartag(a, attr(href=src __VA_ARGS__), echo(t))
#endif /* _HTML_H_ */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment