diff --git a/core/html_builder.cpp b/core/html_builder.cpp new file mode 100644 index 0000000..884cc56 --- /dev/null +++ b/core/html_builder.cpp @@ -0,0 +1,1456 @@ +#include "html_builder.h" + +HTMLTag *HTMLTag::str(const std::string &str) { + result += " " + str; + + return this; +} + +HTMLTag *HTMLTag::style(const std::string &val) { + attrib("style", val); + + return this; +} + +HTMLTag *HTMLTag::href(const std::string &val) { + attrib("href", val); + + return this; +} + +HTMLTag *HTMLTag::cls(const std::string &val) { + attrib("class", val); + + return this; +} +HTMLTag *HTMLTag::id(const std::string &val) { + attrib("id", val); + + return this; +} +HTMLTag *HTMLTag::name(const std::string &val) { + attrib("name", val); + + return this; +} + +HTMLTag *HTMLTag::method(const std::string &val) { + attrib("method", val); + + return this; +} + +HTMLTag *HTMLTag::type(const std::string &val) { + attrib("type", val); + + return this; +} + +HTMLTag *HTMLTag::attrib(const std::string &attr, const std::string &val) { + result += " " + attr + "=\"" + val + "\""; + + return this; +} + +HTMLTag *HTMLTag::start(const std::string &p_tag, const bool p_simple) { + simple = p_simple; + + result = "<" + p_tag; + + return this; +} + +HTMLTag *HTMLTag::reset() { + result.clear(); + + return this; +} + +HTMLTag *HTMLTag::close() { + if (simple) + result += "/>"; + else + result += ">"; + + return this; +} + +bool HTMLTag::has_data() { + return result.size() > 0; +} + +HTMLTag::HTMLTag() { + simple = true; +} + +void HTMLBuilder::comment(const std::string &val) { + write_tag(); + + result += ""; +} + +HTMLTag *HTMLBuilder::doctype() { + write_tag(); + + return tag.start("!DOCTYPE"); +} + +void HTMLBuilder::doctype(const std::string &val) { + write_tag(); + + result += ""; +} + +HTMLTag *HTMLBuilder::a() { + write_tag(); + + return tag.start("a"); +} +HTMLTag *HTMLBuilder::abbr() { + write_tag(); + + return tag.start("abbr"); +} + +HTMLTag *HTMLBuilder::acronym() { //Not supported in HTML5. Use instead. Defines an acronym + write_tag(); + + return tag.start("acronym"); +} + +HTMLTag *HTMLBuilder::address() { + write_tag(); + + return tag.start("address"); +} + +HTMLTag *HTMLBuilder::applet() { //Not supported in HTML5. Use or instead. Defines an embedded applet + write_tag(); + + return tag.start("applet"); +} + +HTMLTag *HTMLBuilder::area() { + write_tag(); + + return tag.start("area"); +} + +HTMLTag *HTMLBuilder::article() { + write_tag(); + + return tag.start("article"); +} + +HTMLTag *HTMLBuilder::aside() { + write_tag(); + + return tag.start("aside"); +} + +HTMLTag *HTMLBuilder::audio() { + write_tag(); + + return tag.start("audio"); +} + +HTMLTag *HTMLBuilder::b() { + write_tag(); + + return tag.start("b"); +} + +HTMLTag *HTMLBuilder::basefont() { //Not supported in HTML5. Use CSS instead. Specifies a default color, size, and font for all text in a document + write_tag(); + + return tag.start("basefont"); +} + +HTMLTag *HTMLBuilder::bdi() { + write_tag(); + + return tag.start("bdi"); +} + +HTMLTag *HTMLBuilder::bdo() { + write_tag(); + + return tag.start("bdo"); +} + +HTMLTag *HTMLBuilder::big() { //Not supported in HTML5. Use CSS instead. Defines big text + write_tag(); + + return tag.start("big"); +} + +HTMLTag *HTMLBuilder::blockquote() { + write_tag(); + + return tag.start("blockquote"); +} + +HTMLTag *HTMLBuilder::body() { + write_tag(); + + return tag.start("body"); +} + +HTMLTag *HTMLBuilder::br() { + write_tag(); + + return tag.start("br", true); +} + +HTMLTag *HTMLBuilder::button() { + write_tag(); + + return tag.start("button"); +} + +HTMLTag *HTMLBuilder::canvas() { + write_tag(); + + return tag.start("canvas"); +} + +HTMLTag *HTMLBuilder::caption() { + write_tag(); + + return tag.start("caption"); +} + +HTMLTag *HTMLBuilder::center() { //Not supported in HTML5. Use CSS instead. Defines centered text + write_tag(); + + return tag.start("center"); +} + +HTMLTag *HTMLBuilder::cite() { + write_tag(); + + return tag.start("cite"); +} + +HTMLTag *HTMLBuilder::code() { + write_tag(); + + return tag.start("code"); +} + +HTMLTag *HTMLBuilder::col() { + write_tag(); + + return tag.start("col"); +} + +HTMLTag *HTMLBuilder::colgroup() { + write_tag(); + + return tag.start("colgroup"); +} + +HTMLTag *HTMLBuilder::data() { + write_tag(); + + return tag.start("cite"); +} + +HTMLTag *HTMLBuilder::datalist() { + write_tag(); + + return tag.start("datalist"); +} + +HTMLTag *HTMLBuilder::dd() { + write_tag(); + + return tag.start("dd"); +} + +HTMLTag *HTMLBuilder::del() { + write_tag(); + + return tag.start("del"); +} + +HTMLTag *HTMLBuilder::details() { + write_tag(); + + return tag.start("details"); +} + +HTMLTag *HTMLBuilder::dfn() { + write_tag(); + + return tag.start("dfn"); +} + +HTMLTag *HTMLBuilder::dialog() { + write_tag(); + + return tag.start("dialog"); +} + +HTMLTag *HTMLBuilder::dir() { // Not supported in HTML5. Use "; +} + +void HTMLBuilder::c_ol() { + write_tag(); + result += ""; +} + +void HTMLBuilder::coptgroup() { + write_tag(); + result += ""; +} + +void HTMLBuilder::coption() { + write_tag(); + result += ""; +} + +void HTMLBuilder::coutput() { + write_tag(); + result += ""; +} + +void HTMLBuilder::cp() { + write_tag(); + result += "

