diff --git a/README.md b/README.md index 0f771c0..7f0cd94 100644 --- a/README.md +++ b/README.md @@ -55,11 +55,11 @@ html_t hello_world() // ^^^ functions that return content to be rendered // must return the html_t or char* types - html_t tmp = malloc(128 * sizeof(char)); + html_t tmp; // ^^^ you don't need to free this malloc, // the framework will do it for you. - sprintf(tmp, "%s", "Hello, World!"); + asprintf(&tmp, "%s", "Hello, World!"); // ^^^ this works because html_t expands // to char* @@ -96,7 +96,7 @@ To test the project, run: `tapm run`. Navigating to `http://127.0.0.1:4000` in y ![getting-started-1](https://i.imgur.com/zGWqSow.png) -For a more complex example, view the project in the `example/` folder of the repo! +For more complex examples, view the project in the `examples/` folder of the repo! ## Appendix @@ -129,13 +129,20 @@ html_t html = HTML( ); ``` -### Building from source +...or: -The root level `Makefile` was designed for POSIX-compliant systems — `tapm build` will not work on Windows outside of WSL. - -### Binding a C function to a DOM event - -![binding-a-c-function-to-a-dom-event](https://camo.githubusercontent.com/339f5dbb4a8a9945034f5b1ba5efc7e5e2780a5353361c2fbea52b5ae47c7c4b/68747470733a2f2f692e696d6775722e636f6d2f6c766e6f646e442e676966) +``` +html_t html = HTMLIFY(" \ +
\ +

${hello_world()}$

\ +
\ + \ +"); +``` ### Returning other types as `html_t` @@ -143,10 +150,9 @@ The root level `Makefile` was designed for POSIX-compliant systems — `tapm bui EMSCRIPTEN_KEEPALIVE html_t add_two_numbers(int a, int b) { - char *tmp = malloc(128 * sizeof(int)); + char *tmp; int result = a + b; - sprintf(tmp, "%d", result); - html_t expands to char* + asprintf(&tmp, "%d", result); return tmp; } ``` @@ -164,20 +170,6 @@ html_t hello_world() ``` ... would also work. Thing is, it is not the best because the framework will try to free a pointer that wasn't malloc-ed. While this doesn't cause an error, I wouldn't call it a best practice. -### Are you updating HTML you've already rendered? - -If that's the case, you must remove the old html before you update your state. For example, when updating the counter in `example/src/hello_world.c`, I do: - -```C -EMSCRIPTEN_KEEPALIVE -void increase_counter() -{ - remove_html(main_h); - counter++; - display_html(main_h); -} -``` - ### Notes to Self To check for any problems (i.e., mem leaks and whatnot) in the code at runtime, compile like so: `emcc hello_world.c ../../src/dcw.c --js-library ../../src/dcw.js -fsanitize=address -s ALLOW_MEMORY_GROWTH -s INITIAL_MEMORY=285212672 -gsource-map --source-map-base http://127.0.0.1:4000`. After that, run with: `tapm run`. diff --git a/Tarantella.toml b/Tarantella.toml index 7f5e228..8004d1e 100644 --- a/Tarantella.toml +++ b/Tarantella.toml @@ -1,6 +1,6 @@ [package] name = "dcw" -version = "0.4.0" # format: major.minor.patch +version = "0.5.0" # format: major.minor.patch module_type = "side_module" # main_module || side_module build_dir = "build" # when modifying this field, change BUILD_DIR in the Makefile too releases_repo = "https://github.com/danbugs/dancing_web" # format: https://github.com// diff --git a/docs/dcw_8c.html b/docs/dcw_8c.html index 4f2832a..4d7ba53 100644 --- a/docs/dcw_8c.html +++ b/docs/dcw_8c.html @@ -70,7 +70,6 @@
@@ -84,43 +83,56 @@ #include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <stdbool.h>
#include "error.cml"
Include dependency graph for dcw.c:
- - - - - - - - - + + + + + + + + + +
- - - -

-Macros

#define dbg(to_print, formatter)
 
- - + + + + + + + + - - + + + + + + + + + + + +

Functions

void displayInner (html_t html)
 
void removeInner (html_t html)
 
void removeStaticInner (html_t html)
 
void removeWithClassInner (char *class_name)
 
void removeChildrenOfIdInner (char *id)
 
void insertAtIdInner (html_t html, char *id)
 
html_t parse_html_wrapper (parse_html_args in)
 
html_t parse_html_core (html_t raw_html, marker_t marker)
 
EMSCRIPTEN_KEEPALIVE void display_html (html_t raw_html)
 
EMSCRIPTEN_KEEPALIVE void remove_html (html_t raw_html)
 
EMSCRIPTEN_KEEPALIVE void remove_static_html (html_t raw_html)
 
EMSCRIPTEN_KEEPALIVE void remove_html_with_class (char *class_name)
 
EMSCRIPTEN_KEEPALIVE void display_html_loop (html_t html_formatter, char **elements, int num_elements, bool reverse)
 
EMSCRIPTEN_KEEPALIVE html_t render_html_loop (html_t html_formatter, char **elements, int num_elements, bool reverse)
 
EMSCRIPTEN_KEEPALIVE void remove_html_children_of_id (char *id)
 
EMSCRIPTEN_KEEPALIVE void insert_html_at_id (html_t raw_html, char *id)
 
@@ -129,24 +141,61 @@

Variables

Detailed Description

A framework to write HTML directly within C and have it call C functions. Users interact with it through the display_html and remove_html functions, and the html_t and HTML macros in dcw.h.

-

Macro Definition Documentation

- -

◆ dbg

+

Function Documentation

+ +

◆ display_html()

- + - - + + + + +
#define dbgEMSCRIPTEN_KEEPALIVE void display_html ( to_print, html_t raw_html)
+
+

A wrapper for parse_html, and the JS funtion displayInner .

+
Parameters
+ + +
raw_htmlThe HTML you want to display. This should be of type html_t or char*.
+
+
+ +
+
+ +

◆ display_html_loop()

+ +
+
+ + + + + + + + + + + + - - + + + + + + + + @@ -155,58 +204,108 @@

-Value:
{ \
-
printf(#to_print ": " #formatter "\n", to_print); \
-
}
-

A utility function for easy debug. It prints the variable name and its' value. Usage: char *hello_world = "Hello, World!"; dbg(hello_world, d);

-

Output: "hello_world: Hello, World!"

+

Displays a char** as HTML inside a specific HTML formatter.

+
Parameters
+

EMSCRIPTEN_KEEPALIVE void display_html_loop (html_t html_formatter,
char ** elements,
 formatter int num_elements,
bool reverse 
+ + + + +
html_formatterWhere each element of your char** will be rendered (e.g., "<p>%s</p>") - use normal C formatters.
elementsThe array you want to render.
num_elementsThe number of elements of the elements array.
reverseIf true , the array will be rendered in reverse. or char*.
+ +
-

Function Documentation

-
-

◆ display_html()

+ +

◆ displayInner()

- + - +
EMSCRIPTEN_KEEPALIVE void display_html void displayInner ( html_t raw_html)html)
-

A wrapper for parse_html, and the JS funtion displayInner .

+

A JS funtion that renders the HTML to the DOM.

Parameters
+ + +
htmlThe HTML you want to render. This should be of type html_t or char*.
+
+
+ +
+
+ +

◆ insert_html_at_id()

+ +
+
+ + + + + + + + + + + + + + + + + + +
EMSCRIPTEN_KEEPALIVE void insert_html_at_id (html_t raw_html,
char * id 
)
+
+

A wrapper for parse_html, and the JS funtion insertAtIdInner .

Parameters
+
raw_htmlThe HTML you want to display. This should be of type html_t or char*.
idThe ID where you want to insert the parsed HTML at.
- -

◆ displayInner()

+ +

◆ insertAtIdInner()

- + - + + + + + + + + + + +
void displayInner void insertAtIdInner ( html_t html)html,
char * id 
)
-

A JS funtion that renders the HTML to the DOM.

Parameters
+

A JS funtion that renders the HTML to the DOM at a specific ID.

Parameters
+
htmlThe HTML you want to render. This should be of type html_t or char*.
idThe id of the HTML you want to render.
@@ -273,14 +372,64 @@

-

◆ remove_html()

+ +

◆ remove_html_children_of_id()

+ +
+
+ + + + + + + + +
EMSCRIPTEN_KEEPALIVE void remove_html_children_of_id (char * id)
+
+

A wrapper for the JS funtion removeChildrenOfIdInner .

+
Parameters
+ + +
class_nameThe id of the HTML you want to remove the children of. This should be of type html_t or char*.
+
+
+ +
+
+ +

◆ remove_html_with_class()

- + + + + + + +
EMSCRIPTEN_KEEPALIVE void remove_html EMSCRIPTEN_KEEPALIVE void remove_html_with_class (char * class_name)
+
+

A wrapper for the JS funtion removeWithClassInner .

+
Parameters
+ + +
class_nameThe class of the HTML you want to remove. This should be of type html_t or char*.
+
+
+ +
+
+ +

◆ remove_static_html()

+ +
+
+ + + @@ -288,7 +437,7 @@

-

A wrapper for parse_html, and the JS funtion removeInner .

+

A wrapper for parse_html, and the JS funtion removeStaticInner .

Parameters

EMSCRIPTEN_KEEPALIVE void remove_static_html ( html_t  raw_html)
@@ -298,14 +447,38 @@

-

◆ removeInner()

+ +

◆ removeChildrenOfIdInner()

+ +
+
+
raw_htmlThe HTML you want to remove. This should be of type html_t or char*.
+ + + + + + + +
void removeChildrenOfIdInner (char * id)
+
+

A JS funtion that removes the children of an HTML node with a specific ID.

Parameters
+ + +
htmlThe ID of the parent HTML.
+
+
+ +
+
+ +

◆ removeStaticInner()

- + @@ -313,13 +486,88 @@

-

A JS funtion that removes HTML from the DOM.

Parameters
+

A JS funtion that removes static HTML from the DOM.

Parameters

void removeInner void removeStaticInner ( html_t  html)
htmlThe HTML you want to remove. This should be of type html_t or char*.

+
+
+ +

◆ removeWithClassInner()

+ +
+
+ + + + + + + + +
void removeWithClassInner (char * class_name)
+
+

A JS funtion that removes HTML from the DOM with a specific class.

Parameters
+ + +
htmlThe class of the HTML you want to remove.
+
+
+ +
+
+ +

◆ render_html_loop()

+ +
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
EMSCRIPTEN_KEEPALIVE html_t render_html_loop (html_t html_formatter,
char ** elements,
int num_elements,
bool reverse 
)
+
+

Renders a char** as HTML inside a specific HTML formatter.

+
Parameters
+ + + + + +
html_formatterWhere each element of your char** will be rendered (e.g., "<p>%s</p>") - use normal C formatters.
elementsThe array you want to render.
num_elementsThe number of elements of the elements array.
reverseIf true , the array will be rendered in reverse.
+
+
+
Returns
The rendered HTML that can be inserted into any part of the DOM. or char*.
+

Variable Documentation

@@ -335,7 +583,7 @@

Initial value:
=
-
# 31 "/mnt/c/Users/danil/source/repos/dancing_web/src/dcw.c" 2
+
# 32 "/mnt/c/Users/danil/source/repos/dancing_web/src/dcw.c" 2

The HTML that is displayed to the users then there is DCW encounters an error.

diff --git a/docs/dcw_8c__incl.map b/docs/dcw_8c__incl.map index 55e55c0..4481b0b 100644 --- a/docs/dcw_8c__incl.map +++ b/docs/dcw_8c__incl.map @@ -1,11 +1,12 @@ - - - - - - - - - - - + + + + + + + + + + + + diff --git a/docs/dcw_8c__incl.md5 b/docs/dcw_8c__incl.md5 index 3383d7c..93fad17 100644 --- a/docs/dcw_8c__incl.md5 +++ b/docs/dcw_8c__incl.md5 @@ -1 +1 @@ -5ce12cf315bdc2fd53b0abe1d271605e \ No newline at end of file +a660977b8c4d102b08b7bf46ef82af80 \ No newline at end of file diff --git a/docs/dcw_8c__incl.png b/docs/dcw_8c__incl.png index b299d4c..821af52 100644 Binary files a/docs/dcw_8c__incl.png and b/docs/dcw_8c__incl.png differ diff --git a/docs/dcw_8h.html b/docs/dcw_8h.html index 40fb687..58e9ca2 100644 --- a/docs/dcw_8h.html +++ b/docs/dcw_8h.html @@ -92,12 +92,50 @@ Macros

#define html_t   char *   -#define HTML(...)   #__VA_ARGS__; +#define HTML(...)   #__VA_ARGS__   + +#define HTMLIFY(str)   (html_t) str +  +#define dbg(to_print, formatter) + 

Detailed Description

Useful macros intended to be used by the end-user.

Macro Definition Documentation

+ +

◆ dbg

+ +
+
+ + + + + + + + + + + + + + + + + + +
#define dbg( to_print,
 formatter 
)
+
+Value:
{ \
+
printf(#to_print ": " #formatter "\n", to_print); \
+
}
+

A utility function for easy debug. It prints the variable name and its' value. Usage: char *hello_world = "Hello, World!"; dbg(hello_world, d);

+

Output: "hello_world: Hello, World!"

+ +
+

◆ HTML

@@ -109,7 +147,7 @@

  ...) -    #__VA_ARGS__; +    #__VA_ARGS__
