code-style improvements - webdump - HTML to plain-text converter for webpages
HTML git clone git://git.codemadness.org/webdump
DIR Log
DIR Files
DIR Refs
DIR README
DIR LICENSE
---
DIR commit 743dc293a681e8543b6aa4e784bc5e2835aba6d4
DIR parent 555cadf2ae02a70c4bd12cd9cd6bca68a50042a9
HTML Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date: Thu, 2 Jul 2026 19:26:02 +0200
code-style improvements
Diffstat:
M webdump.c | 101 +++++++++++++++++++------------
1 file changed, 61 insertions(+), 40 deletions(-)
---
DIR diff --git a/webdump.c b/webdump.c
@@ -52,7 +52,7 @@ static int showurlinline = 0; /* (-I) show full link reference inline */
static int showrefbottom = 0; /* (-l) show link references at the bottom */
static int allowlinewrap = 0; /* (-r) line-wrapping */
static int termwidth = 77; /* (-w) terminal width */
-static int resources = 0; /* (-x) write resources line-by-line to fd 3? */
+static int xresources = 0; /* (-x) write resources line-by-line to fd 3 */
enum DisplayType {
DisplayUnknown = 0,
@@ -240,7 +240,7 @@ static int reader_mode;
/* flag if the tags and their children should be ignored in the current context */
static int reader_ignore;
-static enum MarkupType curmarkup; /* current markup state (bold, underline, etc) */
+static enum MarkupType curmarkup; /* current terminal markup state (bold, underline, etc) */
static int linewrap; /* allow linewrap in this context */
/* selector to match (for -s and -u) */
@@ -248,7 +248,7 @@ static struct selectors *sel_hide, *sel_show;
/* tags table: needs to be sorted like tagcmp(), alphabetically */
-/* tag id displaytype markup parent v o b a i */
+/* tag id displaytype markup parent v o mt mb i */
static struct tag tags[] = {
{ "a", TagA, DisplayInline, MarkupUnderline, 0, 0, 0, 0, 0, 0 },
{ "address", TagAddress, DisplayBlock, 0, 0, 0, 0, 0, 0, 0 },
@@ -1075,9 +1075,11 @@ static void
printc(int c)
{
if (ISSPACE((unsigned char)c)) {
+ /* no white-space previously emitted and there are other
+ characters on this line. */
if (whitespace_mode == 2)
hputchar(' ');
- whitespace_mode |= 1;
+ whitespace_mode |= 1; /* whitespace emitted */
} else {
whitespace_mode = 2;
if (!ISCNTRL((unsigned char)c))
@@ -1493,7 +1495,7 @@ handleinlinelink(void)
char buf[4096], *url;
int r;
- if (!showrefbottom && !showrefinline && !showurlinline && !resources)
+ if (!showrefbottom && !showrefinline && !showurlinline && !xresources)
return; /* there is no need to collect the reference */
if (!attr_href.len && !attr_src.len && !attr_data.len)
@@ -1546,7 +1548,7 @@ printlinkrefs(void)
if (!nvisrefs && !nhiddenrefs)
return;
- if (resources) {
+ if (xresources) {
for (i = 0; i < nvisrefs; i++) {
ref = visrefs[i];
dprintf(3, "%s\t%s\n", ref->type, ref->url);
@@ -1617,10 +1619,24 @@ xmldatastart(XMLParser *p)
}
static void
+printhtmldata(void)
+{
+ char *start, *s, *e;
+
+ if (!htmldata.data || !htmldata.len)
+ return;
+
+ start = htmldata.data;
+ e = htmldata.data + htmldata.len;
+
+ for (s = start; s < e; s++)
+ printc((unsigned char)*s);
+}
+
+static void
xmldataend(XMLParser *p)
{
struct node *cur;
- char *start, *s, *e;
if (!htmldata.data || !htmldata.len)
return;
@@ -1633,11 +1649,7 @@ xmldataend(XMLParser *p)
findparenttype(curnode - 1, DisplayPre)) {
printpre(htmldata.data, htmldata.len);
} else {
- start = htmldata.data;
- e = htmldata.data + htmldata.len;
-
- for (s = start; s < e; s++)
- printc((unsigned char)*s);
+ printhtmldata();
}
string_clear(&htmldata);
@@ -1972,7 +1984,7 @@ xmltagstart(XMLParser *p, const char *t, size_t tl)
parenttype = 0; /* by default, seek until the root */
/* if optional tag <p> is open and a list element is found, close </p>. */
- if (found && found->displaytype & DisplayList) {
+ if (found && (found->displaytype & DisplayList)) {
/* not inside a list */
childs[0] = TagP;
nchilds = 1;
@@ -1980,23 +1992,23 @@ xmltagstart(XMLParser *p, const char *t, size_t tl)
} else if (found && found->isoptional) {
tagid = found->id;
if (tagid == TagLi) {
- childs[0] = TagLi;
+ childs[0] = tagid;
nchilds = 1;
parenttype = DisplayList;
} else if (tagid == TagTd) {
- childs[0] = TagTd;
+ childs[0] = tagid;
nchilds = 1;
parenttype = DisplayTableRow;
} else if (tagid == TagTr) {
- childs[0] = TagTr;
+ childs[0] = tagid;
nchilds = 1;
parenttype = DisplayTable;
} else if (tagid == TagP) {
- childs[0] = TagP;
+ childs[0] = tagid;
nchilds = 1;
parenttype = 0; /* seek until the root */
} else if (tagid == TagOption) {
- childs[0] = TagOption;
+ childs[0] = tagid;
nchilds = 1;
parenttype = DisplaySelect;
} else if (tagid == TagDt) {
@@ -2004,7 +2016,7 @@ xmltagstart(XMLParser *p, const char *t, size_t tl)
nchilds = 1;
parenttype = DisplayDl;
} else if (tagid == TagDd) {
- childs[0] = TagDd;
+ childs[0] = tagid;
childs[1] = TagDt;
nchilds = 2;
parenttype = DisplayDl;
@@ -2292,26 +2304,35 @@ xmlattr(XMLParser *p, const char *t, size_t tl, const char *n,
else if (!attrcmp(n, "value"))
string_append(&attr_value, v, vl);
- /* <base href="..." /> */
- if (!basehrefset && tagid == TagBase && !attrcmp(n, "href"))
- strlcat(basehrefdoc, v, sizeof(basehrefdoc));
-
- if (tagid == TagA && !attrcmp(n, "href"))
- string_append(&attr_href, v, vl);
-
- if (tagid == TagSelect && !attrcmp(n, "multiple"))
- cur->tag.displaytype |= DisplaySelectMulti;
-
- if (tagid == TagObject && !attrcmp(n, "data"))
- string_append(&attr_data, v, vl);
-
- /* show img alt attribute as text. */
- if (tagid == TagImg && !attrcmp(n, "alt"))
- string_append(&attr_alt, v, vl);
-
+ /* input checkbox state (to display if checked) */
if (cur->tag.displaytype & DisplayInput && !attrcmp(n, "checked"))
string_append(&attr_checked, v, vl);
+ switch (tagid) {
+ case TagA:
+ if (!attrcmp(n, "href"))
+ string_append(&attr_href, v, vl);
+ break;
+ case TagBase: /* <base href="..." /> */
+ if (!basehrefset && !attrcmp(n, "href"))
+ strlcat(basehrefdoc, v, sizeof(basehrefdoc));
+ break;
+ case TagImg: /* show img alt attribute as text. */
+ if (!attrcmp(n, "alt"))
+ string_append(&attr_alt, v, vl);
+ break;
+ case TagObject:
+ if (!attrcmp(n, "data"))
+ string_append(&attr_data, v, vl);
+ break;
+ case TagSelect:
+ if (!attrcmp(n, "multiple"))
+ cur->tag.displaytype |= DisplaySelectMulti;
+ break;
+ default:
+ break;
+ }
+
/* src attribute */
switch (tagid) {
case TagAudio:
@@ -2355,9 +2376,9 @@ xmlattrend(XMLParser *p, const char *t, size_t tl, const char *n,
tagid = cur->tag.id;
if (!attr_class_set && !attrcmp(n, "class"))
- attr_class_set = 1;
+ attr_class_set = 1; /* use the first set attribute */
else if (!attr_id_set && !attrcmp(n, "id"))
- attr_id_set = 1;
+ attr_id_set = 1; /* use the first set attribute */
/* set base URL, if it is set it cannot be overwritten again */
if (!basehrefset && basehrefdoc[0] &&
@@ -2463,7 +2484,7 @@ main(int argc, char **argv)
usage();
break;
case 'x':
- resources = !resources;
+ xresources = !xresources;
break;
default:
usage();
@@ -2498,7 +2519,7 @@ main(int argc, char **argv)
if (ncells > 0)
newline();
- if (showrefbottom || resources)
+ if (showrefbottom || xresources)
printlinkrefs();
hflush();