Skip to content
Snippets Groups Projects
Commit f027c56d authored by Jonathan Schöbel's avatar Jonathan Schöbel
Browse files

added "wrap mode" for html generation

When the wrap mode is used, after each tag a newline is started. Also the
html is indented, which can be configured by the parameters indent_base,
indent_base, indent_char. The parameter indent_base specifies the width
the first tag should be indented with, while indent_step specifies the
increment of the indent when switching to a child tag. The character,
that is used for indenting is taken from indent_char. (It could also be
a string longer than a single character).
This aguments can't be set by the user, but are hardcoded (by now).
parent ce4fcf29
Branches
Tags
No related merge requests found
......@@ -28,16 +28,16 @@ long_line_behaviour=1
long_line_column=72
[files]
current_page=20
current_page=7
FILE_NAME_0=607;Sh;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDokumente%2Fprojekte%2Fprgm%2Finternet%2Fweb%2FSeFHT%2Fconfigure.ac;0;8
FILE_NAME_1=395;Make;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDokumente%2Fprojekte%2Fprgm%2Finternet%2Fweb%2FSeFHT%2Fsrc%2FMakefile.am;0;8
FILE_NAME_2=1584;C;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDokumente%2Fprojekte%2Fprgm%2Finternet%2Fweb%2FSeFHT%2Fsrc%2Fmain.c;0;8
FILE_NAME_2=2057;C;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDokumente%2Fprojekte%2Fprgm%2Finternet%2Fweb%2FSeFHT%2Fsrc%2Fmain.c;0;8
FILE_NAME_3=1067;C;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDokumente%2Fprojekte%2Fprgm%2Finternet%2Fweb%2FSeFHT%2Fsrc%2Fcms.c;0;8
FILE_NAME_4=1148;C;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDokumente%2Fprojekte%2Fprgm%2Finternet%2Fweb%2FSeFHT%2Fsrc%2Fcms.h;0;8
FILE_NAME_5=2719;C;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDokumente%2Fprojekte%2Fprgm%2Finternet%2Fweb%2FSeFHT%2Fsrc%2Fdata.c;0;8
FILE_NAME_6=1099;C;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDokumente%2Fprojekte%2Fprgm%2Finternet%2Fweb%2FSeFHT%2Fsrc%2Fdata.h;0;8
FILE_NAME_7=3619;C;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDokumente%2Fprojekte%2Fprgm%2Finternet%2Fweb%2FSeFHT%2Fsrc%2Ffragment.c;0;8
FILE_NAME_8=1565;C;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDokumente%2Fprojekte%2Fprgm%2Finternet%2Fweb%2FSeFHT%2Fsrc%2Ffragment.h;0;8
FILE_NAME_7=9340;C;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDokumente%2Fprojekte%2Fprgm%2Finternet%2Fweb%2FSeFHT%2Fsrc%2Ffragment.c;0;8
FILE_NAME_8=1283;C;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDokumente%2Fprojekte%2Fprgm%2Finternet%2Fweb%2FSeFHT%2Fsrc%2Ffragment.h;0;8
FILE_NAME_9=3600;C;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDokumente%2Fprojekte%2Fprgm%2Finternet%2Fweb%2FSeFHT%2Fsrc%2Ftext.c;0;8
FILE_NAME_10=1397;C;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDokumente%2Fprojekte%2Fprgm%2Finternet%2Fweb%2FSeFHT%2Fsrc%2Ftext.h;0;8
FILE_NAME_11=3094;C;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDokumente%2Fprojekte%2Fprgm%2Finternet%2Fweb%2FSeFHT%2Fsrc%2Fvalidator.c;0;8
......@@ -49,7 +49,7 @@ FILE_NAME_16=847;C;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDokumente%2Fprojekte%2Fpr
FILE_NAME_17=1043;Make;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDokumente%2Fprojekte%2Fprgm%2Finternet%2Fweb%2FSeFHT%2Ftests%2FMakefile.am;0;8
FILE_NAME_18=1113;C;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDokumente%2Fprojekte%2Fprgm%2Finternet%2Fweb%2FSeFHT%2Ftests%2Ftest_cms.c;0;8
FILE_NAME_19=3059;C;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDokumente%2Fprojekte%2Fprgm%2Finternet%2Fweb%2FSeFHT%2Ftests%2Ftest_data.c;0;8
FILE_NAME_20=3241;C;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDokumente%2Fprojekte%2Fprgm%2Finternet%2Fweb%2FSeFHT%2Ftests%2Ftest_fragment.c;0;8
FILE_NAME_20=11151;C;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDokumente%2Fprojekte%2Fprgm%2Finternet%2Fweb%2FSeFHT%2Ftests%2Ftest_fragment.c;0;8
FILE_NAME_21=5331;C;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDokumente%2Fprojekte%2Fprgm%2Finternet%2Fweb%2FSeFHT%2Ftests%2Ftest_text.c;0;8
FILE_NAME_22=6057;C;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDokumente%2Fprojekte%2Fprgm%2Finternet%2Fweb%2FSeFHT%2Ftests%2Ftest_validator.c;0;8
FILE_NAME_23=55;None;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDokumente%2Fprojekte%2Fprgm%2Finternet%2Fweb%2FSeFHT%2Ftodo.txt;0;8
......
......@@ -362,10 +362,15 @@ SH_Fragment_append_child (struct SH_Fragment * fragment,
static struct SH_Text *
Fragment_to_html (struct SH_Fragment * fragment,
enum HTML_MODE mode,
unsigned int indent_base,
unsigned int indent_step,
char * indent_char,
struct SH_Error * error)
{
struct SH_Text * html;
struct SH_Text * child;
struct SH_Text * indent_text;
size_t index;
html = SH_Text_new (error);
......@@ -374,32 +379,98 @@ Fragment_to_html (struct SH_Fragment * fragment,
return NULL;
}
if (mode == WRAP)
{
indent_text = SH_Text_new (error);
if (indent_text == NULL)
{
SH_Text_free (html, NULL);
return NULL;
}
for (index = 0; index < indent_base; index++)
{
if (!SH_Text_append_string (indent_text,
indent_char, error))
{
SH_Text_free (html, NULL);
SH_Text_free (indent_text, NULL);
return NULL;
}
}
if (!SH_Text_append_text (html, indent_text, error))
{
SH_Text_free (html, NULL);
SH_Text_free (indent_text, NULL);
return NULL;
}
}
if (!SH_Text_append_string (html, OPEN_TAG_BEGIN, error))
{
SH_Text_free (html, NULL);
if (mode == WRAP)
{
SH_Text_free (indent_text, NULL);
}
return NULL;
}
if (!SH_Text_append_string (html, fragment->tag, error))
{
SH_Text_free (html, NULL);
if (mode == WRAP)
{
SH_Text_free (indent_text, NULL);
}
return NULL;
}
if (!SH_Text_append_string (html, OPEN_TAG_END, error))
{
SH_Text_free (html, NULL);
if (mode == WRAP)
{
SH_Text_free (indent_text, NULL);
}
return NULL;
}
if (mode == WRAP)
{
if (!SH_Text_append_string (html, NEWLINE, error))
{
SH_Text_free (html, NULL);
SH_Text_free (indent_text, NULL);
return NULL;
}
}
for (index = 0; index < fragment->child_n; index++)
{
child = Fragment_to_html (fragment->childs[index],
mode,
indent_base + indent_step,
indent_step,
indent_char,
error);
if (child == NULL)
{
SH_Text_free (html, NULL);
if (mode == WRAP)
{
SH_Text_free (indent_text, NULL);
}
return NULL;
}
......@@ -407,6 +478,22 @@ Fragment_to_html (struct SH_Fragment * fragment,
{
SH_Text_free (html, NULL);
SH_Text_free (child, NULL);
if (mode == WRAP)
{
SH_Text_free (indent_text, NULL);
}
return NULL;
}
}
if (mode == WRAP)
{
if (!SH_Text_append_text (html, indent_text, error))
{
SH_Text_free (html, NULL);
SH_Text_free (indent_text, NULL);
return NULL;
}
}
......@@ -414,21 +501,51 @@ Fragment_to_html (struct SH_Fragment * fragment,
if (!SH_Text_append_string (html, CLOSE_TAG_BEGIN, error))
{
SH_Text_free (html, NULL);
if (mode == WRAP)
{
SH_Text_free (indent_text, NULL);
}
return NULL;
}
if (!SH_Text_append_string (html, fragment->tag, error))
{
SH_Text_free (html, NULL);
if (mode == WRAP)
{
SH_Text_free (indent_text, NULL);
}
return NULL;
}
if (!SH_Text_append_string (html, CLOSE_TAG_END, error))
{
SH_Text_free (html, NULL);
if (mode == WRAP)
{
SH_Text_free (indent_text, NULL);
}
return NULL;
}
if (mode == WRAP)
{
if (!SH_Text_append_string (html, NEWLINE, error))
{
SH_Text_free (html, NULL);
SH_Text_free (indent_text, NULL);
return NULL;
}
SH_Text_free (indent_text, NULL);
}
if (error != NULL)
{
error->type = SUCCESS;
......@@ -439,7 +556,9 @@ Fragment_to_html (struct SH_Fragment * fragment,
struct SH_Text *
SH_Fragment_to_html (struct SH_Fragment * fragment,
enum HTML_MODE mode,
struct SH_Error * error)
{
return Fragment_to_html (fragment, error);
return Fragment_to_html (fragment, mode, 0, 1, INDENT_TEXT,
error);
}
......@@ -44,6 +44,15 @@ struct SH_Fragment
#define CLOSE_TAG_BEGIN "</"
#define CLOSE_TAG_END ">"
enum HTML_MODE
{
INLINE,
WRAP
};
#define INDENT_TEXT "\t"
#define NEWLINE "\n"
struct SH_Fragment * SH_Fragment_new (const char * tag,
struct SH_Data * data,
struct SH_Error * error);
......@@ -74,6 +83,7 @@ bool SH_Fragment_append_child (struct SH_Fragment * fragment,
struct SH_Error * error);
struct SH_Text * SH_Fragment_to_html (struct SH_Fragment * fragment,
enum HTML_MODE mode,
struct SH_Error * error);
#endif /* _FRAGMENT_H */
......@@ -45,18 +45,35 @@ int main(int argc, char **argv)
struct SH_Data * data;
struct SH_Fragment * root;
struct SH_Fragment * child;
struct SH_Fragment * child1;
struct SH_Fragment * child2;
struct SH_Text * text;
data = SH_Data_new (NULL);
root = SH_Fragment_new ("html", data, NULL);
child = SH_Fragment_new ("body", data, NULL);
child1 = SH_Fragment_new ("head", data, NULL);
SH_Fragment_append_child (root, child1, NULL);
child2 = SH_Fragment_new ("title", data, NULL);
SH_Fragment_append_child (child1, child2, NULL);
SH_Fragment_append_child (root, child, NULL);
child1 = SH_Fragment_new ("body", data, NULL);
SH_Fragment_append_child (root, child1, NULL);
child2 = SH_Fragment_new ("header", data, NULL);
SH_Fragment_append_child (child1, child2, NULL);
child2 = SH_Fragment_new ("main", data, NULL);
SH_Fragment_append_child (child1, child2, NULL);
child2 = SH_Fragment_new ("footer", data, NULL);
SH_Fragment_append_child (child1, child2, NULL);
text = SH_Fragment_to_html (root, NULL);
text = SH_Fragment_to_html (root, WRAP, NULL);
printf ("%s\n", text->text);
SH_Text_free (text, NULL);
text = SH_Fragment_to_html (root, INLINE, NULL);
printf ("%s\n", text->text);
......
......@@ -403,9 +403,13 @@ START_TEST(test_fragment_html)
fragment2 = SH_Fragment_new ("body", data, NULL);
SH_Fragment_append_child (fragment1, fragment2, NULL);
text = SH_Fragment_to_html (fragment1, NULL);
text = SH_Fragment_to_html (fragment1, INLINE, NULL);
ck_assert_str_eq (text->text, "<html><body></body></html>");
text = SH_Fragment_to_html (fragment1, WRAP, NULL);
ck_assert_str_eq (text->text,
"<html>\n\t<body>\n\t</body>\n</html>\n");
SH_Fragment_free (fragment1, NULL);
/* error */
......@@ -414,10 +418,16 @@ START_TEST(test_fragment_html)
SH_Fragment_append_child (fragment1, fragment2, NULL);
error.type = UNDEFINED;
text = SH_Fragment_to_html (fragment1, &error);
text = SH_Fragment_to_html (fragment1, INLINE, &error);
ck_assert_str_eq (text->text, "<html><body></body></html>");
ck_assert_int_eq (error.type, SUCCESS);
error.type = UNDEFINED;
text = SH_Fragment_to_html (fragment1, WRAP, &error);
ck_assert_str_eq (text->text,
"<html>\n\t<body>\n\t</body>\n</html>\n");
ck_assert_int_eq (error.type, SUCCESS);
SH_Fragment_free (fragment1, NULL);
SH_Data_free (data, NULL);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment