Skip to content

Commit cb0926a

Browse files
author
Joshua Pokotilow
committed
Added a generate_snippet mode.
When passing the --generate-snippet parameter to the program, only the contents of the HTML <body> tag are printed. This may be useful for situations when markup needs to be formatted to work with AJAX services, or for other situations where it's inappropriate to print a whole document.
1 parent fc32d0e commit cb0926a

File tree

4 files changed

+56
-31
lines changed

4 files changed

+56
-31
lines changed

src/html2xhtml.c

+2
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,8 @@ static void process_parameters(int argc, char **argv)
211211
} else if (!strcmp(argv[i], "--lcs")) {
212212
charset_dump_aliases(stdout);
213213
exit(0);
214+
} else if (!strcmp(argv[i], "--generate-snippet")) {
215+
param_generate_snippet = 1;
214216
} else {
215217
help();
216218
exit(1);

src/params.c

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ int param_compact_block_elms;
3636
int param_compact_empty_elm_tags;
3737
int param_empty_tags;
3838
int param_crlf_eol;
39+
int param_generate_snippet;
3940

4041
void params_set_defaults()
4142
{
@@ -55,5 +56,6 @@ void params_set_defaults()
5556
param_cgi_html_output = 0;
5657
param_empty_tags = 0;
5758
param_crlf_eol = 0;
59+
param_generate_snippet = 0;
5860
}
5961

src/params.h

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ extern int param_compact_block_elms;
4747
extern int param_compact_empty_elm_tags;
4848
extern int param_empty_tags;
4949
extern int param_crlf_eol;
50+
extern int param_generate_snippet;
5051

5152
void params_set_defaults(void);
5253

src/procesador.c

+51-31
Original file line numberDiff line numberDiff line change
@@ -2113,17 +2113,18 @@ static void write_document(document_t *doc)
21132113

21142114
cprintf_init(param_charset_out, param_outputf);
21152115

2116-
/* write <?xml... */
2117-
cprintf("%s?xml version=\"1.0\"", lt);
2118-
/* if (document->encoding[0]) */
2119-
/* cprintf(" encoding=\"%s\"",document->encoding); */
2120-
cprintf(" encoding=\"%s\"", param_charset_out->preferred_name);
2121-
cprintf("?%s%s%s", gt, eol, eol);
2122-
2123-
/* write <!DOCTYPE... */
2124-
cprintf("%s!DOCTYPE html%s %s%s \"%s\" %s%s",
2125-
lt, eol, doctype_string[doctype], eol, dtd_string[doctype], gt, eol);
2126-
2116+
if (!param_generate_snippet) {
2117+
/* write <?xml... */
2118+
cprintf("%s?xml version=\"1.0\"", lt);
2119+
/* if (document->encoding[0]) */
2120+
/* cprintf(" encoding=\"%s\"",document->encoding); */
2121+
cprintf(" encoding=\"%s\"", param_charset_out->preferred_name);
2122+
cprintf("?%s%s%s", gt, eol, eol);
2123+
2124+
/* write <!DOCTYPE... */
2125+
cprintf("%s!DOCTYPE html%s %s%s \"%s\" %s%s",
2126+
lt, eol, doctype_string[doctype], eol, dtd_string[doctype], gt, eol);
2127+
}
21272128
p = doc->inicio;
21282129
indent = 0;
21292130
write_node(p);
@@ -2157,14 +2158,29 @@ static int write_node(tree_node_t *node)
21572158
return len;
21582159
}
21592160

2161+
static int should_write_element(tree_node_t* nodo)
2162+
{
2163+
return !(param_generate_snippet && (ELM_ID(nodo) == ELMID_HEAD));
2164+
}
2165+
2166+
static int should_write_tags(tree_node_t* nodo)
2167+
{
2168+
return !(param_generate_snippet && (ELM_ID(nodo) == ELMID_HTML
2169+
|| ELM_ID(nodo) == ELMID_BODY));
2170+
}
21602171

21612172
static int write_element(tree_node_t *elm)
21622173
{
21632174
int len = 0;
21642175
tree_node_t *n;
21652176
int is_block;
21662177
int xml_space_activated;
2178+
int writes_tags;
21672179

2180+
if (!should_write_element(elm))
2181+
return 0;
2182+
2183+
writes_tags = should_write_tags(elm);
21682184
is_block = dtd_elm_is_block(ELM_ID(elm));
21692185

21702186
/* activate "xml:space preserve" if necessary */
@@ -2183,45 +2199,50 @@ static int write_element(tree_node_t *elm)
21832199
}
21842200

21852201
/* write start tag */
2186-
if (is_block) {
2187-
len += write_indent(indent, 1);
2188-
inline_on = 0;
2189-
} else {
2190-
if (!inline_on) {
2191-
inline_on = 1;
2192-
whitespace_needed = 0;
2193-
if (!param_compact_block_elms)
2194-
len += write_indent(indent, 1);
2202+
// if (param_generate_snippet && elm == html
2203+
if (writes_tags) {
2204+
if (is_block) {
2205+
len += write_indent(indent, 1);
2206+
inline_on = 0;
2207+
} else {
2208+
if (!inline_on) {
2209+
inline_on = 1;
2210+
whitespace_needed = 0;
2211+
if (!param_compact_block_elms)
2212+
len += write_indent(indent, 1);
2213+
}
21952214
}
2215+
len += write_start_tag(elm);
21962216
}
2197-
len += write_start_tag(elm);
21982217

21992218
/* process children nodes */
22002219
n = elm->cont.elemento.hijo;
2201-
if (is_block)
2220+
if (is_block && writes_tags)
22022221
indent += param_tab_len;
22032222
while (n) {
22042223
len += write_node(n);
22052224
n = n->sig;
22062225
}
2207-
if (is_block)
2226+
if (is_block && writes_tags)
22082227
indent -= param_tab_len;
22092228

22102229
/* write end tag if not empty */
2211-
if (elm->cont.elemento.hijo) {
2230+
if (writes_tags) {
2231+
if (elm->cont.elemento.hijo) {
22122232
if (is_block) {
22132233
if (inline_on) {
2214-
inline_on = 0;
2215-
if (!param_compact_block_elms)
2216-
len += write_indent(indent, 1);
2234+
inline_on = 0;
2235+
if (!param_compact_block_elms)
2236+
len += write_indent(indent, 1);
22172237
} else {
2218-
len += write_indent(indent, 1);
2238+
len += write_indent(indent, 1);
22192239
}
22202240
}
22212241
len += write_end_tag(elm);
2222-
} else if (!param_empty_tags
2223-
&& elm_list[ELM_ID(elm)].contenttype[doctype] != CONTTYPE_EMPTY) {
2242+
} else if (!param_empty_tags
2243+
&& elm_list[ELM_ID(elm)].contenttype[doctype] != CONTTYPE_EMPTY) {
22242244
len += write_end_tag(elm);
2245+
}
22252246
}
22262247

22272248
/* deactivate "xml:space preserve" if activated */
@@ -2403,7 +2424,6 @@ static int write_start_tag(tree_node_t* nodo)
24032424
char *elm_name;
24042425
int elm_name_len;
24052426
int printed;
2406-
24072427

24082428
elm_name = elm_list[ELM_ID(nodo)].name;
24092429
elm_name_len = strlen(elm_name);

0 commit comments

Comments
 (0)