"; +} + +void HTMLBuilder::cparam() { + write_tag(); + result += ""; +} + +void HTMLBuilder::cpicture() { + write_tag(); + result += ""; +} + +void HTMLBuilder::cpre() { + write_tag(); + result += ""; +} + +void HTMLBuilder::cprogress() { + write_tag(); + result += ""; +} + +void HTMLBuilder::cq() { + write_tag(); + result += ""; +} + +void HTMLBuilder::crp() { + write_tag(); + result += ""; +} + +void HTMLBuilder::crt() { + write_tag(); + result += ""; +} + +void HTMLBuilder::cruby() { + write_tag(); + result += ""; +} + +void HTMLBuilder::cs() { + write_tag(); + result += ""; +} + +void HTMLBuilder::csamp() { + write_tag(); + result += ""; +} + +void HTMLBuilder::cscript() { + write_tag(); + result += ""; +} + +void HTMLBuilder::csection() { + write_tag(); + result += ""; +} + +void HTMLBuilder::cselect() { + write_tag(); + result += ""; +} + +void HTMLBuilder::csmall() { + write_tag(); + result += ""; +} + +void HTMLBuilder::csource() { + write_tag(); + result += ""; +} + +void HTMLBuilder::cspan() { + write_tag(); + result += ""; +} + +void HTMLBuilder::cstrike() { + write_tag(); + result += ""; +} + +void HTMLBuilder::cstrong() { + write_tag(); + result += ""; +} + +void HTMLBuilder::cstyle() { + write_tag(); + result += ""; +} + +void HTMLBuilder::csub() { + write_tag(); + result += ""; +} + +void HTMLBuilder::csummary() { + write_tag(); + result += ""; +} + +void HTMLBuilder::csup() { + write_tag(); + result += ""; +} + +void HTMLBuilder::csvg() { + write_tag(); + result += ""; +} + +void HTMLBuilder::ctable() { + write_tag(); + result += ""; +} + +void HTMLBuilder::ctbody() { + write_tag(); + result += ""; +} + +void HTMLBuilder::ctd() { + write_tag(); + result += ""; +} + +void HTMLBuilder::ctemplateh() { + write_tag(); + result += ""; +} + +void HTMLBuilder::ctextarea() { + write_tag(); + result += ""; +} + +void HTMLBuilder::ctfoot() { + write_tag(); + result += ""; +} + +void HTMLBuilder::cth() { + write_tag(); + result += ""; +} + +void HTMLBuilder::cthead() { + write_tag(); + result += ""; +} + +void HTMLBuilder::ctime() { + write_tag(); + result += ""; +} + +void HTMLBuilder::ctitle() { + write_tag(); + result += ""; +} + +void HTMLBuilder::ctr() { + write_tag(); + result += ""; +} + +void HTMLBuilder::ctrack() { + write_tag(); + result += ""; +} + +void HTMLBuilder::ctt() { + write_tag(); + result += ""; +} + +void HTMLBuilder::cu() { + write_tag(); + result += ""; +} + +void HTMLBuilder::cul() { + write_tag(); + result += ""; +} + +void HTMLBuilder::cvar() { + write_tag(); + result += ""; +} + +void HTMLBuilder::cvideo() { + write_tag(); + result += ""; +} + +void HTMLBuilder::cwbr() { + write_tag(); + result += ""; +} + +void HTMLBuilder::w(const std::string &val) { + write_tag(); + + result += val; +} + +//TODO! +void HTMLBuilder::we(const std::string &val) { + printf("HTMLBuilder::write_excaped NYI!"); + + write_tag(); + + result += val; +} + +void HTMLBuilder::write_tag() { + if (tag.has_data()) { + tag.close(); + result += tag.result; + tag.reset(); + } +} + +HTMLBuilder::HTMLBuilder() { +} + +HTMLBuilder::~HTMLBuilder() { +} \ No newline at end of file diff --git a/core/html_builder.h b/core/html_builder.h new file mode 100644 index 0000000..c24cd06 --- /dev/null +++ b/core/html_builder.h @@ -0,0 +1,315 @@ +#ifndef HTML_BUILDER_H +#define HTML_BUILDER_H + +#include +#include +#include + +class Request; + +//This might be converted to a form validator + +class HTMLTag { +public: + bool simple; + std::string result; + + HTMLTag *str(const std::string &str); + HTMLTag *style(const std::string &val); + HTMLTag *href(const std::string &val); + HTMLTag *cls(const std::string &val); + HTMLTag *id(const std::string &val); + HTMLTag *name(const std::string &val); + HTMLTag *method(const std::string &val); + HTMLTag *type(const std::string &val); + + HTMLTag *attrib(const std::string &attr, const std::string &val); + + HTMLTag *start(const std::string &p_new_tag, const bool p_simple = false); + HTMLTag *reset(); + HTMLTag *close(); + + bool has_data(); + + HTMLTag(); +}; + +class HTMLBuilder { +public: + std::string result; + + void comment(const std::string &val); + HTMLTag *doctype(); + void doctype(const std::string &val); + + HTMLTag *a(); + HTMLTag *abbr(); + HTMLTag *acronym(); //Not supported in HTML5. + HTMLTag *address(); + HTMLTag *applet(); //Not supported in HTML5. + HTMLTag *area(); + HTMLTag *article(); + HTMLTag *aside(); + HTMLTag *audio(); + HTMLTag *b(); + HTMLTag *basefont(); //Not supported in HTML5. + HTMLTag *bdi(); + HTMLTag *bdo(); + HTMLTag *big(); //Not supported in HTML5. + HTMLTag *blockquote(); + HTMLTag *body(); + HTMLTag *br(); + HTMLTag *button(); + HTMLTag *canvas(); + HTMLTag *caption(); + HTMLTag *center(); //Not supported in HTML5. + HTMLTag *cite(); + HTMLTag *code(); + HTMLTag *col(); + HTMLTag *colgroup(); + HTMLTag *data(); + HTMLTag *datalist(); + HTMLTag *dd(); + HTMLTag *del(); + HTMLTag *details(); + HTMLTag *dfn(); + HTMLTag *dialog(); + HTMLTag *dir(); // Not supported in HTML5. + HTMLTag *div(); + HTMLTag *dl(); + HTMLTag *dt(); + HTMLTag *em(); + HTMLTag *embed(); + HTMLTag *fieldset(); + HTMLTag *figcaption(); + HTMLTag *figure(); + HTMLTag *font(); //Not supported in HTML5. + HTMLTag *footer(); + HTMLTag *form(); + HTMLTag *frame(); //Not supported in HTML5. + HTMLTag *frameset(); //Not supported in HTML5. + HTMLTag *h1(); + HTMLTag *h2(); + HTMLTag *h3(); + HTMLTag *h4(); + HTMLTag *h5(); + HTMLTag *h6(); + HTMLTag *head(); + HTMLTag *header(); + HTMLTag *hr(); + HTMLTag *html(); + + HTMLTag *i(); + HTMLTag *iframe(); + HTMLTag *img(); + HTMLTag *input(); + HTMLTag *ins(); + HTMLTag *kbd(); + HTMLTag *label(); + HTMLTag *legend(); + HTMLTag *li(); + HTMLTag *link(); + HTMLTag *main(); + HTMLTag *map(); + HTMLTag *mark(); + HTMLTag *meta(); + HTMLTag *meter(); + + HTMLTag *nav(); + HTMLTag *noframes(); //Not supported in HTML5. + HTMLTag *noscript(); + HTMLTag *object(); + HTMLTag *ol(); + HTMLTag *optgroup(); + HTMLTag *option(); + HTMLTag *output(); + HTMLTag *p(); + HTMLTag *param(); + HTMLTag *picture(); + HTMLTag *pre(); + HTMLTag *progress(); + HTMLTag *q(); + HTMLTag *rp(); + + HTMLTag *rt(); + HTMLTag *ruby(); + HTMLTag *s(); + HTMLTag *samp(); + HTMLTag *script(); + HTMLTag *section(); + HTMLTag *select(); + HTMLTag *small(); + HTMLTag *source(); + HTMLTag *span(); + HTMLTag *strike(); //Not supported in HTML5 + HTMLTag *strong(); + HTMLTag *style(); + HTMLTag *sub(); + HTMLTag *summary(); + HTMLTag *sup(); + + HTMLTag *svg(); + HTMLTag *table(); + HTMLTag *tbody(); + HTMLTag *td(); + HTMLTag *templateh(); + HTMLTag *textarea(); + HTMLTag *tfoot(); + HTMLTag *th(); + HTMLTag *thead(); + HTMLTag *time(); + HTMLTag *title(); + HTMLTag *tr(); + HTMLTag *track(); + HTMLTag *tt(); //Not supported in HTML5. + HTMLTag *u(); + HTMLTag *ul(); + HTMLTag *var(); + HTMLTag *video(); + HTMLTag *wbr(); + + //closing tags c prefix means close + //Note simple tags should not have these like
+ //Note that I might have a few that shouldn't be here, those will be removed as I find them + void ca(); + void cabbr(); + void cacronym(); + void caddress(); + void capplet(); + void carea(); + void carticle(); + void caside(); + void caudio(); + void cb(); + void cbasefont(); + void cbdi(); + void cbdo(); + void cbig(); + void cblockquote(); + void cbody(); + void cbutton(); + void ccanvas(); + + void ccaption(); + void ccenter(); + void ccite(); + void ccode(); + void ccol(); + void ccolgroup(); + void cdata(); + void cdatalist(); + void cdd(); + void cdel(); + void cdetails(); + void cdfn(); + void cdialog(); + void cdir(); + void cdiv(); + void cdl(); + void cdt(); + + void cem(); + void cembed(); + void cfieldset(); + void cfigcaption(); + void cfigure(); + void cfont(); + void cfooter(); + void cform(); + void cframe(); + void cframeset(); + void ch1(); + void ch2(); + void ch3(); + void ch4(); + void ch5(); + void ch6(); + void chead(); + void cheader(); + void chr(); + void chtml(); + + void ci(); + void ciframe(); + void cimg(); + void cinput(); + void cins(); + void ckbd(); + void clabel(); + void clegend(); + void cli(); + void clink(); + void cmain(); + void cmap(); + void cmark(); + void cmeta(); + void cmeter(); + + void cnav(); + void cnoframes(); + void cnoscript(); + void cobject(); + void c_ol(); + void coptgroup(); + void coption(); + void coutput(); + void cp(); + void cparam(); + void cpicture(); + void cpre(); + void cprogress(); + void cq(); + void crp(); + + void crt(); + void cruby(); + void cs(); + void csamp(); + void cscript(); + void csection(); + void cselect(); + void csmall(); + void csource(); + void cspan(); + void cstrike(); + void cstrong(); + void cstyle(); + void csub(); + void csummary(); + void csup(); + + void csvg(); + void ctable(); + void ctbody(); + void ctd(); + void ctemplateh(); + void ctextarea(); + void ctfoot(); + void cth(); + void cthead(); + void ctime(); + void ctitle(); + void ctr(); + void ctrack(); + void ctt(); + void cu(); + void cul(); + void cvar(); + void cvideo(); + void cwbr(); + + //write + void w(const std::string &val); + //write_escaped + void we(const std::string &val); + + void write_tag(); + + HTMLBuilder(); + virtual ~HTMLBuilder(); + +protected: + HTMLTag tag; +}; + +#endif \ No newline at end of file diff --git a/rdn_application.cpp b/rdn_application.cpp index d402a0f..9fcf05a 100644 --- a/rdn_application.cpp +++ b/rdn_application.cpp @@ -10,6 +10,8 @@ #include "core/database_manager.h" +#include "core/html_builder.h" + void RDNApplication::index(Object *instance, Request *request) { std::string body; @@ -19,7 +21,32 @@ void RDNApplication::index(Object *instance, Request *request) { return; } - request->body.append("

Test HTML Body

"); + HTMLBuilder b; + + b.h1(); + b.w("Testh1"); + b.ch1(); + + b.h2()->cls("tcls")->id("tid"); + b.w("Testh2"); + b.ch2(); + + b.br(); + + b.p(); + b.w("Test HTML Body HTMLBuilder"); + b.cp(); + + b.form()->method("post")->href("/"); + + //->str("/") is a temp hack + b.input()->type("text")->str("/"); + b.input()->type("submit")->str("/"); + b.cform(); + + request->body = b.result; + + //request->body.append("

Test HTML Body

"); request->compile_body(); FileCache::get_singleton()->set_cached_body("index", request->compiled_body);