diff --git a/docs/dcw_8h_source.html b/docs/dcw_8h_source.html index ad79a00..7eb2f0e 100644 --- a/docs/dcw_8h_source.html +++ b/docs/dcw_8h_source.html @@ -73,21 +73,27 @@
dcw.h
-Go to the documentation of this file.
1 #pragma once
-
2 
-
6 /*
-
7  * Copyright 2021 Danilo Chiarlone
-
8  * LICENSE: GNU AFFERO GENERAL PUBLIC LICENSE
-
9 */
-
10 
-
13 #define html_t char *
-
14 
-
17 #define HTML(...) #__VA_ARGS__;
-
18 
-
27 #define dbg(to_print, formatter) \
-
28  { \
-
29  printf(#to_print ": " #formatter "\n", to_print); \
-
30  }
+Go to the documentation of this file.
1 
+
5 /*
+
6  * Copyright 2021 Danilo Chiarlone
+
7  * LICENSE: GNU AFFERO GENERAL PUBLIC LICENSE
+
8 */
+
9 
+
10 #ifndef DCW_H
+
11 #define DCW_H
+
12 
+
15 #define html_t char *
+
16 
+
19 #define HTML(...) #__VA_ARGS__
+
20 
+
21 #define HTMLIFY(str) (html_t) str
+
22 
+
31 #define dbg(to_print, formatter) \
+
32  { \
+
33  printf(#to_print ": " #formatter "\n", to_print); \
+
34  }
+
35 
+
36 #endif
Go to the documentation of this file.
1 
-
5 #include "dcw.h"
-
6 
- -
12 {
-
13  RENDERABLE_MARKERS,
-
14  EXECUTABLE_MARKERS,
-
15 };
-
16 
-
20 typedef struct
-
21 {
-
22  char expression[20]; // maximum 20 chars (using this instead of char* to avoid a null-pointer-dereference)
-
23  enum marker_type type;
-
24  int size;
-
25  int front_size;
-
26  int back_size;
-
27  int eval_increment;
-
28  // ^ how many characters we need to add to the expression when evaluated
-
29  // (i.e., 1 for RENDERABLE_MARKERS due to the '_' , and 8 for EXECUTABLE_MARKERS due to the 'Module._')
-
30 } marker_t;
-
31 
-
35 typedef struct
-
36 {
-
37  html_t raw_html;
-
38  enum marker_type marker;
- -
40 
-
44 #define parse_html(...) parse_html_wrapper((parse_html_args){__VA_ARGS__});
-
45 
- -
47 
-
48 html_t parse_html_core(html_t raw_html, marker_t marker);
-
49 
-
50 void display_html(html_t raw_html);
+
4 #ifndef DCW_INTERNAL_H
+
5 #define DCW_INTERNAL_H
+
6 #include "dcw.h"
+
7 
+ +
13 {
+
14  RENDERABLE_MARKERS,
+
15  EXECUTABLE_MARKERS,
+
16 };
+
17 
+
21 typedef struct
+
22 {
+
23  char expression[20]; // maximum 20 chars (using this instead of char* to avoid a null-pointer-dereference)
+
24  enum marker_type type;
+
25  int size;
+
26  int front_size;
+
27  int back_size;
+
28  int eval_increment;
+
29  // ^ how many characters we need to add to the expression when evaluated
+
30  // (i.e., 1 for RENDERABLE_MARKERS due to the '_' , and 8 for EXECUTABLE_MARKERS due to the 'Module._')
+
31 } marker_t;
+
32 
+
36 typedef struct
+
37 {
+
38  html_t raw_html;
+
39  enum marker_type marker;
+ +
41 
+
45 #define parse_html(...) parse_html_wrapper((parse_html_args){__VA_ARGS__});
+
46 
+ +
48 
+
49 html_t parse_html_core(html_t raw_html, marker_t marker);
+
50 
+
51 void display_html(html_t raw_html);
+
52 #endif
-
html_t parse_html_core(html_t raw_html, marker_t marker)
Definition: dcw.c:79
-
Definition: dcw_internal.h:35
-
marker_type
Definition: dcw_internal.h:11
+
html_t parse_html_core(html_t raw_html, marker_t marker)
Definition: dcw.c:98
+
Definition: dcw_internal.h:36
+
marker_type
Definition: dcw_internal.h:12
-
html_t parse_html_wrapper(parse_html_args in)
Definition: dcw.c:48
-
#define html_t
Definition: dcw.h:13
-
void display_html(html_t raw_html)
Definition: dcw.c:148
-
Definition: dcw_internal.h:20
+
html_t parse_html_wrapper(parse_html_args in)
Definition: dcw.c:66
+
#define html_t
Definition: dcw.h:15
+
void display_html(html_t raw_html)
Definition: dcw.c:167
+
Definition: dcw_internal.h:21